Multiple splatmaps and how to change them?

Hi all,

I have this problem, which I have searched for a solution to for quite some time now.
The scenario is I have to depict two different seasons in my environment. Like rain and dry season, so I have to have a terrain which has green grassy textures, while opposite I need the dry season to be yellowish in the textures, but all on the same terrain. Since it is an option the user should be able to toggle between.

I was thinking it was easy as this:

var textureSplat1 : Texture2D;
var textureSplat2 : Texture2D;

function OnGUI ()
{
	if(GUI.Button(Rect(0,0,100,100), "Rain"))
	{
		TerrainData.splatPrototypes.texture = textureSplat1;
	}
	if(GUI.Button(Rect(0,100,100,100), "Dry"))
	{
		TerrainData.splatPrototypes.texture = textureSplat2;
	}
}

Having two variables, which contains different splatmaps. This should then be a toggle option. Like on the code example, by pressing a button or whatever boolean if statement.

But with my limited knowledge about coding it won’t ofcause compile.
Error:
Assets/_Scripts/SeasonChange.js(8,29): BCE0020: An instance of type ‘UnityEngine.TerrainData’ is required to access non static member ‘splatPrototypes’.
Assets/_Scripts/SeasonChange.js(12,29): BCE0020: An instance of type ‘UnityEngine.TerrainData’ is required to access non static member ‘splatPrototypes’.

I hope someone out there have some input, which could help me progress towards a solution:)

Best Regards,
Specal

The documentation suggests splatPrototypes is of type array. Try…

TerrainData.splatPrototypes[0].texture = textureSplat2;

Documentation here:

While rooch is correct, the error you’re getting is because the variable is not static, so you will need to obtain the terrain data of a specific terrain in your scene. For example:

Terrain.activeTerrain.terrainData.splatPrototypes[0].texture = textureSplat2;

Thanks for the repons rooch84 and tomvds.

Okay, I’ve tried to read up on the documentation. But it don’t seem to provide the answers I at least understand.
So by altering the code to this:

static var textureSplat1 : Texture2D;
static var textureSplat2 : Texture2D;

function OnGUI ()
{
	if(GUI.Button(Rect(0,0,100,100), "1st"))
	{
		Terrain.activeTerrain.terrainData.splatPrototypes[0].texture = textureSplat1;
	}
	if(GUI.Button(Rect(0,100,100,100), "2nd"))
	{
		Terrain.activeTerrain.terrainData.splatPrototypes[1].texture = textureSplat2;
	}
}

I get no errors, but in the same go no results. I can’t seem to figure out what is placed within the array and how to alter this so I can get the wanted effect of switch back and forward between two different texture maps (splatmaps).

:slight_smile: I hope someone have solved this or know if it is even possible.

Thanks in advance.

I have never changed splat maps from script, so i have no idea wether changing splatPrototypes will do what you want, but if you want to replace one splat by another, I would expect both splats to be assigned to the same splatPrototype. Try changing splatPrototypes[1] to splatPrototypes[0].

I’ve tried to change the values but with no result.

Following code is what I have so far:

static var textureSplat1 : Texture2D;
static var textureSplat2 : Texture2D;
var tex1 : Texture2D;
var tex2 : Texture2D;

function Awake ()
{
	textureSplat1 = tex1;
	textureSplat2 = tex2;
}


function OnGUI ()
{
	if(GUI.Button(Rect(0,0,100,100), "1st"))
	{
		Terrain.activeTerrain.terrainData.splatPrototypes[1].texture = textureSplat2;
	}
	if(GUI.Button(Rect(0,100,100,100), "2nd"))
	{
		Terrain.activeTerrain.terrainData.splatPrototypes[0].texture = textureSplat1;
	}
}

Have anyone an idea on how to solve it? Any ideas are welcome.

Try to call Terrain.activeTerrain.Flush() after you have done the changes.