Skip to content

Instantly share code, notes, and snippets.

@kirillrybin
Created September 26, 2014 08:16
Show Gist options
  • Save kirillrybin/9c57107ab8be8cb54b3b to your computer and use it in GitHub Desktop.
Save kirillrybin/9c57107ab8be8cb54b3b to your computer and use it in GitHub Desktop.
PerlinNoisePlane in Unity3D
using UnityEngine;
using System.Collections;
public class PerlinNoisePlane : MonoBehaviour {
public float power = 3.0f;
public float scale = 1.0f;
private Vector2 v2SampleStart = new Vector2(0f, 0f);
void Start () {
MakeSomeNoise ();
}
void Update () {
if (Input.GetKeyDown (KeyCode.Space)) {
v2SampleStart = new Vector2(Random.Range (0.0f, 100.0f), Random.Range (0.0f, 100.0f));
MakeSomeNoise();
}
}
void MakeSomeNoise() {
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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment