Jump to content

Dynamic (but simple) Shadow


Rolig Loon
 Share

You are about to reply to a thread that has been inactive for 4434 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

This script will let you make a shadow that follows the movement of the sun and adjusts its length to the sun's elevation. It could be handy if you want to create a realistic shadow around something like a fence post. The length of the shadow is limited by the maximum dimension allowed for prims (no megaprims, since you cannot change their dimensions), so it is only realistic for reasonably short objects (no flagpoles).  Still, there are plenty of short objects that deserve a dynamic shadow.

To use, rez a box prim, flatten it along its Z axis and make its Y dimension approximately the width of the target object that it will be a shadow for.  Place the name of your target object in the gName variable in this script and drop the script into your prim.  Touch the prim to reset the script.  Your prim will position itself at the base of the target object and adjust its rotation and length according to the position of the sun.  Thereafter, it will track the sun, disappearing at dusk and reappearing at dawn.

 

// Shadow -- Rolig Loon -- March 2011

// This script controls a shadow that tracks the sun's position (azimuth) and adjusts its length to the sun's elevation

// Rez a prim that is flattened on Z and is as wide (Y axis) as the target object you want to shadow.
// Set the gNAME parameter (below) to the name of the target object being shadowed.
// Set your shadow prim within 10m of the target object and touch it to reset this script.
// The shadow prim will position itself at the base of the target object and will adjust its own length (X) and texture.


string gNAME = "Pole";  // The name of your target (shadowed) object goes here.
float gHeight;  // The target object's height (will be measured by this script).

GoSun()
{
    vector Sun = llGetSunDirection();
    if (Sun.z > 0.0)  //It's daytime.......
    {
        llSetAlpha(0.5,ALL_SIDES);
        // Track the sun's azimuth
        llSetLocalRot(llGetLocalRot() * llRotBetween( <-1.0,0.0,0.0> *llGetLocalRot(), <Sun.x,Sun.y,0.0> ));
        //Calculate the length of the shadow
        float SunAngle = llRot2Angle(llRotBetween(<-1.0,0.0,0.0>*llGetLocalRot(), <Sun.x,0.0,Sun.z> ));
        vector MySize = llGetScale();
        //And set its length
        llSetScale(<2*gHeight/llSin(SunAngle),MySize.y,MySize.z>);
    }
    else  //It's night .....
    {
        llSetAlpha(0.0,ALL_SIDES);
    }
}
    
default
{
    state_entry()
    {
        llSetTexture("5c50e129-0b07-3834-0748-f1df74234a65",ALL_SIDES);  //All transparent
        llSetPrimitiveParams([PRIM_TEXTURE,0,"21b98d3c-617c-c1b7-8165-d1bb67ec9dfb",<0.5,0.8,0.0>,ZERO_VECTOR,0.0]); //Shadow on face 0
        llSetPrimitiveParams([PRIM_TYPE,PRIM_TYPE_BOX,PRIM_HOLE_DEFAULT,<0.125,0.625,0.0>,0.0,ZERO_VECTOR,<1.0,1.0,0.0>,ZERO_VECTOR]);
        llSensor(gNAME,"",PASSIVE,10.0,PI);
        llSetTimerEvent(120.0); //Check sun every 2 minutes
    }
    
    touch_start(integer num)
    {
        if(llDetectedKey(0) == llGetOwner())
        {
            llResetScript();
        }
    }
    
    timer()
    {
        GoSun();
    }
    
    sensor(integer num)
    {
        //How tall is the target object?
        list Box = llGetBoundingBox(llDetectedKey(0));
        vector Low = llList2Vector(Box,0);
        vector High = llList2Vector(Box,1);
        gHeight = High.z - Low.z;
        //Move shadow to the base of the target object
        vector Pos = llDetectedPos(0);
        llSetPos(<Pos.x,Pos.y,Pos.z-0.5*gHeight>);
        GoSun();
    }
    
    no_sensor()
    {
        llOwnerSay("I can't find the object named " + gNAME + ". Check to be sure that its name is entered properly in this script and that it is within 10m.");
    }
}

 

 

Link to comment
Share on other sites

  • 2 weeks later...

Any script posted in the library is presumed to be full perm.  I would be very upset if I heard that someone were trying to sell the script itself, since I could have done that too but chose not to.  You're quite welcome to use it in a product or anything else short of that, however.  Glad that you find it useful.  :smileywink:

Link to comment
Share on other sites

 


Rolig Loon wrote:

Any script posted in the library is presumed to be full perm.  I would be very upset if I heard that someone were trying to sell the script itself, since I could have done that too but chose not to.  You're quite welcome to use it in a product or anything else short of that, however.  Glad that you find it useful.  :smileywink:

Well, what I would do is create a shadow for each of my products and include that shadow in the sale with instructions on its use.  I would never sell the script itself.  I think it would be a nice thing to include and people can either use it or not as they decide!  Thanks very much!  :smileywink:

 

Link to comment
Share on other sites

  • 2 months later...
  • 8 months later...

I assume that you got my private message.  If not, try doing your cut & paste from here with a simple program like Notepad. If you use a word processor like MS Word, it will introduce hidden formatting commands into the script, thinking that you are handling a document that will need to be printed.  Those formatting commands will mess up in the LSO or Mono compiler and generate errors.

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 4434 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...