Change color on the Terrain Lightmap

I’m working on an installation where I need to change the sun / world color.
I’m using the new Terrain engine, and it seems to only way to do what I want is to change the lightmap.

I made a simple test that works fine. The code below tints the lightmap instantly.

// Assign the Terrains Lightmap here //
var lightMap :  Texture2D;
private var lightMapCol : Color[];
// tint the Terrain LightMap to this color //
var newColor : Color;

function Start()
{
	lightMapCol = lightMap.GetPixels (0, 0, lightMap.width, lightMap.height, 0);
	for( var i = 0; i < lightMapCol.Length; ++i ) {
	lightMapCol[i] = Color.Lerp( lightMapCol[i], newColor, 1 ); //
	}
	lightMap.SetPixels( lightMapCol );
	lightMap.Apply( false );
}

But I need the color change to happen smoothly over a given time, so I tried to do the following.

// Assign the Terrains Lightmap here //
var lightMap :  Texture2D;
private var lightMapCol : Color[];
// tint the Terrain LightMap to this color //
var newColor : Color;
// fade time //
var fadeTime : float = 20.0;

function Start()
{
	Fade();
}

function Fade(){
	var oldTime = Time.time;
	var divider : float = 1.0 / fadeTime;
	lightMapCol = lightMap.GetPixels (0, 0, lightMap.width, lightMap.height, 0);
	
	while(true){
		var t : float = divider * (Time.time - oldTime);
		if(t > 1.0)
		break;
		
		for( var i = 0; i < lightMapCol.Length; ++i ) {
		lightMapCol[i] = Color.Lerp( lightMapCol[i], newColor, t ); //
		}
		
		lightMap.SetPixels( lightMapCol);
		lightMap.Apply( false );
		
		yield;
	}
	Debug.Log("FADE UP == TRUE");
}

But this doesn’t work. All it does it make my computer extremely low for a few seconds, I guess while the While Loop runs. When I stop the editor the new color gets applied.

So something is obviously wrong, but where ?

I cant help you sorry, but i will monitor this thread with great interest! :wink:

Thanks for the idea of tinting the lightmap!

First of all, good idea…

So I tried the script and it works perfect. I can see the changes happening in the editor, and the effect is very smooth. If something is wrong, my guess it is not in the script.

After you run the script, you have modified the original image file. Maybe you have to do a recalculation of your lightmap to see any effects after the first time.

Other than that, I wouldn’t know why it doesn’t work for you.

Ohh, your right. It is working. But only on the ground texture and not on the grass that I have painted all over the place. That’s why I didn’t see the change in the first place.

I also discovered another bug. The fade seems to be done way before my timer variable ( t in the script above ) reaches 1.