Game Development Reference
In-Depth Information
The pixel shader (Listing 10.22) is quite compact, since most of the
prep work has been done in the vertex shader. We unpack the normal and
normalize the interpolated L and H vectors. Then we perform the Blinn-
Phong lighting equation, just as in the other examples.
/ /
I n t e r p o l a t e d
i n p u t s
from
t h e
v e r t e x
s h a d e r .
s t r u c t
I n p u t
{
f l o a t 2
uv
:
TEXCOORD0 ;
/ /
t e x t u r e
c o o r d s
f o r
a l l
maps
f l o a t 3
L
:
TEXCOORD1 ;
/ /
v e c t o r
t o
l i g h t ,
i n
TANGENT space
f l o a t 3 H
:
TEXCOORD2 ;
/ /
h a l f w a y
v e c t o r ,
i n
TANGENT space
f l o a t 3
l i g h t C o l o r
:
TEXCOORD3 ;
/ /
l i g h t
c o l o r
+ and
a t t e n u a t i o n
f a c t o r
} ;
/ /
A
g l o b a l
c o n s t a n t
RGB and
o p a c i t y
uniform
f l o a t 4
c o n s t a n t C o l o r ;
/ /
C o n s t a n t
ambient
l i g h t
c o l o r
uniform
f l o a t 3
a m b i e n t L i g h t C o l o r ;
/ /
M a t e r i a l
g l o s s i n e s s
( phong
exponent )
uniform
f l o a t
s p e c E x p o n e n t ;
/ /
D i f f u s e ,
spec ,
and
normal map
s a m p l e r s
sampler2D
d i f f u s e M a p ;
sampler2D
s p e c u l a r M a p ;
sampler2D
normalMap ;
/ /
P i x e l
s h a d e r
body
f l o a t 4
main ( I n p u t
i n p u t ) :
COLOR
{
/ /
F e t c h
t h e
t e x e l s
t o
g e t
t h e
m a t e r i a l
c o l o r s
f l o a t 4
m a t D i f f
= tex2D ( d i f f u s e M a p ,
i n p u t . uv ) ;
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 ) ;
/ /
Decode
t h e
t a n g e n t space
normal
f l o a t 3 N = tex2D ( normalMap ,
i n p u t . uv ) . r g b
2
1 ;
/ /
Normalize
i n t e r p o l a t e d
l i g h t i n g
v e c t o r s
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
and
a t t e n u a t i o n
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 .
f l o a t 4
r e s u l t
=
m a t D i f f ;
/ /
RGB &
o p a c i t y
from
t h e
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 e
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.22
Pixel shader for omni lighting of normal mapped object, with lighting done in tangent space
Search WWH ::




Custom Search