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
@@ -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)
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;
}
}
@@ -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");
}
@@ -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);
}
}
}