0
\$\begingroup\$

I'm following Brackeys' tutorial for perlin noise and i'm at the point where he's just done the offset thing but this whole time all that i'm seeing in my project is a plain gray square, no matter what settings i change. Here's my code:

using UnityEngine;

public class PerlinNoise : MonoBehaviour
{
    public int width = 256;
    public int height = 256;

    public float scale = 20f;

    public float offsetX = 100f;
    public float offsetY = 100f;

    void Update ()
    {
        Renderer renderer = GetComponent<Renderer>();
        renderer.material.mainTexture = GenerateTexture();
    }

    Texture2D GenerateTexture ()
    {
        Texture2D texture = new Texture2D(width, height);

        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                Color color = CalculateColor(x, y);
                texture.SetPixel(x, y, color);
            }
        }

        texture.Apply();
        return texture;
    }

    Color CalculateColor (int x, int y)
    {
        float xCoord = (float)x / width * scale + offsetX;
        float yCoord = (float)y / height * scale + offsetY;

        float sample = Mathf.PerlinNoise(x, y);
        return new Color(sample, sample, sample);
    }
}

Here's the result i get: A gray square with no visible noise.

and here's the settings in the editor: Unity editor script settings.

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

In the function CalculateColor, you calculate values for the variables xCoord and yCoord, but then you never use them. You are using the pixel coordinates x and y instead. The result is that you are generating perlin noise on integer values. Due to the way Perlin noise works, integer values will always give you the same results.

Try this instead:

float sample = Mathf.PerlinNoise(xCoord, yCoord);

By the way, Perlin noise is actually obsolete. Over 20 years ago, Ken Perlin himself created Simplex Noise as a drop-in replacement which not only has less weird quirks and artifacts (including this one) but is also faster to compute. The reason why it didn't replace Perlin noise already is because Simplex noise used to be patented. But the patent ran out 2 years ago. So nowadays Simplex noise is almost always the better choice. You can find a simplex noise implementation in the Unity math library. If you want to give it a try, add using Unity.Mathematics; at the top of your script, and try this instead:

float sample = noise.snoise(new float2(xCoord, yCoord)) * 0.5f + 0.5f;

(the * 0.5f + 0.5f part is required because noise.snoise generates noise in the range of -1 to 1 instead of 0 to 1 like Mathf.PerlinNoise).

\$\endgroup\$
2
  • \$\begingroup\$ thx for responding haha, i posted this in so many places and got no replies \$\endgroup\$ Commented Jun 25 at 17:31
  • \$\begingroup\$ @IamaPineapple24 When this answer answered your question, please mark it as accepted it by clicking the checkmark-icon next to it. \$\endgroup\$
    – Philipp
    Commented Jun 26 at 9:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .