Skip to content

Add Biomes Effect

The biome effect will give different color on different height in the planet. Like picture below, I can put three different color to make 3 different biomes. To make this happen we need to create different color generator. In previous color generator, we only focus on gradient color, right now we put tint color to set the biome color.

public float BiomePercentFromPoint ( Vector3 pointOnUnitSphere)
    {
        float heightPercent = (pointOnUnitSphere.y + 1) / 2f + .001f;
        heightPercent += (biomeNoiseFilter.Evaluate(pointOnUnitSphere)-settings.biomeColorSettings.noiseOffset) * settings.biomeColorSettings.noiseStrength;
        float biomeIndex = 0;
        int numBiomes = settings.biomeColorSettings.biomes.Length;
        float blendRange = settings.biomeColorSettings.blendAmount / 2f;

        for (int i=0; i<numBiomes; i++)
        {
            float dst = heightPercent - settings.biomeColorSettings.biomes[i].startHeight;
            float weight = Mathf.InverseLerp(-blendRange, blendRange, dst);
            biomeIndex *= (1 - weight);
            biomeIndex += i * weight;
        }
        return biomeIndex / Mathf.Max(1, numBiomes - 1);
    }

This script above will calculate the position of each point. When we change the height of the biome it will change the point value and the color position will also change.

To make the color can blend with other biomes color, we need to use the noise filter again.

The picture above is the result of noise filter to make the color blend.

In terrainFaces.cs we add this script below

public void UpdateUVs(ColorGenerator colorGenerator)
    {
        Vector2[] uv = new Vector2[resolution * resolution];

        for (int y = 0; y < resolution; y++)
        {
            for (int x = 0; x < resolution; x++)
            {
                int i = x + y * resolution;
                Vector2 percent = new Vector2(x, y) / (resolution - 1);
                Vector3 pointOnUnitCube = localUp + (percent.x - .5f) * 2 * axisA + (percent.y - .5f) * 2 * axisB;
                Vector3 pointOnUnitSphere = pointOnUnitCube.normalized;

                uv[i] = new Vector2(colorGenerator.BiomePercentFromPoint(pointOnUnitSphere), 0);
            }
        }
        mesh.uv = uv;
    }

This script will create the UV color to blend with other color.

(Visited 25 times, 1 visits today)

Be First to Comment

Leave a Reply

%d bloggers like this: