Light in Unity 2

Light in Unity 2

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.

No alt text provided for this image


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

Phong Lighting Model

No alt text provided for this image

Developed by Phong

No alt text provided for this image

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

According to the Phong 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

No alt text provided for this image

Developed by Jim Blinn

No alt text provided for this image


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


No alt text provided for this image


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

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

No alt text provided for this image


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:

(Left to right) Lambert Lighting, Blinn Lighting, and Phong Lighting, Image Source: CGRU

No alt text provided for this image


Source: THAT GAME DESIGN BLOG

No alt text provided for this image

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;
         
         }


To view or add a comment, sign in

More articles by Ahmed Schrute

  • Genetic Evolution (Behavioral Traits) Simulation using Unity3D

    This is a simulation of Genetic Evolution with a focus on Behavioral traits passed between generation, In this…

  • Vertex Fragment Shader Structure

    This article is a continuum of shader coding articles using Shader lab in Unity, I highly recommend you visit my two…

  • Rendering Queues in Unity

    A lot of times, we need to render certain objects(Player, Gem …etc) to the front even there is an object overlaying our…

  • Normal Mapping

    If you have read the Lighting in Unity 1 and Lighting in Unity2, you have seen how important normal vectors are for…

    2 Comments
  • Light in Unity 1

    Introduction to Lighting in Unity and Lambert Lighting Lighting in Computer Graphics is all about calculating the Pixel…

  • Shader Variables Data Types in Unity

    Why we use different variable types when writing shaders? Shader code is for every pixel(or vertex), therefor efficient…

  • Understanding Mesh Anatomy in Unity

    In order to shade a model we need to understand its geometric structure Let’s take a cube as an example of a mesh model…

  • Rendering Pipeline In Unity

    Introduction to Rendering Pipeline in Unity Rendering Rendering is the process of drawing a scene on the computer…

  • Unity CPU Profiling

    Enhancing unity CPU performance I usually have a routine before I start inspecting the CPU usage tap which is opening…

Explore content categories