1 | ///////////////////////////////////////////////////////////////////////////////
|
---|
2 | //
|
---|
3 | // ## ######
|
---|
4 | // ###### ###
|
---|
5 | // ## ############### Shark 3D Engine (www.shark3d.com)
|
---|
6 | // ########## # # #
|
---|
7 | // ######## Copyright (c) 1996-2006 Spinor GmbH.
|
---|
8 | // ######### # # # All rights reserved.
|
---|
9 | // ## ##########
|
---|
10 | // ##
|
---|
11 | //
|
---|
12 | ///////////////////////////////////////////////////////////////////////////////
|
---|
13 |
|
---|
14 | #include \
|
---|
15 | <levelutil/shader/prog/d3d9_hlsl/include_shmap_filter_d3d9_hlsl.s3d_shadercode_run>
|
---|
16 |
|
---|
17 | ///////////////////////////////////////////////////////////////////////////////
|
---|
18 |
|
---|
19 | struct PS_INPUT
|
---|
20 | {
|
---|
21 | float2 mainTexCoord: TEXCOORD0;
|
---|
22 | float3 diffuseDirSurf: TEXCOORD1;
|
---|
23 | float3 specularDirSurf: TEXCOORD2;
|
---|
24 | float3 scaledSurfToLight: TEXCOORD3;
|
---|
25 | #ifdef S3D_PARALLAX_MAPPING
|
---|
26 | float3 camToVertSurf: TEXCOORD4;
|
---|
27 | #endif
|
---|
28 | #ifdef S3D_LIGHT_BRIGHT
|
---|
29 | float4 lightBrightCoord: TEXCOORD5;
|
---|
30 | float4 secPosScr: TEXCOORD6;
|
---|
31 | #endif
|
---|
32 | #ifdef S3D_LIGHT_PROJ
|
---|
33 | float4 lightProjCoord: TEXCOORD4;
|
---|
34 | #endif
|
---|
35 | #ifdef S3D_LIGHT_SHMAP
|
---|
36 | float4 lightShmapCoord: TEXCOORD6;
|
---|
37 | #endif
|
---|
38 | };
|
---|
39 |
|
---|
40 | ///////////////////////////////////////////////////////////////////////////////
|
---|
41 |
|
---|
42 | sampler mainSamp: register(s0);
|
---|
43 | sampler normalSamp: register(s1);
|
---|
44 | #ifdef S3D_LIGHT_BRIGHT
|
---|
45 | sampler lightBrightSamp: register(s2);
|
---|
46 | #endif
|
---|
47 | #ifdef S3D_LIGHT_SHMAP
|
---|
48 | sampler lightProjSamp: register(s2);
|
---|
49 | sampler lightShmapSamp: register(s3);
|
---|
50 | #endif
|
---|
51 |
|
---|
52 | ///////////////////////////////////////////////////////////////////////////////
|
---|
53 |
|
---|
54 | const float4 lightAmbient;
|
---|
55 | const float4 lightDiffuse;
|
---|
56 | const float4 lightSpecular;
|
---|
57 |
|
---|
58 | #ifdef S3D_LIGHT_SHMAP
|
---|
59 | const float4 lightShmapSize;
|
---|
60 | const float4 lightShmapRcpSize;
|
---|
61 | #endif
|
---|
62 |
|
---|
63 | #ifdef S3D_LIGHT_BRIGHT
|
---|
64 | const float4 lightBrightRcpSize;
|
---|
65 | #endif
|
---|
66 |
|
---|
67 | ///////////////////////////////////////////////////////////////////////////////
|
---|
68 |
|
---|
69 | #define S3D_INTENS_DISCARD_TRESHOLD 0.001
|
---|
70 | #define S3D_BRIGHT_EPS 0.01
|
---|
71 |
|
---|
72 | ///////////////////////////////////////////////////////////////////////////////
|
---|
73 | // Pixelshader
|
---|
74 | // Profile: 2x0
|
---|
75 |
|
---|
76 | float4 main(PS_INPUT input): COLOR0
|
---|
77 | {
|
---|
78 | #ifdef S3D_GEOMETRIC_SHADOW
|
---|
79 | // Geometric shadow
|
---|
80 | //clip(input.diffuseDirSurf.z);
|
---|
81 | // ATI Mobility Radeon X1600 workaround:
|
---|
82 | clip(float4(0, 0, 1, 0) * input.diffuseDirSurf);
|
---|
83 | #endif
|
---|
84 |
|
---|
85 | #ifdef S3D_LIGHT_SHMAP
|
---|
86 | float3 clipVal = input.lightShmapCoord.www
|
---|
87 | - abs(2 * input.lightShmapCoord.xyz - input.lightShmapCoord.www);
|
---|
88 | clip(clipVal.xyz);
|
---|
89 | #endif
|
---|
90 |
|
---|
91 | #ifdef S3D_LIGHT_BRIGHT
|
---|
92 | #ifdef S3D_LIGHT_BRIGHT_CARE
|
---|
93 | float4 brightVecMain = tex2Dproj(lightBrightSamp, input.lightBrightCoord);
|
---|
94 | float brightIdent = frac(100 * input.secPosScr.z / input.secPosScr.w);
|
---|
95 | float brightDeltaMain = abs(brightVecMain.w - brightIdent);
|
---|
96 | float brightVal = brightVecMain.x;
|
---|
97 | if(brightDeltaMain > S3D_BRIGHT_EPS)
|
---|
98 | {
|
---|
99 | float2 brightOffs = lightBrightRcpSize.xy * input.lightBrightCoord.w;
|
---|
100 | float4 brightCoordX = float4(
|
---|
101 | input.lightBrightCoord.xy + brightOffs, input.lightBrightCoord.zw);
|
---|
102 | float4 brightCoordY = float4(
|
---|
103 | input.lightBrightCoord.xy - brightOffs, input.lightBrightCoord.zw);
|
---|
104 | float4 brightCoordZ = brightCoordX;
|
---|
105 | brightCoordZ.y = brightCoordY.y;
|
---|
106 | float4 brightCoordW = brightCoordY;
|
---|
107 | brightCoordW.y = brightCoordX.y;
|
---|
108 | float4 brightVecX = tex2Dproj(lightBrightSamp, brightCoordX);
|
---|
109 | float4 brightVecY = tex2Dproj(lightBrightSamp, brightCoordY);
|
---|
110 | float4 brightVecZ = tex2Dproj(lightBrightSamp, brightCoordZ);
|
---|
111 | float4 brightVecW = tex2Dproj(lightBrightSamp, brightCoordW);
|
---|
112 | brightVal = min(brightVecMain.x,
|
---|
113 | min(min(brightVecX.x, brightVecY.x),
|
---|
114 | min(brightVecZ.x, brightVecW.x)));
|
---|
115 | }
|
---|
116 | #else
|
---|
117 | // Lookup brightness:
|
---|
118 | float brightVal = tex2Dproj(lightBrightSamp, input.lightBrightCoord).x;
|
---|
119 | #endif
|
---|
120 | // Exit if no light:
|
---|
121 | clip(brightVal - S3D_INTENS_DISCARD_TRESHOLD);
|
---|
122 | #endif
|
---|
123 | #ifdef S3D_LIGHT_SHMAP
|
---|
124 | // Shadow map:
|
---|
125 | float shmapVal = s3d_shmapFilter(
|
---|
126 | lightShmapSamp, input.lightShmapCoord,
|
---|
127 | lightShmapSize, lightShmapRcpSize);
|
---|
128 | clip(shmapVal - S3D_INTENS_DISCARD_TRESHOLD);
|
---|
129 | #endif
|
---|
130 |
|
---|
131 | // Texture coordinates:
|
---|
132 | float2 mainTexCoord = input.mainTexCoord;
|
---|
133 | #ifdef S3D_PARALLAX_MAPPING
|
---|
134 | float3 camToVertSurfNrm = normalize(input.camToVertSurf);
|
---|
135 | float height = tex2D(normalSamp, mainTexCoord).a;
|
---|
136 | float2 offset = S3D_PARALLAX_SCALE * (S3D_PARALLAX_BIAS - height)
|
---|
137 | * camToVertSurfNrm.xy;
|
---|
138 | mainTexCoord = mainTexCoord + offset;
|
---|
139 | #endif
|
---|
140 |
|
---|
141 | // Texture lookups:
|
---|
142 | float4 texCol = tex2D(mainSamp, mainTexCoord);
|
---|
143 | float4 rawNormalView = tex2D(normalSamp, mainTexCoord);
|
---|
144 | float3 normalView = (rawNormalView.xyz - 0.5) * 2;
|
---|
145 |
|
---|
146 | // Calculate range-factor:
|
---|
147 | float rangeFac =
|
---|
148 | max(1.0 - dot(input.scaledSurfToLight, input.scaledSurfToLight), 0.0);
|
---|
149 |
|
---|
150 | // Diffuse direction:
|
---|
151 | float3 diffuseDirSurfNrm = normalize(input.diffuseDirSurf);
|
---|
152 |
|
---|
153 | // Calculate diffuse:
|
---|
154 | float4 diffuse = float4(lightDiffuse.xyz * rangeFac, lightDiffuse.w);
|
---|
155 | diffuse += lightAmbient;
|
---|
156 | float diffDot = dot(normalView, diffuseDirSurfNrm);
|
---|
157 | #ifdef S3D_MILD
|
---|
158 | float diffFac = saturate(lerp(diffDot, 1, S3D_MILD));
|
---|
159 | #else
|
---|
160 | float diffFac = saturate(diffDot);
|
---|
161 | #endif
|
---|
162 | diffuse *= diffFac;
|
---|
163 |
|
---|
164 | // Calculate specular:
|
---|
165 | float4 specular = float4(lightSpecular.xyz * rangeFac, lightSpecular.w);
|
---|
166 | float3 specularDirSurfNrm = normalize(input.specularDirSurf);
|
---|
167 | float specDot = dot(normalView, specularDirSurfNrm);
|
---|
168 | float specFac = saturate(pow(saturate(specDot), S3D_SPECULAR_EXP));
|
---|
169 | specular *= specFac;
|
---|
170 |
|
---|
171 | float4 outCol;
|
---|
172 | outCol.rgb = saturate(diffuse * texCol
|
---|
173 | + specular * texCol.a);
|
---|
174 | // Preserve original opacity from texture.
|
---|
175 | outCol.a = texCol.a;
|
---|
176 |
|
---|
177 | #ifdef S3D_GEOMETRIC_SHADOW
|
---|
178 | // Geometric shadow
|
---|
179 | float geomShadow = saturate(diffuseDirSurfNrm.z * 4);
|
---|
180 | outCol.rgb = saturate(outCol.rgb * geomShadow);
|
---|
181 | #endif
|
---|
182 |
|
---|
183 | #ifdef S3D_LIGHT_BRIGHT
|
---|
184 | // Correct brightness:
|
---|
185 | // The smoothing algorithm generates higher brightness gradients
|
---|
186 | // near 0 and 1 due to sticking to 0 and 1.
|
---|
187 | // This is approximately compensated by using a quadratic function
|
---|
188 | // near 0 and 1.
|
---|
189 | brightVal = 2.0 * brightVal * brightVal * (1.5 - brightVal);
|
---|
190 |
|
---|
191 | // Apply brightness:
|
---|
192 | outCol.rgb = outCol.rgb * brightVal;
|
---|
193 | #endif
|
---|
194 |
|
---|
195 | #ifdef S3D_LIGHT_PROJ
|
---|
196 | float4 texColProjTex = tex2Dproj(lightProjSamp, input.lightProjCoord);
|
---|
197 | outCol *= texColProjTex;
|
---|
198 | #endif
|
---|
199 |
|
---|
200 | #ifdef S3D_LIGHT_SHMAP
|
---|
201 | outCol *= shmapVal;
|
---|
202 | #endif
|
---|
203 |
|
---|
204 | return outCol;
|
---|
205 | }
|
---|
206 |
|
---|
207 | ///////////////////////////////////////////////////////////////////////////////
|
---|