/// <summary>
	/// Creates terrain data from heights.
	/// </summary>
	/// <param name="heightPercents">Terrain height percentages ranging from 0 to 1.</param>
	/// <param name="maxHeight">The maximum height of the terrain, corresponding to a height percentage of 1.</param>
	/// <param name="heightSampleDistance">The horizontal/vertical distance between height samples.</param>
	/// <param name="splatPrototypes">The textures used by the terrain.</param>
	/// <param name="alphaMap">Texture blending information.</param>
	/// <returns>A TerrainData instance.</returns>
	public static TerrainData CreateTerrainData(float[,] heightPercents, float maxHeight, float heightSampleDistance, SplatPrototype[] splatPrototypes, float[,,] alphaMap)
	{
		Debug.Assert((heightPercents.GetLength(0) == heightPercents.GetLength(1)) && (maxHeight >= 0) && (heightSampleDistance >= 0));

		// Create the TerrainData.
		var terrainData = new TerrainData();
		terrainData.heightmapResolution = heightPercents.GetLength(0);

		var terrainWidth = (terrainData.heightmapResolution - 1) * heightSampleDistance;

		// If maxHeight is 0, leave all the heights in terrainData at 0 and make the vertical size of the terrain 1 to ensure valid AABBs.
		if(!Mathf.Approximately(maxHeight, 0))
		{
			terrainData.size = new Vector3(terrainWidth, maxHeight, terrainWidth);

			terrainData.SetHeights(0, 0, heightPercents);
		}
		else
		{
			terrainData.size = new Vector3(terrainWidth, 1, terrainWidth);
		}

		// Texture the terrain.
		if((splatPrototypes != null) && (alphaMap != null))
		{
			Debug.Assert(alphaMap.GetLength(0) == alphaMap.GetLength(1));

			terrainData.alphamapResolution = alphaMap.GetLength(0);
			terrainData.splatPrototypes = splatPrototypes;
			terrainData.SetAlphamaps(0, 0, alphaMap);
		}

		return terrainData;
	}
示例#2
0
 public virtual SplatPrototype GetSplatPrototype()
 {
     SplatPrototype splatsPrototypes = new SplatPrototype();
     splatsPrototypes.texture = Texture;
     splatsPrototypes.normalMap = Normal;
     return splatsPrototypes;
 }
示例#3
0
    public void SetTextureOnTerrain(Terrain terrain)
    {
        SplatPrototype[] va_sp = new SplatPrototype[2];

        va_sp[0] = new SplatPrototype();
        va_sp[0].texture = (Texture2D)Resources.Load("MyTextures/"+FirstTexture);
        va_sp[1] = new SplatPrototype();
        va_sp[1].texture = (Texture2D)Resources.Load("MyTextures/"+SecondTexture);
        terrain.terrainData.splatPrototypes = va_sp;
        int v_td_alphaMapResolution = terrain.terrainData.alphamapResolution;

        float[, ,] va_alphamaps = new float[v_td_alphaMapResolution, v_td_alphaMapResolution, va_sp.Length];
        va_alphamaps = terrain.terrainData.GetAlphamaps(0, 0, v_td_alphaMapResolution, v_td_alphaMapResolution);

        for (int ti = 0; ti < v_td_alphaMapResolution; ti++) {
            for (int tj = 0; tj < v_td_alphaMapResolution; tj++) {
                float y_01 = (float)tj/(float)terrain.terrainData.alphamapHeight;
                float x_01 = (float)ti/(float)terrain.terrainData.alphamapWidth;
                float height = terrain.terrainData.GetHeight(Mathf.RoundToInt(y_01 * terrain.terrainData.heightmapHeight),Mathf.RoundToInt(x_01 * terrain.terrainData.heightmapWidth) );
                for (int v_tex = 0; v_tex < va_sp.Length; v_tex++) {
                    if(height>HeightSeperation)
                        va_alphamaps[ti,tj,v_tex] = Random.Range(0.0f, 1);
                }
            }
        }
        terrain.terrainData.SetAlphamaps(0, 0, va_alphamaps);
    }
	void Start () {
		tData = Terrain.activeTerrain.terrainData;
		
		SplatPrototype[] terrainTexture = new SplatPrototype[1]; 
		terrainTexture[0] = new SplatPrototype(); 
		terrainTexture[0].texture = (Texture2D)Resources.Load("MyTextures/Dirt");
		tData.splatPrototypes = terrainTexture;  
	}
示例#5
0
	/// <summary>
	/// Convert to SplatPrototype
	/// </summary>
	/// <returns>
	/// A <see cref="SplatPrototype"/>
	/// </returns>
	public SplatPrototype ToSplat()
	{
		SplatPrototype splat = new SplatPrototype();
		splat.texture = texture;
		splat.tileOffset = tileOffset;
		splat.tileSize = tileSize;
		return splat;
	}
 internal static void RemoveSplatTexture(TerrainData terrainData, int index)
 {
   Undo.RegisterCompleteObjectUndo((Object) terrainData, "Remove texture");
   int alphamapWidth = terrainData.alphamapWidth;
   int alphamapHeight = terrainData.alphamapHeight;
   float[,,] alphamaps = terrainData.GetAlphamaps(0, 0, alphamapWidth, alphamapHeight);
   int length1 = alphamaps.GetLength(2);
   int length2 = length1 - 1;
   float[,,] map = new float[alphamapHeight, alphamapWidth, length2];
   for (int index1 = 0; index1 < alphamapHeight; ++index1)
   {
     for (int index2 = 0; index2 < alphamapWidth; ++index2)
     {
       for (int index3 = 0; index3 < index; ++index3)
         map[index1, index2, index3] = alphamaps[index1, index2, index3];
       for (int index3 = index + 1; index3 < length1; ++index3)
         map[index1, index2, index3 - 1] = alphamaps[index1, index2, index3];
     }
   }
   for (int index1 = 0; index1 < alphamapHeight; ++index1)
   {
     for (int index2 = 0; index2 < alphamapWidth; ++index2)
     {
       float num1 = 0.0f;
       for (int index3 = 0; index3 < length2; ++index3)
         num1 += map[index1, index2, index3];
       if ((double) num1 >= 0.01)
       {
         float num2 = 1f / num1;
         for (int index3 = 0; index3 < length2; ++index3)
           map[index1, index2, index3] *= num2;
       }
       else
       {
         for (int index3 = 0; index3 < length2; ++index3)
           map[index1, index2, index3] = index3 != 0 ? 0.0f : 1f;
       }
     }
   }
   SplatPrototype[] splatPrototypes = terrainData.splatPrototypes;
   SplatPrototype[] splatPrototypeArray = new SplatPrototype[splatPrototypes.Length - 1];
   for (int index1 = 0; index1 < index; ++index1)
     splatPrototypeArray[index1] = splatPrototypes[index1];
   for (int index1 = index + 1; index1 < length1; ++index1)
     splatPrototypeArray[index1 - 1] = splatPrototypes[index1];
   terrainData.splatPrototypes = splatPrototypeArray;
   terrainData.SetAlphamaps(0, 0, map);
 }
		private void InitializeData(Terrain terrain, int index)
		{
			this.m_Terrain = terrain;
			this.m_Index = index;
			SplatPrototype splatPrototype;
			if (index == -1)
			{
				splatPrototype = new SplatPrototype();
			}
			else
			{
				splatPrototype = this.m_Terrain.terrainData.splatPrototypes[index];
			}
			this.m_Texture = splatPrototype.texture;
			this.m_NormalMap = splatPrototype.normalMap;
			this.m_TileSize = splatPrototype.tileSize;
			this.m_TileOffset = splatPrototype.tileOffset;
			this.m_Specular = splatPrototype.specular;
			this.m_Metallic = splatPrototype.metallic;
		}
 private void ApplyTerrainSplat()
 {
   if ((UnityEngine.Object) this.m_Terrain == (UnityEngine.Object) null || (UnityEngine.Object) this.m_Terrain.terrainData == (UnityEngine.Object) null)
     return;
   SplatPrototype[] splatPrototypeArray1 = this.m_Terrain.terrainData.splatPrototypes;
   if (this.m_Index == -1)
   {
     SplatPrototype[] splatPrototypeArray2 = new SplatPrototype[splatPrototypeArray1.Length + 1];
     Array.Copy((Array) splatPrototypeArray1, 0, (Array) splatPrototypeArray2, 0, splatPrototypeArray1.Length);
     this.m_Index = splatPrototypeArray1.Length;
     splatPrototypeArray1 = splatPrototypeArray2;
     splatPrototypeArray1[this.m_Index] = new SplatPrototype();
   }
   splatPrototypeArray1[this.m_Index].texture = this.m_Texture;
   splatPrototypeArray1[this.m_Index].normalMap = this.m_NormalMap;
   splatPrototypeArray1[this.m_Index].tileSize = this.m_TileSize;
   splatPrototypeArray1[this.m_Index].tileOffset = this.m_TileOffset;
   splatPrototypeArray1[this.m_Index].specular = this.m_Specular;
   splatPrototypeArray1[this.m_Index].metallic = this.m_Metallic;
   splatPrototypeArray1[this.m_Index].smoothness = this.m_Smoothness;
   this.m_Terrain.terrainData.splatPrototypes = splatPrototypeArray1;
   EditorUtility.SetDirty((UnityEngine.Object) this.m_Terrain);
 }
示例#9
0
 private void ApplyTerrainSplat()
 {
     if ((this.m_Terrain != null) && (this.m_Terrain.terrainData != null))
     {
         SplatPrototype[] splatPrototypes = this.m_Terrain.terrainData.splatPrototypes;
         if (this.m_Index == -1)
         {
             SplatPrototype[] destinationArray = new SplatPrototype[splatPrototypes.Length + 1];
             Array.Copy(splatPrototypes, 0, destinationArray, 0, splatPrototypes.Length);
             this.m_Index = splatPrototypes.Length;
             splatPrototypes = destinationArray;
             splatPrototypes[this.m_Index] = new SplatPrototype();
         }
         splatPrototypes[this.m_Index].texture = this.m_Texture;
         splatPrototypes[this.m_Index].normalMap = this.m_NormalMap;
         splatPrototypes[this.m_Index].tileSize = this.m_TileSize;
         splatPrototypes[this.m_Index].tileOffset = this.m_TileOffset;
         splatPrototypes[this.m_Index].specular = this.m_Specular;
         splatPrototypes[this.m_Index].metallic = this.m_Metallic;
         splatPrototypes[this.m_Index].smoothness = this.m_Smoothness;
         this.m_Terrain.terrainData.splatPrototypes = splatPrototypes;
         EditorUtility.SetDirty(this.m_Terrain);
     }
 }
		private void ApplyTerrainSplat()
		{
			if (this.m_Terrain == null || this.m_Terrain.terrainData == null)
			{
				return;
			}
			SplatPrototype[] array = this.m_Terrain.terrainData.splatPrototypes;
			if (this.m_Index == -1)
			{
				SplatPrototype[] array2 = new SplatPrototype[array.Length + 1];
				Array.Copy(array, 0, array2, 0, array.Length);
				this.m_Index = array.Length;
				array = array2;
				array[this.m_Index] = new SplatPrototype();
			}
			array[this.m_Index].texture = this.m_Texture;
			array[this.m_Index].normalMap = this.m_NormalMap;
			array[this.m_Index].tileSize = this.m_TileSize;
			array[this.m_Index].tileOffset = this.m_TileOffset;
			array[this.m_Index].specular = this.m_Specular;
			array[this.m_Index].metallic = this.m_Metallic;
			this.m_Terrain.terrainData.splatPrototypes = array;
			EditorUtility.SetDirty(this.m_Terrain);
		}
示例#11
0
	void CreateProtoTypes()
	{
		//Ive hard coded 2 splat prototypes, 3 tree prototypes and 3 detail prototypes.
		//This is a little inflexible way to do it but it made the code simpler and can easly be modified 
		
		m_splatPrototypes = new SplatPrototype[2];
		
		m_splatPrototypes[0] = new SplatPrototype();
		m_splatPrototypes[0].texture = m_splat0;
		m_splatPrototypes[0].tileSize = new Vector2(m_splatTileSize0, m_splatTileSize0);
		
		m_splatPrototypes[1] = new SplatPrototype();
		m_splatPrototypes[1].texture = m_splat1;
		m_splatPrototypes[1].tileSize = new Vector2(m_splatTileSize1, m_splatTileSize1);
		
		m_treeProtoTypes = new TreePrototype[3];
		
		m_treeProtoTypes[0] = new TreePrototype();
		m_treeProtoTypes[0].prefab = m_tree0;
		
		m_treeProtoTypes[1] = new TreePrototype();
		m_treeProtoTypes[1].prefab = m_tree1;
		
		m_treeProtoTypes[2] = new TreePrototype();
		m_treeProtoTypes[2].prefab = m_tree2;
		
		m_detailProtoTypes = new DetailPrototype[3];

		m_detailProtoTypes[0] = new DetailPrototype();
		m_detailProtoTypes[0].prototypeTexture = m_detail0;
		m_detailProtoTypes[0].renderMode = detailMode;
		m_detailProtoTypes[0].healthyColor = m_grassHealthyColor;
		m_detailProtoTypes[0].dryColor = m_grassDryColor;
		
		m_detailProtoTypes[1] = new DetailPrototype();
		m_detailProtoTypes[1].prototypeTexture = m_detail1;
		m_detailProtoTypes[1].renderMode = detailMode;
		m_detailProtoTypes[1].healthyColor = m_grassHealthyColor;
		m_detailProtoTypes[1].dryColor = m_grassDryColor;
		
		m_detailProtoTypes[2] = new DetailPrototype();
		m_detailProtoTypes[2].prototypeTexture = m_detail2;
		m_detailProtoTypes[2].renderMode = detailMode;
		m_detailProtoTypes[2].healthyColor = m_grassHealthyColor;
		m_detailProtoTypes[2].dryColor = m_grassDryColor;

		
	}
示例#12
0
 public void ApplyMapTexture()
 {
     //need this to add the texture to the terrain so it looks like a map
     List<SplatPrototype> splatList = new List<SplatPrototype>();
     SplatPrototype newSplat = new SplatPrototype();
     newSplat.texture = texture;
     float width = GetComponent<Terrain> ().terrainData.size.x;
     newSplat.tileSize = new Vector2( width, width );
     newSplat.tileOffset = Vector2.zero;
     splatList.Add (newSplat);
     GetComponent<Terrain> ().terrainData.splatPrototypes = splatList.ToArray();
 }
    void Awake()
    {
        terrainSizePOT = terrainSize - 1;
        textureRes = terrainSizePOT;
        texture = new Texture2D(textureRes, textureRes, TextureFormat.RGB24, true);
        texture.name = "TextureTest";
        texture.wrapMode = TextureWrapMode.Clamp;
        GetComponent<Renderer>().material.mainTexture = texture;

        for (int i = 0; i < 3; i++)
        {
            splatProto[i] = new SplatPrototype();
            splatProto[i].texture = (Texture2D)Resources.Load(i.ToString(), typeof(Texture2D));
            splatProto[i].tileOffset = new Vector2(0, 0);
            splatProto[i].tileSize = new Vector2(textureRes, textureRes);
        }
        //splatProto[0] = new SplatPrototype();
        //splatProto[0].texture = (Texture2D)Resources.Load("0", typeof(Texture2D));
        //splatProto[0].tileOffset = new Vector2(0, 0);
        //splatProto[0].tileSize = new Vector2(textureRes, textureRes);

        //splatProto[1] = new SplatPrototype();
        //splatProto[1].texture = (Texture2D)Resources.Load("snow", typeof(Texture2D));
        //splatProto[1].tileOffset = new Vector2(0, 0);
        //splatProto[1].tileSize = new Vector2(textureRes, textureRes);
    }
示例#14
0
 public void addSplatPrototype(Texture2D tex, int index)
 {
     SplatPrototype[] splatPrototype = new SplatPrototype[index + 1];
     for (int i = 0; i <= index; i++)
     {
         splatPrototype[i] = new SplatPrototype();
         if (i != index)
         {
             splatPrototype[i].texture = this.splatPrototypes[i].texture;
             splatPrototype[i].tileSize = this.splatPrototypes[i].tileSize;
         }
         else
         {
             splatPrototype[i].texture = tex;
             splatPrototype[i].tileSize = new Vector2(15f, 15f);
         }
     }
     this.splatPrototypes = splatPrototype;
     if (index + 1 > 2)
     {
         this.addBlendPoints();
     }
 }
示例#15
0
 public void deleteSplatPrototype(Texture2D tex, int index)
 {
     int length = 0;
     length = (int)this.splatPrototypes.Length;
     SplatPrototype[] splatPrototype = new SplatPrototype[length - 1];
     int num = 0;
     for (int i = 0; i < length; i++)
     {
         if (i != index)
         {
             splatPrototype[num] = new SplatPrototype();
             splatPrototype[num].texture = this.splatPrototypes[i].texture;
             splatPrototype[num].tileSize = this.splatPrototypes[i].tileSize;
             num++;
         }
     }
     this.splatPrototypes = splatPrototype;
     if (length - 1 > 1)
     {
         this.deleteBlendPoints();
     }
 }
示例#16
0
	void Start () {
		noise = new PerlinNoise();
		noise.baseOctave = noiseBaseOctave;
		noise.weights = noiseWeights;
		noise.Init();

		chunkResolution = (int)Math.Pow(2, chunkExponent) + 1;
		chunkWidth = chunkResolution * chunkScale;

		chunks = new Hashtable();
		heightmapQueue = Queue.Synchronized(new Queue());

		reverseThermalEroder = new ThermalTerrainErosion();
		reverseThermalEroder.talus = this.reverseErosionTalus;
		reverseThermalEroder.strength = this.reverseErosionStrength;
		reverseThermalEroder.reverse = true;
		reverseThermalEroder.reverseTalusCutoff = this.reverseErosionReverseTalusCutoff ;
		reverseThermalEroder.iterations = this.reverseErosionIterations;

		thermalEroder = new ThermalTerrainErosion();
		thermalEroder.talus = this.thermalErosionTalus;
		thermalEroder.strength = this.thermalErosionStrength;
		thermalEroder.reverse = false;
		thermalEroder.iterations = this.thermalErosionIterations;

		hydraulicEroder = new HydraulicTerrainErosion();
		hydraulicEroder.rainfallAmount = hydraulicErosionRainfallAmount; // kr
		hydraulicEroder.evaporationRatio = hydraulicErosionEvaporationRatio; // ke
		hydraulicEroder.sedimentCapacity = hydraulicErosionSedimentCapacity; // kc
		hydraulicEroder.soilSolubility = hydraulicErosionSoilSolubility; // ks
		hydraulicEroder.rainAltitude = hydraulicErosionRainAltitude;
		hydraulicEroder.rainFalloff = hydraulicErosionRainFalloff;
		hydraulicEroder.iterations = hydraulicErosionIterations;

		texturer = new Texturer();
		texturer.slopeValue = slopeValue;
		texturer.mountainPeekHeight = mountainPeekHeight;
		texturer.waterHeight = waterHeight;
		splats = new SplatPrototype[diffuses.Length];

		for (var i = 0; i < diffuses.Length; i++) {
			splats[i] = new SplatPrototype();
			splats[i].texture = diffuses[i];
			splats[i].normalMap = normals[i];
			splats[i].tileSize = new Vector2(5, 5);
		}

		ensureChunk(new ChunkCoord(0, 0), false);
		ensureChunk(new ChunkCoord(-1, -1), true);
		ensureChunk(new ChunkCoord(0, -1), true);
		ensureChunk(new ChunkCoord(-1, 0), true);
	}
示例#17
0
        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);
        }
示例#18
0
    void Start()
    {
        noise = new PerlinNoise ();
        noise.baseOctave = noiseBaseOctave;
        noise.weights = noiseWeights;
        noise.Init ();

        chunkResolution = (int)Math.Pow (2, chunkExponent) + 1;
        chunkWidth = chunkResolution * chunkScale;

        chunks = new Hashtable ();
        heightmapQueue = Queue.Synchronized (new Queue ());

        reverseThermalEroder = new ThermalTerrainErosion ();
        reverseThermalEroder.talus = this.reverseErosionTalus;
        reverseThermalEroder.strength = this.reverseErosionStrength;
        reverseThermalEroder.reverse = true;
        reverseThermalEroder.reverseTalusCutoff = this.reverseErosionReverseTalusCutoff;
        reverseThermalEroder.iterations = this.reverseErosionIterations;

        hydraulicEroder = new HydraulicTerrainErosion ();
        hydraulicEroder.rainfallAmount = hydraulicErosionRainfallAmount; // kr
        hydraulicEroder.evaporationRatio = hydraulicErosionEvaporationRatio; // ke
        hydraulicEroder.sedimentCapacity = hydraulicErosionSedimentCapacity; // kc
        hydraulicEroder.soilSolubility = hydraulicErosionSoilSolubility; // ks
        hydraulicEroder.rainAltitude = hydraulicErosionRainAltitude;
        hydraulicEroder.rainFalloff = hydraulicErosionRainFalloff;
        hydraulicEroder.iterations = hydraulicErosionIterations;

        thermalEroder = new ThermalTerrainErosion ();
        thermalEroder.talus = this.thermalErosionTalus;
        thermalEroder.strength = this.thermalErosionStrength;
        thermalEroder.reverse = false;
        thermalEroder.iterations = this.thermalErosionIterations;

        texturer = new Texturer();
        texturer.Init();
        texturer.slopeValue = slopeValue;
        texturer.mountainPeekStart = mountainPeekStart;
        texturer.mountainPeekHeight = mountainPeekHeight;
        texturer.waterHeight = waterHeight;
        texturer.shoreHeight = shoreHeight;
        splats = new SplatPrototype[diffuses.Length];

        texturer.treeHeightFactor = treeHeightFactor;
        texturer.treeStrength = treeStrength;
        treeProto = new TreePrototype[treePrefab.Length];
        for (var i = 0; i < treePrefab.Length; i++) {
            treeProto[i] = new TreePrototype();
            treeProto[i].bendFactor = 0;
            treeProto[i].prefab = treePrefab[i];
        }

        grassPlacer = new GrassPlacement();
        grassPlacer.grassType = grassType;
        grassPlacer.texturesToAffect = texturesToAffect;

        for (var i = 0; i < diffuses.Length; i++) {
            splats[i] = new SplatPrototype();
            splats[i].texture = diffuses[i];
            splats[i].normalMap = normals[i];
            splats[i].tileSize = new Vector2(5, 5);
        }
        grassProto = new DetailPrototype[1];
        grassProto[0] = new DetailPrototype();
        grassProto[0].prototypeTexture = grass;
        grassProto[0].bendFactor = 0.1f;
        grassProto[0].dryColor = new Color(0.804f, 0.737f, 0.102f, 1.000f);
        grassProto[0].healthyColor = new Color(0.263f, 0.976f, 0.165f, 1.000f);
        grassProto[0].maxHeight = 0.5f;
        grassProto[0].minHeight = 0.2f;
        grassProto[0].maxWidth = 1f;
        grassProto[0].minWidth = 0.5f;
        grassProto[0].noiseSpread = 0.1f;
        grassProto[0].prototype = null;
        grassProto[0].renderMode = DetailRenderMode.GrassBillboard;
        grassProto[0].usePrototypeMesh = false;

        ensureChunk(new ChunkCoord(0, 0), false);
        ensureChunk(new ChunkCoord(-1, -1), true);
        ensureChunk(new ChunkCoord(0, -1), true);
        ensureChunk(new ChunkCoord(-1, 0), true);
    }
        public override void OnInspectorGUI()
        {
            DrawDefaultInspector();

            if (GUILayout.Button("Generuj"))
            {
                var terrainData = new TerrainData { heightmapResolution = 65, size = new Vector3(50f, 20f, 50f) };

                int xSize = terrainData.heightmapHeight;
                int zSize = terrainData.heightmapWidth;

                const float noiseScale = 1.5f;

                var heights = terrainData.GetHeights(0, 0, xSize, zSize);

                float[][] noise = PerlinTools.GeneratePerlinNoise(xSize, zSize, 8);

                for (var x = 0; x < terrainData.heightmapHeight; x++)
                {
                    for (var z = 0; z < terrainData.heightmapWidth; z++)
                    {
                        heights[x, z] = noise[x][z] * noiseScale;
                    }
                }
                terrainData.SetHeights(0, 0, heights);

                var tekstura = Resources.Load<Texture2D>(
                                    "prototype_textures/Textures/proto_blue");
                if (tekstura != null)
                {
                    var sp = new SplatPrototype[1];
                    sp[0] = new SplatPrototype{texture = tekstura};
                    terrainData.splatPrototypes = sp;
                    var alphamaps = new float[xSize, zSize, sp.Length];
                    for(int ax = 0; ax < xSize; ++ax)
                        for(int az = 0; az < zSize; ++az)
                            for (int tex = 0; tex < sp.Length; ++tex)
                                alphamaps[ax, az, tex] = Math.Abs(Mathf.Sin(ax*1.2f + az*1.3f));
                    terrainData.SetAlphamaps(0, 0, alphamaps);
                }

                GameObject terrain = Terrain.CreateTerrainGameObject(terrainData);
            }

            /*
            _Terrain.terr = Terrain.activeTerrain;
            hmWidth = terr.terrainData.heightmapWidth;
            hmHeight = terr.terrainData.heightmapHeight;
            Terrain.activeTerrain.heightmapMaximumLOD = 0;

            // get the heights of the terrain under this game object
            float[,] heights = terr.terrainData.GetHeights(0, 0, hmWidth, hmHeight);

            // we set each sample of the terrain in the size to the desired height
            //for (int i=0; i<hmWidth; i++)
            //for (int j=0; j<hmHeight; j++){
            //heights[i,j] = (Mathf.Sin(i/10f)/100f + Mathf.Cos((i+j)/10f) / 100f) + 0.5f;

            float[][] mapa = PerlinTools.GeneratePerlinNoise(hmWidth, hmHeight, 4);
            for (int x = 0; x < hmWidth; ++x)
                for (int z = 0; z < hmHeight; ++z)
                {
                    heights[x, z] = mapa[x][z] / 1000f;
                }
            //print(heights[i,j]);
            //}
            // set the new height
            terr.terrainData.SetHeights(0, 0, heights);

             */
        }
示例#20
0
    //Advanced texturing
    public static void GenerateTexture(List<TTexture> textures)
    {
        Terrain t = Terrain.activeTerrain;
        TerrainData td = t.terrainData;

        SplatPrototype[] splatPrototypes = new SplatPrototype[textures.Count];

        for (int i = 0; i < textures.Count; i++)
        {
            splatPrototypes[i] = new SplatPrototype() { texture = (Texture2D)textures[i].texture, tileSize = textures[i].tilesize };
        }

        td.splatPrototypes = splatPrototypes;

        float[, ,] splatmaps = new float[td.alphamapWidth, td.alphamapHeight, td.alphamapLayers];

        float terrainMaxHeight = td.size.y;

        float x = 0.0f;
        while (x < td.alphamapHeight)
        {
            float y = 0.0f;
            while (y < td.alphamapWidth)
            {

                float height = td.GetHeight((int)x, (int)y);
                float heightScaled = height / terrainMaxHeight;

                float xS = x / td.heightmapWidth;
                float yS = y / td.heightmapHeight;

                float steepness = td.GetSteepness(xS, yS);
                float angleScaled = steepness / 90.0f;

                for (int i = 0; i < td.alphamapLayers; i++)
                {

                    switch (textures[i].index)
                    {
                        case(0):
                            if (i != 0)
                            {
                                splatmaps[(int)y, (int)x, i] = textures[i].heightCurve.Evaluate(heightScaled);
                                for (int hi = 0; hi < i; hi++)
                                {
                                    splatmaps[(int)y, (int)x, hi] *= (splatmaps[(int)y, (int)x, i] -1 )/ -1;
                                }
                            }
                            else
                            {
                                splatmaps[(int)y, (int)x, i] = textures[i].heightCurve.Evaluate(heightScaled);
                            }
                            break;
                        case(1):
                            splatmaps[(int)y, (int)x, i] = textures[i].angleCurve.Evaluate(angleScaled);
                            for (int ai = 0; ai < i; ai++)
                            {
                                splatmaps[(int)y, (int)x, ai] *= (splatmaps[(int)y, (int)x, i] -1 )/ -1;
                            }
                            break;
                        default:
                            break;
                    }

                    if (splatmaps[(int)y, (int)x, i] > 1.0f) { splatmaps[(int)y, (int)x, i] = 1.0f; }
                }
                y++;
            }
            x++;
        }

        

        //Bump mapping and overlay Colors
        for (int bi = 0; bi < td.alphamapLayers; bi++)
        {
            //Colors;
            Shader.SetGlobalColor("_Color" + bi, textures[bi].color);

            if (textures[bi].useBump)
            {
                if (textures[bi].bumpmap != null)
                {
                    Shader.SetGlobalTexture("_Bump" + bi, textures[bi].bumpmap);
                }
                else
                {
                    Texture2D tex = new Texture2D(td.alphamapWidth, td.alphamapHeight);
                    Shader.SetGlobalTexture("_Bump" + bi, tex);
                }
            }
            else
            {
                Texture2D tex = new Texture2D(td.alphamapWidth, td.alphamapHeight);
                Shader.SetGlobalTexture("_Bump" + bi, tex);
            }
        }

        td.SetAlphamaps(0, 0, splatmaps);

    }
示例#21
0
    static Terrain ImportTerrain(string planet, string map, Revise.Files.ZON.ZoneFile zon, int x, int y)
    {
        var blockName = x.ToString() + "_" + y.ToString();
        var basePath = rootPath + "3DDATA/MAPS/" + planet + "/" + map + "/" + blockName;
        float blockX = (x - 32) * 160;
        float blockY = (32 - y) * 160;

        Object.DestroyImmediate(GameObject.Find(blockName));

        var ifo = new Revise.Files.IFO.MapDataFile();
        ifo.Load(basePath + ".IFO");

        for (int i = 0; i < ifo.Objects.Count; ++i)
        {
            var obj = ifo.Objects[i];
            ImportObject("JUNON_JDT_DECO", x, y, "DECO", i, obj);
        }

        for (int i = 0; i < ifo.Buildings.Count; ++i)
        {
            var obj = ifo.Buildings[i];
            ImportObject("JUNON_JDT_CNST", x, y, "CNST", i, obj);
        }

        for (int i = 0; i < ifo.Animations.Count; ++i)
        {
            //var obj = ifo.Animations[i];
            Debug.LogWarning("Got unexpected animation object.");
        }

        for (int i = 0; i < ifo.Sounds.Count; ++i) {
            var snd = ifo.Sounds[i];
            var sndName = "SND_" + snd.ObjectID.ToString() + " (" + blockName + "_" + i.ToString() + ")";

            var a = new GameObject();

            //var s = a.AddComponent<AudioSource>();
            //TODO: Need to link to audio after copy in prestage

            a.transform.localPosition = ifotruPosition(snd.Position);
            a.transform.localRotation = rtuRotation(snd.Rotation);
            a.transform.localScale = rtuScale(snd.Scale);
            a.name = sndName;
            a.isStatic = true;
        }

        var tex = ImportPlanMap(planet, map, x, y);

        var him = new Revise.Files.HIM.HeightmapFile();
        him.Load(basePath + ".HIM");

        float[,] heights = new float[65,65];
        float heightMin = him.Heights[0, 0];
        float heightMax = him.Heights[0, 0];
        for (int ix = 0; ix < 65; ++ix)
        {
            for (int iy = 0; iy < 65; ++iy)
            {
                if (him.Heights[ix, iy] < heightMin)
                {
                    heightMin = him.Heights[ix, iy];
                }
                if (him.Heights[ix, iy] > heightMax)
                {
                    heightMax = him.Heights[ix, iy];
                }
            }
        }
        float heightBase = heightMin;
        float heightDelta = heightMax - heightMin;
        for (int ix = 0; ix < 65; ++ix)
        {
            for (int iy = 0; iy < 65; ++iy)
            {
                heights[ix, iy] = (him.Heights[64 - ix, iy] - heightBase) / heightDelta;
            }
        }

        var til = new Revise.Files.TIL.TileFile();
        til.Load(basePath + ".TIL");

        /*
        for (int ix = 0; ix < til.Width; ++ix) {
            for (int iy = 0; iy < til.Height; ++iy) {
                var t = til[ix, iy].Tile;
                Debug.Log(
                    til[ix, iy].Brush.ToString() + "," +
                    til[ix, iy].TileSet.ToString() + "," +
                    til[ix, iy].TileIndex.ToString() + "," +
                    til[ix, iy].Tile.ToString());
                Debug.Log(
                    zon.Tiles[t].Layer1.ToString() + "," +
                    zon.Tiles[t].Offset1.ToString() + "," +
                    zon.Tiles[t].Layer2.ToString() + "," +
                    zon.Tiles[t].Offset2.ToString() + "," +
                    zon.Tiles[t].TileType.ToString() + "," +
                    zon.Tiles[t].TileType.ToString() + "," +
                    zon.Tiles[t].Rotation.ToString());
                Debug.Log(zon.Textures[zon.Tiles[t].Layer1 + zon.Tiles[t].Offset1]);
                Debug.Log(zon.Textures[zon.Tiles[t].Layer2 + zon.Tiles[t].Offset2]);
            }
        }
        */

        var td = new TerrainData();
        td.size = new Vector3(80, heightDelta/100, 80);
        td.heightmapResolution = 65;
        td.SetHeights(0, 0, heights);
        var ts = new SplatPrototype[1];
        ts[0] = new SplatPrototype();
        ts[0].texture = tex;
        ts[0].tileSize = new Vector2(160, 160);
        td.splatPrototypes = ts;

        var ter = Terrain.CreateTerrainGameObject(td).GetComponent<Terrain>();
        ter.name = blockName;

        ter.transform.localPosition = new Vector3(blockX, heightBase/100, blockY);
        return ter;
    }
示例#22
0
    /// <summary>
    /// Prototypes are used by Unity internally.
    /// This method must be called explicitly prior to generation, if any related variables changed.
    /// </summary>
    public void CreateProtoTypes()
    {
        m_splatPrototypes = new SplatPrototype[m_splatMapSettings.Length];

        for (var i = 0; i < m_splatMapSettings.Length; ++i) {
            m_splatPrototypes[i] = new SplatPrototype{
                texture = m_splatMapSettings[i].texture,
                tileSize = m_splatMapSettings[i].tileSize,
                tileOffset = m_splatMapSettings[i].tileOffset
            };
        }

        if (m_treeObjects == null) {
            m_treeObjects = new GameObject[0];
        }
        m_treeProtoTypes = new TreePrototype[m_treeObjects.Length];

        for (var i = 0; i < m_treeObjects.Length; ++i) {
            m_treeProtoTypes[i] = new TreePrototype();
            m_treeProtoTypes[i].prefab = m_treeObjects[i];
        }

        m_detailProtoTypes = new DetailPrototype[m_detailTextures.Length];

        for (var i = 0; i < m_detailTextures.Length; ++i) {
            m_detailProtoTypes[i] = new DetailPrototype();
            m_detailProtoTypes[i].prototypeTexture = m_detailTextures[i];
            m_detailProtoTypes[i].renderMode = m_detailMode;
            m_detailProtoTypes[i].healthyColor = m_grassHealthyColor;
            m_detailProtoTypes[i].dryColor = m_grassDryColor;
        }
    }
    static void GenerateTerrain(string output, Hashtable param, string path)
    {
        string terraindatapath = Path.Combine(path, output + ".asset");
        Debug.Log("Generate Terrain: " + terraindatapath);

        TerrainData terrainData = (TerrainData)AssetDatabase.LoadAssetAtPath(terraindatapath, typeof(TerrainData));
        if (!terrainData)
        {
            terrainData = new TerrainData();
            AssetDatabase.CreateAsset(terrainData, terraindatapath);
        }

        FileInfo fi = new FileInfo(Path.Combine(path, (string)param["heightFile"]));

        int hfSamples = (int)fi.Length / 2;
        int hfWidth;
        int hfHeight;
        if (!int.TryParse((string)param["heightFileWidth"], out hfWidth))
            hfWidth = 0;
        if (!int.TryParse((string)param["heightFileHeight"], out hfHeight) || hfHeight <= 0)
        {
            if (hfWidth > 0)
                hfHeight = hfSamples / hfWidth;
            else
                hfHeight = hfWidth = Mathf.CeilToInt(Mathf.Sqrt(hfSamples));
        }
        else
        {
            if (hfWidth <= 0)
                hfWidth = hfSamples / hfHeight;
        }
        int size;
        if (!int.TryParse((string)param["terrainTileSize"], out size))
            size = hfWidth;
        int tOffX;
        if (!int.TryParse((string)param["terrainTileX"], out tOffX))
            tOffX = 0;
        int tOffY;
        if (!int.TryParse((string)param["terrainTileY"], out tOffY))
            tOffY = 0;

        if (tOffX < 0 || tOffY < 0 || (size - 1) * tOffX > hfWidth || (size - 1) * tOffY > hfHeight)
        {
            Debug.LogError("terrainTile (" + tOffX + "," + tOffY + ") of size " + size + "x" + size + " "
                    + "is outside heightFile size " + hfWidth + "x" + hfHeight);
            return; 
        }

        
        tOffX = (size - 1) * tOffX;
        tOffY = (size - 1) * tOffY;

        int bpp = 2; 

        int x;
        int y;

        FileStream fs = fi.OpenRead();
        var b = new byte[size * size * bpp];
        fs.Seek((tOffX + tOffY * hfWidth) * bpp, SeekOrigin.Current);
        if (size == hfWidth)
            fs.Read(b, 0, size * size * bpp);
        else
        {
            for (y = 0; y < size; ++y)
            {
                fs.Read(b, y * size * bpp, size * bpp);
                if (y + 1 < size)
                    fs.Seek((hfWidth - size) * bpp, SeekOrigin.Current);
            }
        }
        fs.Close();

        var h = new float[size, size];
        int i = 0;

        if ((string)param["heightFormat"] == "r16bigendian")
        {
            for (x = 0; x < size; ++x)
            {
                for (y = 0; y < size; ++y)
                {
                    h[size - 1 - x, y] = (b[i++] * 256.0f + b[i++]) / 65535.0f;
                }
            }
        }
        else
        { 
            for (x = 0; x < size; ++x)
            {
                for (y = 0; y < size; ++y)
                {
                    h[size - 1 - x, y] = (b[i++] + b[i++] * 256.0f) / 65535.0f;
                }
            }
        }

        terrainData.heightmapResolution = size - 1;

        if (param.ContainsKey("layer0File") || param.ContainsKey("layer1File"))
        {
            var nlayers = 2;
            while (param["layer" + nlayers + "File"] != null) nlayers++;

            var alphas = new float[1, 1, 1];
            int asize = 0;
            int amWidth = 0;
            for (var lay = 0; lay < nlayers; ++lay)
            {
                if (param.ContainsKey("layer" + lay + "File"))
                {
                    fi = new FileInfo(Path.Combine(path, (string)param["layer" + lay + "File"]));
                    if (asize == 0)
                    {
                        var amSamples = fi.Length;
                        asize = (int)(size * amSamples / hfSamples);
                        amWidth = (int)(hfWidth * amSamples / hfSamples);
                        terrainData.alphamapResolution = (int)asize;
                        alphas = new float[asize, asize, nlayers];
                    }


                    fs = fi.OpenRead();
                    b = new byte[asize * asize];
                    fs.Seek(tOffX + tOffY * amWidth, SeekOrigin.Current);
                    if (asize == amWidth)
                    {
                        fs.Read(b, 0, asize * asize);
                    }
                    else
                    {
                        for (y = 0; y < asize; ++y)
                        {
                            fs.Read(b, y * asize, asize);
                            if (y + 1 < asize)
                                fs.Seek(amWidth - asize, SeekOrigin.Current);
                        }
                    }

                    fs.Close();
                    i = 0;
                    for (x = 0; x < asize; ++x)
                    {
                        for (y = 0; y < asize; ++y)
                        {
                            alphas[asize - 1 - x, y, lay] = b[i++] / 256.0f;
                        }
                    }
                }
            }
            if (param.ContainsKey("equalizeLayers"))
            {
                if (!param.ContainsKey("layer0File"))
                {
                    
                    for (x = 0; x < asize; ++x)
                    {
                        for (y = 0; y < asize; ++y)
                        {
                            float rem = 1.0f;
                            for (var lay = 1; lay < nlayers; ++lay)
                                rem -= alphas[asize - 1 - x, y, lay];
                            if (rem > 0.0f)
                                alphas[asize - 1 - x, y, 0] = rem;
                        }
                    }
                }
                
                for (x = 0; x < asize; ++x)
                {
                    for (y = 0; y < asize; ++y)
                    {
                        float tot = 0.0f;
                        for (var lay = 0; lay < nlayers; ++lay)
                            tot += alphas[asize - 1 - x, y, lay];
                        if (tot > 0.0f)
                        {
                            for (var lay = 0; lay < nlayers; ++lay)
                                alphas[asize - 1 - x, y, lay] = alphas[asize - 1 - x, y, lay] / tot;
                        }
                    }
                }
            }
            SplatPrototype[] oldsp = terrainData.splatPrototypes;
            SplatPrototype[] sp = new SplatPrototype[nlayers];
            for (var lay = 0; lay < nlayers; ++lay)
            {
                string splat = "layer" + lay;
                sp[lay] = new SplatPrototype();

                Vector2 ts;
                if (!float.TryParse((string)param[splat + "Width"], out ts.x))
                {
                    if (lay >= oldsp.Length) ts.x = 15;
                    else ts.x = oldsp[lay].tileSize.x;
                }
                if (!float.TryParse((string)param[splat + "Height"], out ts.y))
                {
                    if (lay >= oldsp.Length) ts.y = 15;
                    else ts.y = oldsp[lay].tileSize.y;
                }

                Vector2 to;
                if (!float.TryParse((string)param[splat + "OffsetX"], out to.x))
                {
                    if (lay >= oldsp.Length) to.x = 0;
                    else to.x = oldsp[lay].tileOffset.x;
                }
                if (!float.TryParse((string)param[splat + "OffsetY"], out to.y))
                {
                    if (lay >= oldsp.Length) to.y = 0;
                    else to.y = oldsp[lay].tileOffset.y;
                }
                Texture2D tex = null;
                var texfile = (string)param[splat + "Texture"];
                if (texfile != null)
                {
                    if (texfile.StartsWith("/") || texfile.StartsWith("\\"))
                    {
                        tex = (Texture2D)AssetDatabase.LoadMainAssetAtPath("Assets" + (string)param[splat + "Texture"]);
                    }
                    else
                    {
                        tex = (Texture2D)AssetDatabase.LoadMainAssetAtPath(Path.Combine(path, (string)param[splat + "Texture"]));
                    }
                }
                else if (lay < oldsp.Length)
                {
                    tex = oldsp[lay].texture;
                }
                if (tex == null)
                {
                    tex = new Texture2D(1, 1);
                    int g = (lay - 1) / nlayers;
                    tex.SetPixel(0, 0,
                        lay == 0 ? Color.red :
                        lay == 1 ? Color.green :
                        lay == 2 ? Color.blue : new Color(g, g, g));
                    tex.Apply();
                }
                sp[lay].texture = tex;
                sp[lay].tileSize = ts;
                sp[lay].tileOffset = to;
            }
            terrainData.splatPrototypes = sp;
            terrainData.SetAlphamaps(0, 0, alphas);
        }

        Vector3 sz;
        sz.x = float.Parse((string)param["terrainWidth"]);
        sz.y = float.Parse((string)param["terrainHeight"]);
        sz.z = float.Parse((string)param["terrainLength"]);

        terrainData.size = sz;
        terrainData.SetHeights(0, 0, h);
        terrainData.RefreshPrototypes();
    }
示例#24
0
 public void addSplatPrototype(Texture2D tex, int index)
 {
     SplatPrototype[] newSplatPrototypes = new SplatPrototype[index + 1];
     for (int i = 0; i <= index; i++) {
         newSplatPrototypes[i] = new SplatPrototype();
         if (i == index) {
             newSplatPrototypes[i].texture = tex;
             newSplatPrototypes[i].tileSize = new Vector2(15, 15);
         } else {
             newSplatPrototypes[i].texture = splatPrototypes[i].texture;
             newSplatPrototypes[i].tileSize = splatPrototypes[i].tileSize;
         }
     }
     splatPrototypes = newSplatPrototypes;
     if (index + 1 > 2) {
         addBlendPoints();
     }
 }
示例#25
0
 public void deleteSplatPrototype(Texture2D tex, int index)
 {
     int c = 0;
     c = splatPrototypes.Length;
     SplatPrototype[] newSplatPrototypes = new SplatPrototype[c - 1];
     int n = 0;
     for (int i = 0; i < c; i++) {
         if (i != index) {
             newSplatPrototypes[n] = new SplatPrototype();
             newSplatPrototypes[n].texture = splatPrototypes[i].texture;
             newSplatPrototypes[n].tileSize = splatPrototypes[i].tileSize;
             n++;
         }
     }
     splatPrototypes = newSplatPrototypes;
     if (c - 1 > 1) {
         deleteBlendPoints();
     }
 }
示例#26
0
 public void deleteAllSplatPrototypes()
 {
     SplatPrototype[] newSplatPrototypes = new SplatPrototype[0];
     splatPrototypes = newSplatPrototypes;
 }
示例#27
0
    private void startTerrainUpdate(Terrain t)
    {
        int mLayer = 1 << meshLayer;

        raycastDistance = maxBounds.y - t.transform.position.y + 10;

        Vector3 vScale = t.terrainData.heightmapScale;
        Vector3 beginPoint = t.transform.position;
        beginPoint.y += raycastDistance;
        float[,] heights = new float[t.terrainData.heightmapWidth, t.terrainData.heightmapHeight];
        float tdist = raycastDistance;

        RaycastHit hit;
        Vector3 curPoint;

        for (int i = 0; i < t.terrainData.heightmapWidth; i++)
        {
            for (int j = 0; j < t.terrainData.heightmapHeight; j++)
            {
                curPoint = beginPoint + new Vector3(i * vScale.x, 0, j * vScale.z);
                if (Physics.Raycast(curPoint, -Vector3.up, out hit, raycastDistance, mLayer)) heights[j, i] = (tdist - hit.distance) / vScale.y;
                else heights[j, i] = 0;
            }
        }

        t.terrainData.SetHeights(0, 0, heights);

        if (textureType == MeshToTerrainTextureType.bakeMainTextures)
        {
            Texture2D texture = new Texture2D(textureWidth, textureHeight);
            vScale.x = vScale.x * t.terrainData.heightmapWidth / textureWidth;
            vScale.z = vScale.z * t.terrainData.heightmapHeight / textureHeight;

            Color[] colors = new Color[textureWidth * textureHeight];
            Renderer lastRenderer = null;
            Mesh m = null;
            Vector2[] uv = null;
            int []triangles = null;
            Vector3[] verticles = null;

            for (int i = 0; i < textureWidth; i++)
            {
                for (int j = 0; j < textureHeight; j++)
                {
                    curPoint = beginPoint + new Vector3(i * vScale.x, 0, j * vScale.z);
                    int cPos = j * textureWidth + i;

                    if (Physics.Raycast(curPoint, -Vector3.up, out hit, raycastDistance, mLayer))
                    {
                        Renderer renderer = hit.collider.GetComponent<Renderer>();
                        if (renderer != null && renderer.sharedMaterial != null)
                        {
                            if (lastRenderer != renderer)
                            {
                                lastRenderer = renderer;
                                m = renderer.GetComponent<MeshFilter>().sharedMesh;
                                triangles = m.triangles;
                                verticles = m.vertices;
                                uv = m.uv;
                            }
                            Material mat = renderer.sharedMaterial;
                            if (mat.mainTexture != null)
                            {
                                Texture2D mainTexture = (Texture2D)mat.mainTexture;
                                Vector3 localPoint = renderer.transform.InverseTransformPoint(hit.point);
                                int triangle = hit.triangleIndex * 3;
                                Vector3 v1 = verticles[triangles[triangle]];
                                Vector3 v2 = verticles[triangles[triangle + 1]];
                                Vector3 v3 = verticles[triangles[triangle + 2]];
                                Vector3 f1 = v1 - localPoint;
                                Vector3 f2 = v2 - localPoint;
                                Vector3 f3 = v3 - localPoint;
                                float a = Vector3.Cross(v1 - v2, v1 - v3).magnitude;
                                float a1 = Vector3.Cross(f2, f3).magnitude / a;
                                float a2 = Vector3.Cross(f3, f1).magnitude / a;
                                float a3 = Vector3.Cross(f1, f2).magnitude / a;
                                Vector3 textureCoord = uv[triangles[triangle]] * a1 + uv[triangles[triangle + 1]] * a2 + uv[triangles[triangle + 2]] * a3;

                                colors[cPos] = mainTexture.GetPixelBilinear(textureCoord.x, textureCoord.y);
                            }
                            else
                            {
                                colors[cPos] = mat.color;
                            }
                        }
                        else
                        {
                            colors[cPos] = textureEmptyColor;
                        }
                    }
                    else colors[cPos] = textureEmptyColor;
                }
            }

            texture.SetPixels(colors);
            texture.Apply();

            string textureFilename = Path.Combine("Assets", t.name + ".png");
            int index = 1;

            while(File.Exists(textureFilename) && !overwriteExists)
            {
                textureFilename = Path.Combine("Assets", t.name + " " + index.ToString() + ".png");
                index++;
            }

            File.WriteAllBytes(textureFilename, texture.EncodeToPNG());
            AssetDatabase.Refresh();
            texture = (Texture2D)AssetDatabase.LoadAssetAtPath(textureFilename, typeof(Texture2D));

            List<SplatPrototype> sps = new List<SplatPrototype>();
            SplatPrototype sp = new SplatPrototype();
            sp.tileSize = new Vector2(t.terrainData.heightmapWidth * t.terrainData.heightmapScale.x, t.terrainData.heightmapHeight * t.terrainData.heightmapScale.z);
            sp.texture = texture;
            sps.Add(sp);

            t.terrainData.splatPrototypes = sps.ToArray();
        }
        t.Flush();
    }
	private void fillPrototypes(){
		
		for (int i= 0; i < textures.Length ; i++) {
			
			SplatPrototype splatPrototype = new SplatPrototype();
			splatPrototype.texture = textures[i];
			splatPrototype.tileSize = new Vector2 (2, 2);
			
			texturePrototypes.Add( splatPrototype);
			
		}

		for (int i= 0; i < trees.Length ; i++) {
			
			TreePrototype treePrototype = new TreePrototype();
			treePrototype.prefab = trees[i];
			
			treePrototypes.Add( treePrototype);
			
		}

		for (int i=0; i< details.Length; i++) {
		
			DetailPrototype detailPrototype = new DetailPrototype();
			detailPrototype.prototypeTexture = details[i];
			detailPrototype.renderMode = detailMode;
			detailPrototype.healthyColor = m_grassHealthyColor;
			detailPrototype.dryColor = m_grassDryColor;

			detailPrototypes.Add (detailPrototype);
		}

	}
示例#29
0
        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);
        }
示例#30
0
 private void GetSplatsFromGlobalSettingsHolder()
 {
     SplatPrototype[] splatPrototypes=new SplatPrototype[globalSettingsHolder.numLayers];
     for(int i=0; i<globalSettingsHolder.numLayers; i++) {
         //Debug.Log(""+globalSettingsHolder.splats[i]);
         splatPrototypes[i]=new SplatPrototype();
         splatPrototypes[i].tileSize=new Vector2(3,3);
         splatPrototypes[i].texture=globalSettingsHolder.splats[i];
     }
     Terrain terrainComp = (Terrain)GetComponent(typeof(Terrain));
     terrainComp.terrainData.splatPrototypes=splatPrototypes;
 }