killerbng

Object2Terrian - Javascript

Jan 3rd, 2017
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @MenuItem ("Terrain/Object to Terrain")
  2.  
  3. static function Object2Terrain () {
  4.     // See if a valid object is selected
  5.     var obj = Selection.activeObject as GameObject;
  6.     if (obj == null) {
  7.         EditorUtility.DisplayDialog("No object selected", "Please select an object.", "Cancel");
  8.         return;
  9.     }
  10.     if (obj.GetComponent(MeshFilter) == null) {
  11.         EditorUtility.DisplayDialog("No mesh selected", "Please select an object with a mesh.", "Cancel");
  12.         return;
  13.     }
  14.     else if ((obj.GetComponent(MeshFilter) as MeshFilter).sharedMesh == null) {
  15.         EditorUtility.DisplayDialog("No mesh selected", "Please select an object with a valid mesh.", "Cancel");
  16.         return;    
  17.     }
  18.     if (Terrain.activeTerrain == null) {
  19.         EditorUtility.DisplayDialog("No terrain found", "Please make sure a terrain exists.", "Cancel");
  20.         return;
  21.     }  
  22.     var terrain = Terrain.activeTerrain.terrainData;
  23.  
  24.     // If there's no mesh collider, add one (and then remove it later when done)
  25.     var addedCollider = false;
  26.     var addedMesh = false;
  27.     var objCollider = obj.collider as MeshCollider;
  28.     if (objCollider == null) {
  29.         objCollider = obj.AddComponent(MeshCollider);
  30.         addedCollider = true;
  31.     }
  32.     else if (objCollider.sharedMesh == null) {
  33.         objCollider.sharedMesh = (obj.GetComponent(MeshFilter) as MeshFilter).sharedMesh;
  34.         addedMesh = true;
  35.     }
  36.  
  37.     Undo.RegisterUndo (terrain, "Object to Terrain");
  38.  
  39.     var resolutionX = terrain.heightmapWidth;
  40.     var resolutionZ = terrain.heightmapHeight;
  41.     var heights = terrain.GetHeights(0, 0, resolutionX, resolutionZ);
  42.  
  43.     // Use bounds a bit smaller than the actual object; otherwise raycasting tends to miss at the edges
  44.     var objectBounds = objCollider.bounds;
  45.     var leftEdge = objectBounds.center.x - objectBounds.extents.x + .01;
  46.     var bottomEdge = objectBounds.center.z - objectBounds.extents.z + .01;
  47.     var stepX = (objectBounds.size.x - .019) / resolutionX;
  48.     var stepZ = (objectBounds.size.z - .019) / resolutionZ;
  49.  
  50.     // Set up raycast vars
  51.     var y = objectBounds.center.y + objectBounds.extents.y + .01;
  52.     var hit : RaycastHit;
  53.     var ray = new Ray(Vector3.zero, -Vector3.up);
  54.     var rayDistance = objectBounds.size.y + .02;
  55.     var heightFactor = 1.0 / rayDistance;
  56.  
  57.     // Do raycasting samples over the object to see what terrain heights should be
  58.     var z = bottomEdge;
  59.     for (zCount = 0; zCount < resolutionZ; zCount++) {
  60.         var x = leftEdge;
  61.         for (xCount = 0; xCount < resolutionX; xCount++) {
  62.             ray.origin = Vector3(x, y, z);
  63.             if (objCollider.Raycast(ray, hit, rayDistance)) {
  64.                 heights[zCount, xCount] = 1.0 - (y - hit.point.y)*heightFactor;
  65.             }
  66.             else {
  67.                 heights[zCount, xCount] = 0.0;
  68.             }
  69.             x += stepX;
  70.         }
  71.         z += stepZ;
  72.     }
  73.  
  74.     terrain.SetHeights(0, 0, heights);
  75.  
  76.     if (addedMesh) {
  77.         objCollider.sharedMesh = null;
  78.     }
  79.     if (addedCollider) {
  80.         DestroyImmediate(objCollider);
  81.     }
  82. }
Add Comment
Please, Sign In to add comment