Game Development Reference
In-Depth Information
Why is the name of this funcion not
RampSpecular
? First, we call this
funcion by using
#pragma surface surf
RampSpecular
, but to have
this funcion working properly, we need to add
Lighting
in front of the
name of our custom lighing funcion, so that Unity will know that this funcion
is a custom lighing funcion. This is the way that the surface shaders are set up
in Unity. You can find out more details from the following website:
http://unity3d.com/support/documentation/Components/
SL-SurfaceShaderLighting.html
In this funcion, we irst get the ambient color value by geing
s.Albedo
, which is
the parameter from the
surf()
funcion
o.Albedo
, and then muliply the
s.Albedo
by
_AmbientColor.rgb
, where
_AmbientColor
is the color informaion from the
Properties
secion at the beginning of our code.
The
fixed
,
half
, and
float
parameters in
Cg/HLSL
can contain one,
two, three, or four values of loaing number such as 1.0, (1.0, 1.0), (1.0, 1.0,
1.0), or (1.0, 1.0, 1.0, 1.0) by calling it
fixed
,
fixed2
,
fixed3
,
fixed4
,
half
,
half2
,
half3
,
half4
,
float
,
float2
,
float3
,
float4
. We
can also access the value in these parameters by using (x, y, z, w) or (r, g, b,
a). For example, if you have
fixed4 color = (1.0, 0.5, 0.3,
0.8);
and you want to create another parameter, which will contain only
three values
(1.0, 0.5, 0.3)
from the
fixed4 color
, you can do
it like this:
fixed3 newColor = color.rgb;
. However, if we want
the
newColor
value equal to
(0.5, 1.0, 0.3)
, you can do it like this:
fixed3 newColor = color.grb;
.
Then, we calculate the difuse color by geing the dot product of the surface normal of the
object (
s.Normal
) that we pass out from the
surf()
funcion (
o.Normal
), and the light
direcion (
fixed NdotL = dot (s.Normal, lightDir);
). Then, we use that value to
muliply with the object difuse texture (
s.Albedo
) and light color (
_LightColor0.rgb
),
which is similar to the
Lambert
model.
Next, we calculate the specular color by irst geing the normalize vector of light direcion
and view direcion (
fixed3 h = normalize (lightDir + viewDir);
). In
float
nh = saturate(dot (s.Normal, h));
, we calculate the dot product of the surface
normal and normalize vector, and make sure that the return number isn't greater than 1
or lower than 0 by using
saturate()
. Then, we use
nh
to calculate the specular power
by powering it with the
_Glossiness
properies (
float specPower = pow (nh, _
Glossiness);)
, and we get the specular color from muliplying the light color, specular
power, and the specular color properies
(_LightColor0.rgb * specPower * _
SpecularColor.rgb;
), which is similar to the Blinn-Phong model.