Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Script: Basic Lighting

Peter Linden
Registered User
Join date: 18 Nov 2002
Posts: 177
07-11-2003 08:58
CODE
//==========================================================================
// Luminance Control Test Script
// Original Script By Unknown
// Modifications made by Zanlew Wu 30-Apr-2003
//

//============================================
// Declare global fields.
//
integer gfLtOn; // Used to remember whether the light is on or off.
float gfLtIncr; // Used to hold the lighting increment to adjust with.

//============================================
// gmInitFields()
//
gmInitFields()
{
//
// Set the default state of the lit object.
//
gfLtOn = TRUE;

//
// Set the increment to increase and decrease the lighting level.
//
gfLtIncr = 0.1;
}
//
// End of gmInitFields
//============================================

//============================================
// gmSetColor
//
gmSetColor()
{
//------------------------
// Declare local variables
//
float l; // used for loop control and setting color
vector v; // used to hold a color value

//
// First, check to see if the object is already "lit" or not.
//
if (gfLtOn)
{
//
// Yes, it is lit, so darken it. Do this within a loop.
//
// The loop below basically runs a color-setting loop where the
// the loop variable itself is used as the color increment. From
// what I can gather by seeing it work, color is held in a "vector"
// construct where the three values of the vector represent
// RED, GREEN, and BLUE values. The values apparently can range from
// 0.0 to 1.0 (they are floating point values--as vectors always
// are).
//
// Since this is the area where we darken the light source, I use
// the loop to decrement it's value from 1.0 to 0.0 in steps. The
// size of the step is defined in the field gfLtIncr.
//
for (l = 1.0; l >= 0.0; l = l - gfLtIncr)
{
//
// Now that the value of l has been set, we need to assign
// that color to all the fields of the color vector v. To
// do that, we use direct assignments.
//
// In order to keep the color cycling of the object very
// non-colorful (since it is supposed to be a light source,
// after all), we will adjust all three fields of the vector
// to the same value.
//
v.x = l; // Assign the color value l to the x field of v
v.y = l; // Same, but to y
v.z = l; // "

//
// Now that the color vector is set, assign that vector as the
// color of the object. Instruct the SetColor command to set
// the color to all sides of the object.
//
llSetColor(v, ALL_SIDES);

//
// Now we stay in the loop if we are not at the last color yet,
// or exit the loop if we are at the last color change.
//
}
}
else
{
//
// No, it is not lit, so light it up.
//
// As mentioned before, I use a loop to cycle through the color
// change of the light source. Since this time we are lighting the
// object up, we cycle the loop from 0.0 to 1.0 in gfLtIncr
// increments. Same methdology applies here.
//
for (l = 0.0; l <= 1.0; l = l + gfLtIncr)
{
v.x = l; //
v.y = l; // Same as before
v.z = l; //

llSetColor(v, ALL_SIDES); // Same here too.
}
}
}
//
// End of gmSetColor
//============================================


//============================================
// Default State
//
// This is the state that is automatically bootstrapped when the object
// is first created/rez'd, or the world or environment resets.
//
default
{
//
// state_entry() is the first method executed when the state it resides
// in is run. So State A, B, and C all can have state_entry methods,
// and if they do, they are run when their respective states are called
// and or executed.
//
state_entry()
{
//
// First, initialize the global fields.
//
gmInitFields();

//
// Now set the object to it's default color state.
//
gmSetColor();
}

touch_start(integer total_number)
{
//
// The light has been touched. Now we can switch the state of the
// light to what it is supposed to be.
//
// Since we are using this interger gfLtOn as a boolean, the
// operation of reversing the state of the light from on to off to
// on again can be done with a very simple application of the
// boolean operator ! (not). While I normally like to code the
// long way (the gmSetColor method could have been written more
// efficiently), this is one place I like to take the shortcut.
//
gfLtOn = !gfLtOn;

//
// Now set the color of the light source.
//
gmSetColor();
}
}

edit-just added the code blocks.
Ingie Bach
Registered User
Join date: 17 Dec 2002
Posts: 254
08-04-2003 10:13
Errrr... That's basic?? Hee hee hee ;P
_____________________
I love modeling in Blender, if you want to check out a fantastic package for modeling and game developement (great for Architectural Walkthroughs), go to my site: http://www.ingiebee.com
Huns Valen
Don't PM me here.
Join date: 3 May 2003
Posts: 2,749
08-20-2003 01:03
If you strip out the comments there's like... 5 lines of code in there... :p
Musicteacher Rampal
Registered User
Join date: 20 Feb 2004
Posts: 824
04-18-2004 08:18
Could you please post it as 5 lines of code without all the comments?
Nada Epoch
The Librarian
Join date: 4 Nov 2002
Posts: 1,423
Re: Script: Basic Lighting
04-18-2004 18:14
Comment-less version.
CODE
//==========================================================================
// Luminance Control Test Script
// Original Script By Unknown
// Modifications made by Zanlew Wu 30-Apr-2003
//

//============================================

integer gfLtOn;
float gfLtIncr;

//============================================
// gmInitFields()
//
gmInitFields()
{
gfLtOn = TRUE;
gfLtIncr = 0.1;
}
//
// End of gmInitFields
//============================================

//============================================
// gmSetColor
//
gmSetColor()
{
float l;
vector v;

if (gfLtOn)
{
for (l = 1.0; l >= 0.0; l = l - gfLtIncr)
{
v.x = l;
v.y = l;
v.z = l;

llSetColor(v, ALL_SIDES);

}
}
else
{
for (l = 0.0; l <= 1.0; l = l + gfLtIncr)
{
v.x = l;
v.y = l;
v.z = l;

llSetColor(v, ALL_SIDES);
}
}
}
//
// End of gmSetColor
//============================================


//============================================
default
{
state_entry()
{
gmInitFields();
gmSetColor();
}

touch_start(integer total_number)
{
gfLtOn = !gfLtOn;
gmSetColor();
}
}
_____________________
i've got nothing. ;)
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
04-19-2004 01:00
this should work and is lighter on comments & code.

CODE

integer gfLtOn;
float gfLtIncr;

gmInitFields()
{
gfLtOn = TRUE;
gfLtIncr = 0.1;
}

gmSetColor()
{
float l;
for (l = gfLtOn; l >= (!gfLtOn); l -=(gfLtIncr*(gfLtOn*2-1)))
llSetColor(<l,l,l>, ALL_SIDES);
}

default
{
state_entry()
{
gmInitFields();
gmSetColor();
}

touch_start(integer total_number)
{
gfLtOn = !gfLtOn;
gmSetColor();
}
}
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
Musicteacher Rampal
Registered User
Join date: 20 Feb 2004
Posts: 824
05-05-2004 11:49
thank you!