- Home /
Can i change the Texture of terrain
Hi Guys i need to change the texture of the terrain by a butt0n press and i want to call seprate textures i know i can do it with predefined terrain tool kit but i want to do it in Editor mode with scripting i am building a script for various functionalities of terrain and i want to add and change texture of terrain as well Nw My Queastion is that how can i do it i dnt knw any thing about maps and how to obtain them and i ave searched but i am not geting the idea of Spalt maps and alpha maps like what r thy and how to use them
P.s i am real confussed plz help
Answer by Ian-McCleary · Aug 23, 2013 at 06:05 AM
I think this would work..there are some things you would need to do to make it ok for your situation but this should be a good outline or start. If im wrong someone please correct me.
var TerrainOne : Terrain;
var TerrainTwo :Terrain;
function Start () {
Terrain = TerrainOne;
}
function Update () {
if(!Input.GetButtonDown(TerrainTwo)){
}
if(Input.GetButtonDown(TerrainOne)){
}
}
Answer by sooncat · Aug 23, 2013 at 09:44 AM
SplatPrototype[] splats = yourTerrainGameObjct.GetComponent<Terrain>().terrainData.splatPrototypes;
for(int i=0;i<splats.Length;i++)
{
splats[i].texture = (Texture2D)yourTexture;
}
thanks guys fr ur replies i ave tested this code but it giving error that
Assets/_Scripts/TerrainTextureChanger.cs(28,26): error CS1061: Type UnityEngine.SplatPrototype[]' does not contain a definition for
count' and no extension method count' of type
UnityEngine.SplatPrototype[]' could be found (are you missing a using directive or an assembly reference?)
and what can i use in the place of your texture coz i am using the texture variable but it gives me error
like
Assets/_Scripts/TerrainTextureChanger.cs(30,15): error CS0266: Cannot implicitly convert type UnityEngine.Texture' to
UnityEngine.Texture2D'. An explicit conversion exists (are you missing a cast?)
the first error: splats.count ==> splats.Length the second error : splats[i].texture = (Texture2D)yourtexture;
I reEdit the answer. "(Texture2D)" is not necessary if you define "yourTexture" as "Texture2D".
Answer by AR_Rizvi · Aug 27, 2013 at 09:48 AM
so far i achive this my texture swap is working but i want the whole terrain texture to change at once and the scond function is not working i dnt knw thier are no errors in code but its not working texture swap is only working on the painted areas can i repaint the texture of the terrian throug scripting
here is my code
using UnityEngine;
using System.Collections;
public class TerrainTextureChanger : MonoBehaviour {
public Terrain terrain;
public Texture2D MyTexture;
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
//switch all painted in texture 1 to texture 2
UpdateTerrainTexture(terrain.terrainData, 1, 2);
}
if (Input.GetKeyUp(KeyCode.Space))
{
//switch all painted in texture 2 to texture 1
UpdateTerrainTexture(terrain.terrainData, 2, 1);
}
if (Input.GetKeyDown(KeyCode.LeftControl))
{
textureChange();
}
}
void textureChange(){
Debug.Log ("i am hapnig");
SplatPrototype[] splats = terrain.GetComponent<Terrain>().terrainData.splatPrototypes;
for(int i=0;i<splats.Length;i++)
{
splats[i].texture = MyTexture;
// (Texture2D)
}
}
static void UpdateTerrainTexture(TerrainData terrainData, int textureNumberFrom, int textureNumberTo)
{
//get current paint mask
float[, ,] alphas = terrainData.GetAlphamaps(0, 0, terrainData.alphamapWidth, terrainData.alphamapHeight);
// make sure every grid on the terrain is modified
Debug.Log (alphas);
for (int i = 0; i < terrainData.alphamapWidth; i++)
{
for (int j = 0; j < terrainData.alphamapHeight; j++)
{
//for each point of mask do:
//paint all from old texture to new texture (saving already painted in new texture)
alphas[i, j, textureNumberTo] = Mathf.Max(alphas[i, j, textureNumberFrom], alphas[i, j, textureNumberTo]);
//set old texture mask to zero
alphas[i, j, textureNumberFrom] = 0f;
}
}
// apply the new alpha
terrainData.SetAlphamaps(0, 0, alphas);
}
}
it is for swap the texture on painted areas to another texture in the terrain texture slot
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Modifying Terrain Splat Texture at Runtime 2 Answers
How can I perform some action based on the terrain texture currently under my player? 4 Answers
How to automatically apply different textures on terrain based on height? 5 Answers
How can I automatically place grass and other details on my terrain to correspond with the splatmap? 5 Answers