cells now change color based on value!!!
colors are a bit weird though, need to figure that out
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 1.7 KiB |
@@ -0,0 +1,127 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 951cf69dd3ce5204ab23a45cb2cfb39c
|
||||||
|
TextureImporter:
|
||||||
|
internalIDToNameTable: []
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 12
|
||||||
|
mipmaps:
|
||||||
|
mipMapMode: 0
|
||||||
|
enableMipMap: 0
|
||||||
|
sRGBTexture: 1
|
||||||
|
linearTexture: 0
|
||||||
|
fadeOut: 0
|
||||||
|
borderMipMap: 0
|
||||||
|
mipMapsPreserveCoverage: 0
|
||||||
|
alphaTestReferenceValue: 0.5
|
||||||
|
mipMapFadeDistanceStart: 1
|
||||||
|
mipMapFadeDistanceEnd: 3
|
||||||
|
bumpmap:
|
||||||
|
convertToNormalMap: 0
|
||||||
|
externalNormalMap: 0
|
||||||
|
heightScale: 0.25
|
||||||
|
normalMapFilter: 0
|
||||||
|
flipGreenChannel: 0
|
||||||
|
isReadable: 0
|
||||||
|
streamingMipmaps: 0
|
||||||
|
streamingMipmapsPriority: 0
|
||||||
|
vTOnly: 0
|
||||||
|
ignoreMipmapLimit: 0
|
||||||
|
grayScaleToAlpha: 0
|
||||||
|
generateCubemap: 6
|
||||||
|
cubemapConvolution: 0
|
||||||
|
seamlessCubemap: 0
|
||||||
|
textureFormat: 1
|
||||||
|
maxTextureSize: 2048
|
||||||
|
textureSettings:
|
||||||
|
serializedVersion: 2
|
||||||
|
filterMode: 1
|
||||||
|
aniso: 1
|
||||||
|
mipBias: 0
|
||||||
|
wrapU: 1
|
||||||
|
wrapV: 1
|
||||||
|
wrapW: 1
|
||||||
|
nPOTScale: 0
|
||||||
|
lightmap: 0
|
||||||
|
compressionQuality: 50
|
||||||
|
spriteMode: 1
|
||||||
|
spriteExtrude: 1
|
||||||
|
spriteMeshType: 1
|
||||||
|
alignment: 0
|
||||||
|
spritePivot: {x: 0.5, y: 0.5}
|
||||||
|
spritePixelsToUnits: 100
|
||||||
|
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
|
||||||
|
spriteGenerateFallbackPhysicsShape: 1
|
||||||
|
alphaUsage: 1
|
||||||
|
alphaIsTransparency: 1
|
||||||
|
spriteTessellationDetail: -1
|
||||||
|
textureType: 8
|
||||||
|
textureShape: 1
|
||||||
|
singleChannelComponent: 0
|
||||||
|
flipbookRows: 1
|
||||||
|
flipbookColumns: 1
|
||||||
|
maxTextureSizeSet: 0
|
||||||
|
compressionQualitySet: 0
|
||||||
|
textureFormatSet: 0
|
||||||
|
ignorePngGamma: 0
|
||||||
|
applyGammaDecoding: 0
|
||||||
|
swizzle: 50462976
|
||||||
|
cookieLightType: 0
|
||||||
|
platformSettings:
|
||||||
|
- serializedVersion: 3
|
||||||
|
buildTarget: DefaultTexturePlatform
|
||||||
|
maxTextureSize: 2048
|
||||||
|
resizeAlgorithm: 0
|
||||||
|
textureFormat: -1
|
||||||
|
textureCompression: 1
|
||||||
|
compressionQuality: 50
|
||||||
|
crunchedCompression: 0
|
||||||
|
allowsAlphaSplitting: 0
|
||||||
|
overridden: 0
|
||||||
|
ignorePlatformSupport: 0
|
||||||
|
androidETC2FallbackOverride: 0
|
||||||
|
forceMaximumCompressionQuality_BC6H_BC7: 0
|
||||||
|
- serializedVersion: 3
|
||||||
|
buildTarget: Standalone
|
||||||
|
maxTextureSize: 2048
|
||||||
|
resizeAlgorithm: 0
|
||||||
|
textureFormat: -1
|
||||||
|
textureCompression: 1
|
||||||
|
compressionQuality: 50
|
||||||
|
crunchedCompression: 0
|
||||||
|
allowsAlphaSplitting: 0
|
||||||
|
overridden: 0
|
||||||
|
ignorePlatformSupport: 0
|
||||||
|
androidETC2FallbackOverride: 0
|
||||||
|
forceMaximumCompressionQuality_BC6H_BC7: 0
|
||||||
|
- serializedVersion: 3
|
||||||
|
buildTarget: Server
|
||||||
|
maxTextureSize: 2048
|
||||||
|
resizeAlgorithm: 0
|
||||||
|
textureFormat: -1
|
||||||
|
textureCompression: 1
|
||||||
|
compressionQuality: 50
|
||||||
|
crunchedCompression: 0
|
||||||
|
allowsAlphaSplitting: 0
|
||||||
|
overridden: 0
|
||||||
|
ignorePlatformSupport: 0
|
||||||
|
androidETC2FallbackOverride: 0
|
||||||
|
forceMaximumCompressionQuality_BC6H_BC7: 0
|
||||||
|
spriteSheet:
|
||||||
|
serializedVersion: 2
|
||||||
|
sprites: []
|
||||||
|
outline: []
|
||||||
|
physicsShape: []
|
||||||
|
bones: []
|
||||||
|
spriteID: 5e97eb03825dee720800000000000000
|
||||||
|
internalID: 0
|
||||||
|
vertices: []
|
||||||
|
indices:
|
||||||
|
edges: []
|
||||||
|
weights: []
|
||||||
|
secondaryTextures: []
|
||||||
|
nameFileIdTable: {}
|
||||||
|
mipmapLimitGroupName:
|
||||||
|
pSDRemoveMatte: 0
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
+3
-3
@@ -7,8 +7,8 @@ Material:
|
|||||||
m_CorrespondingSourceObject: {fileID: 0}
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
m_PrefabInstance: {fileID: 0}
|
m_PrefabInstance: {fileID: 0}
|
||||||
m_PrefabAsset: {fileID: 0}
|
m_PrefabAsset: {fileID: 0}
|
||||||
m_Name: GridMaterial
|
m_Name: cellMaterial
|
||||||
m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0}
|
m_Shader: {fileID: 10752, guid: 0000000000000000f000000000000000, type: 0}
|
||||||
m_Parent: {fileID: 0}
|
m_Parent: {fileID: 0}
|
||||||
m_ModifiedSerializedProperties: 0
|
m_ModifiedSerializedProperties: 0
|
||||||
m_ValidKeywords: []
|
m_ValidKeywords: []
|
||||||
@@ -44,7 +44,7 @@ Material:
|
|||||||
m_Scale: {x: 1, y: 1}
|
m_Scale: {x: 1, y: 1}
|
||||||
m_Offset: {x: 0, y: 0}
|
m_Offset: {x: 0, y: 0}
|
||||||
- _MainTex:
|
- _MainTex:
|
||||||
m_Texture: {fileID: 0}
|
m_Texture: {fileID: 2800000, guid: 951cf69dd3ce5204ab23a45cb2cfb39c, type: 3}
|
||||||
m_Scale: {x: 1, y: 1}
|
m_Scale: {x: 1, y: 1}
|
||||||
m_Offset: {x: 0, y: 0}
|
m_Offset: {x: 0, y: 0}
|
||||||
- _MetallicGlossMap:
|
- _MetallicGlossMap:
|
||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
fileFormatVersion: 2
|
fileFormatVersion: 2
|
||||||
guid: 6ab99349792397f45a20efcb8c623941
|
guid: 5c15fb7e68981214b8a7478ca8c22bf0
|
||||||
NativeFormatImporter:
|
NativeFormatImporter:
|
||||||
externalObjects: {}
|
externalObjects: {}
|
||||||
mainObjectFileID: 2100000
|
mainObjectFileID: 2100000
|
||||||
@@ -326,7 +326,7 @@ MeshRenderer:
|
|||||||
m_RenderingLayerMask: 1
|
m_RenderingLayerMask: 1
|
||||||
m_RendererPriority: 0
|
m_RendererPriority: 0
|
||||||
m_Materials:
|
m_Materials:
|
||||||
- {fileID: 2100000, guid: 6ab99349792397f45a20efcb8c623941, type: 2}
|
- {fileID: 2100000, guid: 5c15fb7e68981214b8a7478ca8c22bf0, type: 2}
|
||||||
m_StaticBatchInfo:
|
m_StaticBatchInfo:
|
||||||
firstSubMesh: 0
|
firstSubMesh: 0
|
||||||
subMeshCount: 0
|
subMeshCount: 0
|
||||||
|
|||||||
@@ -4,9 +4,6 @@ using UnityEngine;
|
|||||||
|
|
||||||
public class CellMesh : MonoBehaviour
|
public class CellMesh : MonoBehaviour
|
||||||
{
|
{
|
||||||
|
|
||||||
Material m_Material;
|
|
||||||
|
|
||||||
private LogicGrid grid;
|
private LogicGrid grid;
|
||||||
private Mesh mesh;
|
private Mesh mesh;
|
||||||
|
|
||||||
@@ -20,11 +17,20 @@ public class CellMesh : MonoBehaviour
|
|||||||
{
|
{
|
||||||
this.grid = grid;
|
this.grid = grid;
|
||||||
UpdateCellVisual();
|
UpdateCellVisual();
|
||||||
|
|
||||||
|
// Subscribe to event
|
||||||
|
grid.OnGridValueChanged += GridValueChanged;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GridValueChanged(object sender, LogicGrid.OnGridValueChangedEventArgs e)
|
||||||
|
{
|
||||||
|
Debug.Log("FIRE!!");
|
||||||
|
UpdateCellVisual();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void 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 x = 0; x < grid.GetWidth(); x++)
|
||||||
for(int y = 0; y < grid.GetHeight(); y++)
|
for(int y = 0; y < grid.GetHeight(); y++)
|
||||||
@@ -33,7 +39,12 @@ public class CellMesh : MonoBehaviour
|
|||||||
|
|
||||||
Vector3 quadSize = new Vector3(1, 1) * grid.GetCellSize();
|
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;
|
mesh.vertices = vertices;
|
||||||
@@ -43,55 +54,37 @@ public class CellMesh : MonoBehaviour
|
|||||||
|
|
||||||
void Start()
|
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()
|
private void Update()
|
||||||
{
|
{
|
||||||
if(Input.GetKeyDown("1"))
|
/* if(Input.GetKeyDown("1"))
|
||||||
{
|
{
|
||||||
Debug.Log("1");
|
Debug.Log("1");
|
||||||
m_Material.color = Color.white;
|
m_Material.color = Color.white;
|
||||||
@@ -110,67 +103,6 @@ public class CellMesh : MonoBehaviour
|
|||||||
Debug.Log("3");
|
Debug.Log("3");
|
||||||
m_Material.color = Color.green;
|
m_Material.color = Color.green;
|
||||||
//m_Material.SetColor("_Color", Color.white);
|
//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;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
|
||||||
// INSPIRED BY "CODE MONKEY" ON YOUTUBE
|
// INSPIRED BY "CODE MONKEY" ON YOUTUBE
|
||||||
@@ -14,10 +15,16 @@ public class LogicGrid
|
|||||||
|
|
||||||
// Actual Grid Array
|
// Actual Grid Array
|
||||||
private int[,] gridArray;
|
private int[,] gridArray;
|
||||||
|
|
||||||
// Debug Grid Array used for updating the text-objects
|
// Debug Grid Array used for updating the text-objects
|
||||||
private TextMesh[,] debugTextArray;
|
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)
|
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
|
// Set the value in our debug mesh so that our Text on-screen gets updated
|
||||||
debugTextArray[x, y].text = gridArray[x, y].ToString();
|
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");
|
Debug.Log("Placing Mode: Agent");
|
||||||
placementMode = 'a';
|
placementMode = 'a';
|
||||||
placementValue = 2;
|
placementValue = 3;
|
||||||
}
|
}
|
||||||
// Place Sample in grid
|
// Place Sample in grid
|
||||||
if(Input.GetKeyDown("s"))
|
if(Input.GetKeyDown("s"))
|
||||||
{
|
{
|
||||||
Debug.Log("Placing Mode: Sample");
|
Debug.Log("Placing Mode: Sample");
|
||||||
placementMode = 's';
|
placementMode = 's';
|
||||||
placementValue = 1;
|
placementValue = 2;
|
||||||
}
|
}
|
||||||
// Place Obstacle in grid
|
// Place Obstacle in grid
|
||||||
if(Input.GetKeyDown("o"))
|
if(Input.GetKeyDown("o"))
|
||||||
{
|
{
|
||||||
Debug.Log("Placing Mode: Obstacle");
|
Debug.Log("Placing Mode: Obstacle");
|
||||||
placementMode = 'o';
|
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:
|
||||||
Reference in New Issue
Block a user