Created
June 16, 2018 19:31
-
-
Save CreatureSurvive/11dddf96c7aa90b8696c600a877fb9ab to your computer and use it in GitHub Desktop.
Stitch terrain in unity
This file contains hidden or 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
| class TerrainStitcher extends EditorWindow { | |
| public var terrain1: Terrain = null; | |
| public var terrain2: Terrain = null; | |
| public var options: String[] = ["X", "Z"]; | |
| public var index: int = 0; | |
| @MenuItem ("Tools/Terrain stitcher") | |
| static function ShowWindow () { | |
| EditorWindow.GetWindow (TerrainStitcher); | |
| } | |
| function OnGUI () { | |
| index = EditorGUILayout.Popup("Axis:", index, options); | |
| terrain1 = EditorGUILayout.ObjectField("Terrain 1:", terrain1, Terrain, true); | |
| terrain2 = EditorGUILayout.ObjectField("Terrain 2:", terrain2, Terrain, true); | |
| if (GUILayout.Button("Stitch")) | |
| Stitch (); | |
| } | |
| function Stitch () { | |
| if (!(terrain1 && terrain2)) { | |
| Debug.LogError("No terrain selected."); | |
| return; | |
| } | |
| var data1 : TerrainData = terrain1.terrainData; | |
| var data2 : TerrainData = terrain2.terrainData; | |
| if (!(data1.heightmapWidth == data2.heightmapWidth)) { | |
| Debug.LogError("Width of the terrains must be equal."); | |
| return; | |
| } | |
| var a : float[,]; | |
| var b : float[,]; | |
| var h : int = data1.heightmapWidth; | |
| var i : int = 0; | |
| a = data1.GetHeights(0,0,h,h); | |
| b = data2.GetHeights(0,0,h,h); | |
| h-=1; | |
| if (index==0) //X axis | |
| for(i = 0; i < h; i++) { | |
| a[i,h]=(a[i,h]+b[i,0])/2; | |
| b[i,0]=a[i,h]; | |
| } | |
| else //Z axis | |
| for(i = 0; i <= h; i++) { | |
| a[h,i]=(a[h,i]+b[0,i])/2; | |
| b[0,i]=a[h,i]; | |
| } | |
| data1.SetHeights(0,0,a); | |
| data2.SetHeights(0,0,b); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment