- Home /
Perlin Noise applied to plane, except endpoints
Here goes,
I have created a script that when applied to a plane it creates random hills and valleys as it should. Now here lies my question. How do I make the end points stop at 0 naturally. For example I would have a plane and in the center would be random hills and valleys but the edges would be flat. Here is my Perlin Noise Script;
public float power = 3.0f;
public float scale = 1.0f;
private Vector2 v2SampleStart = new Vector2(0f, 0f);
void Start ()
{
Noise ();
}
void Update () {
if (Input.GetKeyDown (KeyCode.Space))
{
v2SampleStart = new Vector2(Random.Range (0.0f, 100.0f), Random.Range (0.0f, 100.0f));
Noise();
}
}
void Noise() {
MeshFilter mf = GetComponent<MeshFilter>();
Vector3[] vertices = mf.mesh.vertices;
for (int i = 0; i < vertices.Length; i++)
{
float xCoord = v2SampleStart.x + vertices[i].x * scale;
float yCoord = v2SampleStart.y + vertices[i].z * scale;
vertices[i].y = (Mathf.PerlinNoise (xCoord, yCoord) - 0.5f) * power;
}
mf.mesh.vertices = vertices;
mf.mesh.RecalculateBounds();
mf.mesh.RecalculateNormals();
}
Thanks in advanced!
Answer by twobob · Nov 18, 2014 at 03:42 AM
erm.
off the top of my head.
determine the total mesh size.
and use an exp rolloff beyond a certain x , y threshold taken absolutely from the center.
something like 0.9 of the radius/width (circle might be nice) would be a decent start value and tend toward zero after that with a factor.
just use the x,y since it looks like you know that already. so piece of cake.
As an aside there should be some way of dealing with the numbers offset absolutely to negate needing to know the direction of falloff but either way it's calculable and the shortcut evades me.
But roughly. As a method:
if ( NeedsRolloffApplying(vertices[i]))
vertices[i].y = (Mathf.PerlinNoise (xCoord, yCoord) - 0.5f) * power * factor;
or just
vertices[i].y = (Mathf.PerlinNoise (xCoord, yCoord) - 0.5f) * power * factor;
and make the factor unity (err 1) in the central cases and toward zero where the x or y is greater 40% distance away from the central value exponentially.
So. No Code. But that's what I would do.
This is how I would do it too. You can experiment with different drop off functions for different appearances.
Note that this won't look overly natural up close. But then nature doesn't do this either.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Raycast visual and moving text possition. 1 Answer
Aligning planes thin gap? c# 0 Answers
How to detect if number has a decimal point or not 2 Answers