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