Author Topic: Gradient block models from central point of chunk  (Read 44 times)

Brad

  • Newbie
  • *
  • Posts: 2
    • View Profile
Gradient block models from central point of chunk
« on: January 07, 2019, 06:05:05 AM »
Hi,

I've been trying to find something on this but can't see anything already noted, I'm looking to have it so that there is a gradient to colour the blocks as they disperse and get lower. I thought using noise would help with this but no idea on how to implement it within this framework.

I have created an example here and attached it.


Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 4083
    • View Profile
Re: Gradient block models from central point of chunk
« Reply #1 on: January 07, 2019, 09:20:50 AM »
This can be achieved modifying the detault terrain generator, basically specifying a tint color when setting a voxel in the PaintChunk() method.

1) Locate TerrainDefaultGenerator.cs script.

2) Locate the PaintChunk() method. In that method you will find this line:

Code
voxels [voxelIndex].SetFast (biome.voxelDirt, 15, 1);

Modify it with:

Code
voxels [voxelIndex].SetFast (biome.voxelDirt, 15, 1, tintColor);

Where tintColor is the desired color for the voxel which you can previously compute based on the elevation (pos.y contains the elevation, you will need to decrease it in the inner loop as well).

Brad

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Gradient block models from central point of chunk
« Reply #2 on: January 07, 2019, 02:34:30 PM »
How exactly would I go about setting the tint in comparison to the pos.y?

Would you be able to provide a further example with a colour specified?

Thanks

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 4083
    • View Profile
Re: Gradient block models from central point of chunk
« Reply #3 on: January 08, 2019, 12:49:57 AM »
Sample code:

Code
					const float minAltitudeGradient = 0;
const float maxAltitudeGradient = 50;

while (voxelIndex >= 0) {
if (voxels [voxelIndex].hasContent == 0) {
byte g = (byte)(255 * Mathf.Clamp01 ((pos.y - minAltitudeGradient) / (maxAltitudeGradient - minAltitudeGradient)));
Color32 tintColor = new Color32 (g, g, g, 255);
voxels [voxelIndex].SetFast (biome.voxelDirt, 15, 1, tintColor);
}
voxelIndex -= ONE_Y_ROW;
pos.y--;
}

There's another SetFast(biome.voxelDirt) above the same lines so you will need to change that as well to pass the tintColor.
Also remember to enable tinting in Voxel Play environment inspector.

Note that this code only sets the tintColor for the terrain - if you paint voxels using other methods you will need to pass the appropriate tintColor to the VoxelPlace method.

Probably an easier way to achieve a vertical gradient effect would be to change the shader themselves so they affect everything if that's what you want.