-
-
Save beugnen/dedd2eebaefe282b0e0c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void Start() | |
{ | |
var mesh = new Mesh(); | |
_heightMap1 = new float[Width,Height]; | |
_heightMap2 = new float[Width,Height]; | |
_buffer2 = _heightMap1; | |
_buffer1 = _heightMap2; | |
NumberOfVertices = Width*Height; | |
var vertices = new Vector3[NumberOfVertices]; | |
var uvs = new Vector2[vertices.Length]; | |
for (var y = 0; y < Height; y++) | |
{ | |
for (var x = 0; x < Width; x++) | |
{ | |
_heightMap1[x, y] = 0; | |
_heightMap2[x, y] = 0; | |
var p = new Vector3(Mathf.Lerp(-.5f, .5f, (float) x/Width), | |
Mathf.Lerp(-.5f, .5f, (float) y/Height), | |
0); | |
vertices[y*Width + x] = p; | |
uvs[y*Width + x] = new Vector2(p.x, p.y); | |
} | |
} | |
mesh.vertices = vertices; | |
mesh.uv = uvs; | |
var triangles = new List<int>(); | |
var i = Width; | |
for (var y = 1; y < Height; y++) | |
{ | |
for (var x = 0; x < Width - 1; x++) | |
{ | |
triangles.Add(i + x); | |
triangles.Add(i + x - Width + 1); | |
triangles.Add(i + x - Width); | |
triangles.Add(i + x); | |
triangles.Add(i + x + 1); | |
triangles.Add(i + x + 1 - Width); | |
} | |
i += Width; | |
} | |
mesh.triangles = triangles.ToArray(); | |
_buffer1[Width/2, Height/2] = 1; | |
var filter = GetComponent<MeshFilter>(); | |
filter.mesh = mesh; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment