Skip to content

Instantly share code, notes, and snippets.

@Journeyman1337
Created August 11, 2020 22:47
Show Gist options
  • Save Journeyman1337/839bbaadc226e179bd18c3cb6374f0ad to your computer and use it in GitHub Desktop.
Save Journeyman1337/839bbaadc226e179bd18c3cb6374f0ad to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEditor.Build;
using UnityEngine;
using UnityEngine.Tilemaps;
/// <summary>
/// An experimental chunk made with a procedural mesh.
/// </summary>
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class Chunk : MonoBehaviour
{
private static int _chunk_id_counter = 0;
public MeshFilter MeshFilter { get; private set; }
public MeshRenderer MeshRenderer { get; private set; }
public int ChunkID { get; private set; }
private Mesh _mesh;
private Texture3D _alphamap;
private Vector3[] _vertices;
private int[] _triangles;
private void Awake()
{
ChunkID = _chunk_id_counter++;
name = ChunkID.ToString();
MeshFilter = GetComponent<MeshFilter>();
MeshRenderer = GetComponent<MeshRenderer>();
MeshFilter.sharedMesh = _mesh = new Mesh();
_mesh.MarkDynamic();
}
private void Start()
{
byte[,] test_heights = new byte[TerrainConstants.CHUNK_TILE_DIMENSIONS + 1, TerrainConstants.CHUNK_TILE_DIMENSIONS + 1];
for (var x = 0; x < test_heights.GetLength(0); x++)
{
for (var z = 0; z < test_heights.GetLength(1); z++)
{
test_heights[x, z] = (byte)UnityEngine.Random.Range(0, 255);
}
}
test_heights[0, 0] = 0;
BuildChunk(test_heights);
}
public void BuildChunk(byte[,] heights)
{
_mesh.Clear();
#region old methods
/*
var triangle_offset = 0;
for (var x = 0; x < TerrainConstants.CHUNK_TILE_DIMENSIONS; x++)
{
for (var z = 0; z < TerrainConstants.CHUNK_TILE_DIMENSIONS; z++)
{
vertices.Add(new Vector3(x * TerrainConstants.UNIT_CONVERT_SCALAR, heights[x, z] * TerrainConstants.SINGLE_HEIGHT_SIZE, z * TerrainConstants.UNIT_CONVERT_SCALAR));
vertices.Add(new Vector3((x + 1) * TerrainConstants.UNIT_CONVERT_SCALAR, heights[x, z] * TerrainConstants.SINGLE_HEIGHT_SIZE, z * TerrainConstants.UNIT_CONVERT_SCALAR));
vertices.Add(new Vector3((x + 1) * TerrainConstants.UNIT_CONVERT_SCALAR, heights[x, z] * TerrainConstants.SINGLE_HEIGHT_SIZE, (z + 1) * TerrainConstants.UNIT_CONVERT_SCALAR));
vertices.Add(new Vector3(x * TerrainConstants.UNIT_CONVERT_SCALAR, heights[x, z] * TerrainConstants.SINGLE_HEIGHT_SIZE, (z + 1) * TerrainConstants.UNIT_CONVERT_SCALAR));
vertices.Add(new Vector3( ((float)x + 0.5f) * TerrainConstants.UNIT_CONVERT_SCALAR, heights[x, z] * TerrainConstants.SINGLE_HEIGHT_SIZE, ((float)z + 0.5f) * TerrainConstants.UNIT_CONVERT_SCALAR));
triangles.Add(triangle_offset + 4);
triangles.Add(triangle_offset + 1);
triangles.Add(triangle_offset);
triangles.Add(triangle_offset + 4);
triangles.Add(triangle_offset + 2);
triangles.Add(triangle_offset + 1);
triangles.Add(triangle_offset + 4);
triangles.Add(triangle_offset + 3);
triangles.Add(triangle_offset + 2);
triangles.Add(triangle_offset + 4);
triangles.Add(triangle_offset);
triangles.Add(triangle_offset + 3);
triangle_offset += 5;
}
}
*/
/*
* Vector3[] vertices = new Vector3[
((TerrainConstants.CHUNK_TILE_DIMENSIONS + 1) * (TerrainConstants.CHUNK_TILE_DIMENSIONS + 1)) + //corners
(TerrainConstants.TILES_PER_CHUNK)]; //centers
int[] triangles = new int[TerrainConstants.TILES_PER_CHUNK * 4 * 3];
for (int x = 0, v = 0, t = 0; x <= TerrainConstants.CHUNK_TILE_DIMENSIONS; x++)
{
for (var z = 0; z <= TerrainConstants.CHUNK_TILE_DIMENSIONS; z++)
{
vertices[v++] = new Vector3(x * TerrainConstants.UNIT_CONVERT_SCALAR, (heights[x, z] + heights[x+1, z+1] / 2f) * TerrainConstants.SINGLE_HEIGHT_SIZE, z * TerrainConstants.UNIT_CONVERT_SCALAR); //corner
if (x != TerrainConstants.CHUNK_TILE_DIMENSIONS && z != TerrainConstants.CHUNK_TILE_DIMENSIONS)
{
vertices[v] = new Vector3((x + 0.5f) * TerrainConstants.UNIT_CONVERT_SCALAR, heights[x+1, z+1] * TerrainConstants.SINGLE_HEIGHT_SIZE, (z + 0.5f) * TerrainConstants.UNIT_CONVERT_SCALAR); //center
bool next_row_edge = ((x + 1 == TerrainConstants.CHUNK_TILE_DIMENSIONS) ? true : false);
bool next_column_edge = ((z + 1 == TerrainConstants.CHUNK_TILE_DIMENSIONS) ? true : false);
int tri1 = v + 1 - 1 + TerrainConstants.CHUNK_TILE_DIMENSIONS + (next_row_edge?TerrainConstants.CHUNK_TILE_DIMENSIONS - x:TerrainConstants.CHUNK_TILE_DIMENSIONS);
int tri2 = tri1 + (next_row_edge?0:1) + 1;
int tri3 = v + (next_column_edge?1:0) + 1;
int tri4 = v - 1;
int tri5 = v++;
//Debug.Log("1:" + tri1.ToString() + " 2:" + tri2.ToString() + " 3:" + tri3.ToString() + " 4:" + tri4.ToString() + " 5:" + tri5.ToString());
triangles[t++] = tri1;
triangles[t++] = tri5;
triangles[t++] = tri2;
triangles[t++] = tri2;
triangles[t++] = tri5;
triangles[t++] = tri3;
triangles[t++] = tri3;
triangles[t++] = tri5;
triangles[t++] = tri4;
triangles[t++] = tri4;
triangles[t++] = tri5;
triangles[t++] = tri1;
}
}
}
/*
_vertices = new Vector3[TerrainConstants.TILES_PER_CHUNK * 4];
_triangles = new int[TerrainConstants.TILES_PER_CHUNK * 2 * 3];
for (int x = 0, v = 0, t = 0; x < TerrainConstants.CHUNK_TILE_DIMENSIONS; x++)
{
for (var z = 0; z < TerrainConstants.CHUNK_TILE_DIMENSIONS; z++)
{
var triangle_offset = v;
_vertices[v++] = new Vector3(x * TerrainConstants.UNIT_CONVERT_SCALAR, heights[x, z] * TerrainConstants.SINGLE_HEIGHT_SIZE, z * TerrainConstants.UNIT_CONVERT_SCALAR);
_vertices[v++] = new Vector3((x + 1) * TerrainConstants.UNIT_CONVERT_SCALAR, heights[x, z] * TerrainConstants.SINGLE_HEIGHT_SIZE, z * TerrainConstants.UNIT_CONVERT_SCALAR);
_vertices[v++] = new Vector3((x + 1) * TerrainConstants.UNIT_CONVERT_SCALAR, heights[x, z] * TerrainConstants.SINGLE_HEIGHT_SIZE, (z + 1) * TerrainConstants.UNIT_CONVERT_SCALAR);
_vertices[v++] = new Vector3(x * TerrainConstants.UNIT_CONVERT_SCALAR, heights[x, z] * TerrainConstants.SINGLE_HEIGHT_SIZE, (z + 1) * TerrainConstants.UNIT_CONVERT_SCALAR);
_triangles[t++] = triangle_offset;
_triangles[t++] = triangle_offset + 2;
_triangles[t++] = triangle_offset + 1;
_triangles[t++] = triangle_offset;
_triangles[t++] = triangle_offset + 3;
_triangles[t++] = triangle_offset + 2;
}
}
*/
#endregion
_vertices = new Vector3[(TerrainConstants.CHUNK_TILE_DIMENSIONS + 1) * (TerrainConstants.CHUNK_TILE_DIMENSIONS + 1)];
for (int i = 0, z = 0; z <= TerrainConstants.CHUNK_TILE_DIMENSIONS; z++)
{
for (int x = 0; x <= TerrainConstants.CHUNK_TILE_DIMENSIONS; x++, i++)
{
_vertices[i] = new Vector3(x * TerrainConstants.UNIT_CONVERT_SCALAR, heights[x, z] * TerrainConstants.SINGLE_HEIGHT_SIZE, z * TerrainConstants.UNIT_CONVERT_SCALAR);
}
}
_triangles = new int[TerrainConstants.TILES_PER_CHUNK * 6];
for (int ti = 0, vi = 0, z = 0; z < TerrainConstants.CHUNK_TILE_DIMENSIONS; z++, vi++)
{
for (int x = 0; x < TerrainConstants.CHUNK_TILE_DIMENSIONS; x++, ti += 6, vi++)
{
_triangles[ti] = vi;
_triangles[ti + 3] = _triangles[ti + 2] = vi + 1;
_triangles[ti + 4] = _triangles[ti + 1] = vi + TerrainConstants.CHUNK_TILE_DIMENSIONS + 1;
_triangles[ti + 5] = vi + TerrainConstants.CHUNK_TILE_DIMENSIONS + 2;
}
}
_mesh.vertices = _vertices;
_mesh.triangles = _triangles;
_mesh.RecalculateBounds();
_mesh.MarkModified();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment