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 ?