- Home /
Substituting Sinus function with Perlin noise?
2D Perlin noise has an X and Y axis so it's basically a zigzag function.
please explain how to use the Mathf.PerlinNoise instead of Mathf.Sin to set the mesh coordinates abit randomly.
// Code from procedural examples to sine-ify a mesh
var scale = 10.0;
var speed = 1.0;
private var baseHeight : Vector3[];
function Update () {
var mesh : Mesh = GetComponent(MeshFilter).mesh;
if (baseHeight == null)
baseHeight = mesh.vertices;
var vertices = new Vector3[baseHeight.Length];
for (var i=0;i<vertices.Length;i++)
{
var vertex = baseHeight[i];
vertex.y += Mathf.Sin(baseHeight[i].x ) * scale; //<<<---THIS LINE PLEASE :)
vertices[i] = vertex;
}
mesh.vertices = vertices;
mesh.RecalculateNormals();
}
EDIT-okay so here is a three-dimensional Perlin mashed deformation script, there are other noise functions on the forums that may have more control of the noise.
var scale = 0.40;
var speed = 1.0;
private var baseHeight : Vector3[];
function Update () {
var mesh : Mesh = GetComponent(MeshFilter).mesh;
if (baseHeight == null)
baseHeight = mesh.vertices;
var vertices = new Vector3[baseHeight.Length];
for (var i=0;i<vertices.Length;i++)
{
var vertex = baseHeight[i];
vertex.y += Mathf.PerlinNoise(baseHeight[i].x*3+77,12) * scale;
vertex.x += Mathf.PerlinNoise(baseHeight[i].z*2+77,22) * scale;
vertex.z += Mathf.PerlinNoise(baseHeight[i].y*3+77,42) * scale;
vertices[i] = vertex;
}
mesh.vertices = vertices;
mesh.RecalculateNormals();
}
Answer by phodges · Oct 19, 2012 at 06:30 PM
The Perlin noise function accepts a pair of values, for your case you might consider feature baseHeight[i].x and z into it. Send those numbers, or any transformation of them that amuses you, into the Perlin method and then scale the result similarly to how you are making use of Sin. Note that the returned value is in the range 0,1 and so if you want your results to be in the same range as your previous experiment then double the output from the noise function and subtract 1.0.
so to make a sin it's x = $$anonymous$$athf.Sin(y) and to make a perlin graph it's x= $$anonymous$$athf.PerlinNoise(y,1) for example? where could i get some pics/info's ?
is one of the 2 variables the number of octaves of the perlin function? same as these perlin 2d graphs: http://libnoise.sourceforge.net/glossary/index.html#octave
Google will give you as much Perlin noise as you can handle. What I would suggest though is that you build yourself a test scene and just play with some meshes.
okay, because somewhere on the forums should say which of the 2 variables of the function is a modifier/seed, and what kind, and which is a linear input at 1st glance, it seems neither of the variables controls either the octave or the frequency, it is probably a seed, and frequency can be controlled by multiplying one of the values.
The documentation is a bit sparse, just saying it's a 2D Perlin noise generator. Ordinarily, I would expect there to be some other control for seeding it but that doesn't seem to be present. Worst comes to the worst, you could implement the noise generator yourself if there's not enough control in there for you.
thanks phodges! someone has made 30 noise generators for unity:) http://u3d.as/content/chaos-cult/coherent-noise/1Uc