Wednesday, 8 August 2018

HLSL tutorial : Create capsule light


line light
When calculating point light and direction light,  we take the light as a point. and we use the point to calculate how much the object is lighten by the light. For capsule light we treat the light as a Line, every point on the line can lid the scene.

Algorithm
Here's the strategy to calculate the intensity of the light on one pixel.
Given line AB the presentation of the light, 
To calculate light intensity on pixel P, we need to find the closest point O on line AB and treat O as the only point which lid P. 
This means after we find out point O, the lid model is simplify as a point light. (right image)


Math
To implement this Algorithm we need to map position of P (float3) to O (float3) on AB.
Look at it closely, we can find out the point O is always on line AB. Given the fact that we know vector AB,  as O moves along along AB, the scale M = AO / AB is changing from 0 to 1. When O == A, M = 0; when O ==B, M = 1. As long as we can get the scale calculate, the problem is solved.

Now the equation should be something look like this:
O = A + M (normalize (AB)) ;
to get the point O on AB, we start from point A and displacement along AB vector with M . 

we know:
M = dot (AP, normalized(AB)) * length(AB);
0 <= M <= length(A, B)
Now , Think about these 2 extreme cases :
 
In case 1 : scale M < 0; A is the closest point
case 2 : scale M > length(A, B); B is the closest point. 

we can do some math to map M's value to [0,length]

M = saturate(dot (AP, normalized(AB)) )* length(AB);

code


No comments:

Post a Comment