2D Dynamic Lights and Shadows | Unity

2D Dynamic Lights and Shadows | Unity

2D Lights and Shadows PRO is available in assets Store !

650X290

Demos Scenes

WASD for move the Light.
Simple Point of Light

 

Concave Shapes

 

Multiple Lights

 

 

Multiple Lights in WebGL (PRO package only and UNITY 5)

webglimg

 

[NEW] Cover Exposion (bomb shockwave ) . Only in 2DLights PRO Package

 

Visibility Depth Enhanced. Only in 2DLights PRO Package

 

[NEW] Intellider ready!  Only in 2DLights PRO Package

[NEW] Visible Minimal: get the occlusion behavior with simple setup. Only in 2DLights PRO Package

 

Remember Phases? – Lights + Physics

 

// — ************************************************************** — //

 

 

 

// — ************************************************************** — //

 

 

 

 

How i get it?

Performance of 2D dynamic lighting and cast shadow in unity environment:

I will explain my approach to achieve a light and to perceive the environment objects that create shade and to deduct the generated mesh.
I have divided the pseudocode in steps to make their interpretation easier.

 

 

1) I get all PolygonCollider2D on the scene with a loop.

2) Convert each vector in the global coordinate Position

3) We cast a ray from the position of the Light GameObject with direction towards each polygonCollider2D position vector with a magnitude of 20 (radio light);

Screen Shot 2014-10-16 at 20.25.36

4)  If the ray makes contact (hit) I store the vector position as a point of contact to generate the mesh shade otherwise be the maximum point reached by the ray

5) Convert the point of local coordinate position as to build the mesh, I need that.

6) Now I need to calculate the angle of each position vector. I need to sort the points and triangulate the mesh, then use the PseudoAngle method. It is very efficient focusing on calculus cost against Atan2 function.

7) Sort each  vertex position in ascendent way, but i do it individually for each PolygonCollider2D.  This way i can get the endpoints of each object. knowing that we have sorted the vertices by angle, we know that the element 0 (first) is the lowest angle, therefore, is the left endpoint. To the right endpoint will be the n-1 element.

Screen Shot 2014-10-16 at 20.52.57

Pay attention in Rectangle most left, how block the green Ray of cube. And also too detect if right EndPoint of rectangle is inside of angle of sight of light.

Another thing is Cast a ray from endpoint, same direction like  Light -> Endpoint, but must be start with a little offset for avoid the collision itself.

offset

 

8) Put all vertices, including Endpoints in one common array.

9) Generate vertices for Radial Light and putting all together -> see this approach

10) Sort all vertices in ascending order.

11) Then, sort only the vertices we have the same angle, ascending way in left Endpoint; and sort in descending for right EndPoint.

sortedVertices

 

12) Only left, triangulate the vertices to render the mesh and recalculate normals.

 

Final Screenshots:

Editor
finalEditor

 

GamePort
finalGamePort

 

Performance:

*) Pseudo Angle method used for sorted vertices.
*) Only cast one ray per vertex. (2 rays if is EndPoint)
*) Optimal performance on mobile device : video
*) Concave Collider full compatibility.

 

Self Suggestions:
*) Build a penumbra region.

Source:

https://www.assetstore.unity3d.com/en/#!/content/24083 

 

References:

http://forum.unity3d.com/threads/2d-dynamic-shadows.165283/

http://archive.gamedev.net/archive/reference/programming/features/2dsoftshadow/

http://www.redblobgames.com/articles/visibility/