Monday 14 May 2007

Automatic light script

I was making a light for my stores and wanted the lights to fade on and off as the sun set and rose. The example scripts switch on and off, which looked ugly.

This script fades ON as the sun sets untill the lights are fully on as the sun dips below the horizon.

// Automatic lighting
// Light prim script
// by Asp Grelling
// 1MAY07

// The lights fade on and off according to the suns position above the horizon

vector lightColour = < 1.0, 1.0, 1.0> ;
float lightIntensity = 1.0 ;
float lightRadius = 8.0 ;
float lightFalloff = 0.75 ;


default
{
state_entry()
{
// Function entry
llSetTimerEvent(10); // Check every X seconds
}

timer()
{
integer lightOn ;
vector sun = llGetSunDirection();
float intensity = (0.25 - sun.z)*4;
if ( intensity < 0.0 ) {
lightON = FALSE ;
intensity = 0.0;
else {
lightON = TRUE ;
if ( intensity > 1.0 ) intensity = 1.0 ;
}
// Turn light on or off depending on intensity
llSetPrimitiveParams([PRIM_POINT_LIGHT, lightOn, lightColour , intensity, lightRadius, lightFalloff ]);
}
}


The call to llGetSunDirection() retrieves a vector pointing to the suns position in the sky, with x, y and z pointing to the sun. Z gives the angle above the horizon and a negative number indicates the sun is below the horizon. Another script I saw turned the lights on and off based on whether the sun was above the horizon (z > 0) or below the horizon (z <> float intensity = (0.25 - sun.z)*4;" changes the rate at which the light come on. A smaller number makes the light come on more slowly.

My actual script has a bit more with the fade happening a smothly and the light temperature changing so the light has a warmth as it comes on.

The lights will be on sale in my store and I will post some pictures when Dutch Business Park opens and my shops are accessible.

Note: Moved from http://designasp.blogspot.com

3 comments:

Ted Goff said...

Thanks, Asp. I had a few minor problems with your script that you might want to correct:

integer lightOn ;
vector sun = llGetSunDirection();
float intensity = (0.25 - sun.z)*4;
if ( intensity < 0.0 ) {
lightON = FALSE ;

// lightOn , not lightON

intensity = 0.0;
else {

// } else {, not else {

lightON = TRUE ;

// lightOn, not lightON

Casper said...

Script is full of errors and wont work

Please fix

Unknown said...

9 years later, this topic is still relevant and was useful to me in learning about sun position in association to object manipulation, Thank you! It does work prefect with the corrected errors as stated above by ted. Thanks Ted and thank you Asp for the OP.