diff options
| author | Simon O'Shea <osheas1@wit.edu> | 2023-08-05 15:34:41 -0400 |
|---|---|---|
| committer | Simon O'Shea <osheas1@wit.edu> | 2023-08-05 15:34:41 -0400 |
| commit | 94b8f55509cfea3706299abb7a909c9cd413ba80 (patch) | |
| tree | 199206ab5289a917faeb4b88cbf3759f5a75c4d0 /Pathfinding Visualizer/Assets/Scripts | |
| parent | 31151bc9b6fdcd5ade2f8176c17f50f67b58e5d0 (diff) | |
created mesh array - WIP
the meshes are generated in a grid formation but not positioned correctly :(
Diffstat (limited to 'Pathfinding Visualizer/Assets/Scripts')
| -rw-r--r-- | Pathfinding Visualizer/Assets/Scripts/CellMesh.cs | 176 | ||||
| -rw-r--r-- | Pathfinding Visualizer/Assets/Scripts/CellMesh.cs.meta (renamed from Pathfinding Visualizer/Assets/Scripts/MeshTesting.cs.meta) | 0 | ||||
| -rw-r--r-- | Pathfinding Visualizer/Assets/Scripts/LogicGrid.cs | 18 | ||||
| -rw-r--r-- | Pathfinding Visualizer/Assets/Scripts/Main.cs | 6 | ||||
| -rw-r--r-- | Pathfinding Visualizer/Assets/Scripts/MeshTesting.cs | 80 |
5 files changed, 198 insertions, 82 deletions
diff --git a/Pathfinding Visualizer/Assets/Scripts/CellMesh.cs b/Pathfinding Visualizer/Assets/Scripts/CellMesh.cs new file mode 100644 index 0000000..4e3840e --- /dev/null +++ b/Pathfinding Visualizer/Assets/Scripts/CellMesh.cs @@ -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; + }*/ +} diff --git a/Pathfinding Visualizer/Assets/Scripts/MeshTesting.cs.meta b/Pathfinding Visualizer/Assets/Scripts/CellMesh.cs.meta index 49560ea..49560ea 100644 --- a/Pathfinding Visualizer/Assets/Scripts/MeshTesting.cs.meta +++ b/Pathfinding Visualizer/Assets/Scripts/CellMesh.cs.meta diff --git a/Pathfinding Visualizer/Assets/Scripts/LogicGrid.cs b/Pathfinding Visualizer/Assets/Scripts/LogicGrid.cs index e9dc790..873822a 100644 --- a/Pathfinding Visualizer/Assets/Scripts/LogicGrid.cs +++ b/Pathfinding Visualizer/Assets/Scripts/LogicGrid.cs @@ -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) - 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) @@ -104,4 +104,18 @@ public class LogicGrid return GetValue(x, y); } + public int GetHeight() + { + return this.height; + } + + public int GetWidth() + { + return this.width; + } + + public float GetCellSize() + { + return this.cellSize; + } } diff --git a/Pathfinding Visualizer/Assets/Scripts/Main.cs b/Pathfinding Visualizer/Assets/Scripts/Main.cs index 44e2212..30f2671 100644 --- a/Pathfinding Visualizer/Assets/Scripts/Main.cs +++ b/Pathfinding Visualizer/Assets/Scripts/Main.cs @@ -5,10 +5,13 @@ using UnityEngine; public class Main : MonoBehaviour { + [SerializeField] private CellMesh cellMesh; + private LogicGrid world; char placementMode; int placementValue; + // Start is called before the first frame update void Start() { @@ -18,6 +21,9 @@ public class Main : MonoBehaviour // Set default placement to Obstacle placementValue = -1; placementMode = 'o'; + + cellMesh.SetGrid(world); + Debug.Log("Loaded. Placing Mode: Obstacle"); } diff --git a/Pathfinding Visualizer/Assets/Scripts/MeshTesting.cs b/Pathfinding Visualizer/Assets/Scripts/MeshTesting.cs deleted file mode 100644 index 5baba56..0000000 --- a/Pathfinding Visualizer/Assets/Scripts/MeshTesting.cs +++ /dev/null @@ -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); - } - } -} |
