Light in Unity 1

Ahmed Schrute
Shader Coding in Unity from a to z
3 min readNov 4, 2019

--

Introduction to Lighting in Unity and Lambert Lighting

Lighting Example

Lighting in Computer Graphics is all about calculating the Pixel color and its intensity to simulate lighting in real life.

In real life the brightness of an object is dependent on :

  1. Its orientation relative to the light source (e.g.: the Sun)
  2. Its orientation relative to the eye (viewer)

In Computer graphics, we try to simulate lighting by simulating those two factors providing the same effect we get in real life.

Understanding light is important for shader coding and Augmented Reality Development.

Lambert Lighting

Developed by Lambert

Lambert Lighting is the conceptual model for simulating Light

And the model in Lehman Terms can be described as:

A surface is lit according to the angle between the surface normal vector and the light source vector

Lambert Lighting
Cosine of the angle between the surface normal and the light source defines the intensity of the light
Source: GameDev.net
Source: scratchpixel.com

When the angle between the Surface Normal and the light source is 0 then the Cosine of the angle will return 1, which will turn the brightness all the way.

The light intensity at a surface (pixel or vertex in Unity) in the Lambert model is determined by the dot product of the Surface Normal Vector* Light Source Vector.

Lambert lighting can be called in a CGPROGRAM

by adding it to the pragma compiler directive

#pragma surface surf Lambert

If you want to recreate Lambert Lighting in a CGPROGRAM

#pragma surface surf LambertLighting
half4 LightingLambertLighting(SurfaceOutput s, half3 lightDirection,half LossFactor)
{
half4 returnedColor;
half NormalDotLightDirection; NormalDotLightDirection=dot(s.Normal,lightDirection); returnedColor.rgb=s.Albedo*_LightColor0*(NormalDotLightDirection*LossFactor); returnedColor.a=s.Alpha; return returnedColor;

}

Hope you find this tutorial useful, I am currently looking for a Unity Dev job (Remote or in the Bay Area). If you know someone who can might be looking for a Unity Developer, my email is ah1053@stanford.edu

--

--