1 | float4x4 WorldViewProj;
|
---|
2 | float4x4 WorldView;
|
---|
3 | float4x4 WorldViewIT;
|
---|
4 | float3 mCameraPos; // model-space Camera position
|
---|
5 | float3 mLightPos; // model-space Light position
|
---|
6 | float focalDist;
|
---|
7 | float focalRange;
|
---|
8 | //------------------------------------------------------------------------------------ |
---|
9 | // macro definition for filtered samplers
|
---|
10 | //------------------------------------------------------------------------------------ |
---|
11 | #define SAMPLER_LINEAR(g_samplerMap, g_txMap); \
|
---|
12 | sampler2D g_samplerMap = sampler_state { \
|
---|
13 | Texture = <g_txMap>; \
|
---|
14 | MinFilter = Linear; \
|
---|
15 | MagFilter = Linear; \
|
---|
16 | MipFilter = Linear; \
|
---|
17 | AddressU = WRAP; \
|
---|
18 | AddressV = WRAP; \
|
---|
19 | };
|
---|
20 |
|
---|
21 | texture ColorMap;
|
---|
22 | SAMPLER_LINEAR(ColorMapSampler, ColorMap);
|
---|
23 | texture BumpMap;
|
---|
24 | SAMPLER_LINEAR(BumpMapSampler,BumpMap);
|
---|
25 |
|
---|
26 | // Illumination functions
|
---|
27 | #include "shaders/illum.hlsl"
|
---|
28 |
|
---|
29 | struct VS_INPUT {float4 Position : POSITION;
|
---|
30 | float3 Normal : NORMAL;
|
---|
31 | float3 Binormal : BINORMAL;
|
---|
32 | float3 Tangent : TANGENT;
|
---|
33 | float4 TexCoord0 : TEXCOORD0;
|
---|
34 | }; |
---|
35 | |
---|
36 | struct VS_OUTPUT { |
---|
37 | float4 hPosition : POSITION; // point in normalized device space before homogeneous division |
---|
38 | float2 TexCoord : TEXCOORD0; // texture coordinates |
---|
39 | float3 tView : TEXCOORD1; // tangent space view vector |
---|
40 | float3 tLight : TEXCOORD2; // tangent space light vector |
---|
41 | float Depth : TEXCOORD3;
|
---|
42 | }; |
---|
43 | |
---|
44 | struct PS_OUTPUT {
|
---|
45 | float4 Color : COLOR0;
|
---|
46 | float4 Depth : COLOR1;
|
---|
47 | };
|
---|
48 | |
---|
49 | //------------------------------------------------------------------------------------ |
---|
50 | // |
---|
51 | // Base vertex shader: vertex shader for all methods: calculates tangent-space |
---|
52 | // tLight, tView, hPosition vectors |
---|
53 | // |
---|
54 | //------------------------------------------------------------------------------------ |
---|
55 | VS_OUTPUT BumpVS(VS_INPUT IN)
|
---|
56 | {
|
---|
57 | VS_OUTPUT output;
|
---|
58 |
|
---|
59 | // object-space tangent matrix
|
---|
60 | float3x3 Tan = float3x3(normalize(IN.Tangent), normalize(IN.Binormal), IN.Normal);
|
---|
61 | // position in view-space
|
---|
62 | float3 P = mul(IN.Position, WorldView);
|
---|
63 | // model-space view vector
|
---|
64 | float3 mView = mCameraPos - IN.Position;
|
---|
65 | // model-space light vector
|
---|
66 | float3 mLight = mLightPos - IN.Position;
|
---|
67 | // tangent-space view vector
|
---|
68 | output.tView = mul(Tan, mView);
|
---|
69 | // tangent-space light vector
|
---|
70 | output.tLight = mul(Tan, mLight);
|
---|
71 | // vertex position before homogenious division
|
---|
72 | output.hPosition = mul(IN.Position, WorldViewProj);
|
---|
73 | // tex coordinates passed to pixel shader
|
---|
74 | output.TexCoord = IN.TexCoord0;
|
---|
75 | output.Depth = output.hPosition.z;
|
---|
76 |
|
---|
77 | return output;
|
---|
78 | } |
---|
79 | //------------------------------------------------------------------------------------ |
---|
80 | // |
---|
81 | // BumpPS: Bump Mapping pixel shader |
---|
82 | // |
---|
83 | //------------------------------------------------------------------------------------ |
---|
84 | PS_OUTPUT BumpPS(VS_OUTPUT IN) |
---|
85 | { |
---|
86 | PS_OUTPUT output; |
---|
87 | // needs normalization because of linear interpolation |
---|
88 | float3 View = normalize( IN.tView ); |
---|
89 | // needs normalization because of linear interpolation |
---|
90 | float3 Light = normalize( IN.tLight ); |
---|
91 | // get tangent-space normal from normal map |
---|
92 | float3 Normal = tex2D(BumpMapSampler, IN.TexCoord).rgb; |
---|
93 | // illumination calculation |
---|
94 | output.Color = Illumination(Light, Normal, View, IN.TexCoord, Attenuation(IN.tLight)); |
---|
95 | |
---|
96 | float blur = saturate(abs(IN.Depth - focalDist) * focalRange);
|
---|
97 | output.Depth = float4(IN.Depth, blur, 0, 0); |
---|
98 | |
---|
99 | return output; |
---|
100 | }
|
---|
101 | //------------------------------------------------------------------------------------
|
---|
102 | // Technique definitions
|
---|
103 | //------------------------------------------------------------------------------------
|
---|
104 | technique BumpMapping
|
---|
105 | {
|
---|
106 | pass p0
|
---|
107 | {
|
---|
108 | VertexShader = compile vs_3_0 BumpVS();
|
---|
109 | PixelShader = compile ps_3_0 BumpPS();
|
---|
110 | }
|
---|
111 | } |
---|