- Home /
creating a perlin noise generates all pixels the with the same shade of gray
hello everyone, i am trying to create a bump map for a terrain using perlin noise. first time i use it and firt time i create a texture in script. if someone could pls check and see where did i go wrong i would much apreciate it. here is the script:
using UnityEngine;
using System.Collections;
public class Perlin_Noise_Texture : MonoBehaviour
{
public Texture2D pNoise;
public int width;
public int height;
public Color[] pixlColor;
void Start ()
{
pixlColor = new Color[width * height];
pNoise = new Texture2D(width, height, TextureFormat.ARGB32, false);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int currPixl = width * y + x;
float currPNC = Mathf.PerlinNoise(x, y);
pixlColor[currPixl] = new Color(currPNC, currPNC, currPNC, 1);
pNoise.SetPixel(x, y, pixlColor[currPixl]);
}
}
pNoise.Apply();
}
void Update ()
{
}
}
its probably something realy stupid but its driving me crazy. thanks again for your time.
I'd check out http://docs.unity3d.com/ScriptReference/$$anonymous$$athf.PerlinNoise.html
The textureFormat could be a problem too.
thanks, though im still getting the same results. thanks a lot.
Answer by Paulius-Liekis · Jan 15, 2015 at 06:29 PM
You passing int values to Mathf.PerlinNoise(x, y). It takes float (from 0 to 10). You should do something like this:
Mathf.PerlinNoise((float)x/width*10, (float)y/height*10)
oh, thanks a lot. it is still giving me the exact same value regardless of the vector 2 i input. I was wondering: does $$anonymous$$athf.PerlinNoise use directX? because if so i think that might be the problem, or a problem.
No, I don't think it uses DirectX. I edited my answer. You code works fine for me (with my change), I tested it. It even works without "*10" part (you just get less "waves"). Are you sure you didn't mess up something? What is the size of texture that you're generating?
Answer by Bene_Dev · Jun 12, 2018 at 12:14 PM
I was just trying to create a texture2D for a different purpose, as i stumbled upon your question. I based my solution on your code and I got the error removed, as I was changing the pixlColor to be a temporary variable. This is my code:
[SerializeField] GameObject imageObject;
RawImage image;
Texture2D mapFogTexture;
int textureWidth;
int textureHeight;
private void Awake()
{
image = imageObject.GetComponent<RawImage>();
textureWidth = (int)imageObject.GetComponent<RectTransform>().rect.width;
textureHeight = (int)imageObject.GetComponent<RectTransform>().rect.height;
mapFogTexture = new Texture2D(textureWidth, textureHeight);
for (int x = 0; x < textureWidth; x++)
{
for (int y = 0; y < textureHeight; y++)
{
int currPixl = textureWidth * y + x;
float currPNC = Mathf.PerlinNoise((float)x / textureWidth * 10, (float)y / textureHeight * 10);
Color tempColor = new Color(currPNC, currPNC, currPNC, 1);
mapFogTexture.SetPixel(x, y, tempColor);
}
}
mapFogTexture.Apply();
image.texture = mapFogTexture;
}
I hope this helps you or people watching this question later.
Your answer
Follow this Question
Related Questions
Why does Perlin-generated Texture lose gradient when with colour? 1 Answer
Applying noise to a texture 1 Answer
Using noise as texture 2 Answers
Creating Perlin Noise 2 Answers
Using SetPixel in Update() 1 Answer