Guest User

Untitled

a guest
Sep 11th, 2010
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. public static function GenerateHeightmap (tex : Texture2D, size : Vector3,  offset : Vector2, mat : Material)
  2. {
  3.     GO = new GameObject ("terrain");
  4.     GO.AddComponent("MeshFilter");
  5.     GO.AddComponent("MeshRenderer");
  6.     GO.renderer.material = mat;
  7.     // Retrieve a mesh instance
  8.     var mesh : Mesh = GO.GetComponent(MeshFilter).mesh;
  9.     var width : int = 255;//Mathf.Min(tex.width, 255);
  10.     var height : int = 255;//Mathf.Min(tex.height, 255);
  11.     var y = 0;
  12.     var x = 0;
  13.     // Build vertices and UVs
  14.     //This is kept as normal.  256^2  is more polygons than unity can handle.
  15.     var vertices = new Vector3[height * width];
  16.     var uv = new Vector2[height * width];
  17.     var tangents = new Vector4[height * width];
  18.    
  19.     //scale of things..I hope!
  20.     var uvScale = Vector2 (1.0 / (width - 1), 1.0 / (height - 1));
  21.     var sizeScale = Vector3 (size.x / (width - 1), size.y, size.z / (height - 1));
  22.    
  23.     for (y=0; y<height  ;y++)
  24.     {
  25.         for (x=0 ; x<width ;x++)
  26.         {
  27.             var pixelHeight = tex.GetPixel(x + offset.x, y + offset.y).a;
  28.             var vertex = Vector3 (x + offset.x, pixelHeight, y + offset.y);
  29.             vertices[y*width + x] = Vector3.Scale(sizeScale, vertex);
  30.             uv[y*width + x] = Vector2.Scale(Vector2 (x + offset.x, y + offset.y), uvScale * 0.5); // *0.5  because max texture size is 512 (256 * 2)
  31.  
  32.             // Calculate tangent vector: a vector that goes from previous vertex
  33.             // to next along X direction. We need tangents if we intend to
  34.             // use bumpmap shaders on the mesh.
  35.             var vertexL = Vector3( x-1, tex.GetPixel(x-1, y).a, y );
  36.             var vertexR = Vector3( x+1, tex.GetPixel(x+1, y).a, y );
  37.             var tan = Vector3.Scale( sizeScale, vertexR - vertexL ).normalized;
  38.             tangents[y*width + x] = Vector4( tan.x, tan.y, tan.z, -1.0 );
  39.         }
  40.     }
  41.     mesh.vertices = vertices;
  42.     mesh.uv = uv;
  43.  
  44.     // Build triangle indices: 3 indices into vertex array for each triangle
  45.     var triangles = new int[(height - 1) * (width - 1) * 6];
  46.     var index = 0;
  47.     for (y=0;y<height-1;y++) //-1
  48.     {
  49.         for (x=0;x<width-1;x++)
  50.         {
  51.             // For each grid cell output two triangles
  52.             triangles[index++] = (y     * width) + x;
  53.             triangles[index++] = ((y+1) * width) + x;
  54.             triangles[index++] = (y     * width) + x + 1;
  55.  
  56.             triangles[index++] = ((y+1) * width) + x;
  57.             triangles[index++] = ((y+1) * width) + x + 1;
  58.             triangles[index++] = (y     * width) + x + 1;
  59.         }
  60.     }
  61.     // And assign them to the mesh
  62.     mesh.triangles = triangles;
  63.        
  64.     // Auto-calculate vertex normals from the mesh
  65.     mesh.RecalculateNormals();
  66.    
  67.     // Assign tangents after recalculating normals
  68.     mesh.tangents = tangents;
  69.  
  70.     //THESE LINES ADD 5 SECONDS ROUGH TO LOAD
  71.     //GO.AddComponent("MeshCollider");
  72.     //GO.collider.sharedMesh = mesh;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment