Guest User

Untitled

a guest
Jan 21st, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. // This script is placed in public domain. The author takes no responsibility for any possible harm.
  2.  
  3. var heightMap : Texture2D;
  4. var material : Material;
  5. var size = Vector3(200, 30, 200);
  6.  
  7. function Start ()
  8. {
  9. GenerateHeightmap();
  10. }
  11.  
  12. function GenerateHeightmap ()
  13. {
  14. // Create the game object containing the renderer
  15. gameObject.AddComponent(MeshFilter);
  16. gameObject.AddComponent("MeshRenderer");
  17. gameObject.AddComponent(Mesh);
  18. if (material)
  19. renderer.material = material;
  20. else
  21. renderer.material.color = Color.white;
  22.  
  23. // Retrieve a mesh instance
  24. var mesh : Mesh = GetComponent(MeshFilter).mesh;
  25.  
  26. var width : int = Mathf.Min(heightMap.width, 255);
  27. var height : int = Mathf.Min(heightMap.height, 255);
  28. var y = 0;
  29. var x = 0;
  30.  
  31. // Build vertices and UVs
  32. var vertices = new Vector3[height * width];
  33. var uv = new Vector2[height * width];
  34. var tangents = new Vector4[height * width];
  35.  
  36. var uvScale = Vector2 (1.0 / (width - 1), 1.0 / (height - 1));
  37. var sizeScale = Vector3 (size.x / (width - 1), size.y, size.z / (height - 1));
  38.  
  39. for (y=0;y<height;y++)
  40. {
  41. for (x=0;x<width;x++)
  42. {
  43. var pixelHeight = heightMap.GetPixel(x, y).grayscale;
  44. var vertex = Vector3 (x, pixelHeight, y);
  45. vertices[y*width + x] = Vector3.Scale(sizeScale, vertex);
  46. uv[y*width + x] = Vector2.Scale(Vector2 (x, y), uvScale);
  47.  
  48. // Calculate tangent vector: a vector that goes from previous vertex
  49. // to next along X direction. We need tangents if we intend to
  50. // use bumpmap shaders on the mesh.
  51. var vertexL = Vector3( x-1, heightMap.GetPixel(x-1, y).grayscale, y );
  52. var vertexR = Vector3( x+1, heightMap.GetPixel(x+1, y).grayscale, y );
  53. var tan = Vector3.Scale( sizeScale, vertexR - vertexL ).normalized;
  54. tangents[y*width + x] = Vector4( tan.x, tan.y, tan.z, -1.0 );
  55. }
  56. }
  57.  
  58. // Assign them to the mesh
  59. mesh.vertices = vertices;
  60. mesh.uv = uv;
  61.  
  62. // Build triangle indices: 3 indices into vertex array for each triangle
  63. var triangles = new int[(height - 1) * (width - 1) * 6];
  64. var index = 0;
  65. for (y=0;y<height-1;y++)
  66. {
  67. for (x=0;x<width-1;x++)
  68. {
  69. // For each grid cell output two triangles
  70. triangles[index++] = (y * width) + x;
  71. triangles[index++] = ((y+1) * width) + x;
  72. triangles[index++] = (y * width) + x + 1;
  73.  
  74. triangles[index++] = ((y+1) * width) + x;
  75. triangles[index++] = ((y+1) * width) + x + 1;
  76. triangles[index++] = (y * width) + x + 1;
  77. }
  78. }
  79. // And assign them to the mesh
  80. mesh.triangles = triangles;
  81.  
  82. // Auto-calculate vertex normals from the mesh
  83. mesh.RecalculateNormals();
  84.  
  85. // Assign tangents after recalculating normals
  86. mesh.tangents = tangents;
  87. }
Add Comment
Please, Sign In to add comment