- Home /
How to use Mathf.PerlinNoise ?
Hello, for several projects now i have tried to use perlin noise with aboslutely no succes. I have searched everywhere on information, and tried hundreds of inputs(literaly). I have bveen unable to pinpoint what am i doing wrong. Here are two ways I have tried that seemed logical but did not work:
you pass two floats (any number) and it gives you the apropiate 0 - 1 value in that point of the texture.
you pass in two floats from 0 - 1 and it will retourn a 0 - 1 value of that spot.
the problem is regardless of what I input i always end up getting the same value on several spots. here is a bunch of stuff i am doing every time that might help pinpoint what is wrong:
use two for loops to get the values of a square area of the perlin noise. -im using an old pc but i am 99% sure that it is capable of this because when i left the y fixed at 0.5 and changed the x it gave me diferent values, also, though every time i get the same value on every sample, when i change the method i get diferent values.
idk what ell could i add. its driving me nuts and i have abandoned several projects because of this. thanks a lot to all of you.
Answer by DanSuperGP · Feb 19, 2015 at 12:24 AM
Try this, you'll notice that if length and width divider are 1 you'll get a uniform grey... but if you use other settings your noise will be different. You'll also notice that it's the same each time... which can be very useful. If you don't want it to be the same every time... generate two random values before the loop, and add it to the x and y positions fed into Mathf.PerlinNoise()
using UnityEngine;
using System.Collections;
public class NoiseMaterial : MonoBehaviour {
public Material noiseMat;
public float lengthDivisor = 2.0f;
public float widthDivisor = 2.0f;
// Use this for initialization
void Start () {
Texture2D noiseTexture = new Texture2D(100,100);
for(int i = 0; i < 100; i++)
{
for( int j = 0; j < 100; j++)
{
float noisePixel = Mathf.PerlinNoise(((float) i)/lengthDivisor , ((float) j)/widthDivisor );
noiseTexture.SetPixel( i, j, new Color(noisePixel, noisePixel, noisePixel));
}
}
noiseTexture.Apply();
noiseMat.mainTexture = noiseTexture;
}
// Update is called once per frame
void Update () {
}
}
THAN$$anonymous$$S A LOT! finaly a simple explanation.
Your answer
![](https://koobas.hobune.stream/wayback/20220613183716im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to use Perlin Noise for waves 0 Answers
Help Perlin Noise for Cubes? 1 Answer
What's wrong with this code? Perlin Noise 0 Answers
Perlin Noise Issue 0 Answers
Perlin noise tutorials ? 2 Answers