- Home /
Mathf.PerlinNoise only outputs a single number in editor window
I have created a button that opens a editor window that displays a texture as a way to preview my heightmap/noisemap, but when i try to use Mathf.PerlinNoise i only get the nummer 0.4652731 regardless of what values i input. Any ideas why it only outputs 0.4652731?
On the third code sample you can see the specific problem, but i included the whole call sequence.
if(GUILayout.Button("Preview Noise"))
{
texture = TerrainNoiseGeneration.NoiseMap(10,10);
TerrainGeneratorEditorWindow.OpenNoisePreviewWindow(texture);
}
public static void OpenNoisePreviewWindow(Texture2D previewTexture)
{
EditorWindow.GetWindow<TerrainGeneratorEditorWindow>("Preview Noise");
EditorWindow.GetWindow<TerrainGeneratorEditorWindow>("Preview Noise").texture = previewTexture;
}
void OnGUI()
{
GUILayout.Label("Test", EditorStyles.boldLabel);
//texture = new Texture2D(200, 200);
if (texture == null)
{
texture = Texture2D.blackTexture;
}
EditorGUI.DrawPreviewTexture(new Rect(20, 20, 300, 300), texture);
}
public static Texture2D NoiseMap(int mapWidth, int mapHeight)
{
float[,] noiseMap = new float[mapWidth, mapHeight];
float maxNoiseHeight = float.MinValue;
float minNoiseHeight = float.MaxValue;
float noiseHeight = 0;
//Mathf.PerlinNoise
// float centerWidth = mapWidth / 2f;
// float centerHeight = mapHeight / 2f;
for (int y = 0; y < mapHeight; y++)
{
for (int x = 0; x < mapWidth; x++)
{
//values
float sampleX = x;
float sampleY = y;
float perlinNoise = Mathf.PerlinNoise(sampleX, sampleY); //0.4652731 ???
noiseHeight = perlinNoise;
if (noiseHeight > maxNoiseHeight)
{
maxNoiseHeight = noiseHeight;
}
else if (noiseHeight < minNoiseHeight)
{
minNoiseHeight = noiseHeight;
}
noiseMap[x, y] = perlinNoise;
}
}
for (int y = 0; y < mapHeight; y++)
{
for (int x = 0; x < mapWidth; x++)
{
noiseMap[x, y] = Mathf.InverseLerp(minNoiseHeight, maxNoiseHeight, noiseMap[x, y]);
}
}
int width = noiseMap.GetLength(0);
int height = noiseMap.GetLength(1);
Texture2D texture = new Texture2D(width, height);
texture.GetPixels();
Color[] colourMap = new Color[width * height];
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
colourMap[y * width + x] = Color.Lerp(Color.black, Color.white, noiseMap[x, y]);
}
}
texture.SetPixels(colourMap);
texture.Apply();
return texture;
}
Answer by Eno-Khaon · Jan 17, 2021 at 11:29 PM
Unity's Perlin Noise sampling repeats every 1.0 value. Because you're sampling using your integer iterators directly, you're sampling exactly 1 position in the repeating pattern over and over again.
For a simple, scale-independent solution to the problem, you can divide your inputs by your map width and height.
float sampleX = (float)x / mapWidth;
float sampleY = (float)y / mapHeight;
Thanks, that kind of helped, so i tried to just added additional float value to the X and Y value and it solved it
float sampleX = (x + 0.01f) / mapWidth;
float sampleY = (y + 0.01f) / mapHeight;
Ah, whoops. $$anonymous$$y mistake on that. I forgot to factor in integer math being performed in this case. $$anonymous$$odified my answer to reflect my intent, although your solution works well.
Your answer
![](https://koobas.hobune.stream/wayback/20220613023501im_/https://answers.unity.com/themes/thub/images/avi.jpg)