Skip to content

Add Shader to The Planet

This part will add color to the planet. To have different color on the continent, I need to calculate the elevation of continent.

The elevation will lead to the maximum and minimum value of the elevation.

This script below will get value the maximum and minimum value of the elevation.

for (int i = 0; i < noiseFilters.Length; i++)
        {
            if (settings.noiseLayers[i].enabled)
            {
                float mask = (settings.noiseLayers[i].useFirstLayerAsMask) ? firstLayerValue : 1;
                elevation += noiseFilters[i].Evaluate(pointOnUnitSphere)*mask;
            }
        }
        elevation = settings.planetRadius * (1 + elevation);
        elevationMinMax.AddValue(elevation);
        return pointOnUnitSphere * elevation;

The maximum and minimum value will be used to determine the gradient color for the continent. Using this script below will calculate the color based on the gradient.

public void UpdateColors()
    {
        Color[] colors = new Color[textureResolution];
        for( int i = 0; i<textureResolution; i++)
        {
            colors[i] = settings.gradient.Evaluate(i / (textureResolution - 1f));
        }
        texture.SetPixels(colors);
        texture.Apply();
        settings.planetMaterial.SetTexture("_texture", texture);
    }

To make the concept above possible we use rendering package in Unity called render pipeline lightweight.

We use this rendering pipeline to has our own customized Physically Based Rendering (PBR) to set the color based on gradient elevation.

Above is PBR settings I create to produce color material for the planet. I took the maximum and minimum value from the script above and determine the position using node in this PBR settings called “position”. Then using Inverse lerp node, we compare those 3 values to get the UV value for sample texture 2D. This value will become input into Albedo in PBR master.

(Visited 17 times, 1 visits today)

Be First to Comment

Leave a Reply

%d bloggers like this: