Jump to content

Strange Shadows


The Snow Irbix
 Share

Recommended Posts

Hi Irbix!

   Setting "direction vectors" sucks.  I am still looking for better ways to set directions.  Let's take a look at your line 13...

var light = new BABYLON.DirectionalLight("dir01", new BABYLON.Vector3(-0.5, -2, -0.5), scene);

I could be wrong about ANY of this, but it is my experience that when dealing with direction vectors (-0.5, -2, -0.5), the values are always between -1 and +1.  They don't HAVE TO be, but when they are, the vector is said to be "normalized".  Any direction can be set... using only values between -1 and +1 inclusive.

On directional and spot lights (lights that use directions)... we have a helper function...

light.setDirectionToTarget(anyVec3Position)

So, you can turn off the animation, make a temporary_box, put it out into the scene where you want to aim a light.  Then add this somewhere...

light.setDirectionToTarget(temporary_box.position);

alert(light.direction);

// or alert(light.direction.x.toFixed(3) + " : " + light.direction.y.toFixed(3) + " : " + light.direction.z.toFixed(3));

Now RUN.  :)

The numbers in the alert... are the numbers to use when you want to set your light.direction.  Then remove or invisible the temporary aiming box, because you now have the numbers.

light.setDirectionToTarget also works inside animation loops.  In this version of the demo, I keep re-aiming your directional lights at the flying torus.  It's probably not what you wanted to do, but it is still somewhat fun.  It makes your two directional lights... act like point lights (and point lights normally cannot do shadows).  :)

http://playground.babylonjs.com/#1WAGFM#1

Notice the two setDirectionToTarget in the animation loop.

Using vector3's for position... quite easy.  Using vector3's for rotation, a bit harder.  Using vector3's for directions... blech!  Light.setDirectionToTarget makes it a bit easier... maybe.  Hope this helps.  Smarter people than I are nearby.  :)  Good luck.

Link to comment
Share on other sites

Hi Irbix, hi Wingnut,

 

@Wingnut

 

 

I could be wrong about ANY of this, but it is my experience that when dealing with direction vectors (-0.5, -2, -0.5), the values are always between -1 and +1.  They don't HAVE TO be, but when they are, the vector is said to be "normalized".  Any direction can be set... using only values between -1 and +1 inclusive.

In that case you are wrong. 1st - a normalized vector is far more than a vector with values between -1 and 1. Check the normalize() function of Vector3 (or read here - http://www.fundza.com/vectors/normalize/ ) to understand a bit more what it means. But you are totally right about the fact that a normalized vector gives direction.

 

However, in directional lights, the direction of a directional light is a point in space and not a normalized vector. You can see that from the playground's Shadow example, the vector -1, -2, -1 (which is not normalized) is used to set the light's direction. I have to admit that it is confusing, as I would also expect a normalized vector to be the direction variable.

 

@Irbix, I assume you are talking about those "cuts" in the shadows. The simplest answer would be - set the light further away (in the correct direction, or else the shadow will not be emitted) from the shadow source and shadowed surface, it will be solved. I have asked the same question a few months ago, this is the thread:

http://www.html5gamedevs.com/topic/9964-shadows-directional-light-and-all-that-jazz/?hl=%2Bshadow+%2Bdirection+%2Blight

Link to comment
Share on other sites

Thanks raananw!  I guess I need to study these things a bit closer.  :)  Thanks for the info and leads!  I thought Irbix set that -2 in there, but i guess the author of the shadows demo... set it.

 

But still, I don't think the 2nd parameter of a directionalLight constructor... is a point in space.  Its a direction, I think.  That's why you can do this...

 

13.  var light = new BABYLON.DirectionalLight("dir01", new BABYLON.Vector3(-1, -2, -1).normalize(), scene);

  and

23.  var light2 = new BABYLON.DirectionalLight("dir02", new BABYLON.Vector3(-1, -2, -1).normalize(), scene);

 

...and the direction of the lights do not change.   It just LOOKS like a position.

Link to comment
Share on other sites

Looking at the code, I see you are right. The demos should be updated :-) the function setDirectionToTarget is actually calculating the direction between the target entered and the position given. Maybe the constructor should modified to actually use this function and accept target and not direction...

Link to comment
Share on other sites

That had to be Deltakosh or some other person who has a "3D Brain".  These guys can THINK a direction.  They're 3D fry-daddies.  I heard Davrous mention it... said Deltakosh had a 3D brain.  :)

 

Yep, I did some work on the lights tutorial, and realized that I didn't understand directions, and I had to go on a research mission.  I didn't learn anything.  :)  But directions need to have SOME "magnitude" or else they essentially error-out or something like that.  Sooo... a light.direction = new BABYLON.Vector3(0, 0, 0) has no magnitude on any axis.  If you use it like this, your computer will blow up.  No magnitude to the direction. 

 

Deltakosh used a -1, -2, -1.  It's a ratio, and the direction has substantial magnitude.  It has "length".  A setting of -10, -20, -10 has 10 times the magnitude... but still points the light in the same direction.  Weird.  :)

 

I fought and fought with trying to master directions, and finally stole the subtract code from somewhere on the internet.  light.direction = BABYLON.Vector3.Normalize(target.position.subtract(light.position));

 

I don't think the normalize really needs to be in there, but it worked.  Then I begged DK to install it in the framework for me/us, and he did.  The birth of light.setDirectionToTarget().  An early version of it was used on this nightmare demo.

 

Yep, directions.  It takes a MAN to type-in directions perfectly... off the top of his head.  I'm not man enough.  :)

Link to comment
Share on other sites

I was wrong because of this sentence: "The light is emitted from everywhere." (from https://github.com/BabylonJS/Babylon.js/wiki/06-Lights)

But I have finally understood how it work :)

Thank you wingnut and raananw for your explanations !

And the BABYLON.Ray object seems interesting luaacro, I will take a look at it ;)

Merry Christmas everybody !

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...