Game Development Reference
In-Depth Information
/ /
U n a t t e n u a t e d
omni
l i g h t
c o l o r
uniform
f l o a t 3
o m n i C o l o r ;
/ /
View
p o s i t i o n ,
i n MODEL space
uniform
f l o a t 3
v i e w P o s ;
/ /
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
and
s p e c u l a r
map
s a m p l e r s .
Note we assume
t h a t
d i f f u s e
/ /
and
spec
maps
use
t h e
same UV c o o r d s
sampler2D
d i f f u s e M a p ;
sampler2D
s p e c u l a r M a p ;
/ /
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 ) ;
/ /
Normalize
i n t e r p o l a t e d
v e r t e x
normal
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 ) ;
/ /
Compute
v e c t o r
t o
l i g h t
f l o a t 3
L
=
omniPos
i n p u t . modelPos ;
/ /
Normalize
i t ,
and
s a v e
o f f
d i s t a n c e
t o
use
l a t e r
/ /
f o r
a t t e n u a t i o n
f l o a t
d i s t
=
l e n g t h ( L ) ;
L
/ =
d i s t ;
/ /
Compute
view
v e c t o r
and
h a l f w a y
v e c t o r
f l o a t 3
V
=
n o r m a l i z e ( v i e w P o s
i n p u t . modelPos ) ;
f l o a t 3 H =
n o r m a l i z e ( V
+
L ) ;
/ /
Compute
a t t e n u a t e d
l i g h t
c o l o r .
f l o a t 3
l i g h t C o l o r
=
o m n i C o l o r
max ( 1
d i s t invOmniRad , 0 ) ;
/ /
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 ) ;
/ /
Compute
e f f e c t i v e
l i g h t
c o l o r s
l i g h t C o l o r d i f f F a c t o r
f l o a t 3
d i f f C o l o r
=
+
a m b i e n t L i g h t 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 ;
f l o a t 3
s p e c C o l 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.14
Pixel shader for per-pixel lighting of a single omni plus ambient
Search WWH ::




Custom Search