/// <summary> /// Clone TerrainData object /// </summary> /// <param name="source"> /// A <see cref="TerrainData"/> /// </param> /// <param name="detailResolutionPerPatch"> /// A <see cref="System.Int32"/> /// </param> /// <returns> /// A <see cref="TerrainData"/> /// </returns> public static TerrainData Clone(TerrainData source, int detailResolutionPerPatch) { TerrainData dest = new TerrainData(); dest.alphamapResolution = source.alphamapResolution; dest.baseMapResolution = source.baseMapResolution; dest.SetDetailResolution(source.detailResolution, detailResolutionPerPatch); DetailPrototype[] dp = new DetailPrototype[source.detailPrototypes.Length]; for (int i=0; i<dp.Length; i++) dp[i] = new DetailPrototype(); dest.detailPrototypes = dp; for (int i=0; i<dp.Length; i++) { dest.detailPrototypes[i].bendFactor = source.detailPrototypes[i].bendFactor; dest.detailPrototypes[i].dryColor = source.detailPrototypes[i].dryColor; dest.detailPrototypes[i].healthyColor = source.detailPrototypes[i].healthyColor; dest.detailPrototypes[i].maxHeight = source.detailPrototypes[i].maxHeight; dest.detailPrototypes[i].maxWidth = source.detailPrototypes[i].maxWidth; dest.detailPrototypes[i].minHeight = source.detailPrototypes[i].minHeight; dest.detailPrototypes[i].minWidth = source.detailPrototypes[i].minWidth; dest.detailPrototypes[i].noiseSpread = source.detailPrototypes[i].noiseSpread; dest.detailPrototypes[i].prototype = source.detailPrototypes[i].prototype; dest.detailPrototypes[i].prototypeTexture = source.detailPrototypes[i].prototypeTexture; dest.detailPrototypes[i].renderMode = source.detailPrototypes[i].renderMode; dest.detailPrototypes[i].usePrototypeMesh = source.detailPrototypes[i].usePrototypeMesh; dest.SetDetailLayer(0,0,i,source.GetDetailLayer(0,0,source.detailWidth,source.detailHeight,i)); } dest.RefreshPrototypes(); dest.heightmapResolution = source.heightmapResolution; dest.SetHeights(0,0,source.GetHeights(0,0,source.heightmapWidth,source.heightmapHeight)); dest.hideFlags = source.hideFlags; dest.name = source.name+" (Clone)"; dest.size = source.size; dest.splatPrototypes = source.splatPrototypes; dest.treeInstances = source.treeInstances; dest.treePrototypes = source.treePrototypes; dest.wavingGrassAmount = source.wavingGrassAmount; dest.wavingGrassSpeed = source.wavingGrassSpeed; dest.wavingGrassStrength = source.wavingGrassStrength; dest.wavingGrassTint = source.wavingGrassTint; return dest; }
private void ApplyTextures(TerrainData terrainData) { var flatSplat = new SplatPrototype(); var steepSplat = new SplatPrototype(); flatSplat.texture = Settings.FlatTexture; steepSplat.texture = Settings.SteepTexture; terrainData.splatPrototypes = new SplatPrototype[] { flatSplat, steepSplat }; terrainData.RefreshPrototypes(); var splatMap = new float[terrainData.alphamapResolution, terrainData.alphamapResolution, 2]; for (var zRes = 0; zRes < terrainData.alphamapHeight; zRes++) { for (var xRes = 0; xRes < terrainData.alphamapWidth; xRes++) { var normalizedX = (float)xRes / (terrainData.alphamapWidth - 1); var normalizedZ = (float)zRes / (terrainData.alphamapHeight - 1); var steepness = terrainData.GetSteepness(normalizedX, normalizedZ); var steepnessNormalized = Mathf.Clamp(steepness / 1.5f, 0, 1f); splatMap[zRes, xRes, 0] = 1f - steepnessNormalized; splatMap[zRes, xRes, 1] = steepnessNormalized; } } terrainData.SetAlphamaps(0, 0, splatMap); }
private void ApplyTextures(TerrainData data) { var flatSplat = new SplatPrototype(); var steepSplat = new SplatPrototype(); flatSplat.texture = Settings.FlatTexture; steepSplat.texture = Settings.SteepTexture; data.splatPrototypes = new [] { flatSplat, steepSplat }; data.RefreshPrototypes(); var splatMap = new float[data.alphamapResolution, data.alphamapResolution, 2]; for (var z = 0; z < data.alphamapHeight; z++) { for (var x = 0; x < data.alphamapWidth; x++) { var normalizedX = (float)x / (data.alphamapWidth - 1); var normalizedZ = (float)z / (data.alphamapHeight - 1); var steepness = data.GetSteepness(normalizedX, normalizedZ) * Settings.SteepnessTextureMultiplier; var steepnessNormalized = Mathf.Clamp(steepness, 0, 1f); splatMap[z, x, 0] = 1f - steepnessNormalized; splatMap[z, x, 1] = steepnessNormalized; } } data.SetAlphamaps(0, 0, splatMap); }
void finalizeHeightmap(RenderedHeightmap rh) { TerrainData tData = new TerrainData(); tData.heightmapResolution = chunkResolution; tData.alphamapResolution = chunkResolution; tData.size = new Vector3(chunkWidth, terrainHeight, chunkWidth); tData.splatPrototypes = splats; tData.RefreshPrototypes(); /* Create and position the terrain */ GameObject terrain = Terrain.CreateTerrainGameObject(tData); terrain.transform.position = new Vector3( chunkWidth * rh.coord.x, 0.0f, chunkWidth * rh.coord.z); /* Set the heightmap data from the background thread. */ tData.SetHeights(0, 0, rh.heightmap); stitchTerrain(tData, rh.coord); chunks[rh.coord] = tData; // texturing tData.SetAlphamaps(0, 0, rh.splatmap); }
void finalizeHeightmap(RenderedHeightmap rh) { TerrainData tData = new TerrainData(); tData.heightmapResolution = chunkResolution; tData.alphamapResolution = chunkResolution; tData.SetDetailResolution(chunkResolution*2, 1); tData.size = new Vector3(chunkWidth, terrainHeight, chunkWidth); tData.splatPrototypes = splats; tData.detailPrototypes = grassProto; tData.treePrototypes = treeProto; tData.RefreshPrototypes(); // tree placement tData.treeInstances = rh.treeInstances.ToArray(); /* Create and position the terrain */ GameObject terrain = Terrain.CreateTerrainGameObject(tData); terrain.transform.position = new Vector3 ( chunkWidth * rh.coord.x, 0.0f, chunkWidth * rh.coord.z); /* Set the heightmap data from the background thread. */ tData.SetHeights(0, 0, rh.heightmap); stitchTerrain(tData, rh.coord); chunks[rh.coord] = tData; // texturing tData.SetAlphamaps(0, 0, rh.splatmap); // grass placement /*if (texturesToAffect.Length != 0) { var detailedLayer = grassPlacement (tData.detailResolution, tData.GetAlphamaps (0, 0, tData.alphamapWidth, tData.alphamapHeight)); tData.SetDetailLayer (0, 0, grassType, detailedLayer); }*/ }
// Use this for initialization void Start() { t = terrain.GetComponent<Terrain>(); td = t.terrainData; float[,] heights = new float[(int)td.size.x, (int)td.size.z]; System.Random rand = new System.Random(); for(int i = 0 ; i < td.size.x; i++) { for(int j = 0 ; j < td.size.z; j++) { // heights[i, j] += ((float) SimplexNoise.noise(i, j))>0?(float) SimplexNoise.noise(i, j):0; } } normalizePerlin(ref heights, new Vector2(td.size.x - 1, td.size.z - 1)); td.SetHeights(0, 0, heights); td.RefreshPrototypes(); t.Flush(); }
void makeTerrain(int x, int z) { EditorUtility.DisplayProgressBar("Expand Terrain", "Creating GameObject...", 0.5f); TerrainData terrainData = new TerrainData(); TerrainData selTerrainData = Selection.activeGameObject.GetComponent<Terrain>().terrainData; terrainData.heightmapResolution = selTerrainData.heightmapResolution; terrainData.alphamapResolution = selTerrainData.alphamapResolution; terrainData.baseMapResolution = selTerrainData.baseMapResolution; terrainData.SetDetailResolution(selTerrainData.detailResolution, 8); terrainData.name = "Terrain" + x + "_" + z; Vector3 pos; pos.x = selTerrainData.size.x * x; pos.y = 0.0f; pos.z = selTerrainData.size.z * z; terrainData.size = selTerrainData.size; terrainData.RefreshPrototypes(); GameObject terrObj = Terrain.CreateTerrainGameObject(terrainData); terrObj.transform.position = pos; terrObj.name = "t" + x + "." + z; terrObj.GetComponent<Terrain>().Flush(); AssetDatabase.CreateAsset(terrainData, "Assets/Terrain" + x + "_" + z + ".asset"); if (teExpandOpts[0] == true) { EditorUtility.DisplayProgressBar("Expand Terrain", "Generating Heightmap...", 0.5f); TENoiseLab.genIt(terrObj); } if (teExpandOpts[1] == true) { EditorUtility.DisplayProgressBar("Expand Terrain", "Generating Textures...", 0.5f); terrainData.splatPrototypes = selTerrainData.splatPrototypes; TETextures.genIt(terrObj); } if (teExpandOpts[2] == true) { EditorUtility.DisplayProgressBar("Expand Terrain", "Generating Detail...", 0.5f); terrainData.detailPrototypes = selTerrainData.detailPrototypes; TEDetail.genIt(terrObj); } if (teExpandOpts[3] == true) { EditorUtility.DisplayProgressBar("Expand Terrain", "Generating Foliage...", 0.5f); terrainData.treePrototypes = selTerrainData.treePrototypes; TETrees.genIt(terrObj); } EditorUtility.ClearProgressBar(); TerrainEdge.getNeighbors(Selection.activeGameObject); }
public static void reRes(GameObject go,int detailRes, int heightRes, int alphaRes) { TerrainData tdat = go.GetComponent<Terrain>().terrainData; TerrainData terrainData = new TerrainData(); string goName = go.name+"_new"; string terName = tdat.name; terrainData.heightmapResolution = heightRes; terrainData.alphamapResolution = alphaRes; terrainData.SetDetailResolution(detailRes,8); terrainData.baseMapResolution = tdat.baseMapResolution; terrainData.detailPrototypes = tdat.detailPrototypes; terrainData.splatPrototypes = tdat.splatPrototypes; terrainData.treePrototypes = tdat.treePrototypes; terrainData.treeInstances = tdat.treeInstances; terrainData.size = tdat.size; terrainData.RefreshPrototypes(); GameObject terrObj = Terrain.CreateTerrainGameObject(terrainData); terrObj.transform.position=new Vector3(0f,0f,0f); terrObj.name = goName; terrObj.GetComponent<Terrain>().Flush(); float mult = 0.0f; int destres = 0; destres = detailRes; int[,] detaildatasource = new int[tdat.detailResolution,tdat.detailResolution]; int[,] detaildatadest = new int[destres,destres]; for(int layer=0; layer<tdat.detailPrototypes.Length; layer++){ detaildatasource = tdat.GetDetailLayer(0,0,tdat.detailResolution,tdat.detailResolution,layer); mult=(float)tdat.detailResolution/(float)destres; for(int y=0;y<destres;y++){ for(int x=0;x<destres;x++){ int xold = (int)(mult * (float)x); int yold = (int)(mult * (float)y); detaildatadest[x,y]=detaildatasource[xold,yold]; } } terrainData.SetDetailLayer(0,0,layer,detaildatadest); } destres = heightRes; float[,] heightdatasource = new float[tdat.heightmapResolution,tdat.heightmapResolution]; float[,] heightdatadest = new float[destres,destres]; heightdatasource = tdat.GetHeights(0,0,tdat.heightmapResolution,tdat.heightmapResolution); mult=((float)tdat.heightmapResolution-1.0f)/((float)destres-1.0f); for(int y=0;y<destres;y++){ for(int x=0;x<destres;x++){ int xold = (int)(mult * (float)x); int yold = (int)(mult * (float)y); heightdatadest[x,y]=heightdatasource[xold,yold]; } } destres = alphaRes; float[,,] alphadata = tdat.GetAlphamaps(0,0,tdat.alphamapResolution,tdat.alphamapResolution); float[,,] alphadatadest = new float[destres,destres,tdat.splatPrototypes.Length]; mult=(float)tdat.alphamapResolution/(float)destres; for(int i=0;i<tdat.splatPrototypes.Length;i++){ for(int y=0;y<destres;y++){ for(int x=0;x<destres;x++){ int xold = (int)(mult * (float)x); int yold = (int)(mult * (float)y); alphadatadest[x,y,i] = alphadata[xold,yold,i]; } } } terrObj.GetComponent<Terrain>().terrainData.SetHeights(0,0,heightdatadest); AssetDatabase.CreateAsset(terrainData, "Assets/"+terName+"_new.asset"); AssetDatabase.SaveAssets(); TerrainData terrainData2Splat = (TerrainData)AssetDatabase.LoadAssetAtPath("Assets/"+terName+"_new.asset", typeof(TerrainData)); terrainData2Splat.splatPrototypes = tdat.splatPrototypes; terrainData2Splat.RefreshPrototypes(); terrainData2Splat.SetAlphamaps (0, 0, alphadatadest); AssetDatabase.SaveAssets(); go.active = false; }
public static void newTerrainAutoName(int x, int z, float hMapWidth, float hMapHeight, int hMapRes, int aMapRes, int bMapRes, int dRes, int dResPP){ TerrainData terrainData = new TerrainData(); terrainData.heightmapResolution = hMapRes; terrainData.alphamapResolution = aMapRes; terrainData.baseMapResolution = bMapRes; terrainData.SetDetailResolution(dRes,dResPP); terrainData.name = "Terrain"+x+"_"+z; Vector3 size; size.x = hMapWidth; size.y = hMapHeight; size.z = hMapWidth; Vector3 pos; pos.x = hMapWidth * x; pos.y = 0.0f; pos.z = hMapWidth * z; terrainData.size = size; terrainData.RefreshPrototypes(); GameObject terrObj = Terrain.CreateTerrainGameObject(terrainData); terrObj.transform.position=pos; terrObj.name = "t"+x+"."+z; terrObj.GetComponent<Terrain>().Flush(); AssetDatabase.CreateAsset(terrainData, "Assets/Terrain"+x+"_"+z+".asset"); }