I’m trying to get a marching squares algorithm from a tutorial working in 3D as a bit of an exercise, but I’m having a problem with the code. I’m not exactly sure what it is, as I’ve checked it numerous times.
The problem should be obivous.
using UnityEngine;
using System.Collections.Generic;
public class Chunk : MonoBehaviour {
public Voxel[] Voxels;
public GameObject voxelFab;
private int chunkResolution;
private float voxelSize;
public Mesh mesh;
private List<Vector3> vertices;
private List<Vector2> uv;
private List<int> triangles;
public void Awake (/*int resolution, float size*/) {
chunkResolution = 8;
voxelSize = 1f / chunkResolution;
Voxels = new Voxel[chunkResolution * chunkResolution * chunkResolution];
vertices = new List<Vector3>();
uv = new List<Vector2>();
triangles = new List<int>();
GetComponent<MeshFilter>().mesh = mesh = new Mesh();
GenerateChunkData();
RefreshChunk();
}
public void GenerateChunkData() {
for (int i = 0, y = 0; y < chunkResolution; y++)
{
for (int z = 0; z < chunkResolution; z++)
{
for (int x = 0; x < chunkResolution; x++, i++)
{
CreateVoxel (i, x, y, z);
Debug.Log ("Voxel at: " + Voxels<em>.position + "has it's edge positions as follows: " + "x: " + Voxels<em>.xEdge + " z: " + Voxels_.zEdge + " y: " + Voxels*.yEdge);*_</em></em>
* }*
* }*
* }*
* SetVoxels();*
* }*
* //Creates helper objects at the position of each voxel. Also create the voxel objects.*
* public void CreateVoxel (int i, int x, int y, int z) {*
* GameObject o = Instantiate (voxelFab) as GameObject;*
* o.transform.parent = transform;*
* o.transform.localPosition = new Vector3 (x + 0.5f, y + 0.5f, z + 0.5f);*
* o.transform.localScale = Vector3.one;*
* o.GetComponent().material.color = new Color (0.3f, 0.3f, 0.3f, 0.15f);*
_ Voxels = new Voxel (x, y, z, 1f);
* }*_
_ //Manually set the state of each voxel. The index is y * chunkResolution squared + z * chunkResolution + x;
* void SetVoxels() {
Voxels[5 * chunkResolution * chunkResolution + 5 * chunkResolution + 8].State = true;
Voxels[2 * chunkResolution * chunkResolution + 1 * chunkResolution + 0].State = true;
}*_
* //Function for refreshing the chunk. Currently only does the triangulation.*
* public void RefreshChunk() {*
* Triangulate();*
* }*
* //Clears the old lists of vertices, uvs and triangle indices and triangulates the mesh, then sets the new mesh variables.*
* public void Triangulate() {*
* mesh.Clear ();*
* vertices.Clear ();*
* uv.Clear ();*
* triangles.Clear ();*
* TriangulateCellRows();*
* mesh.vertices = vertices.ToArray ();*
* mesh.uv = uv.ToArray ();*
* mesh.triangles = triangles.ToArray ();*
* mesh.RecalculateNormals ();*
* }*
* //Loops through each cell and triangulates it.*
* public void TriangulateCellRows() {*
* int cells = chunkResolution - 1;*
* for (int i = 0, y = 0; y < cells; y++, i++)*
* {*
* for (int z = 0; z < cells; z++, i++)*
* {*
* for (int x = 0; x < cells; x++, i++)*
* {*
* TriangulateCell (*
_ Voxels*,
Voxels[i + 1],
Voxels[i + chunkResolution],
Voxels[i + chunkResolution + 1],
Voxels[i + chunkResolution * chunkResolution],
Voxels[i + chunkResolution * chunkResolution + 1],
Voxels[i + chunkResolution * chunkResolution + chunkResolution],
Voxels[i + chunkResolution * chunkResolution + chunkResolution + 1]
);*_
* }*
* }*
* }*
* }*
* public void TriangulateCell (Voxel a, Voxel b, Voxel c, Voxel d, Voxel e, Voxel f, Voxel g, Voxel h) {*
* int cellType = 0;*
* //Add a number to cellType depending on which voxels of that cell are active.*
* if (a.State)*
* cellType |= 1;*
* if (b.State)*
* cellType |= 2;*
* if (c.State)*
* cellType |= 4;*
* if (d.State)*
* cellType |= 8;*
* if (e.State)*
* cellType |= 16;*
* if (f.State)*
* cellType |= 32;*
* if (g.State)*
* cellType |= 64;*
* if (h.State)*
* cellType |= 128;*
* #region Single Triangles and 0, 255 cases*
* //Adds a triangle when either one voxel of the cell is cell active, or only one is inactive.*
* switch (cellType)*
* {*
* //None of the voxels are active, don’t draw the cell.*
* case 0:*
* return;*
* //The bottom left front (0,0,0) of the cell is the only active/inactive, draw a triangle there.*
* case 1:*
* AddTriangle (a.yEdge, a.zEdge, a.xEdge);*
* break;*
* case 254:*
* AddTriangle (a.xEdge, a.zEdge, a.yEdge);*
* break;*
* //The bottom right front (1,0,0) of the cell is the only active/inactive, draw a triangle there.*
* case 2:*
* AddTriangle (b.yEdge, a.xEdge, b.zEdge);*
* break;*
* case 253:*
* AddTriangle (b.zEdge, a.xEdge, b.yEdge);*
* break;*
* //The bottom left back (0,0,1) of the cell is the only active/inactive, draw a triangle there.*
* case 4:*
* AddTriangle (c.yEdge, c.xEdge, a.zEdge);*
* break;*
* case 251:*
* AddTriangle (a.zEdge, c.xEdge, c.yEdge);*
* break;*
* //The bottom right back (1,0,1) of the cell is the only active/inactive, draw a triangle there.*
* case 8:*
* AddTriangle (d.yEdge, b.zEdge, c.xEdge);*
* break;*
* case 247:*
* AddTriangle (c.xEdge, b.zEdge, d.yEdge);*
* break;*
* //The top left front (0,1,0) of the cell is the only active/inactive, draw a triangle there.*
* case 16:*
* AddTriangle (a.yEdge, e.xEdge, e.zEdge);*
* break;*
* case 239:*
* AddTriangle (e.zEdge, e.xEdge, a.yEdge);*
* break;*
* //The top right front (1,1,0) of the cell is the only active/inactive, draw a triangle there.*
* case 32:*
* AddTriangle (b.yEdge, f.zEdge, e.xEdge);*
* break;*
* case 223:*
* AddTriangle (e.xEdge, f.zEdge, b.yEdge);*
* break;*
* //The top left back (0,1,1) of the cell is the only active/inactive, draw a triangle there.*
* case 64:*
* AddTriangle (c.yEdge, e.zEdge, g.xEdge);*
* break;*
* case 191:*
* AddTriangle (c.yEdge, g.xEdge, e.zEdge);*
* break;*
* //The top right back (1,1,1) of the cell is the only active/inactive, draw a triangle there.*
* case 128:*
* AddTriangle (d.yEdge, g.xEdge, f.zEdge);*
* break;*
* case 127:*
* AddTriangle (f.zEdge, g.xEdge, d.yEdge);*
* break;*
* //All voxels are active, don’t draw the cell.*
* case 255:*
* return;*
* }*
* #endregion*
* }*
* //Adds a triangle based on the given points. The UV values are arbitrary guesses for now.*
* void AddTriangle (Vector3 a, Vector3 b, Vector3 c) {*
* int triIndex = vertices.Count;*
* vertices.Add (a);*
* vertices.Add (b);*
* vertices.Add (c);*
* uv.Add (new Vector2 (0,0));*
* uv.Add (new Vector2 (0.5f,1));*
* uv.Add (new Vector2 (1,0));*
* triangles.Add (triIndex);*
* triangles.Add (triIndex + 1);*
* triangles.Add (triIndex + 2);*
* }*
}