- Home /
Perlin noise low resolution when scaling
Hi I've been playing around with perlin noise for a while. The issue occurred when I tried to scale down the perlin noise coordinates to create a texture which has much lower value differences but also perserving as much detail as possible. Creating a 4096x4096 texture with a scale factor of 0.1 creates a texture which is heavily pixelated. Here is the output:
Generating a map with resolution of 512x512 and scale of 0.1 results in exactly the same image but with lower output resolution. Changing the scale to 1 makes the squares (that somewhat resemble pixels) much smaller on both resolutions but it also makes the perlin noise features much smaller, as can be seen in the image below.
My expected result would be that the amount of these squares or "pixels" would be the same as the pixel count of the generated texture but that's not the case. For the purpose of generating the noise I'm using the built in Math.PerlinNoise() method.
Here is the code that I used for the purpose of generating the noise:
public static float[,] GenerateNoiseMap(int width, int height, float scale, int seed, int octaves, float persistance, float lacunarity, Vector2 offset)
{
float[,] noiseMap = new float[width, height];
System.Random prng = new System.Random(seed);
Vector2[] octaveOffsets = new Vector2[octaves];
for (int o = 0; o < octaves; o++)
{
float offsetX = prng.Next(-100000, 100000);
float offsetY = prng.Next(-100000, 100000);
octaveOffsets[o] = new Vector2(offsetX, offsetY);
}
scale = Mathf.Clamp(scale, 0.0001f, float.MaxValue);
float maxNoiseHeight = float.MinValue;
float minNoiseHeight = float.MaxValue;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
float amplitude = 1;
float frequency = 1;
float noiseHeight = 0;
for (int o = 0; o < octaves; o++)
{
float sampleX = (float) x / width * scale * frequency + octaveOffsets[o].x + offset.x;
float sampleY = (float) y / height * scale * frequency + octaveOffsets[o].y + offset.y;
float perlinValue = Mathf.PerlinNoise(sampleX, sampleY) / 2 - 1;
noiseHeight += perlinValue * amplitude;
amplitude *= persistance;
frequency *= lacunarity;
}
if (noiseHeight > maxNoiseHeight)
{
maxNoiseHeight = noiseHeight;
} else if (noiseHeight < minNoiseHeight)
{
minNoiseHeight = noiseHeight;
}
noiseMap[x, y] = noiseHeight;
}
}
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
noiseMap[x, y] = Mathf.InverseLerp(minNoiseHeight, maxNoiseHeight, noiseMap[x, y]);
}
}
return noiseMap;
}
The settings that I used during generation: height=4096, width = 4096, scale = 0.1, octaves = 8, persistance = 0.2, lacunarity = 2, seed = 0, offset = (0, 0).
And here is the code I used to convert the perlin noise float array to a Texture:
public static Texture2D Float2DToTexture2D(float[,] array)
{
int width = array.GetLength(0);
int height = array.GetLength(1);
Texture2D noiseTexture = new Texture2D(width, height);
Color32[] colorMap = new Color32[width * height];
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
colorMap[y * width + x] = Color32.Lerp(Color.black, Color.white, array[x, y]);
}
}
noiseTexture.SetPixels32(colorMap);
noiseTexture.Apply();
return noiseTexture;
}
Am I missing something in my usage of the PerlinNoise() method or is this a limitation of its implementation in Unity?
I'm using Unity 2021.1.0f.
Thank you in advance.