Game Development Reference
In-Depth Information
f l o a t 4
matSpec
= tex2D ( s p e c u l a r M a p ,
i n p u t . uv ) ;
/ /
Normalize
i n t e r p o l a t e d
v e c t o r s
f l o a t 3 N =
n o r m a l i z e ( i n p u t . n o r m a l ) ;
f l o a t 3
L
=
n o r m a l i z e ( i n p u t . L ) ;
f l o a t 3 H =
n o r m a l i z e ( i n p u t .H ) ;
/ /
Compute
d i f f u s e
and
s p e c u l a r
f a c t o r s
f l o a t
d i f f F a c t o r
= max ( dot ( N,
L ) , 0 ) ;
f l o a t
s p e c F a c t o r
= pow ( max ( dot ( N,H ) , 0 ) ,
s p e c E x p o n e n t ) ;
/ /
Clamp
t h e
l i g h t
c o l o r ,
( Note
t h a t
t h i s
max
i s
a p p l i e d
/ / component wize )
f l o a t 3
l i g h t C o l o r
= max ( i n p u t . l i g h t C o l o r , 0 ) ;
/ /
Compute
e f f e c t i v e
l i g h t
c o l o r s
f l o a t 3
d i f f C o l o r
=
l i g h t C o l o r d i f f F a c t o r
+
a m b i e n t L i g h t C o l o r ;
f l o a t 3
s p e c C o l o r
=
l i g h t C o l o r s p e c F a c t o r ;
/ /
Sum up
c o l o r s .
Note
t h a t
HLSL
has
a
v e r y
f l e x i b l e
s w i z z l i n g
s y s t e m
/ /
which
a l l o w s
us
t o
a c c e s s
a
p o r t i o n
o f
a
v e c t o r
a s
i f
were
a
/ /
”member”
o f
t h e
v e c t o r
f l o a t 4
r e s u l t
=
m a t D i f f ;
/ /
RGB and
o p a c i t y
from
d i f f u s e
map
r e s u l t . r g b
=
d i f f C o l o r ;
/ /
modulate
by
d i f f u s e +ambient
l i g h t i n g
r e s u l t . r g b
+=
matSpec . r g b s p e c C o l o r ;
/ /
add
spec ,
i g n o r i n g
map
a l p h a
/ /
Modulate
i t
by
t h e
c o n s t a n t
and
o u t p u t
r e t u r n
r e s u l t c o n s t a n t C o l o r ;
}
Listing 10.16
Alternate pixel shader for per-pixel lighting of a single omni plus ambient
Finally, we present one last variation on this example. Notice that in
the previous pixel shader, Listing 10.16, the code does not assume that the
lighting is taking place in any particular coordinate space. We have been
performing the lighting calculations in model space, but it is also common
to do it in camera space. The advantage is that we do not need to resend
shader constants for lighting data for each object that is rendered, as we do
when those values are specified in modeling space (which will vary for each
object). Listing 10.17 is a vertex shader that illustrates this technique.
/ /
Mesh
i n p u t s .
{
s t r u c t
I n p u t
f l o a t 4
p o s
:
POSITION ;
/ /
p o s i t i o n
i n
model
space
f l o a t 3
n o r m a l
: NORMAL ;
/ /
v e r t e x
normal
i n
model
space
f l o a t 2
uv
:
TEXCOORD0 ;
/ /
t e x t u r e
c o o r d s
f o r
d i f f u s e ,
spec
maps
} ;
/ /
V e r t e x
s h a d e r
o u t p u t
s t r u c t
O u t p u t
{
f l o a t 4
c l i p P o s
:
POSITION ;
/ /
c l i p space
p o s i t i o n
f l o a t 2
uv
:
TEXCOORD0 ;
/ /
t e x t u r e
c o o r d s
f o r
d i f f u s e ,
spec
maps
f l o a t 3
n o r m a l
:
TEXCOORD1 ;
/ /
v e r t e x
normal
i n
camera
space
f l o a t 3
L
:
TEXCOORD2 ;
/ /
v e c t o r
t o
l i g h t
i n
camera
space
f l o a t 3 H
:
TEXCOORD3 ;
/ /
h a l f w a y
v e c t o r
i n
camera
space
f l o a t 3
l i g h t C o l o r
:
TEXCOORD4 ;
/ /
l i g h t
c o l o r
+
a t t e n u a t i o n
f a c t o r
} ;
Search WWH ::




Custom Search