Created
January 18, 2017 23:14
-
-
Save Ethan-Bierlein/5d944d7461203e305a76e31b129ec099 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Generate the chunk mesh. | |
/// </summary> | |
public void GenerateMesh() | |
{ | |
Stopwatch stopwatch = new Stopwatch(); | |
stopwatch.Start(); | |
DynamicMesh chunkDynamicMesh = new DynamicMesh(65535); | |
for(int x = 0; x < CHUNK_WIDTH; x++) | |
{ | |
for(int y = 0; y < CHUNK_HEIGHT; y++) | |
{ | |
for(int z = 0; z < CHUNK_DEPTH; z++) | |
{ | |
IVoxelBase currentVoxel = this.GetVoxel(x, y, z); | |
VoxelProperties voxelProperties = currentVoxel.GetProperties(); | |
if(voxelProperties.IsMeshable) | |
{ | |
IVoxelBase northVoxel = this.GetVoxel(x, y, z + 1); | |
IVoxelBase southVoxel = this.GetVoxel(x, y, z - 1); | |
IVoxelBase eastVoxel = this.GetVoxel(x + 1, y, z); | |
IVoxelBase westVoxel = this.GetVoxel(x - 1, y, z); | |
IVoxelBase upVoxel = this.GetVoxel(x, y + 1, z); | |
IVoxelBase downVoxel = this.GetVoxel(x, y - 1, z); | |
Color northFaceColor = ((Color)voxelProperties.Color * 0.9f) * (northVoxel.Lighting / 16.0f); | |
Color southFaceColor = ((Color)voxelProperties.Color * 0.9f) * (southVoxel.Lighting / 16.0f); | |
Color eastFaceColor = ((Color)voxelProperties.Color * 0.8f) * (eastVoxel.Lighting / 16.0f); | |
Color westFaceColor = ((Color)voxelProperties.Color * 0.8f) * (westVoxel.Lighting / 16.0f); | |
Color upFaceColor = ((Color)voxelProperties.Color * 1.0f) * (upVoxel.Lighting / 16.0f); | |
Color downFaceColor = ((Color)voxelProperties.Color * 0.7f) * (downVoxel.Lighting / 16.0f); | |
northFaceColor.a = 1.0f; | |
southFaceColor.a = 1.0f; | |
eastFaceColor.a = 1.0f; | |
westFaceColor.a = 1.0f; | |
upFaceColor.a = 1.0f; | |
downFaceColor.a = 1.0f; | |
if(!northVoxel.GetProperties().IsSolid) | |
{ | |
float aoFactorOne = this.CalculateAOFactor(this.GetVoxel(x + 1, y - 1, z + 1), this.GetVoxel(x + 1, y, z + 1), this.GetVoxel(x, y - 1, z + 1), AO_DARKNESS_LIMIT); | |
float aoFactorTwo = this.CalculateAOFactor(this.GetVoxel(x + 1, y + 1, z + 1), this.GetVoxel(x + 1, y, z + 1), this.GetVoxel(x, y + 1, z + 1), AO_DARKNESS_LIMIT); | |
float aoFactorThree = this.CalculateAOFactor(this.GetVoxel(x - 1, y + 1, z + 1), this.GetVoxel(x - 1, y, z + 1), this.GetVoxel(x, y + 1, z + 1), AO_DARKNESS_LIMIT); | |
float aoFactorFour = this.CalculateAOFactor(this.GetVoxel(x - 1, y - 1, z + 1), this.GetVoxel(x - 1, y, z + 1), this.GetVoxel(x, y - 1, z + 1), AO_DARKNESS_LIMIT); | |
chunkDynamicMesh.AddVertex(new Vector3(x + voxelProperties.MeshRadii.x, y - voxelProperties.MeshRadii.y, z + voxelProperties.MeshRadii.z), northFaceColor * aoFactorOne); | |
chunkDynamicMesh.AddVertex(new Vector3(x + voxelProperties.MeshRadii.x, y + voxelProperties.MeshRadii.y, z + voxelProperties.MeshRadii.z), northFaceColor * aoFactorTwo); | |
chunkDynamicMesh.AddVertex(new Vector3(x - voxelProperties.MeshRadii.x, y + voxelProperties.MeshRadii.y, z + voxelProperties.MeshRadii.z), northFaceColor * aoFactorThree); | |
chunkDynamicMesh.AddVertex(new Vector3(x - voxelProperties.MeshRadii.x, y - voxelProperties.MeshRadii.y, z + voxelProperties.MeshRadii.z), northFaceColor * aoFactorFour); | |
chunkDynamicMesh.AddUVs(currentVoxel.GetFaceUVs(VoxelFace.North)); | |
chunkDynamicMesh.AddQuad(aoFactorOne + aoFactorThree > aoFactorTwo + aoFactorFour); | |
} | |
if(!southVoxel.GetProperties().IsSolid) | |
{ | |
float aoFactorOne = this.CalculateAOFactor(this.GetVoxel(x - 1, y - 1, z - 1), this.GetVoxel(x - 1, y, z - 1), this.GetVoxel(x, y - 1, z - 1), AO_DARKNESS_LIMIT); | |
float aoFactorTwo = this.CalculateAOFactor(this.GetVoxel(x - 1, y + 1, z - 1), this.GetVoxel(x - 1, y, z - 1), this.GetVoxel(x, y + 1, z - 1), AO_DARKNESS_LIMIT); ; | |
float aoFactorThree = this.CalculateAOFactor(this.GetVoxel(x + 1, y + 1, z - 1), this.GetVoxel(x + 1, y, z - 1), this.GetVoxel(x, y + 1, z - 1), AO_DARKNESS_LIMIT); ; | |
float aoFactorFour = this.CalculateAOFactor(this.GetVoxel(x + 1, y - 1, z - 1), this.GetVoxel(x + 1, y, z - 1), this.GetVoxel(x, y - 1, z - 1), AO_DARKNESS_LIMIT); ; | |
chunkDynamicMesh.AddVertex(new Vector3(x - voxelProperties.MeshRadii.x, y - voxelProperties.MeshRadii.y, z - voxelProperties.MeshRadii.z), southFaceColor * aoFactorOne); | |
chunkDynamicMesh.AddVertex(new Vector3(x - voxelProperties.MeshRadii.x, y + voxelProperties.MeshRadii.y, z - voxelProperties.MeshRadii.z), southFaceColor * aoFactorTwo); | |
chunkDynamicMesh.AddVertex(new Vector3(x + voxelProperties.MeshRadii.x, y + voxelProperties.MeshRadii.y, z - voxelProperties.MeshRadii.z), southFaceColor * aoFactorThree); | |
chunkDynamicMesh.AddVertex(new Vector3(x + voxelProperties.MeshRadii.x, y - voxelProperties.MeshRadii.y, z - voxelProperties.MeshRadii.z), southFaceColor * aoFactorFour); | |
chunkDynamicMesh.AddUVs(currentVoxel.GetFaceUVs(VoxelFace.South)); | |
chunkDynamicMesh.AddQuad(aoFactorOne + aoFactorThree > aoFactorTwo + aoFactorFour); | |
} | |
if(!eastVoxel.GetProperties().IsSolid) | |
{ | |
float aoFactorOne = this.CalculateAOFactor(this.GetVoxel(x + 1, y - 1, z - 1), this.GetVoxel(x + 1, y - 1, z), this.GetVoxel(x + 1, y, z - 1), AO_DARKNESS_LIMIT); | |
float aoFactorTwo = this.CalculateAOFactor(this.GetVoxel(x + 1, y + 1, z - 1), this.GetVoxel(x + 1, y + 1, z), this.GetVoxel(x + 1, y, z - 1), AO_DARKNESS_LIMIT); | |
float aoFactorThree = this.CalculateAOFactor(this.GetVoxel(x + 1, y + 1, z + 1), this.GetVoxel(x + 1, y + 1, z), this.GetVoxel(x + 1, y, z + 1), AO_DARKNESS_LIMIT); | |
float aoFactorFour = this.CalculateAOFactor(this.GetVoxel(x + 1, y - 1, z + 1), this.GetVoxel(x + 1, y - 1, z), this.GetVoxel(x + 1, y, z + 1), AO_DARKNESS_LIMIT); | |
chunkDynamicMesh.AddVertex(new Vector3(x + voxelProperties.MeshRadii.x, y - voxelProperties.MeshRadii.y, z - voxelProperties.MeshRadii.z), eastFaceColor * aoFactorOne); | |
chunkDynamicMesh.AddVertex(new Vector3(x + voxelProperties.MeshRadii.x, y + voxelProperties.MeshRadii.y, z - voxelProperties.MeshRadii.z), eastFaceColor * aoFactorTwo); | |
chunkDynamicMesh.AddVertex(new Vector3(x + voxelProperties.MeshRadii.x, y + voxelProperties.MeshRadii.y, z + voxelProperties.MeshRadii.z), eastFaceColor * aoFactorThree); | |
chunkDynamicMesh.AddVertex(new Vector3(x + voxelProperties.MeshRadii.x, y - voxelProperties.MeshRadii.y, z + voxelProperties.MeshRadii.z), eastFaceColor * aoFactorFour); | |
chunkDynamicMesh.AddUVs(currentVoxel.GetFaceUVs(VoxelFace.East)); | |
chunkDynamicMesh.AddQuad(aoFactorOne + aoFactorThree > aoFactorTwo + aoFactorFour); | |
} | |
if(!westVoxel.GetProperties().IsSolid) | |
{ | |
float aoFactorOne = this.CalculateAOFactor(this.GetVoxel(x - 1, y - 1, z + 1), this.GetVoxel(x - 1, y - 1, z), this.GetVoxel(x - 1, y, z + 1), AO_DARKNESS_LIMIT); | |
float aoFactorTwo = this.CalculateAOFactor(this.GetVoxel(x - 1, y + 1, z + 1), this.GetVoxel(x - 1, y + 1, z), this.GetVoxel(x - 1, y, z + 1), AO_DARKNESS_LIMIT); | |
float aoFactorThree = this.CalculateAOFactor(this.GetVoxel(x - 1, y + 1, z - 1), this.GetVoxel(x - 1, y + 1, z), this.GetVoxel(x - 1, y, z - 1), AO_DARKNESS_LIMIT); | |
float aoFactorFour = this.CalculateAOFactor(this.GetVoxel(x - 1, y - 1, z - 1), this.GetVoxel(x - 1, y - 1, z), this.GetVoxel(x - 1, y, z - 1), AO_DARKNESS_LIMIT); | |
chunkDynamicMesh.AddVertex(new Vector3(x - voxelProperties.MeshRadii.x, y - voxelProperties.MeshRadii.y, z + voxelProperties.MeshRadii.z), westFaceColor * aoFactorOne); | |
chunkDynamicMesh.AddVertex(new Vector3(x - voxelProperties.MeshRadii.x, y + voxelProperties.MeshRadii.y, z + voxelProperties.MeshRadii.z), westFaceColor * aoFactorTwo); | |
chunkDynamicMesh.AddVertex(new Vector3(x - voxelProperties.MeshRadii.x, y + voxelProperties.MeshRadii.y, z - voxelProperties.MeshRadii.z), westFaceColor * aoFactorThree); | |
chunkDynamicMesh.AddVertex(new Vector3(x - voxelProperties.MeshRadii.x, y - voxelProperties.MeshRadii.y, z - voxelProperties.MeshRadii.z), westFaceColor * aoFactorFour); | |
chunkDynamicMesh.AddUVs(currentVoxel.GetFaceUVs(VoxelFace.West)); | |
chunkDynamicMesh.AddQuad(aoFactorOne + aoFactorThree > aoFactorTwo + aoFactorFour); | |
} | |
if(!upVoxel.GetProperties().IsSolid) | |
{ | |
float aoFactorOne = this.CalculateAOFactor(this.GetVoxel(x - 1, y + 1, z + 1), this.GetVoxel(x - 1, y + 1, z), this.GetVoxel(x, y + 1, z + 1), AO_DARKNESS_LIMIT); | |
float aoFactorTwo = this.CalculateAOFactor(this.GetVoxel(x + 1, y + 1, z + 1), this.GetVoxel(x + 1, y + 1, z), this.GetVoxel(x, y + 1, z + 1), AO_DARKNESS_LIMIT); | |
float aoFactorThree = this.CalculateAOFactor(this.GetVoxel(x + 1, y + 1, z - 1), this.GetVoxel(x + 1, y + 1, z), this.GetVoxel(x, y + 1, z - 1), AO_DARKNESS_LIMIT); | |
float aoFactorFour = this.CalculateAOFactor(this.GetVoxel(x - 1, y + 1, z - 1), this.GetVoxel(x - 1, y + 1, z), this.GetVoxel(x, y + 1, z - 1), AO_DARKNESS_LIMIT); | |
chunkDynamicMesh.AddVertex(new Vector3(x - voxelProperties.MeshRadii.x, y + voxelProperties.MeshRadii.y, z + voxelProperties.MeshRadii.z), upFaceColor * aoFactorOne); | |
chunkDynamicMesh.AddVertex(new Vector3(x + voxelProperties.MeshRadii.x, y + voxelProperties.MeshRadii.y, z + voxelProperties.MeshRadii.z), upFaceColor * aoFactorTwo); | |
chunkDynamicMesh.AddVertex(new Vector3(x + voxelProperties.MeshRadii.x, y + voxelProperties.MeshRadii.y, z - voxelProperties.MeshRadii.z), upFaceColor * aoFactorThree); | |
chunkDynamicMesh.AddVertex(new Vector3(x - voxelProperties.MeshRadii.x, y + voxelProperties.MeshRadii.y, z - voxelProperties.MeshRadii.z), upFaceColor * aoFactorFour); | |
chunkDynamicMesh.AddUVs(currentVoxel.GetFaceUVs(VoxelFace.Up)); | |
chunkDynamicMesh.AddQuad(aoFactorOne + aoFactorThree > aoFactorTwo + aoFactorFour); | |
} | |
if(!downVoxel.GetProperties().IsSolid) | |
{ | |
float aoFactorOne = this.CalculateAOFactor(this.GetVoxel(x - 1, y - 1, z - 1), this.GetVoxel(x - 1, y - 1, z), this.GetVoxel(x, y - 1, z - 1), AO_DARKNESS_LIMIT); | |
float aoFactorTwo = this.CalculateAOFactor(this.GetVoxel(x + 1, y - 1, z - 1), this.GetVoxel(x + 1, y - 1, z), this.GetVoxel(x, y - 1, z - 1), AO_DARKNESS_LIMIT); | |
float aoFactorThree = this.CalculateAOFactor(this.GetVoxel(x + 1, y - 1, z + 1), this.GetVoxel(x + 1, y - 1, z), this.GetVoxel(x, y - 1, z + 1), AO_DARKNESS_LIMIT); | |
float aoFactorFour = this.CalculateAOFactor(this.GetVoxel(x - 1, y - 1, z + 1), this.GetVoxel(x - 1, y - 1, z), this.GetVoxel(x, y - 1, z + 1), AO_DARKNESS_LIMIT); | |
chunkDynamicMesh.AddVertex(new Vector3(x - voxelProperties.MeshRadii.x, y - voxelProperties.MeshRadii.y, z - voxelProperties.MeshRadii.z), downFaceColor); | |
chunkDynamicMesh.AddVertex(new Vector3(x + voxelProperties.MeshRadii.x, y - voxelProperties.MeshRadii.y, z - voxelProperties.MeshRadii.z), downFaceColor); | |
chunkDynamicMesh.AddVertex(new Vector3(x + voxelProperties.MeshRadii.x, y - voxelProperties.MeshRadii.y, z + voxelProperties.MeshRadii.z), downFaceColor); | |
chunkDynamicMesh.AddVertex(new Vector3(x - voxelProperties.MeshRadii.x, y - voxelProperties.MeshRadii.y, z + voxelProperties.MeshRadii.z), downFaceColor); | |
chunkDynamicMesh.AddUVs(currentVoxel.GetFaceUVs(VoxelFace.Down)); | |
chunkDynamicMesh.AddQuad(aoFactorOne + aoFactorThree > aoFactorTwo + aoFactorFour); | |
} | |
} | |
} | |
} | |
} | |
this.MeshFilter.mesh.Clear(); | |
this.MeshFilter.mesh.vertices = chunkDynamicMesh.RenderingVertices.ToArray(); | |
this.MeshFilter.mesh.triangles = chunkDynamicMesh.RenderingTriangles.ToArray(); | |
this.MeshFilter.mesh.uv = chunkDynamicMesh.RenderingUVs.ToArray(); | |
this.MeshFilter.mesh.colors = chunkDynamicMesh.RenderingColors.ToArray(); | |
this.MeshFilter.mesh.RecalculateNormals(); | |
Mesh newMeshCollider = new Mesh(); | |
newMeshCollider.vertices = chunkDynamicMesh.ColliderVertices.ToArray(); | |
newMeshCollider.triangles = chunkDynamicMesh.ColliderTriangles.ToArray(); | |
this.MeshCollider.sharedMesh = null; | |
this.MeshCollider.sharedMesh = newMeshCollider; | |
stopwatch.Stop(); | |
UnityEngine.Debug.Log(stopwatch.ElapsedMilliseconds); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment