created mesh array - WIP

the meshes are generated in a grid formation but not positioned correctly :(
This commit is contained in:
Simon O'Shea
2023-08-05 15:34:41 -04:00
parent 31151bc9b6
commit 94b8f55509
6 changed files with 297 additions and 180 deletions
@@ -123,29 +123,6 @@ NavMeshSettings:
debug: debug:
m_Flags: 0 m_Flags: 0
m_NavMeshData: {fileID: 0} m_NavMeshData: {fileID: 0}
--- !u!33 &45376068
MeshFilter:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 480666364}
m_Mesh: {fileID: 0}
--- !u!4 &45376070
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 480666364}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &144141393 --- !u!1 &144141393
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@@ -175,6 +152,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 240ade3f16cf736479449e5fca9a29e1, type: 3} m_Script: {fileID: 11500000, guid: 240ade3f16cf736479449e5fca9a29e1, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
cellMesh: {fileID: 1473159509}
--- !u!4 &144141395 --- !u!4 &144141395
Transform: Transform:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@@ -184,85 +162,12 @@ Transform:
m_GameObject: {fileID: 144141393} m_GameObject: {fileID: 144141393}
serializedVersion: 2 serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 6.1189265, y: 0.17161107, z: -7.555224} m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &480666364
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 45376070}
- component: {fileID: 45376068}
- component: {fileID: 480666365}
- component: {fileID: 480666366}
m_Layer: 0
m_Name: MeshTesting
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!23 &480666365
MeshRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 480666364}
m_Enabled: 1
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_StaticShadowCaster: 0
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RayTracingMode: 2
m_RayTraceProcedural: 0
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 2100000, guid: 6ab99349792397f45a20efcb8c623941, type: 2}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_ReceiveGI: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 1
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_AdditionalVertexStreams: {fileID: 0}
--- !u!114 &480666366
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 480666364}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 2f03132b374e88b42a1e696cdfdf2a5c, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &519420028 --- !u!1 &519420028
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@@ -355,10 +260,106 @@ Transform:
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1473159508
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1473159510}
- component: {fileID: 1473159509}
- component: {fileID: 1473159511}
- component: {fileID: 1473159512}
m_Layer: 0
m_Name: CellMesh
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1473159509
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1473159508}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 2f03132b374e88b42a1e696cdfdf2a5c, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!4 &1473159510
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1473159508}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!23 &1473159511
MeshRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1473159508}
m_Enabled: 1
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_StaticShadowCaster: 0
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RayTracingMode: 2
m_RayTraceProcedural: 0
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 2100000, guid: 6ab99349792397f45a20efcb8c623941, type: 2}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_ReceiveGI: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 1
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_AdditionalVertexStreams: {fileID: 0}
--- !u!33 &1473159512
MeshFilter:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1473159508}
m_Mesh: {fileID: 0}
--- !u!1660057539 &9223372036854775807 --- !u!1660057539 &9223372036854775807
SceneRoots: SceneRoots:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
m_Roots: m_Roots:
- {fileID: 45376070}
- {fileID: 144141395} - {fileID: 144141395}
- {fileID: 519420032} - {fileID: 519420032}
- {fileID: 1473159510}
@@ -0,0 +1,176 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CellMesh : MonoBehaviour
{
Material m_Material;
private LogicGrid grid;
private Mesh mesh;
private void Awake()
{
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
}
public void SetGrid(LogicGrid grid)
{
this.grid = grid;
UpdateCellVisual();
}
private void UpdateCellVisual()
{
CreateEmptyMeshArrays(grid.GetWidth() * grid.GetHeight(), out Vector3[] vertices, out Vector2[] uv, out int[] triangles);
for(int x = 0; x < grid.GetWidth(); x++)
for(int y = 0; y < grid.GetHeight(); y++)
{
int index = x * grid.GetHeight() + y;
Vector3 quadSize = new Vector3(1, 1) * grid.GetCellSize();
AddQuad(vertices, uv, triangles, index, grid.GetWorldPosition(x, y), quadSize, Vector2.zero);
}
mesh.vertices = vertices;
mesh.uv = uv;
mesh.triangles = triangles;
}
void Start()
{
/**
m_Material = GetComponent<Renderer>().material;
Debug.Log("Mesh Testing");
Mesh mesh = new Mesh();
// Build required data-structures for square mesh
Vector3[] vertices = new Vector3[4];
Vector2[] uv = new Vector2[4];
int[] triangles = new int[6];
// Set vertices of polygon (2 triangles -> 1 quad)
vertices[0] = new Vector3(0, 0);
vertices[1] = new Vector3(0, 10);
vertices[2] = new Vector3(10, 10);
vertices[3] = new Vector3(10, 0);
// UV index contains texture position that should match to vertex with same index
uv[0] = new Vector2(0, 0);
uv[1] = new Vector2(0, 1);
uv[2] = new Vector2(1, 1);
uv[3] = new Vector2(1, 0);
// Set order of indexes of vertices to draw polygon (v1 -> v2 -> v3 -> v1)
// Note: always set clockwise, otherwise mesh will be facing backwards
triangles[0] = 0;
triangles[1] = 1;
triangles[2] = 2;
triangles[3] = 0;
triangles[4] = 2;
triangles[5] = 3;
mesh.vertices = vertices;
mesh.uv = uv;
mesh.triangles = triangles;
GetComponent<MeshFilter>().mesh = mesh;
*/
}
private void Update()
{
if(Input.GetKeyDown("1"))
{
Debug.Log("1");
m_Material.color = Color.white;
//m_Material.SetColor("_Color", Color.white);
}
if (Input.GetKeyDown("2"))
{
Debug.Log("2");
m_Material.color = Color.black;
//m_Material.SetColor("_Color", Color.white);
}
if (Input.GetKeyDown("3"))
{
Debug.Log("3");
m_Material.color = Color.green;
//m_Material.SetColor("_Color", Color.white);
}
}
// CREDIT FOR BELOW FUNCTIONS: CODEMONKEY
// https://www.youtube.com/watch?v=mZzZXfySeFQ
public static void CreateEmptyMeshArrays(int quadCount, out Vector3[] vertices, out Vector2[] uvs, out int[] triangles)
{
vertices = new Vector3[4 * quadCount];
uvs = new Vector2[4 * quadCount];
triangles = new int[6 * quadCount];
}
private void AddQuad(Vector3[] vertices, Vector2[] uvs, int[] triangles, int index, Vector3 GridPos, Vector3 QuadSize, Vector2 Uv)
{
vertices[index * 4] = new Vector3((-0.5f + GridPos.x) * QuadSize.x, (-0.5f + GridPos.y) * QuadSize.y);
vertices[(index * 4) + 1] = new Vector3((-0.5f + GridPos.x) * QuadSize.x, (+0.5f + GridPos.y) * QuadSize.y);
vertices[(index * 4) + 2] = new Vector3((+0.5f + GridPos.x) * QuadSize.x, (+0.5f + GridPos.y) * QuadSize.y);
vertices[(index * 4) + 3] = new Vector3((+0.5f + GridPos.x) * QuadSize.x, (-0.5f + GridPos.y) * QuadSize.y);
Debug.Log(vertices[0]);
Debug.Log(vertices[1]);
Debug.Log(vertices[2]);
Debug.Log(vertices[3]);
uvs[(index * 4)] = Uv;
uvs[(index * 4) + 1] = Uv;
uvs[(index * 4) + 2] = Uv;
uvs[(index * 4) + 3] = Uv;
triangles[(index * 6) + 0] = (index * 4) + 0;
triangles[(index * 6) + 1] = (index * 4) + 1;
triangles[(index * 6) + 2] = (index * 4) + 2;
triangles[(index * 6) + 3] = (index * 4) + 2;
triangles[(index * 6) + 4] = (index * 4) + 3;
triangles[(index * 6) + 5] = (index * 4) + 0;
}
/* private void AddQuad(Vector3[] vertices, Vector2[] uvs, int[] triangles, int index, Vector3 GridPos, Vector3 QuadSize, Vector2 Uv)
{
vertices[index * 4] = new Vector3((-0.5f + GridPos.x) * QuadSize.x, (-0.5f + GridPos.y) * QuadSize.y);
vertices[(index * 4) + 1] = new Vector3((-0.5f + GridPos.x) * QuadSize.x, (+0.5f + GridPos.y) * QuadSize.y);
vertices[(index * 4) + 2] = new Vector3((+0.5f + GridPos.x) * QuadSize.x, (+0.5f + GridPos.y) * QuadSize.y);
vertices[(index * 4) + 3] = new Vector3((+0.5f + GridPos.x) * QuadSize.x, (-0.5f + GridPos.y) * QuadSize.y);
Debug.Log(vertices[0]);
Debug.Log(vertices[1]);
Debug.Log(vertices[2]);
Debug.Log(vertices[3]);
uvs[(index * 4)] = Uv;
uvs[(index * 4) + 1] = Uv;
uvs[(index * 4) + 2] = Uv;
uvs[(index * 4) + 3] = Uv;
triangles[(index * 6) + 0] = (index * 4) + 0;
triangles[(index * 6) + 1] = (index * 4) + 1;
triangles[(index * 6) + 2] = (index * 4) + 2;
triangles[(index * 6) + 3] = (index * 4) + 2;
triangles[(index * 6) + 4] = (index * 4) + 3;
triangles[(index * 6) + 5] = (index * 4) + 0;
}*/
}
@@ -53,9 +53,9 @@ public class LogicGrid
// Enables us to convert the array's (x,y) pair into the user's world-space (also used to scale grid in constructor) // Enables us to convert the array's (x,y) pair into the user's world-space (also used to scale grid in constructor)
private Vector3 GetWorldPosition(int x, int y) public Vector3 GetWorldPosition(int x, int y)
{ {
return new Vector3(x, y) * this.cellSize + originPosition; return new Vector3(x, y) * this.cellSize + this.originPosition;
} }
private void getXY(Vector3 worldPosition, out int x, out int y) private void getXY(Vector3 worldPosition, out int x, out int y)
@@ -104,4 +104,18 @@ public class LogicGrid
return GetValue(x, y); return GetValue(x, y);
} }
public int GetHeight()
{
return this.height;
}
public int GetWidth()
{
return this.width;
}
public float GetCellSize()
{
return this.cellSize;
}
} }
@@ -5,10 +5,13 @@ using UnityEngine;
public class Main : MonoBehaviour public class Main : MonoBehaviour
{ {
[SerializeField] private CellMesh cellMesh;
private LogicGrid world; private LogicGrid world;
char placementMode; char placementMode;
int placementValue; int placementValue;
// Start is called before the first frame update // Start is called before the first frame update
void Start() void Start()
{ {
@@ -18,6 +21,9 @@ public class Main : MonoBehaviour
// Set default placement to Obstacle // Set default placement to Obstacle
placementValue = -1; placementValue = -1;
placementMode = 'o'; placementMode = 'o';
cellMesh.SetGrid(world);
Debug.Log("Loaded. Placing Mode: Obstacle"); Debug.Log("Loaded. Placing Mode: Obstacle");
} }
@@ -1,80 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MeshTesting : MonoBehaviour
{
Material m_Material;
void Start()
{
m_Material = GetComponent<Renderer>().material;
Debug.Log("Mesh Testing");
Mesh mesh = new Mesh();
// Build required data-structures for triangle mesh
Vector3[] vertices = new Vector3[4];
Vector2[] uv = new Vector2[4];
int[] triangles = new int[6];
// Set vertices of polygon (2 triangles -> 1 quad)
vertices[0] = new Vector3( 0, 0);
vertices[1] = new Vector3( 0, 10);
vertices[2] = new Vector3(10, 10);
vertices[3] = new Vector3(10, 0);
// UV index contains texture position that should match to vertex with same index
uv[0] = new Vector2(0, 0);
uv[1] = new Vector2(0, 1);
uv[2] = new Vector2(1, 1);
uv[3] = new Vector2(1, 0);
// Set order of indexes of vertices to draw polygon (v1 -> v2 -> v3 -> v1)
// Note: always set clockwise, otherwise mesh will be facing backwards
triangles[0] = 0;
triangles[1] = 1;
triangles[2] = 2;
triangles[3] = 0;
triangles[4] = 2;
triangles[5] = 3;
mesh.vertices = vertices;
mesh.uv = uv;
mesh.triangles = triangles;
GetComponent<MeshFilter>().mesh = mesh;
}
private void Update()
{
if(Input.GetKeyDown("1"))
{
Debug.Log("1");
m_Material.color = Color.white;
//m_Material.SetColor("_Color", Color.white);
}
if (Input.GetKeyDown("2"))
{
Debug.Log("2");
m_Material.color = Color.black;
//m_Material.SetColor("_Color", Color.white);
}
if (Input.GetKeyDown("3"))
{
Debug.Log("3");
m_Material.color = Color.green;
//m_Material.SetColor("_Color", Color.white);
}
}
}