Skip to content

Instantly share code, notes, and snippets.

@GarethIW GarethIW/GenerateVoxels.cs Secret
Created Aug 19, 2017

Embed
What would you like to do?
// Get the volume and perform an initial generation
var voxelVolume = GetComponent<Volume>();
voxelVolume.GenerateBasic(FillMode.None);
// Set the desired size (in voxels)
voxelVolume.XSize = 32;
voxelVolume.YSize = 32;
voxelVolume.ZSize = 32;
voxelVolume.Frames[0].XSize = voxelVolume.XSize;
voxelVolume.Frames[0].YSize = voxelVolume.YSize;
voxelVolume.Frames[0].ZSize = voxelVolume.ZSize;
// Initialise the array
voxelVolume.Frames[0].Voxels = new Voxel[voxelVolume.XSize * voxelVolume.YSize * voxelVolume.ZSize];
// Set the value of all voxels to 128 (if needed)
for (int i = 0; i < voxelVolume.Frames[0].Voxels.Length; i++) voxelVolume.Frames[0].Voxels[i].Value = 128;
/////////////////////////////
// This is where you would add voxels to the model. You can access the array directly:
int index = x + voxelVolume.XSize * (y + voxelVolume.YSize * z); // where x,y, and z are the array co-ordinates of the voxel
voxelVolume.Frames[0].Voxels[index].Color = new Color(1f, 0f, 0f, 1f);
voxelVolume.Frames[0].Voxels[index].State = VoxelState.Active;
voxelVolume.Frames[0].Voxels[index].Value = 128;
//// Or, you can use the helper functions on the Volume:
voxelVolume.SetVoxelAtArrayPosition(x, y, z, new Voxel() { State = VoxelState.Active, Color = new Color(1f, 0f, 0f, 1f), Value = 128 });
/////////////////////////////
// This is an example of a random noise volume:
//for (int i = 0; i < voxelVolume.Frames[0].Voxels.Length; i++)
// voxelVolume.Frames[0].Voxels[i] = new Voxel() { Color=Color.red, State=(VoxelState)Random.Range(0,2) };
// Set the Unity unit size of the voxels
voxelVolume.VoxelSize = 0.1f;
// Create the chunks
voxelVolume.CreateChunks();
// Set the pivot if necessary (this sets the pivot to the center of the volume)
voxelVolume.Pivot = (new Vector3(voxelVolume.XSize, voxelVolume.YSize, voxelVolume.ZSize) * voxelVolume.VoxelSize) / 2f;
voxelVolume.UpdatePivot();
// Update the chunks after setting the pivot
voxelVolume.UpdateAllChunks();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.