Over the last few days, I have been learning about the CG shader language for Unity 3D. I started out following a tutorial to make a simple lambert shader, as pictured here. Shaders use trigonometry to calculate the lighting on each pixel or vertex or the mesh. To find the brightness of a specific area, you can take the dot product of the surface’s normal direction, and the direction of the light you are calculating for. The color of the area is then found by multiplying the light’s color
( _LightColor0.xyz )
by the calculated shadow. In CG, this looks like
1 |
float3 brightness = atten * _LightColor0.xyz * max(0.0, dot(normalDirection, lightDirection)); |
The attenuation value ( atten )
is used for light falloff, so as a point light gets further away from the surface, it provides fewer lumens. The brightness
variable is then added to UNITY_LIGHTMODEL_AMBIENT.xyz
to account for the ambient light in the scene, and then returned as part of the vertex shader function’s output.
This new knowledge will be useful for the new game I will be working on over the last semester of my senior year in high school for the Computer Game Programming course at Fullerton College.