Why does Perlin-generated Texture lose gradient when with colour?

I am generating a Texture to be used as a Splatmap using Perlin noise. As a greyscale image, it generates as expected, with normal noise waves, gradients, etc. This is an example:

10062-map999.png

But when converting the Perlin noise output to R, G and B values using the below code, the gradient is lost and instead it is solid blobs of colour, example below. Why is this?

For clarity: I would like the colours to be blue where the Perlin output is 0, gradually change to red at 0.25, stay as red until 0.75, then gradually change to green at 1.0. In the picture is it pink where should be blue, and yellow where should be green.

        for (var y = 0.0; y < noiseTex.height; y++) {
            for (var x = 0.0; x < noiseTex.width; x++) {
                // Get a sample from the corresponding position in the noise plane
                // and create a greyscale pixel from it.
                var xCoord = xOrg + x / noiseTex.width * scale;
                var yCoord = yOrg + y / noiseTex.height * scale;
                var sample = Mathf.PerlinNoise (xCoord, yCoord);
                var r;
                var g;
                var b;
                if (sample > 0 && sample < 0.25f) {
                	r = sample * 1020;
                	g = 0;
                	b = ((sample * 1020) * -1) + 255;
                }  
                if (sample > 0.25 && sample < 0.75) {
                	r = 255;
                	g = 0;
                	b = 0;
                } 
                if (sample > 0.75 && sample < 1.0) {
                	r = ((sample * 1020) * -1) + 1020;
                	g = (sample * 1020) - 765;
                	b = 0;
                }               
                pix[y * noiseTex.width + x] = Color(sample, sample, sample);

//Or     pix[y * noiseTex.width + x] = Color(r, g, b);    for coloured!

            }
        }

10064-map9999.png

Thanks in advance!

1 Answer

1

It’s because 1 values in Unity are 0-1, not 0-255…