Created
July 27, 2016 23:23
-
-
Save Ethan-Bierlein/7935e9f2e80656a5ec009a3c4f946fc6 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 appropriate lighting for each voxel based on the voxel | |
/// data itself as a whole. | |
/// </summary> | |
public void GenerateVoxelLighting() | |
{ | |
// Fill the top layer of voxels with full lighting values to | |
// simulate sunlight hitting the top of the voxel data. | |
for(int x = 0; x < this.Width; x++) | |
{ | |
for(int z = 0; z < this.Depth; z++) | |
{ | |
Voxel currentVoxel = this.FetchVoxel(x, this.Height - 1, z); | |
currentVoxel.Lighting = 16; | |
this.SetVoxel(currentVoxel, x, this.Height - 1, z); | |
} | |
} | |
// Iterate downwards over the entire array of voxel | |
// data and determine the lighting values for the current | |
// voxel based on it's neighbors on the voxel layer | |
// above it. | |
for (int y = this.Height - 2; y >= 0; y--) | |
{ | |
// Fill the current layer with updated lighting values which are determined | |
// based on the neighbors of the voxels in the layer above. | |
for(int x = 0; x < this.Width; x++) | |
{ | |
for(int z = 0; z < this.Height; z++) | |
{ | |
Voxel currentVoxel = this.FetchVoxel(x, y, z); | |
Voxel currentVoxelUp = this.FetchVoxel(x, y + 1, z); | |
Voxel currentVoxelNorth = this.FetchVoxel(x, y, z + 1); | |
Voxel currentVoxelSouth = this.FetchVoxel(x, y, z - 1); | |
Voxel currentVoxelEast = this.FetchVoxel(x + 1, y, z); | |
Voxel currentVoxelWest = this.FetchVoxel(x - 1, y, z); | |
byte maxLightValue = new byte[] | |
{ | |
currentVoxel.Lighting, | |
currentVoxelUp.Lighting, | |
currentVoxelNorth.Lighting, | |
currentVoxelSouth.Lighting, | |
currentVoxelEast.Lighting, | |
currentVoxelWest.Lighting | |
}.Max(); | |
if(maxLightValue == currentVoxelUp.Lighting) | |
{ | |
currentVoxel.Lighting = (byte)MathHelper.Clamp(maxLightValue - currentVoxelUp.Transparency, 1, 16); | |
this.SetVoxel(currentVoxel, x, y, z); | |
} | |
else | |
{ | |
currentVoxel.Lighting = (byte)MathHelper.Clamp(maxLightValue - 1, 1, 16); | |
this.SetVoxel(currentVoxel, x, y, z); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment