Light's Math Part 1

In this tutorial we will learn the basic Light Math.

Project Setup

Add a sphere, and apply an Unlit shader to sphere's material.
The sphere is not recieving any light, because it is not defined in the Unlit shader.

Bootstrap Themes

Open the Unlit shader in visual studio.
Delete the lines of code (highlighted with yellow cells in right image).

Set the color to white. (highlighted with orange cell in left image)

Bootstrap Themes
Bootstrap Themes

Before implementing the light functionality in Unlit shader, first we have to understand the light behaviour.

For example we have a light at point P.
It is not directional/point light, it's just a light.

At point A, the light intensity is high.
At point C, the light intensity is low.
At point D, the light intensity is zero.

What parameters defines light intensity high at point A, low at point C and zero light intensity at point D?

Bootstrap Themes

Dot(.) product between the normal vector and light vector defines the intensity of light, at different point.

At point A, Dot(.) product between normal vector and light vector is 1, so light intensity is high at point A.
At point C, it is 0.05, results in low intensity of light at that point.
At point D, Dot(.) product is -0.3, so light intensity is zero. (negative number taken as black color)

Bootstrap Themes

Dot(Vector A, Vector B)

Dot(.) product is projection of one vector onto other vector.

Dot(.) product between vector O & i is 0.95,
between O & j is 0.5 and
between O & k is 0.05.

Dot(.) product between vector O & m is -0.5.



Next thing is what is a normal vector and light vector?

Bootstrap Themes

[Left Image Below]
Sphere is made up of vertices.
All the black dots on sphere are representing vertices, and the blue arrows represents the normal vectors, pointing in outward direction.

[Center Image Below]
Light vector are represented by yellow arrows.

[Right Image Below]
Light vector at point A is,
Light vector = LightPosition - PointPosition
Light vector = P - A

Light vector at point C is,
Light vector = P - C

We will calculate the light vector at each vertex.
It is just like we calculate the directional vector in c#.

Bootstrap Themes
Bootstrap Themes
Bootstrap Themes

Next is to define the normal vector and light vector in Unlit shader.

Code highlighted with orange cell that's how we get the normal vector in shader.

Bootstrap Themes

Light vector = LightPosition - Vertex World Position

We need light position and vertex world position to calculate the light vector.

We will use a simple 3DPoint, as a light. (Code highlighted with green cells)

Code highlighted with red cell, that is how we get the Vertex World Position.

At each vertex(point), we subtract Vertex World Position from light position to get the light vector.
(Code highlighted with orange cell)

Bootstrap Themes

Take the Dot(.) product of light vector with normal vector and multiply it's positive values with the color.
Max(DotProduct, 0.0) function returns values greater than 0.0 of Dot(.) product.

Bootstrap Themes
Bootstrap Themes

You can change the light position from inspector.