1 | //------------------------------------
|
---|
2 | float4x4 g_mWorldView; // World View Matrix
|
---|
3 | float4x4 g_mView; // View Matrix
|
---|
4 | float4x4 g_mProj; // Projection Matrix
|
---|
5 | float4x4 g_mWorldViewProjection; // World View Projection Matrix
|
---|
6 | float4x4 g_mViewProjection;
|
---|
7 |
|
---|
8 | float ParticleSize; // Particle's size in world units.
|
---|
9 | float3 ParticlePos; //Position of Particle
|
---|
10 | float ReciprocalParticleSize; // 1/Particle size.
|
---|
11 | float NearPlaneDistance; // Distance of the near plane.
|
---|
12 | float FarPlaneMinusNearPlane; // Distance of the far plane minus distance of the near plane.
|
---|
13 | bool animatedSprite;
|
---|
14 | bool useDepthImposter;
|
---|
15 | float invNbColumns;
|
---|
16 | float invNbRows;
|
---|
17 | int nbColumns;
|
---|
18 | int nbRows;
|
---|
19 | int nbFrame;
|
---|
20 |
|
---|
21 | //CurrentValues
|
---|
22 | float4 argb;
|
---|
23 | float2 hWidthHeight;
|
---|
24 |
|
---|
25 | texture g_txCurrentTexture;
|
---|
26 | texture g_txDepthBuffer;
|
---|
27 | texture preDistortion;
|
---|
28 | texture distortionMap1;
|
---|
29 | texture distortionMap2;
|
---|
30 | texture screenDistortionMap;
|
---|
31 |
|
---|
32 | //------------------------------------
|
---|
33 | struct vertexInput {
|
---|
34 | float3 position : POSITION;
|
---|
35 | float4 texCoord : TEXCOORD0;
|
---|
36 | };
|
---|
37 |
|
---|
38 | struct vertexOutput {
|
---|
39 | float4 HPosition : POSITION;
|
---|
40 | float4 ColorTextureCoord : TEXCOORD0;
|
---|
41 | float2 DepthBufferCoord : TEXCOORD1;
|
---|
42 | float ParticleDepth : TEXCOORD2;
|
---|
43 | };
|
---|
44 |
|
---|
45 | struct cpVertexOutput {
|
---|
46 | float4 HPosition : POSITION;
|
---|
47 | float4 ColorTextureCoord : TEXCOORD0;
|
---|
48 | };
|
---|
49 |
|
---|
50 | struct pixelOutput
|
---|
51 | {
|
---|
52 | float4 color : COLOR;
|
---|
53 | };
|
---|
54 |
|
---|
55 |
|
---|
56 | struct depthVertexInput {
|
---|
57 | float4 position : POSITION;
|
---|
58 | float4 texCoord : TEXCOORD0;
|
---|
59 | };
|
---|
60 |
|
---|
61 | struct depthVertexOutput {
|
---|
62 | float4 HPosition : POSITION;
|
---|
63 | float4 texCoord : TEXCOORD0;
|
---|
64 | };
|
---|
65 |
|
---|
66 | //------------------------------------
|
---|
67 | #define SAMPLER_LINEAR(g_samplerMap, g_txMap); \
|
---|
68 | sampler2D g_samplerMap = sampler_state { \
|
---|
69 | Texture = <g_txMap>; \
|
---|
70 | MinFilter = Linear; \
|
---|
71 | MagFilter = Linear; \
|
---|
72 | MipFilter = Linear; \
|
---|
73 | AddressU = BORDER; \
|
---|
74 | AddressV = BORDER; \
|
---|
75 | };
|
---|
76 |
|
---|
77 | #define SAMPLER_POINT(g_samplerMap, g_txMap); \
|
---|
78 | sampler2D g_samplerMap = sampler_state { \
|
---|
79 | Texture = <g_txMap>; \
|
---|
80 | MinFilter = Point; \
|
---|
81 | MagFilter = Point; \
|
---|
82 | MipFilter = Point; \
|
---|
83 | AddressU = BORDER; \
|
---|
84 | AddressV = BORDER; \
|
---|
85 | };
|
---|
86 |
|
---|
87 | #define SAMPLER_LINEAR_WRAP(g_samplerMap, g_txMap); \
|
---|
88 | sampler2D g_samplerMap = sampler_state { \
|
---|
89 | Texture = <g_txMap>; \
|
---|
90 | MinFilter = Linear; \
|
---|
91 | MagFilter = Linear; \
|
---|
92 | MipFilter = Linear; \
|
---|
93 | AddressU = WRAP; \
|
---|
94 | AddressV = WRAP; \
|
---|
95 | };
|
---|
96 |
|
---|
97 | SAMPLER_LINEAR(g_samplerCurrentTexture, g_txCurrentTexture);
|
---|
98 | SAMPLER_POINT(g_samplerDepthBuffer, g_txDepthBuffer);
|
---|
99 | SAMPLER_POINT(samplerPreDistortion, preDistortion);
|
---|
100 | SAMPLER_LINEAR_WRAP(samplerDistMap1, distortionMap1);
|
---|
101 | SAMPLER_LINEAR_WRAP(samplerDistMap2, distortionMap2);
|
---|
102 | SAMPLER_POINT(samplerScreenDistMap, screenDistortionMap);
|
---|
103 |
|
---|
104 |
|
---|
105 | //----------------------------------------------
|
---|
106 | depthVertexOutput DepthPassVS(depthVertexInput IN)
|
---|
107 | {
|
---|
108 | depthVertexOutput OUT;
|
---|
109 | float4 vertexpos = float4(IN.position.xyz, 1.0);
|
---|
110 | float4 eyespace = mul(vertexpos, g_mWorldViewProjection);
|
---|
111 | OUT.HPosition = eyespace;
|
---|
112 | float tempDepth = saturate(eyespace.z/FarPlaneMinusNearPlane);
|
---|
113 | IN.texCoord.a = tempDepth;
|
---|
114 | OUT.texCoord = IN.texCoord;
|
---|
115 | return OUT;
|
---|
116 | }
|
---|
117 |
|
---|
118 | pixelOutput DepthPassPS(depthVertexOutput IN)
|
---|
119 | {
|
---|
120 | pixelOutput OUT;
|
---|
121 | float depth = IN.texCoord.a;
|
---|
122 | OUT.color = float4(depth, depth, depth, depth);
|
---|
123 | return OUT;
|
---|
124 | }
|
---|
125 |
|
---|
126 | //------------------------------------
|
---|
127 | vertexOutput DepthImposterVS(vertexInput IN)
|
---|
128 | {
|
---|
129 | vertexOutput OUT;
|
---|
130 |
|
---|
131 | //Billboarding
|
---|
132 | float3 rightVec;
|
---|
133 | rightVec.x = g_mView[0][0];
|
---|
134 | rightVec.y = g_mView[1][0];
|
---|
135 | rightVec.z = g_mView[2][0];
|
---|
136 | float3 upVec;
|
---|
137 | upVec.x = g_mView[0][1];
|
---|
138 | upVec.y = g_mView[1][1];
|
---|
139 | upVec.z = g_mView[2][1];
|
---|
140 | float3 frontVec;
|
---|
141 | frontVec.x = g_mView[0][2];
|
---|
142 | frontVec.y = g_mView[1][2];
|
---|
143 | frontVec.z = g_mView[2][2];
|
---|
144 | float3 pos;
|
---|
145 | pos = ParticlePos;
|
---|
146 |
|
---|
147 | pos += (hWidthHeight.x*IN.position.x*rightVec + hWidthHeight.y*IN.position.y*upVec);
|
---|
148 | pos += ParticleSize*frontVec;
|
---|
149 | float4 finalpos = mul(float4(pos,1), g_mViewProjection);
|
---|
150 | OUT.HPosition = finalpos;
|
---|
151 |
|
---|
152 | if(useDepthImposter) {
|
---|
153 | OUT.ParticleDepth = finalpos.z/FarPlaneMinusNearPlane; //Dividieren durch zfar usw
|
---|
154 |
|
---|
155 | // Apply w-division to get normalized device coordinates [-1,1].
|
---|
156 | float2 devicecoords = finalpos.xy / finalpos.w;
|
---|
157 |
|
---|
158 | // No transform them into texture coordinates [0,1].
|
---|
159 | devicecoords = devicecoords * 0.5 + 0.5;
|
---|
160 | devicecoords.y = 1.0 - devicecoords.y;
|
---|
161 | OUT.DepthBufferCoord = devicecoords;
|
---|
162 | }
|
---|
163 |
|
---|
164 | // Texture coordinate depends on current pattern index and pattern size.
|
---|
165 | if(!animatedSprite) {
|
---|
166 | OUT.ColorTextureCoord = IN.texCoord;
|
---|
167 | } else {
|
---|
168 | int column = nbFrame%nbColumns;
|
---|
169 | int framesSub = nbFrame - column;
|
---|
170 | int row = framesSub/nbColumns;
|
---|
171 |
|
---|
172 | OUT.ColorTextureCoord.x = invNbColumns*(column+IN.texCoord.x);
|
---|
173 | OUT.ColorTextureCoord.y = invNbRows*(row+IN.texCoord.y);
|
---|
174 | }
|
---|
175 |
|
---|
176 | return OUT;
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | //------------------------------------
|
---|
181 | pixelOutput DepthImposterPS(vertexOutput IN)
|
---|
182 | {
|
---|
183 | pixelOutput OUT;
|
---|
184 |
|
---|
185 | // Do a lookup in the depth buffer.
|
---|
186 | float difference = 1.0f;
|
---|
187 | if(useDepthImposter) {
|
---|
188 | float4 lookup = float4(IN.DepthBufferCoord.xy, 0.0, 1.0);
|
---|
189 | float depth = tex2D(g_samplerDepthBuffer, lookup).a;
|
---|
190 |
|
---|
191 | // Calculate visible range of the particle.
|
---|
192 | difference = abs(depth - IN.ParticleDepth);
|
---|
193 | difference *= ReciprocalParticleSize;
|
---|
194 |
|
---|
195 | difference = min(difference, 1.0);
|
---|
196 | }
|
---|
197 | // Multiply texel by input color.
|
---|
198 | float4 texturecolor = tex2D(g_samplerCurrentTexture, IN.ColorTextureCoord);
|
---|
199 | texturecolor.a *= difference;
|
---|
200 | texturecolor*=argb;
|
---|
201 | OUT.color = texturecolor;
|
---|
202 |
|
---|
203 | return OUT;
|
---|
204 | }
|
---|
205 |
|
---|
206 | //------------------------------------
|
---|
207 | cpVertexOutput HeatHazeCopyVS(vertexInput IN)
|
---|
208 | {
|
---|
209 | cpVertexOutput OUT;
|
---|
210 |
|
---|
211 | OUT.HPosition = float4(IN.position,1);
|
---|
212 | OUT.ColorTextureCoord = IN.texCoord;
|
---|
213 | return OUT;
|
---|
214 | }
|
---|
215 |
|
---|
216 |
|
---|
217 | //------------------------------------
|
---|
218 | pixelOutput HeatHazeCopyPS(cpVertexOutput IN)
|
---|
219 | {
|
---|
220 | pixelOutput OUT;
|
---|
221 | float2 deviceCoord = IN.ColorTextureCoord.xy;
|
---|
222 | float2 offset = tex2D(samplerScreenDistMap, deviceCoord).xy;
|
---|
223 | offset = ((offset*2)-1)*0.005;
|
---|
224 | OUT.color = float4(tex2D(samplerPreDistortion, deviceCoord+offset).xyz, 1);
|
---|
225 | return OUT;
|
---|
226 | }
|
---|
227 |
|
---|
228 |
|
---|
229 | //------------------------------------
|
---|
230 | vertexOutput HeatHazeVS(vertexInput IN)
|
---|
231 | {
|
---|
232 | vertexOutput OUT;
|
---|
233 |
|
---|
234 | //Billboarding
|
---|
235 | float3 rightVec;
|
---|
236 | rightVec.x = g_mView[0][0];
|
---|
237 | rightVec.y = g_mView[1][0];
|
---|
238 | rightVec.z = g_mView[2][0];
|
---|
239 | float3 upVec;
|
---|
240 | upVec.x = g_mView[0][1];
|
---|
241 | upVec.y = g_mView[1][1];
|
---|
242 | upVec.z = g_mView[2][1];
|
---|
243 | float3 frontVec;
|
---|
244 | frontVec.x = g_mView[0][2];
|
---|
245 | frontVec.y = g_mView[1][2];
|
---|
246 | frontVec.z = g_mView[2][2];
|
---|
247 | float3 pos;
|
---|
248 | pos = ParticlePos;
|
---|
249 |
|
---|
250 | pos += (hWidthHeight.x*IN.position.x*rightVec + hWidthHeight.y*IN.position.y*upVec);
|
---|
251 | pos += ParticleSize*frontVec;
|
---|
252 | //pos+=IN.position;
|
---|
253 |
|
---|
254 |
|
---|
255 | // Calculate particle's center position in eye space.
|
---|
256 | /*float4 vertexpos = float4(IN.position.xyz, 1.0);
|
---|
257 | float4 eyespace = mul(vertexpos, g_mWorldView);
|
---|
258 |
|
---|
259 | eyespace.z += ParticleSize;
|
---|
260 |
|
---|
261 | // Multiplicate with projection matrix to get final vertex position.
|
---|
262 | float4 finalpos = mul(eyespace, g_mProj);*/
|
---|
263 | float4 finalpos = mul(float4(pos,1), g_mViewProjection);
|
---|
264 | OUT.HPosition = finalpos;
|
---|
265 | OUT.ParticleDepth = finalpos.z/FarPlaneMinusNearPlane; //Dividieren durch zfar usw
|
---|
266 |
|
---|
267 | // Apply w-division to get normalized device coordinates [-1,1].
|
---|
268 | float2 devicecoords = finalpos.xy / finalpos.w;
|
---|
269 |
|
---|
270 | // No transform them into texture coordinates [0,1].
|
---|
271 | devicecoords = devicecoords * 0.5 + 0.5;
|
---|
272 | devicecoords.y = 1.0 - devicecoords.y;
|
---|
273 | OUT.DepthBufferCoord = devicecoords;
|
---|
274 |
|
---|
275 | // Texture coordinate depends on current pattern index and pattern size.
|
---|
276 | OUT.ColorTextureCoord = IN.texCoord;
|
---|
277 |
|
---|
278 | return OUT;
|
---|
279 | }
|
---|
280 |
|
---|
281 |
|
---|
282 | //------------------------------------
|
---|
283 | pixelOutput HeatHazePS(vertexOutput IN)
|
---|
284 | {
|
---|
285 | pixelOutput OUT;
|
---|
286 | float2 deviceCoord = IN.DepthBufferCoord.xy;
|
---|
287 | float depth = tex2D(g_samplerDepthBuffer, deviceCoord).a;
|
---|
288 |
|
---|
289 | if(depth>IN.ParticleDepth) {
|
---|
290 | float difference = abs(depth - IN.ParticleDepth);
|
---|
291 | difference *= ReciprocalParticleSize;
|
---|
292 | difference = min(difference, 1.0);
|
---|
293 | float2 offset = tex2D(samplerDistMap1, IN.ColorTextureCoord.xy).xy;
|
---|
294 |
|
---|
295 | float fade = abs(offset.x-0.5)*2*difference;
|
---|
296 | OUT.color = float4((offset-0.5)*difference, 0.5, fade);
|
---|
297 | } else {
|
---|
298 | OUT.color = float4(0.5, 0.5, 0.5, 0);
|
---|
299 | }
|
---|
300 | return OUT;
|
---|
301 | }
|
---|
302 |
|
---|
303 | //
|
---|
304 | // Effect
|
---|
305 | //
|
---|
306 |
|
---|
307 | #define Technique(name); \
|
---|
308 | technique name \
|
---|
309 | { \
|
---|
310 | pass p0 \
|
---|
311 | { \
|
---|
312 | VertexShader = compile vs_3_0 name##VS(); \
|
---|
313 | PixelShader = compile ps_3_0 name##PS(); \
|
---|
314 | } \
|
---|
315 | }
|
---|
316 |
|
---|
317 | Technique( DepthImposter );
|
---|
318 | Technique( DepthPass );
|
---|
319 | Technique( HeatHazeCopy );
|
---|
320 | Technique( HeatHaze );
|
---|