Calculate the size of the shadow

I created a Unity3d project and I used some spotlights behind the objects to get their shadows. I am trying to get the real size (using my scale) of the shadow reflected on the floor. Is there a way to do this?

+3


source to share


1 answer


I suppose your question will be mostly from Exchange Math Stack , but here's an approach that I hope will lead you in the right direction.

Following are the following hypotheses:

  • You know the height of the object when scale = 1
  • your object is not too big (or you will need to include half its width in the math)
  • your object is at its base (on a person: underfoot).
  • your object is placed on the floor (and therefore not in the air: otherwise it is a little harder to calculate, but the idea remains the same)

Here's a quick outline of the situation: enter image description here



Now you can calculate your shadow size using something like this:

Vector3 topPoint = YOUR_OBJECT.transform.position + YOUR_OBJECT.transform.lossyScale.y * YOUR_OBJECT_HEIGHT;
Vector3 lightFlatPoint = new Vector3(YOUR_LIGHT.transform.position.x, topPoint.y, YOUR_LIGHT.transform.position.z);

float lightDeltaY = YOUR_LIGHT.transform.position.y - topPoint.y;
float lightFlatToTopPointDistance = Vector3.Distance(lightFlatPoint, topPoint);

float shadowSize = ((YOUR_OBJECT.transform.lossyScale.y * YOUR_OBJECT_HEIGHT) / lightDeltaY) * lightFlatToTopPointDistance;

      

Hope it helps,

+8


source







All Articles