Game Development Reference
In-Depth Information
main texture. Then, we passed
_Shininess
to
o.Specular
and
c.a
to
o.Gloss
to
control the amount of gloss on our model.
Next, we created our custom lighting function, which is
inline half4 Light-
ingRampSpecular (SurfaceOutput s, half3 lightDir, half3
viewDir, half atten)
. This function passes four parameters, surface output, light
direction, view direction, and light attenuation, which we will use to calculate the output
for our shader. Then, we changed
#pragma surface surf
from
Lambert
to
RampSpecular
, which means that we changed our lighting calculated from the built-in
Lambert
model to
RampSpecular
in our custom lighting function,
Light-
ingRampSpecular
.
Note
Why is the name of this function not
RampSpecular
? First, we call this function by us-
ing
#pragma surface surf RampSpecular
, but to have this function working
properly, we need to add
Lighting
as a prefix to the name of our custom lighting func-
tion so that Unity will know that this function is a custom lighting function. This is how
surface shaders are set up in Unity. You can find out more detail on this from
ht-
In the
LightingRampSpecular()
function, we first got the ambient color value by
getting
s.Albedo
, which is the
o.Albedo
parameter from the
surf()
function, and
then multiplied
s.Albedo
by
_AmbientColor.rgb
, where
_AmbientColor
is the
color information from the
Properties
section at the beginning of our code.
Note
The
fixed
,
half
, and
float
parameters in Cg/HLSL can contain one, two, three, or
four values of floating numbers 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
; or
float
,
float2
,
float3
,
float4,
respectively. 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 that will contain only three values
(1.0, 0.5, 0.3)
from
fixed4 color
, you can name it like the following:
fixed3 newColor =
color.rgb;
. However, if you want the
newColor
value equal to
(0.5, 1.0,
0.3)
, you can name it
fixed3 newColor = color.grb;
.