SetDetailLayer() public method

public SetDetailLayer ( int xBase, int yBase, int layer, int details ) : void
xBase int
yBase int
layer int
details int
return void
Beispiel #1
0
        /// <summary>
        /// This method allows to load the terrain data (details, heightmaps).
        /// </summary>
        public void LoadTerrainData()
        {
            if (!IsInitialized)
            {
                return;
            }

            if (ActiveTerrain == null)
            {
                return;
            }

            for (int Layer = 0; Layer < Data.detailPrototypes.Length; Layer++)
            {
                Data.SetDetailLayer(0, 0, Layer, TerrainDetails[Layer]);
            }

            Data.SetHeights(0, 0, TerrainHeights);
        }
		private static void ResizeDetailResolution(TerrainData terrainData, int resolution, int resolutionPerPatch)
		{
			if (resolution == terrainData.detailResolution)
			{
				List<int[,]> list = new List<int[,]>();
				for (int i = 0; i < terrainData.detailPrototypes.Length; i++)
				{
					list.Add(terrainData.GetDetailLayer(0, 0, terrainData.detailWidth, terrainData.detailHeight, i));
				}
				terrainData.SetDetailResolution(resolution, resolutionPerPatch);
				for (int j = 0; j < list.Count; j++)
				{
					terrainData.SetDetailLayer(0, 0, j, list[j]);
				}
			}
			else
			{
				terrainData.SetDetailResolution(resolution, resolutionPerPatch);
			}
		}
Beispiel #3
0
	/// <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 ResizeDetailResolution(TerrainData terrainData, int resolution, int resolutionPerPatch)
 {
   if (resolution == terrainData.detailResolution)
   {
     List<int[,]> numArrayList = new List<int[,]>();
     for (int layer = 0; layer < terrainData.detailPrototypes.Length; ++layer)
       numArrayList.Add(terrainData.GetDetailLayer(0, 0, terrainData.detailWidth, terrainData.detailHeight, layer));
     terrainData.SetDetailResolution(resolution, resolutionPerPatch);
     for (int layer = 0; layer < numArrayList.Count; ++layer)
       terrainData.SetDetailLayer(0, 0, layer, numArrayList[layer]);
   }
   else
     terrainData.SetDetailResolution(resolution, resolutionPerPatch);
 }
	public void PlaceDetails(ProgressDetailsDelegate progressDelegate)
	{
		Terrain ter = (Terrain) GetComponent(typeof(Terrain));
		terrainData = ter.terrainData;
		
		int alphamapWidth = terrainData.alphamapWidth;
		int alphamapHeight = terrainData.alphamapHeight;
		
		int detailWidth = terrainData.detailResolution;
		int detailHeight = detailWidth;
		
		float resolutionDiffFactor = (float)alphamapWidth/detailWidth;
		float[,,] splatmap = terrainData.GetAlphamaps(0,0,alphamapWidth,alphamapHeight);
		float alphaValue = 0;
		int count = terrainData.detailPrototypes.Length * DetailPlacement.Textures.Count;
		float increment = 1 / count;
		float percentCompleted = 0.0f;
		//how big is one unit in real world
		xUnit = (float)terrainData.size.x / (float)(detailWidth);
		yUnit = (float)terrainData.size.z / (float)(detailHeight);
		System.Random random = new System.Random();
		int randomInt;
		for(int detailIndex = 0; detailIndex < terrainData.detailPrototypes.Length; detailIndex++)
		{
			int[,] newDetailLayer = new int[detailWidth,detailHeight];
			foreach(DetailTextureSettings texture in DetailPlacement.Textures)
			{
				DetailSettings ds = texture.Details[detailIndex];
				ds.MinimumHeight = (int)ds.MinHeight;
				ds.MaximumHeight = (int)ds.MaxHeight;
				percentCompleted += increment;
				progressDelegate("Placing details", "Placing details", percentCompleted);
				
				if (ds.IsUsed == false)
					continue;
				//find where the texture is present
				for (int j = 0; j < detailWidth; j++) 
				{
					for (int k = 0; k < detailHeight; k++) 
					{
						alphaValue = splatmap[(int)(resolutionDiffFactor*j),(int)(resolutionDiffFactor*k),texture.Index];
						
						if (alphaValue >= ds.AlphamapValue && !ds.FluentCoverage)
						{
							//recalculate this position to real height
							//also check if minimum height is allowed for this texture
							if (CalculateHeight(ter, j, k, ds))
							{
								//rare plant is only low chance
								if (ds.Rare)
								{
									randomInt = random.Next(ds.ChanceToFind);
									if (randomInt == 1)
										newDetailLayer[j,k] = ds.Intensity;
									else
										newDetailLayer[j,k] = 0;
								}
								else
								{
									newDetailLayer[j,k] = ds.Intensity;
								}
							}
							
							else
							{
								newDetailLayer[j,k] = 0;
							}
						}
						else if (ds.FluentCoverage && alphaValue >= ds.AlphamapValue)
						{
							if (!CalculateHeight(ter, j, k, ds))
							{
								newDetailLayer[j,k] = 0;
								continue;
							}
							float wholeCoverage = 1 - ds.AlphamapValue;
							float partCoverage = 1 - alphaValue;
							int intensity = 1;
							
							if (wholeCoverage != 0 && partCoverage != 0)
							{
								float percent = (wholeCoverage * 100) / partCoverage - 100;
								intensity = (int)(ds.Intensity * percent / 100 / ds.Smoothness);
								if (intensity == 0)
									intensity = 1;
								if (intensity > ds.Intensity)
									intensity = ds.Intensity;
							}
							if (alphaValue == 1 || ds.AlphamapValue == 1)
								intensity = ds.Intensity;
							
							newDetailLayer[j,k] = intensity;
						}
					}
				
				}	
				
			}
			terrainData.SetDetailLayer(0,0,detailIndex,newDetailLayer);		
		}
	}
        /// <summary>
        /// Create terrain for the given tile
        /// </summary>
        private Terrain CreateTerrainTile(int x, int z)
        {
            float height = _terrainSettings.TerrainHeight;
            var centerTile = x == (_tilesX / 2) && z == (_tilesZ / 2);

            //Create terrain data and set prototypes
            var data = new TerrainData
            {
                heightmapResolution = _terrainSettings.HeightmapSize,
                alphamapResolution = _terrainSettings.AlphamapSize,
                size = new Vector3(_terrainSize, height, _terrainSize),
                splatPrototypes = _splatPrototypes,
                treePrototypes = _treePrototypes,
                detailPrototypes = _detailPrototypes
            };

            //Generate heightmap
            data.SetHeights(0, 0, GenerateHeightMap(x, z));

            //create terrain gameobject and position it correctly
            var xPos = _terrainSize * x + _terrainOffset.x;
            var zPos = _terrainSize * z + _terrainOffset.y;

            var terrain = Terrain.CreateTerrainGameObject(data).GetComponent<Terrain>();
            terrain.transform.position = new Vector3(xPos, 0, zPos);
            terrain.gameObject.SetParent(_terrainGameObject);
            terrain.name = string.Format("tile{0}x{1}", x, z);
            terrain.castShadows = false;
            terrain.detailObjectDensity = _terrainSettings.GrassDensity;
            terrain.detailObjectDistance = 200.0f;
            terrain.treeDistance = 1000.0f;
            terrain.treeBillboardDistance = 400.0f;
            terrain.treeCrossFadeLength = 20.0f;
            terrain.treeMaximumFullLODCount = 400;

            //Generate trees
            if (!centerTile)
            {
                //Possibly generate some lakes
                var rng = Random.value;

                if (_terrainSettings.GenerateLake && rng < 0.75f)
                {
                    var it = Random.Range(1, 4);
                    for (int i = 0; i < it; i++)
                    {
                        //add a lake o the terrain with a random size
                        AddLake(terrain, x, z, Random.Range(25, 60));
                    }
                }

                var trees = GenerateTrees(data, x, z);
                if (trees != null)
                {
                    foreach (var tree in trees)
                    {
                        terrain.AddTreeInstance(tree);
                    }
                }
            }

            //Generate alphamap if valid
            var alphamap = GenerateAlphaMap(data);
            if (alphamap != null)
            {
                data.SetAlphamaps(0, 0, alphamap);
            }

            //Generate Detailsmap if valid for every layer
            var detailLayers = GenerateDetailLayers(data, x, z);
            if (detailLayers != null)
            {
                //set some detail settings
                data.SetDetailResolution(_terrainSettings.DetailMapSize, _terrainSettings.DetailResolution);
                data.wavingGrassStrength = 0.1f;
                data.wavingGrassAmount = 0.1f;
                data.wavingGrassSpeed = 0.2f;
                data.wavingGrassTint = Color.white;
                for (int layer = 0; layer < detailLayers.Count; ++layer)
                {
                    data.SetDetailLayer(0, 0, layer, detailLayers[layer]);
                }
            }

            //make sure the changes are updated
            terrain.Flush();

            return terrain;
        }
        // Add Grass
        private void AddGrass(DaggerfallTerrain daggerTerrain, TerrainData terrainData)
        {
            //			Used to check performance
            //			Stopwatch stopwatch = new Stopwatch();
            //			stopwatch.Start();

            details = new int[256, 256];

            //Get the current season
            currentSeason = DaggerfallUnity.Instance.WorldTime.Now.SeasonValue;

            //Proceed if it's NOT winter, and if the worldClimate contains grass, which is everything above 225, with the exception of 229
            if (currentSeason != DaggerfallDateTime.Seasons.Winter && (daggerTerrain.MapData.worldClimate > 225 && daggerTerrain.MapData.worldClimate != 229))
            {
                //Switch the grass texture based on the climate
                if (daggerTerrain.MapData.worldClimate == 226 || daggerTerrain.MapData.worldClimate == 227 || daggerTerrain.MapData.worldClimate == 228 || daggerTerrain.MapData.worldClimate == 230)
                    detailPrototype[0].prototypeTexture = brownGrass;
                else
                    detailPrototype[0].prototypeTexture = greenGrass;

                tilemap = daggerTerrain.TileMap;
                terrainData.detailPrototypes = detailPrototype;
                terrainData.wavingGrassTint = Color.gray;
                terrainData.SetDetailResolution(256, 8);

                int colorValue;

                //Check all the tiles, Daggerfall uses the red color value to draw tiles
                for (int i = 0; i < 128; i++)
                {
                    for (int j = 0; j < 128; j++)
                    {
                        colorValue = tilemap[(i * 128) + j].r; //For easier checking

                        switch (colorValue)
                        {
                            //Four corner tiles
                            case 8:
                            case 9:
                            case 10:
                            case 11:
                                details[i * 2, j * 2] = Random.Range(thickLower, thickHigher);
                                details[i * 2, (j * 2) + 1] = Random.Range(thickLower, thickHigher);
                                details[(i * 2) + 1, j * 2] = Random.Range(thickLower, thickHigher);
                                details[(i * 2) + 1, (j * 2) + 1] = Random.Range(thickLower, thickHigher);
                                break;

                            //Upper left corner
                            case 40:
                            case 224:
                            case 164:
                            case 176:
                            case 181:
                                details[(i * 2) + 1, j * 2] = Random.Range(thinLower, thinHigher);
                                break;

                            //Lower left corner
                            case 41:
                            case 221:
                            case 165:
                            case 177:
                            case 182:
                                details[i * 2, j * 2] = Random.Range(thinLower, thinHigher);
                                break;

                            //Lower right corner
                            case 42:
                            case 222:
                            case 166:
                            case 178:
                            case 183:
                                details[i * 2, (j * 2) + 1] = Random.Range(thinLower, thinHigher);
                                break;

                            //Upper right corner
                            case 43:
                            case 223:
                            case 167:
                            case 179:
                            case 180:
                                details[(i * 2) + 1, (j * 2) + 1] = Random.Range(thinLower, thinHigher);
                                break;

                            //Left side
                            case 44:
                            case 66:
                            case 84:
                            case 160:
                            case 168:
                                details[(i * 2) + 1, j * 2] = Random.Range(thinLower, thinHigher);
                                details[i * 2, j * 2] = Random.Range(thinLower, thinHigher);
                                break;

                            //lower side
                            case 45:
                            case 67:
                            case 85:
                            case 161:
                            case 169:
                                details[i * 2, (j * 2) + 1] = Random.Range(thinLower, thinHigher);
                                details[i * 2, j * 2] = Random.Range(thinLower, thinHigher);
                                break;

                            //right side
                            case 46:
                            case 64:
                            case 86:
                            case 162:
                            case 170:
                                details[(i * 2) + 1, (j * 2) + 1] = Random.Range(thinLower, thinHigher);
                                details[i * 2, (j * 2) + 1] = Random.Range(thinLower, thinHigher);
                                break;

                            //upper side
                            case 47:
                            case 65:
                            case 87:
                            case 163:
                            case 171:
                                details[(i * 2) + 1, (j * 2) + 1] = Random.Range(thinLower, thinHigher);
                                details[(i * 2) + 1, j * 2] = Random.Range(thinLower, thinHigher);
                                break;

                            //All expect lower right
                            case 48:
                            case 62:
                            case 88:
                            case 156:
                                details[i * 2, j * 2] = Random.Range(thinLower, thinHigher);
                                details[(i * 2) + 1, j * 2] = Random.Range(thinLower, thinHigher);
                                details[(i * 2) + 1, (j * 2) + 1] = Random.Range(thinLower, thinHigher);
                                break;

                            //All expect upper right
                            case 49:
                            case 63:
                            case 89:
                            case 157:
                                details[i * 2, j * 2] = Random.Range(thinLower, thinHigher);
                                details[i * 2, (j * 2) + 1] = Random.Range(thinLower, thinHigher);
                                details[(i * 2) + 1, j * 2] = Random.Range(thinLower, thinHigher);
                                break;

                            //All expect upper left
                            case 50:
                            case 60:
                            case 90:
                            case 158:
                                details[i * 2, j * 2] = Random.Range(thinLower, thinHigher);
                                details[i * 2, (j * 2) + 1] = Random.Range(thinLower, thinHigher);
                                details[(i * 2) + 1, (j * 2) + 1] = Random.Range(thinLower, thinHigher);
                                break;

                            //All expect lower left
                            case 51:
                            case 61:
                            case 91:
                            case 159:
                                details[i * 2, (j * 2) + 1] = Random.Range(thinLower, thinHigher);
                                details[(i * 2) + 1, j * 2] = Random.Range(thinLower, thinHigher);
                                details[(i * 2) + 1, (j * 2) + 1] = Random.Range(thinLower, thinHigher);
                                break;

                            //Left to right
                            case 204:
                            case 206:
                            case 214:
                                details[i * 2, j * 2] = Random.Range(thinLower, thinHigher);
                                details[(i * 2) + 1, (j * 2) + 1] = Random.Range(thinLower, thinHigher);
                                break;

                            //Right to left
                            case 205:
                            case 207:
                            case 213:
                                details[(i * 2) + 1, j * 2] = Random.Range(thinLower, thinHigher);
                                details[i * 2, (j * 2) + 1] = Random.Range(thinLower, thinHigher);
                                break;

                        }

                    }
                }
            }

            terrainData.SetDetailLayer(0, 0, 0, details);

            //			stopwatch.Stop();
            //			// Write result
            //			UnityEngine.Debug.Log("Time elapsed: " +
            //			                      stopwatch.Elapsed);
        }
Beispiel #8
0
    private void CreateLand()
    {
        #region Preparations

        var mapData = Data.Battle["gamebody"]["map_info"]["types"];
        var worldSize = realMapSize * Settings.DimensionScaleFactor;
        var resolution = Mathf.RoundToInt(Mathf.Sqrt(realMapSize.x * realMapSize.y) * Settings.Terrain.Smoothness);
        var terrainData = new TerrainData { heightmapResolution = Mathf.ClosestPowerOfTwo(resolution) + 1, size = new Vector3(worldSize.y, Settings.Map.HeightOfLevel[2], worldSize.x), alphamapResolution = resolution, baseMapResolution = resolution };

        #endregion

        #region Set Heights

        float landArea = 0;
        var heights = new float[terrainData.heightmapHeight, terrainData.heightmapWidth];
        var mapRect = new Rect(0, 0, Data.MapSize.x, Data.MapSize.y);
        for (int bumpNum = Mathf.RoundToInt(realMapSize.x * realMapSize.y / 10), i = 0; i < bumpNum; ++i)
        {
            var sigmaX = Random.Range(0.8f, 2) * terrainData.heightmapHeight / realMapSize.x;
            var sigmaY = Random.Range(0.8f, 2) * terrainData.heightmapWidth / realMapSize.y;
            var muX = Random.Range(0, terrainData.heightmapHeight);
            var muY = Random.Range(0, terrainData.heightmapWidth);
            var h = Random.Range(-0.6f, 0.4f);
            for (var x = Mathf.Max(-muX, -Mathf.RoundToInt(sigmaX * 3)); x < Mathf.Min(terrainData.heightmapHeight - muX, Mathf.RoundToInt(sigmaX * 3)); ++x)
                for (var y = Mathf.Max(-muY, -Mathf.RoundToInt(sigmaY * 3)); y < Mathf.Min(terrainData.heightmapWidth - muY, Mathf.RoundToInt(sigmaY * 3)); ++y)
                    heights[muX + x, muY + y] += h * Mathf.Exp(-Mathf.Pow(x / sigmaX, 2) - Mathf.Pow(y / sigmaY, 2));
        }
        var threshold = Settings.Map.HeightOfLevel[0] / Settings.Map.HeightOfLevel[2] * 0.8f;
        for (var x = 0; x < terrainData.heightmapHeight; ++x)
            for (var y = 0; y < terrainData.heightmapWidth; ++y)
            {
                if ((heights[x, y] += threshold / 2) > threshold)
                    heights[x, y] = threshold;
                var i = (float)x / (terrainData.heightmapHeight - 1) * realMapSize.x - Settings.Map.MapSizeOffset.top;
                var j = (1 - (float)y / (terrainData.heightmapWidth - 1)) * realMapSize.y - Settings.Map.MapSizeOffset.left;
                int i0 = Mathf.FloorToInt(i), j0 = Mathf.FloorToInt(j);
                float ul = 0, ur = 0, br = 0, bl = 0, di = i - i0, dj = j - j0;
                if (mapRect.Contains(new Vector2(i0, j0)))
                    ul = mapData[i0][j0].n;
                if (mapRect.Contains(new Vector2(i0, j0 + 1)))
                    ur = mapData[i0][j0 + 1].n;
                if (mapRect.Contains(new Vector2(i0 + 1, j0 + 1)))
                    br = mapData[i0 + 1][j0 + 1].n;
                if (mapRect.Contains(new Vector2(i0 + 1, j0)))
                    bl = mapData[i0 + 1][j0].n;
                var height = (1 - di) * (1 - dj) * ul + (1 - di) * dj * ur + di * (1 - dj) * bl + di * dj * br;
                if (Math.Abs(height) < Mathf.Epsilon)
                    continue;
                heights[x, y] = Mathf.Max(heights[x, y], height = Mathf.Sign(height - 0.5f) * Mathf.Pow(Mathf.Abs(height * 2 - 1), 0.25f) / 2 + 0.5f);
                landArea += height;
            }
        terrainData.SetHeights(0, 0, heights);
        landArea *= realMapSize.x * realMapSize.y / (terrainData.heightmapHeight * terrainData.heightmapWidth);

        #endregion

        #region Paint Texture

        var splatPrototypes = new SplatPrototype[splatTextures.Length];
        for (var i = 0; i < splatPrototypes.Length; i++)
        {
            var splatPrototype = new SplatPrototype { texture = splatTextures[i], tileSize = Vector2.one * Settings.DimensionScaleFactor * 4 };
            splatPrototypes[i] = splatPrototype;
        }
        terrainData.splatPrototypes = splatPrototypes;
        var alphamapResolution = terrainData.alphamapResolution;
        var alphamaps = new float[alphamapResolution, alphamapResolution, splatPrototypes.Length];
        for (var i = 0; i < alphamapResolution; i++)
            for (var j = 0; j < alphamapResolution; j++)
            {
                var height = heights[Mathf.RoundToInt((float)i / (alphamapResolution - 1) * (terrainData.heightmapHeight - 1)), Mathf.RoundToInt((float)j / (alphamapResolution - 1) * (terrainData.heightmapWidth - 1))];
                alphamaps[i, j, 0] = height;
                alphamaps[i, j, 1] = 1 - height;
            }
        terrainData.SetAlphamaps(0, 0, alphamaps);

        #endregion

        #region Place Trees

        var treePrototypes = new TreePrototype[treePrefabs.Length];
        for (var i = 0; i < treePrototypes.Length; i++)
        {
            var treePrototype = new TreePrototype { prefab = treePrefabs[i], bendFactor = 1 };
            treePrototypes[i] = treePrototype;
        }
        terrainData.treePrototypes = treePrototypes;
        var treeInstances = new TreeInstance[Mathf.RoundToInt(landArea * Settings.Terrain.Tree.Density)];
        var range = new Vector4(Settings.Map.MapSizeOffset.right / realMapSize.y, 1 - Settings.Map.MapSizeOffset.left / realMapSize.y, Settings.Map.MapSizeOffset.top / realMapSize.x, 1 - Settings.Map.MapSizeOffset.bottom / realMapSize.x);
        for (var i = 0; i < treeInstances.Length; i++)
        {
            var treeScale = Random.Range(0.08f, 0.16f) * Settings.DimensionScaleFactor;
            Vector3 treePosition;
            do
                treePosition = new Vector3(Random.Range(range.x, range.y), 0, Random.Range(range.z, range.w));
            while ((treePosition.y = heights[Mathf.RoundToInt(treePosition.z * (terrainData.heightmapHeight - 1)), Mathf.RoundToInt(treePosition.x * (terrainData.heightmapWidth - 1))]) < Mathf.Lerp(Settings.Map.HeightOfLevel[1] / Settings.Map.HeightOfLevel[2], 1, 0.6f) || Methods.Coordinates.IsOccupied(Methods.Coordinates.InternalToExternal(Vector3.Scale(treePosition, new Vector3(worldSize.y, 0, worldSize.x)))));
            var treeInstance = new TreeInstance { prototypeIndex = Random.Range(0, treePrototypes.Length), position = treePosition + Vector3.up * Settings.Terrain.Tree.VerticalPositionOffset * treeScale, color = new Color(0, 0.8f, 0, 1), lightmapColor = new Color(1, 1, 1, 1), heightScale = treeScale, widthScale = treeScale };
            treeInstances[i] = treeInstance;
        }
        terrainData.treeInstances = treeInstances;

        #endregion

        #region Paint Details

        var detailPrototypes = new DetailPrototype[detailTextures.Length];
        for (var i = 0; i < detailPrototypes.Length; i++)
        {
            var detailPrototype = new DetailPrototype { prototypeTexture = detailTextures[i], minWidth = Settings.Terrain.Detail.MinDimension, minHeight = Settings.Terrain.Detail.MinDimension, maxWidth = Settings.Terrain.Detail.MaxDimension, maxHeight = Settings.Terrain.Detail.MaxDimension, renderMode = DetailRenderMode.GrassBillboard };
            detailPrototypes[i] = detailPrototype;
        }
        terrainData.detailPrototypes = detailPrototypes;
        terrainData.SetDetailResolution(resolution, Mathf.Clamp(resolution, 8, 128));
        var detailLayers = new int[detailPrototypes.Length][,];
        for (var i = 0; i < detailPrototypes.Length; i++)
            detailLayers[i] = new int[terrainData.detailResolution, terrainData.detailResolution];
        for (var i = 0; i < terrainData.detailResolution; i++)
            for (var j = 0; j < terrainData.detailResolution; j++)
            {
                var layer = Random.Range(0, detailPrototypes.Length);
                var height = heights[Mathf.RoundToInt((float)i / (terrainData.detailResolution - 1) * (terrainData.heightmapHeight - 1)), Mathf.RoundToInt((float)j / (terrainData.detailResolution - 1) * (terrainData.heightmapWidth - 1))];
                if (height > Mathf.Lerp(Settings.Map.HeightOfLevel[1] / Settings.Map.HeightOfLevel[2], 1, 0.4f))
                    detailLayers[layer][i, j] = 1;
            }
        for (var i = 0; i < detailPrototypes.Length; i++)
            terrainData.SetDetailLayer(0, 0, i, detailLayers[i]);
        terrainData.wavingGrassAmount = Settings.Terrain.Detail.Waving.Amount;
        terrainData.wavingGrassSpeed = Settings.Terrain.Detail.Waving.Speed;
        terrainData.wavingGrassStrength = Settings.Terrain.Detail.Waving.Strength;

        #endregion

        #region Final Settings

        var terrain = Terrain.CreateTerrainGameObject(terrainData).GetComponent<Terrain>();
        terrain.treeBillboardDistance = Settings.Terrain.Tree.BillboardDistance;
        terrain.detailObjectDistance = Settings.Terrain.Detail.MaxVisibleDistance;
        terrain.detailObjectDensity = Settings.Terrain.Detail.Density;

        #endregion
    }
Beispiel #9
0
		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;
		}