public void AddNewSplatHeights() { SplatHeights newSplatHeight = new SplatHeights(); newSplatHeight.tsplatOffset = splatOffset; newSplatHeight.tsplatNoiseXscale = splatNoiseXscale; newSplatHeight.tsplatNoiseYscale = splatNoiseYscale; newSplatHeight.tsplatNoisescaler = splatNoisescaler; splatHeights.Add(newSplatHeight); }
public void SplatMaps() { splatHeights.RemoveAll(h => h.texture == null); TerrainLayer[] terrainLayers = new TerrainLayer[splatHeights.Count]; for (int i = 0; i < splatHeights.Count; i++) { SplatHeights height = splatHeights[i]; terrainLayers[i] = new TerrainLayer { diffuseTexture = height.texture, tileOffset = height.tileOffset, tileSize = height.tileSize }; TerrainLayer layer = terrainLayers[i]; // Create terrain layers folder string path = Application.dataPath + "/TerrainLayers"; if (System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); } // Create new terrain layer asset string assetPath = "Assets/TerrainLayers/New Terrain Layer " + i + ".terrainlayer"; AssetDatabase.CreateAsset(layer, assetPath); terrainLayers[i].diffuseTexture.Apply(true); Selection.activeObject = gameObject; } terrainData.terrainLayers = terrainLayers; float[,] heightMap = terrainData.GetHeights(0, 0, terrainData.heightmapWidth, terrainData.heightmapHeight); float[,,] splatmapData = new float[terrainData.alphamapWidth, terrainData.alphamapHeight, terrainData.alphamapLayers]; for (int y = 0; y < terrainData.alphamapHeight; ++y) { for (int x = 0; x < terrainData.alphamapWidth; ++x) { float[] splat = new float[terrainData.alphamapLayers]; for (int i = 0; i < splatHeights.Count; ++i) { float noise = Mathf.PerlinNoise(x * splatHeights[i].splatNoiseXScale, y * splatHeights[i].splatNoiseYScale) * splatHeights[i].splatNoiseMultiplier; float offset = splatHeights[i].splatOffset + noise; float thisHeightStart = splatHeights[i].minHeight - offset; float thisHeightStop = splatHeights[i].maxHeight + offset; //float steepness = GetSteepness( heightMap, x, y, // terrainData.heightmapWidth, // terrainData.heightmapHeight); float steepness = terrainData.GetSteepness(y / (float)terrainData.alphamapHeight, x / (float)terrainData.alphamapWidth); if ((heightMap[x, y] >= thisHeightStart && heightMap[x, y] <= thisHeightStop) && (steepness >= splatHeights[i].minSlope && steepness <= splatHeights[i].maxSlope)) { splat[i] = 1; } } NormalizeVector(splat); for (int j = 0; j < splatHeights.Count; ++j) { splatmapData[x, y, j] = splat[j]; } } } terrainData.SetAlphamaps(0, 0, splatmapData); }