Light in Unity 2

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

--

Intro to Blinn Phong Lighting model

In the previous article, We have been through Lambert Lighting Model and how it doesn't consider the viewer’s orientation.

Lambert Lighting

Lambert lighting model considers the brightness of an object is affected by its orientation from the light source.

Phong Lighting Model

Developed by Phong

Phong Model takes into consideration the viewer angle and how the light gets reflected from the surface.

According to this model, the reflection of a pixel should be maximized if the angle between (the light source and the surface normal) == the angle between (the viewer and the surface normal)

Blinn Phong Light Model

Developed by Jim Blinn

Blinn added some corrections to the Phong model and he added the halfway vector.

Halfway vector is the vector in the middle between the light source vector and the viewer vector (Calculated by adding those two vectors).

Light Source is at the middle of the screen, therefor reflection is maximized at the center and damps towards the edge

When the angle between the surface normal and the halfway vector is zero, reflection intensity is maximized, otherwise, the reflection damps relative to the variation of this angle.

Blinn Phong is the default lighting model for different editors and environments, with its specular reflection.

Some Comparison Images:

Lambert, Blinn and Phong, Source: CGRU
Source: THAT GAME DESIGN BLOG

Recreating the Blinn Phong model in CGPROGRAM


#pragma surface surf BasicBlinnPhong
// Blinn Phong Lighting considers the viewer direction and specular reflections
half4 LightingBasicBlinnPhong(SurfaceOutput s, half3 lightDirection, half3 viewerDirection,half LossFactor){

// halfway vector is the vector in-between the light source vector and the viewer direction vector
half3 halfwayVector;
halfwayVector=normalize(lightDirection+viewerDirection);
// Light diffusion depends on the angle between the light direction vector and the surface normal vector
// if the angle is more than 90 degrees let it be zero
half DiffusionFactor = max(0,dot(s.Normal,lightDirection));

// The Angle between the halfway vector and the surface normal determine the amount of specular reflection
// if the angle is more than 90 degrees let it be zero
float NormalDotHalfway=dot(s.Normal,halfwayVector); // Specular(Relative) Reflection factor
// you can play with power but Unity default is 48

float SpecularFactor= pow(NormalDotHalfway,48);

//Lets mix all of these inputs to get the lightingColor on the pixel

half4 returnedColor;
returnedColor.rgb=((s.Albedo*_LightColor0.rgb*DiffusionFactor)+(_LightColor0.rgb*SpecularFactor))*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

--

--