The script below creates a simple procedural grid based mesh with height data taken from a procedural perlin noise map, it is a simple combination of two of the scripts supplied in the Unity3d procedural examples found here. For the script to run you will also need a copy of the perlin.cs plugin script file also found in the Unity3d procedural examples and to place it in a folder named Plugins in your project assets directory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
//Created for Unity v3.5 //procedural grid mesh based on perlin noise. using UnityEngine; using System.Collections; public class PerlinHeightMesh : MonoBehaviour { public Material material; public Vector3 Unitsize =new Vector3(200, 30, 200); public Vector2 MeshSize =new Vector2(50,50); //texturesize public float lacunarity = 2.404f; public float h = 0.354f; public float gain = 1.0f; public float octaves = 2.315f; public float offset = 0.245f; public float scale = 0.053f; public float offsetPos = 0.0f; public Vector2 TimeMove=new Vector2(0.005f,0); private Vector2 FracTime=Vector2.zero; private Perlin perlin; private FractalNoise fractal; private Mesh mesh; private Vector3[] vertices; private Vector2[] uv; private Vector4[] tangents; private Vector2 uvScale; private Vector3 sizeScale; private int[] triangles; void Awake() { perlin = new Perlin(); gameObject.AddComponent<MeshFilter>(); gameObject.AddComponent<MeshRenderer>(); if (material) { renderer.material = material; } else { renderer.material.color = Color.white; } mesh = GetComponent<MeshFilter>().mesh; // Build vertices and UVs uvScale = new Vector2(1.0f/(MeshSize.x-1f),1.0f/(MeshSize.y-1f)); sizeScale = new Vector3 (Unitsize.x/(MeshSize.x-1f),Unitsize.y,Unitsize.z/(MeshSize.y-1f)); //Set up triangle array, will need to be reset if you dynamically change size triangles = new int[((int)MeshSize.y - 1) * ((int)MeshSize.x - 1) * 6]; int x,y,index = 0; for (y=0;y<(int)MeshSize.y-1;y++) { for (x=0;x<(int)MeshSize.x-1;x++) { // For each grid cell output two triangles triangles[index++] = (y * (int)MeshSize.x)+x; triangles[index++] = ((y+1) * (int)MeshSize.x)+x; triangles[index++] = (y * (int)MeshSize.x)+x+1; triangles[index++] = ((y+1) * (int)MeshSize.x)+x; triangles[index++] = ((y+1) * (int)MeshSize.x)+x+1; triangles[index++] = (y * (int)MeshSize.x)+x+1; } } GenerateFractalPlaneMesh(); } void Update() { GenerateFractalPlaneMesh(); } private void GenerateFractalPlaneMesh() { mesh.Clear(); vertices = new Vector3[(int)MeshSize.y * (int)MeshSize.x]; uv = new Vector2[(int)MeshSize.y * (int)MeshSize.x]; tangents = new Vector4[(int)MeshSize.y * (int)MeshSize.x]; int x,y; FracTime+=TimeMove; fractal = new FractalNoise(h, lacunarity, octaves, perlin); for (y=0;y<(int)MeshSize.y;++y) { for (x=0;x<(int)MeshSize.x;++x) { float value=fractal.HybridMultifractal(x*scale + FracTime.x, y*scale + FracTime.y, offset); float pixelHeight = new Color(value,value,value).grayscale; Vector3 vertex =new Vector3(x, pixelHeight, y); vertices[y*(int)MeshSize.x+x] = Vector3.Scale(sizeScale, vertex); uv[y*(int)MeshSize.x + x] = Vector2.Scale(new Vector2(x, y), uvScale); Vector3 vertexL = new Vector3(x-1, new Color(value,value,value).grayscale,y ); Vector3 vertexR = new Vector3( x+1, new Color(value,value,value).grayscale, y ); Vector3 tan = Vector3.Scale( sizeScale, vertexR - vertexL ).normalized; tangents[y*(int)MeshSize.x + x] =new Vector4( tan.x, tan.y, tan.z, -1.0f ); } } mesh.vertices = vertices; mesh.uv = uv; mesh.triangles = triangles; // Auto-calculate vertex normals from the mesh mesh.RecalculateNormals(); // Assign tangents after recalculating normals mesh.tangents = tangents; } } |
17746 Total Views 3 Views Today
“float pixelHeight = new Color(value,value,value).grayscale” — or just use “float pixelHeight = value” ;D
I think from what I recall, I had issue as the plugin returned a larger bit size and would not convert to a float originally, I think I used this as work around, but fair point!