cells now change color based on value!!!

colors are a bit weird though, need to figure that out
This commit is contained in:
Simon O'Shea
2023-08-07 13:59:44 -04:00
parent 94b8f55509
commit 7c3fd8d563
10 changed files with 366 additions and 120 deletions
+43 -111
View File
@@ -4,9 +4,6 @@ using UnityEngine;
public class CellMesh : MonoBehaviour
{
Material m_Material;
private LogicGrid grid;
private Mesh mesh;
@@ -20,11 +17,20 @@ public class CellMesh : MonoBehaviour
{
this.grid = grid;
UpdateCellVisual();
// Subscribe to event
grid.OnGridValueChanged += GridValueChanged;
}
private void GridValueChanged(object sender, LogicGrid.OnGridValueChangedEventArgs e)
{
Debug.Log("FIRE!!");
UpdateCellVisual();
}
private void UpdateCellVisual()
{
CreateEmptyMeshArrays(grid.GetWidth() * grid.GetHeight(), out Vector3[] vertices, out Vector2[] uv, out int[] triangles);
MeshUtils.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++)
@@ -33,7 +39,12 @@ public class CellMesh : MonoBehaviour
Vector3 quadSize = new Vector3(1, 1) * grid.GetCellSize();
AddQuad(vertices, uv, triangles, index, grid.GetWorldPosition(x, y), quadSize, Vector2.zero);
int gridValue = grid.GetValue(x, y);
float gridValueNormalized = gridValue / 4f;
Vector2 gridValueUV = new Vector2(gridValueNormalized, 0f);
MeshUtils.AddToMeshArrays(vertices, uv, triangles, index, grid.GetWorldPosition(x, y) + (quadSize * .5f), 0f, quadSize, gridValueUV, gridValueUV);
}
mesh.vertices = vertices;
@@ -43,55 +54,37 @@ public class CellMesh : MonoBehaviour
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;
*/
}
/* MeshRenderer myRenderer = GetComponent<MeshRenderer>();
Material m_material;
if (myRenderer != null)
{
m_material = myRenderer.material;
if(gridValue == -1)
{
m_material.color = Color.black;
}
if (gridValue == 0)
{
m_material.color = Color.white;
}
if (gridValue == 1)
{
m_material.color = Color.green;
}
if (gridValue == 2)
{
m_material.color = Color.cyan;
}*//*
}
*/
private void Update()
{
if(Input.GetKeyDown("1"))
/* if(Input.GetKeyDown("1"))
{
Debug.Log("1");
m_Material.color = Color.white;
@@ -110,67 +103,6 @@ public class CellMesh : MonoBehaviour
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;
}*/
}
}
@@ -1,6 +1,7 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
// INSPIRED BY "CODE MONKEY" ON YOUTUBE
@@ -14,10 +15,16 @@ public class LogicGrid
// Actual Grid Array
private int[,] gridArray;
// Debug Grid Array used for updating the text-objects
private TextMesh[,] debugTextArray;
public event EventHandler<OnGridValueChangedEventArgs> OnGridValueChanged;
public class OnGridValueChangedEventArgs : EventArgs
{
public int x;
public int y;
}
public LogicGrid(int width, int height, float cellSize, Vector3 originPosition)
@@ -83,6 +90,14 @@ public class LogicGrid
// Set the value in our debug mesh so that our Text on-screen gets updated
debugTextArray[x, y].text = gridArray[x, y].ToString();
if (OnGridValueChanged != null)
OnGridValueChanged(this, new OnGridValueChangedEventArgs
{
x = x,
y = y
});
;
}
}
@@ -38,21 +38,21 @@ public class Main : MonoBehaviour
{
Debug.Log("Placing Mode: Agent");
placementMode = 'a';
placementValue = 2;
placementValue = 3;
}
// Place Sample in grid
if(Input.GetKeyDown("s"))
{
Debug.Log("Placing Mode: Sample");
placementMode = 's';
placementValue = 1;
placementValue = 2;
}
// Place Obstacle in grid
if(Input.GetKeyDown("o"))
{
Debug.Log("Placing Mode: Obstacle");
placementMode = 'o';
placementValue = -1;
placementValue = 1;
}
@@ -0,0 +1,161 @@
/*
------------------- Code Monkey -------------------
Thank you for downloading this package
I hope you find it useful in your projects
If you have any questions let me know
Cheers!
unitycodemonkey.com
--------------------------------------------------
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class MeshUtils {
private static readonly Vector3 Vector3zero = Vector3.zero;
private static readonly Vector3 Vector3one = Vector3.one;
private static readonly Vector3 Vector3yDown = new Vector3(0,-1);
private static Quaternion[] cachedQuaternionEulerArr;
private static void CacheQuaternionEuler() {
if (cachedQuaternionEulerArr != null) return;
cachedQuaternionEulerArr = new Quaternion[360];
for (int i=0; i<360; i++) {
cachedQuaternionEulerArr[i] = Quaternion.Euler(0,0,i);
}
}
private static Quaternion GetQuaternionEuler(float rotFloat) {
int rot = Mathf.RoundToInt(rotFloat);
rot = rot % 360;
if (rot < 0) rot += 360;
//if (rot >= 360) rot -= 360;
if (cachedQuaternionEulerArr == null) CacheQuaternionEuler();
return cachedQuaternionEulerArr[rot];
}
public static Mesh CreateEmptyMesh() {
Mesh mesh = new Mesh();
mesh.vertices = new Vector3[0];
mesh.uv = new Vector2[0];
mesh.triangles = new int[0];
return mesh;
}
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];
}
public static Mesh CreateMesh(Vector3 pos, float rot, Vector3 baseSize, Vector2 uv00, Vector2 uv11) {
return AddToMesh(null, pos, rot, baseSize, uv00, uv11);
}
public static Mesh AddToMesh(Mesh mesh, Vector3 pos, float rot, Vector3 baseSize, Vector2 uv00, Vector2 uv11) {
if (mesh == null) {
mesh = CreateEmptyMesh();
}
Vector3[] vertices = new Vector3[4 + mesh.vertices.Length];
Vector2[] uvs = new Vector2[4 + mesh.uv.Length];
int[] triangles = new int[6 + mesh.triangles.Length];
mesh.vertices.CopyTo(vertices, 0);
mesh.uv.CopyTo(uvs, 0);
mesh.triangles.CopyTo(triangles, 0);
int index = vertices.Length / 4 - 1;
//Relocate vertices
int vIndex = index*4;
int vIndex0 = vIndex;
int vIndex1 = vIndex+1;
int vIndex2 = vIndex+2;
int vIndex3 = vIndex+3;
baseSize *= .5f;
bool skewed = baseSize.x != baseSize.y;
if (skewed) {
vertices[vIndex0] = pos+GetQuaternionEuler(rot)*new Vector3(-baseSize.x, baseSize.y);
vertices[vIndex1] = pos+GetQuaternionEuler(rot)*new Vector3(-baseSize.x, -baseSize.y);
vertices[vIndex2] = pos+GetQuaternionEuler(rot)*new Vector3( baseSize.x, -baseSize.y);
vertices[vIndex3] = pos+GetQuaternionEuler(rot)*baseSize;
} else {
vertices[vIndex0] = pos+GetQuaternionEuler(rot-270)*baseSize;
vertices[vIndex1] = pos+GetQuaternionEuler(rot-180)*baseSize;
vertices[vIndex2] = pos+GetQuaternionEuler(rot- 90)*baseSize;
vertices[vIndex3] = pos+GetQuaternionEuler(rot- 0)*baseSize;
}
//Relocate UVs
uvs[vIndex0] = new Vector2(uv00.x, uv11.y);
uvs[vIndex1] = new Vector2(uv00.x, uv00.y);
uvs[vIndex2] = new Vector2(uv11.x, uv00.y);
uvs[vIndex3] = new Vector2(uv11.x, uv11.y);
//Create triangles
int tIndex = index*6;
triangles[tIndex+0] = vIndex0;
triangles[tIndex+1] = vIndex3;
triangles[tIndex+2] = vIndex1;
triangles[tIndex+3] = vIndex1;
triangles[tIndex+4] = vIndex3;
triangles[tIndex+5] = vIndex2;
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.uv = uvs;
//mesh.bounds = bounds;
return mesh;
}
public static void AddToMeshArrays(Vector3[] vertices, Vector2[] uvs, int[] triangles, int index, Vector3 pos, float rot, Vector3 baseSize, Vector2 uv00, Vector2 uv11) {
//Relocate vertices
int vIndex = index*4;
int vIndex0 = vIndex;
int vIndex1 = vIndex+1;
int vIndex2 = vIndex+2;
int vIndex3 = vIndex+3;
baseSize *= .5f;
bool skewed = baseSize.x != baseSize.y;
if (skewed) {
vertices[vIndex0] = pos+GetQuaternionEuler(rot)*new Vector3(-baseSize.x, baseSize.y);
vertices[vIndex1] = pos+GetQuaternionEuler(rot)*new Vector3(-baseSize.x, -baseSize.y);
vertices[vIndex2] = pos+GetQuaternionEuler(rot)*new Vector3( baseSize.x, -baseSize.y);
vertices[vIndex3] = pos+GetQuaternionEuler(rot)*baseSize;
} else {
vertices[vIndex0] = pos+GetQuaternionEuler(rot-270)*baseSize;
vertices[vIndex1] = pos+GetQuaternionEuler(rot-180)*baseSize;
vertices[vIndex2] = pos+GetQuaternionEuler(rot- 90)*baseSize;
vertices[vIndex3] = pos+GetQuaternionEuler(rot- 0)*baseSize;
}
//Relocate UVs
uvs[vIndex0] = new Vector2(uv00.x, uv11.y);
uvs[vIndex1] = new Vector2(uv00.x, uv00.y);
uvs[vIndex2] = new Vector2(uv11.x, uv00.y);
uvs[vIndex3] = new Vector2(uv11.x, uv11.y);
//Create triangles
int tIndex = index*6;
triangles[tIndex+0] = vIndex0;
triangles[tIndex+1] = vIndex3;
triangles[tIndex+2] = vIndex1;
triangles[tIndex+3] = vIndex1;
triangles[tIndex+4] = vIndex3;
triangles[tIndex+5] = vIndex2;
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 984c4cdb3d17696468650e34e19e16b8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: