Public Group

# Getting distance between light source and four vertices

This topic is 2056 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

if I have a plane which has four vertices, as shown in the following figure. I would like based on the distance between the light source and positions of those four vertices, to scale( shrink or extend )the plane accordingly to that distance. What maths involved in that?

In plain words, I have a "directional" light source in the scene, and I have a plan with 4 vertices, I would like to shrink or extend ( scale) the four vertices positions, based on how far or how close I'm to the directional light. so that I can simulate a shadow...

##### Share on other sites
Distance from where? Center of the 4 vertices? Nearest vertex? Nearest point of the nearest edge on the plane? Assuming you decide on a point, the way you calculate a distance in 3d is remarkably similar to calculating the distance in 2d. in 2d the distance is sqrt((x2-x1)^2+(y2-y1)^2) In 3d it's sqrt((x2-x1)^2+(y2-y1)^2+(z2-z1)^2) Where the x, y, and z coordinates correspond to the points of the light and wherever on the plane you picked. Order doesn't matter. Now, depending on how you plan on actually utilizaing the distance calculated you might not *need* to calculate the sqrt of the 3 squares. But without knowing more about what you're doing I couldn't advise either way.

##### Share on other sites
Are you sure you want to use a directional light for this? Because that has no "position", it's "infinite". In that case I think you need some sort of reference to measure from. Another option could be to use a spot light.

##### Share on other sites

Hi nobdoynews,

I have a directional light. That plane is a decal projected on the floor as a shadow. I have the 4 vertices of the decal. I would like based on the directional light on the scene, to modify, scale the 4 vertices according to that light, to simulate the shadow behaviour. The directional light in unity has a position. I do not what maths to do, so that I get a scale value, and scale the vertices of the plane.

@Cozzie,

Yea, I have a directional light in the scene, I only have its position in the scene.

Edited by AhmedCoeia

##### Share on other sites

A directional light's position does not matter. It just uses the other settings (like angle, color, etc.), and places it infinitely far away.

If you put a directional light pointing straight down underneath a bridge, everything will be lit from above (including all the stuff that's above the bridge).

In short, distance to a directional light does not make sense.

Looking at the documentation, you might want to use either Point or Spot.

http://docs.unity3d.com/Documentation/Components/class-Light.html

As nobodynews said, getting a distance between two points (in this case, P1 and L) is done by summing up the difference in all dimensions, the taking the square root of that:

dist = sqrt( (P1.x - L.x)^2 + (P1.y - L.y)^2 + (P1.z - L.z)^2 )

---

EDIT: Corrected who I attributed distance to.

Edited by Lactose!

##### Share on other sites

but is there any other way to do that effect using directional light ?

I just want to shrink or extend the plane based on light or based on how far or how close that plane ?

##### Share on other sites

With a directional light, no.

Everything is infinitely far away from a directional light. Thus there can be no concept of how far/close you are to the light.

Directional light does not have a position, even though it might look like it does in Unity.

---

EDIT: Meaning you'll have to use a different kind of light (which has a position), or you need to figure out a different way to do it (i.e. not based on your light).

Edited by Lactose!

##### Share on other sites

I made the following but it seems its working but doesn't make any sense...

I dot product the position of the light and the each vertex position, I got a scalar value, then I divided it by 1000, then I lerp between from 1 to 0, and t = final_scale

When I get farway, the plane gets shrinked...

##### Share on other sites

I don't remember any platform games changing the shadow size.
They would mostly fade the shadow away when the character is off the ground.

What you could do is perform raycast right below the player and, based on the distance that he is to the ground, fade the shadow away.

Const MAX_PLAYER_DISTANCE:Float = 50.0

...

shadowAlpha = 1.0 - ( Min( playerDistance, MAX_PLAYER_DISTANCE ) / MAX_PLAYER_DISTANCE )

##### Share on other sites

I'm interested to know how did you limit the shadowalpha by the above equation ?

playerdistance can be from 1 to 80

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 21
• 21
• 11
• 9