source: GTP/branches/IllumWPdeliver2008dec/IlluminationWP/demos/Standalone/RayTraceEffects [DirectX]/_RayTraceEffects.fx @ 3255

Revision 3255, 35.6 KB checked in by szirmay, 15 years ago (diff)
Line 
1#define SHADOWMAP_SIZE  512.0f                          // The size of ShadowMap
2
3// Offsets in the ShadowMap
4#define HALF    0.5f  / SHADOWMAP_SIZE
5#define ONE             1.0f  / SHADOWMAP_SIZE
6#define TWO             2.0f  / SHADOWMAP_SIZE
7#define THREE   3.0f  / SHADOWMAP_SIZE
8#define FIVE    5.0f  / SHADOWMAP_SIZE
9#define TEN             10.0f / SHADOWMAP_SIZE
10
11#define AMB float4(0.5,0.5,0.5,1)                       // Power of the ambient light
12#define FI 1000                                                         // Summarized power of the light source
13
14//----------------------------------------------
15// GLOBAL VARIABLES
16//---------------------------------------------
17float3   g_vCameraPos3f;                                        // Camera position
18float3   g_vLightPos3f;                                         // Light source position
19int              g_iNumberOfIteration;                          // Number of iteration
20int      g_iObjectID;                                           // ID of the current object
21int              g_iPhotonMapSize;                                      // PhotonMap size
22float    g_fFresnelFactor;                                      // Fresnel factor
23float    g_fPower;                                                      // Power of light source
24float    g_fRefractionIndex;                            // Refraction index     
25float    g_fShadowIntensity;                            // Shadow intensity
26float    g_fCausticsIntensity;                          // The intensity of a caustics snippet                         
27bool     g_bShadowON;                                           // Shadow is used?
28bool     g_bShowHelp;                                           // Darken the scene if help is on
29
30float4x4 g_mWorldView;                                          // WorldView transformation matrix
31float4x4 g_mWorldViewProjection;                        // WorldViewProjection transformation matrix
32float4x4 g_mWorldCenterObject;                          // World transformation matrix of CenterObject
33float4x4 g_mLightViewTexBias;                           // Light space transformation matrix
34
35texture  g_txRoomMap;                                           // Contains the original texture of the Room
36texture  g_txRoomCubeMapColorDistanceMap;       // Contains the color and distance information in a CubeMap
37texture  g_txRoomCubeMapUVMap;                          // Contains the UV and ID of the object in a CubeMap
38texture  g_txPhotonUVMap;                                       // Contains the photon hits UV and ID information
39texture  g_txPowerOfSnippetMap;                         // Contains the Gaussian filter
40texture  g_txRoomModifyMap;                                     // Contains the texture of the Room with shadow and Caustics effect and black edges
41texture  g_txRoomLastMap;                                       // Contains the texture of the Room
42
43// Contain the textures of the Columns with shadow and Caustics effect
44texture g_txColumnModifyTexture0, g_txColumnLastTexture0;
45texture g_txColumnModifyTexture1, g_txColumnLastTexture1;
46texture g_txColumnModifyTexture2, g_txColumnLastTexture2;
47texture g_txColumnModifyTexture3, g_txColumnLastTexture3;
48
49texture g_txColumnOriginalTexture;                      // Contains the original texture of a Column
50texture  g_txShadowMap;                                         // Contains the shadowMap
51
52
53//-----------------------------------------------------------------------------
54// SAMPLERS
55// Each texture must have its own sampler
56//-----------------------------------------------------------------------------
57
58//-----------------------------------------------------------------------------
59// macro definition for filtered samplers
60//-----------------------------------------------------------------------------
61
62#define SAMPLER_LINEAR(g_samplerMap, g_txMap);  \
63        sampler2D g_samplerMap = sampler_state {        \
64    Texture = <g_txMap>;                                                \
65    MinFilter = Linear;                                                 \
66    MagFilter = Linear;                                                 \
67    MipFilter = Linear;                                                 \
68    AddressU  = BORDER;                                                 \
69    AddressV  = BORDER;                                                 \
70};
71
72SAMPLER_LINEAR(g_samplerRoomMap,                        g_txRoomMap);
73SAMPLER_LINEAR(g_samplerRoomLastMap,            g_txRoomLastMap);
74SAMPLER_LINEAR(g_samplerPhotonUVMap,            g_txPhotonUVMap);
75SAMPLER_LINEAR(g_samplerPowerOfSnippetMap,      g_txPowerOfSnippetMap);
76SAMPLER_LINEAR(g_samplerColumnLast0Map,         g_txColumnLastTexture0);
77SAMPLER_LINEAR(g_samplerColumnLast1Map,         g_txColumnLastTexture1);
78SAMPLER_LINEAR(g_samplerColumnLast2Map,         g_txColumnLastTexture2);
79SAMPLER_LINEAR(g_samplerColumnLast3Map,         g_txColumnLastTexture3);
80SAMPLER_LINEAR(g_samplerColumnOriginalMap,      g_txColumnOriginalTexture);
81
82//-----------------------------------------------------------------------------
83// macro definition for non-filtered samplers
84//-----------------------------------------------------------------------------
85
86#define SAMPLER_POINT(g_samplerMap, g_txMap);   \
87        sampler2D g_samplerMap = sampler_state {        \
88    Texture = <g_txMap>;                                                \
89    MinFilter = Point;                                                  \
90    MagFilter = Point;                                                  \
91    MipFilter = Point;                                                  \
92    AddressU  = BORDER;                                                 \
93    AddressV  = BORDER;                                                 \
94};
95
96SAMPLER_POINT(g_samplerRoomModifyMap,           g_txRoomModifyMap);     // must be point, it will be used for filtering
97SAMPLER_POINT(g_samplerColumnModify0Map,        g_txColumnModifyTexture0);     
98SAMPLER_POINT(g_samplerColumnModify1Map,        g_txColumnModifyTexture1);     
99SAMPLER_POINT(g_samplerColumnModify2Map,        g_txColumnModifyTexture2);     
100SAMPLER_POINT(g_samplerColumnModify3Map,        g_txColumnModifyTexture3);     
101
102sampler2D g_samplerShadowMap = sampler_state
103{
104    Texture = <g_txShadowMap>;
105    MinFilter = Linear;
106    MagFilter = Linear;
107    MipFilter = Linear;
108    AddressU  = CLAMP;
109    AddressV  = CLAMP;
110};
111
112samplerCUBE g_samplerRoomCubeMapColorDistanceMap = sampler_state
113{
114    Texture = <g_txRoomCubeMapColorDistanceMap>;
115    MinFilter = Linear;
116    MagFilter = Linear;
117    MipFilter = Linear;
118    AddressU  = BORDER;
119    AddressV  = BORDER;
120};
121
122samplerCUBE g_samplerRoomCubeMapUVMap = sampler_state
123{
124    Texture = <g_txRoomCubeMapUVMap>;
125    MinFilter = Linear;
126    MagFilter = Linear;
127    MipFilter = Linear;
128    AddressU  = BORDER;
129    AddressV  = BORDER;
130};
131
132samplerCUBE g_samplerRoomCubeMapUVMap_NO_FILTER = sampler_state
133{
134    Texture = <g_txRoomCubeMapUVMap>;
135    MinFilter = Point;
136    MagFilter = Point;
137    MipFilter = Point;
138    AddressU  = BORDER;
139    AddressV  = BORDER;
140};
141
142//-----------------------------------------------------------------------------
143// FUNCTIONS
144//-----------------------------------------------------------------------------
145
146// This function is called several times.
147float3 Hit( float3 x, float3 R, samplerCUBE mp )
148{
149        float rl = texCUBE( mp, R ).a;                                                  // |r|
150        float ppp = length( x ) / texCUBE( mp, x ).a;                   // |p|/|p’|
151        float dun = 0, pun = ppp, dov = 0, pov;
152        float dl = rl * ( 1 - ppp );                                                    // eq. 2
153        float3 l = x + R * dl;                                                                  // ray equation
154
155        // iteration
156        for( int i = 0; i < g_iNumberOfIteration; i++ )
157        {
158                float llp = length( l ) / texCUBE( mp,l ).a;            // |l|/|l’|
159                if ( llp < 0.999f )                                                                     // undershooting
160                {
161                        dun = dl; pun = llp;                                                    // last undershooting
162                        dl += ( dov == 0 ) ? rl * ( 1 - llp ) :                 // eq. 2
163                                ( dl - dov ) * ( 1 - llp ) / ( llp - pov );     // eq. 3
164                } else if ( llp > 1.001f )                                                      // overshooting
165                {
166                        dov = dl; pov = llp;                                                    // last overshooting
167                        dl += ( dl -dun ) * ( 1 - llp ) / ( llp - pun );// eq. 3
168                }
169                l = x + R * dl;                                                                         // ray equation
170        }
171        return l;                                                                                               // computed hit point
172}
173
174//*****************************************************************************
175
176// This function enlarges the usefull areas of the atlases.
177float4 FlowLight( sampler2D mp, float2 Tex )
178{
179        // Reads the current pixel
180        float4 preVal = tex2D( mp, Tex );
181        // If it is an unused pixel (.a=0)
182        if ( preVal.a == 0 )
183        {
184                float dist = ONE;
185                if( g_iObjectID != -1 ) dist *= 3.0f;
186       
187                // Reads a neighbor
188                float2 modTex = Tex + float2( dist, 0.0 );
189                float4 candid = tex2D( mp, modTex );
190               
191                // Calculate its intensity.
192                // If it is more intensive than the previous (original) one, it is stored.
193                if( dot( candid.xyz, candid.xyz ) > dot( preVal.xyz, preVal.xyz ) )
194                        preVal = candid;
195               
196                // Reads another neighbor
197                modTex += float2( -dist, dist );
198                candid = tex2D( mp, modTex );
199               
200                // Calculate its intensity.
201                // If it is more intensive than the previous one, it is stored.
202                if( dot( candid.xyz, candid.xyz ) > dot( preVal.xyz, preVal.xyz ) )
203                        preVal = candid;
204       
205                // Reads another neighbor. If it is more intensive than the previous, it is stored.
206                modTex += float2( -dist, -dist );
207                candid = tex2D( mp, modTex );
208                if( dot( candid.xyz, candid.xyz ) > dot( preVal.xyz, preVal.xyz ) )
209                        preVal = candid;
210       
211                // Reads another neighbor. If it is more intensive than the previous, it is stored.
212                modTex += float2( dist, -dist );
213                candid = tex2D( mp, modTex );
214                if( dot( candid.xyz, candid.xyz ) > dot( preVal.xyz, preVal.xyz ) )
215                        preVal = candid;
216        }       
217        return preVal;
218}
219
220
221//-----------------------------------------------------------------------------
222// SHADERS
223// Here are the different shaders.
224//-----------------------------------------------------------------------------
225
226
227//*****************************************************************************
228//-----------------------------------------------------------------------------
229// RenderRoomColorDistance
230// This shader stores the viewable object's color and its distance from the CenterObject's position.
231// The result is stored in a CubeMap.
232//-----------------------------------------------------------------------------
233void RenderRoomColorDistanceVS(
234        float4 Pos     : POSITION,
235        float2 Tex     : TEXCOORD0,
236    out float4 outhPos : POSITION,
237    out float2 outTex  : TEXCOORD0,
238    out float4 outPos  : TEXCOORD1 )
239{
240    outhPos = mul( Pos, g_mWorldViewProjection );
241    outTex  = Tex;
242    // Calculates the world position
243    outPos  = mul( Pos, g_mWorldView );
244}
245// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
246
247float4 RenderRoomColorDistancePS(
248        float2 Tex : TEXCOORD0,
249        float4 Pos : TEXCOORD1 ) : COLOR
250{
251    // The color of the viewable object in the current pixel.
252    // In the scene there are 2 different textured object. (the room, and the Columns)
253    // Every column has the same brdf texture.
254    float3 retColor = float4(0,0,0,0);
255    if ( g_iObjectID == -1 ) retColor = float4( tex2D(           g_samplerRoomMap, Tex ).xyz, 1);
256    else                                         retColor = float4( tex2D( g_samplerColumnOriginalMap, Tex ).xyz, 1);
257   
258    // The distance from the Center object
259    float Distance = (float) ( length( Pos.xyz ) );
260    return float4( retColor, Distance );                // Color(x,y,z) + Distance(w)
261}
262//*****************************************************************************
263
264
265//-----------------------------------------------------------------------------
266// RenderRoomUV
267// This shader stores the viewable object's UV and unique ObjectID.
268// The ObjectID identify the object, because the UV pair is not enough.
269// The result is stored in a CubeMap.
270//-----------------------------------------------------------------------------
271void RenderRoomUVVS(
272        float4 Pos     : POSITION,
273        float2 Tex     : TEXCOORD0,
274    out float4 outhPos : POSITION,
275    out float2 outTex  : TEXCOORD0 )
276{
277    outhPos = mul( Pos, g_mWorldViewProjection );
278    outTex  = Tex;
279}
280// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
281
282float4 RenderRoomUVPS(
283       float2 Tex : TEXCOORD0) : COLOR
284{       // The UV value and the ID of the viewable object in the current pixel
285                                                                                                // Here the "1" value is unnecessary
286    return float4( Tex, g_iObjectID, 1 );               // Tex(x,y) + g_iObjectID(z) + "1"(w)
287}
288//*****************************************************************************
289
290
291//-----------------------------------------------------------------------------
292// RenderUmbra
293// This shader copies the brdf color from the original texture Atlas of the Object to the
294// actual texture Atlas of the Object.
295// It is needed, because the brdf value is modified with shadow.
296// If the pixel is in shadow, the brdf value is decreased.
297// The distance from the light source is also a modifying factor.
298// The result is stored in a 2D map (Atlas).
299//-----------------------------------------------------------------------------
300void RenderUmbraVS(
301        float4 Pos     : POSITION,
302        float2 Tex     : TEXCOORD0,
303        float3 Normal  : NORMAL,
304    out float4 outhPos : POSITION,
305    out float2 outTex  : TEXCOORD0,
306    out float3 outPos  : TEXCOORD1,
307    out float  outCosT : TEXCOORD2,
308    out float4 outLPos : TEXCOORD3)
309{
310        // The Room has no offset.
311        // The Columns have offset from the world's center position.
312        float4 PosWorld = Pos;
313        if ( g_iObjectID > -0.1f ) PosWorld = mul( Pos, g_mWorldView );
314        // Light vector
315        float3 L = normalize( g_vLightPos3f - PosWorld.xyz );
316
317        // Transformation between texture and screen space.
318        // HALF is an offset between the two space.
319    outhPos.x = Tex.x * 2.0f - 1.0f - HALF;
320    outhPos.y = 1.0f - Tex.y * 2.0f + HALF;
321    outhPos.z = 0;
322    outhPos.w = 1;
323   
324    outTex  = Tex;
325    outPos  = PosWorld.xyz;
326    // The direction of the normal of the Room and the Columns are different.
327    if (g_iObjectID < -0.5f)
328                Normal = -Normal;
329    outCosT = dot( Normal, L );
330    outLPos = mul( PosWorld, g_mLightViewTexBias );
331}
332// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
333
334float4 RenderUmbraPS(
335       float2 Tex  : TEXCOORD0,
336       float3 Pos  : TEXCOORD1,
337       float  CosT : TEXCOORD2,
338       float4 LPos : TEXCOORD3 ) : COLOR
339{
340        // return Color
341        float4 retColor = float4(0,0,0,0);
342        // ShadowMap distance values
343        float2 texDist, t051, t052, t053, t054, t101, t102, t103, t104, t031, t032, t033, t034;
344        texDist = t051 = t052 = t053 = t054 = t101 = t102 = t103 = t104 = t031 = t032 = t033 = t034 = 0.0f;
345       
346        // Read the brdf from the atlas of the object
347        // The Room and the Columns have 2 different brdf Atlas.
348        if ( g_iObjectID == -1 )
349                retColor = float4( tex2D(           g_samplerRoomMap, Tex ).xyz, 1);
350        else
351                retColor = float4( tex2D( g_samplerColumnOriginalMap, Tex ).xyz, 1);
352       
353        // Use shadow?
354        if (g_bShadowON)
355        {
356                // Read the ShadowMap for the distance values
357                // and store the alpha value to decide wether its depth value is a real or zero from the ShadowMap border.
358                texDist = float2( LPos.z, 0.0f ) - tex2Dlod( g_samplerShadowMap,  float4(   LPos.xyz / LPos.w, 0 ) ).xw;
359               
360                t031 = float2( LPos.z, 0.0f ) - tex2Dlod( g_samplerShadowMap,  float4( ( LPos.xyz / LPos.w ) + float3( THREE, 0,      0 ), 0 ) ).xw;
361                t032 = float2( LPos.z, 0.0f ) - tex2Dlod( g_samplerShadowMap,  float4( ( LPos.xyz / LPos.w ) + float3( -THREE,ONE,    0 ), 0 ) ).xw;
362                t033 = float2( LPos.z, 0.0f ) - tex2Dlod( g_samplerShadowMap,  float4( ( LPos.xyz / LPos.w ) + float3( ONE,   THREE,  0 ), 0 ) ).xw;
363                t034 = float2( LPos.z, 0.0f ) - tex2Dlod( g_samplerShadowMap,  float4( ( LPos.xyz / LPos.w ) + float3( 0,     -THREE, 0 ), 0 ) ).xw;
364               
365                t051 = float2( LPos.z, 0.0f ) - tex2Dlod( g_samplerShadowMap,  float4( ( LPos.xyz / LPos.w ) + float3( FIVE,  ONE,    0 ), 0 ) ).xw;
366                t052 = float2( LPos.z, 0.0f ) - tex2Dlod( g_samplerShadowMap,  float4( ( LPos.xyz / LPos.w ) + float3( -FIVE, 0,      0 ), 0 ) ).xw;
367                t053 = float2( LPos.z, 0.0f ) - tex2Dlod( g_samplerShadowMap,  float4( ( LPos.xyz / LPos.w ) + float3( -ONE,  FIVE,   0 ), 0 ) ).xw;
368                t054 = float2( LPos.z, 0.0f ) - tex2Dlod( g_samplerShadowMap,  float4( ( LPos.xyz / LPos.w ) + float3( 0,     -FIVE,  0 ), 0 ) ).xw;
369               
370                t101 = float2( LPos.z, 0.0f ) - tex2Dlod( g_samplerShadowMap,  float4( ( LPos.xyz / LPos.w ) + float3( TEN,   THREE,  0 ), 0 ) ).xw;
371                t102 = float2( LPos.z, 0.0f ) - tex2Dlod( g_samplerShadowMap,  float4( ( LPos.xyz / LPos.w ) + float3( -TEN,  -ONE,   0 ), 0 ) ).xw;
372                t103 = float2( LPos.z, 0.0f ) - tex2Dlod( g_samplerShadowMap,  float4( ( LPos.xyz / LPos.w ) + float3( -ONE,  TEN,    0 ), 0 ) ).xw;
373                t104 = float2( LPos.z, 0.0f ) - tex2Dlod( g_samplerShadowMap,  float4( ( LPos.xyz / LPos.w ) + float3( FIVE,  -TEN,   0 ), 0 ) ).xw;
374               
375                // Decrease the intensity of the brdf in the area of the shadow,
376                // and increase it if the pixel has an illuminated neighbor.
377               
378               
379                // This variable modifies the brdf.
380                float summarize = 1.0f;
381               
382                // Decrease it if it is in shadow and it is a real depth value ( alpha = 1 )
383                if ( ( texDist.x > 0.05f ) && ( texDist.y < -0.5f ) )
384                        summarize = g_fShadowIntensity;
385               
386                // Increase it if it is in light.
387                // The power of effect of a neighbor pixel depends on the distance from the original pixel.
388                if ( t031.x < 0.05f && t031.y < -0.5f ) summarize += 0.85;
389                if ( t032.x < 0.05f && t032.y < -0.5f ) summarize += 0.85;
390                if ( t033.x < 0.05f && t033.y < -0.5f ) summarize += 0.85;
391                if ( t034.x < 0.05f && t034.y < -0.5f ) summarize += 0.85;
392                if ( t051.x < 0.05f && t051.y < -0.5f ) summarize += 0.42;
393                if ( t052.x < 0.05f && t052.y < -0.5f ) summarize += 0.42;
394                if ( t053.x < 0.05f && t053.y < -0.5f ) summarize += 0.42;
395                if ( t054.x < 0.05f && t054.y < -0.5f ) summarize += 0.42;
396                if ( t101.x < 0.05f && t101.y < -0.5f ) summarize += 0.10;
397                if ( t102.x < 0.05f && t102.y < -0.5f ) summarize += 0.10;
398                if ( t103.x < 0.05f && t103.y < -0.5f ) summarize += 0.10;
399                if ( t104.x < 0.05f && t104.y < -0.5f ) summarize += 0.10;
400               
401                // The brdf can not be increased by the shadow computation.
402                if ( summarize > 1.0f ) summarize = 1.0;
403                if ( ( texDist.x < 0.05f ) && ( texDist.y < -0.5f ) ) summarize = 1.0;
404                retColor *= summarize;
405               
406                if ( texDist.x > 0.05f ) retColor *= g_fShadowIntensity;
407                if ( g_fShadowIntensity > 0.999 ) retColor /= summarize; 
408        }
409       
410        // The intensity of the brdf depends on the position of the light source.
411       
412        float3 L = g_vLightPos3f;
413        float pll2 = dot( ( Pos - L ), ( Pos - L ) );
414        retColor *= AMB + ( FI / pll2 ) * CosT * g_fCausticsIntensity;
415
416        if (g_bShowHelp)        // light's effect is decreased to improve the visibility of help text
417                retColor *= 0.5f;
418       
419    return retColor;
420}
421//*****************************************************************************
422
423
424//-----------------------------------------------------------------------------
425// RenderRoomAndColumnsScreen
426// This shader render the scene's objects to the screen with their own atlas.
427//-----------------------------------------------------------------------------
428void RenderRoomAndColumnsScreenVS(
429
430        float4 Pos         : POSITION,
431        float2 Tex         : TEXCOORD0,
432    out float4 outhPos : POSITION,
433    out float2 outTex  : TEXCOORD0 )
434{
435    outhPos = mul( Pos, g_mWorldViewProjection );
436    outTex  = Tex;
437}
438// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
439
440float4 RenderRoomAndColumnsScreenPS(
441       float2 Tex : TEXCOORD0) : COLOR
442{
443        float4 retColor = float4(1,1,1,1);
444       
445        // Five different atlas for the five object.
446        if (g_iObjectID == -1 ) retColor = float4( tex2D(    g_samplerRoomLastMap, Tex ).xyz, 1 );
447        if (g_iObjectID ==  0 ) retColor = float4( tex2D( g_samplerColumnLast0Map, Tex ).xyz, 1 );
448        if (g_iObjectID ==  1 ) retColor = float4( tex2D( g_samplerColumnLast1Map, Tex ).xyz, 1 );
449        if (g_iObjectID ==  2 ) retColor = float4( tex2D( g_samplerColumnLast2Map, Tex ).xyz, 1 );
450        if (g_iObjectID ==  3 ) retColor = float4( tex2D( g_samplerColumnLast3Map, Tex ).xyz, 1 );
451       
452        return retColor;
453}
454//*****************************************************************************
455
456
457//-----------------------------------------------------------------------------
458// RenderPhotonUVMap
459// This shader creates the PhotonUVTexture with Hit function.
460// It is a 2D map with the photon hits. It is created from the position of the light source.
461// Every pixel stores the UV, and ObjectID information.
462// The result is stored in a 2D map.
463//-----------------------------------------------------------------------------
464void RenderPhotonUVMapVS(
465        float4 Pos     : POSITION,
466        float3 Normal  : NORMAL,
467        float3 Tex         : TEXCOORD0,
468    out float4 outhPos : POSITION,
469    out float3 outTex  : TEXCOORD0,
470    out float3 outPos  : TEXCOORD1,
471    out float3 outN    : TEXCOORD2,
472    out float3 outL    : TEXCOORD3)
473{
474        // The offset (+float4...) is needed to move the Center Object into the center of the PhotonMap.
475    outhPos = mul( Pos, g_mWorldViewProjection ) + float4( -4.0f / (float)g_iPhotonMapSize , 4.0f / (float)g_iPhotonMapSize , 0, 0 );
476   
477    outTex  = Tex;
478    outPos  = mul( Pos, g_mWorldCenterObject );
479    outN    = Normal;   
480    outL    = normalize( outPos - g_vLightPos3f );
481}
482// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
483
484float4 RenderPhotonUVMapPS(
485        float3 Tex : TEXCOORD0,
486        float3 Pos : TEXCOORD1,
487        float3 N   : TEXCOORD2,
488        float3 L   : TEXCOORD3 ) : COLOR
489{
490        // By default, there is no Hit.
491        float a = -1;
492        float4 hitSurf = float4(0,0,0,0);
493       
494        if ( dot( L, N ) < -0.1 ){
495                L = normalize( L );
496                N = normalize( N );
497                float3 T = normalize( refract( L, N, g_fRefractionIndex ) );
498               
499                // Look up from the CubeMap
500                // The return value is a vector. The CubeUVMap is read at that position.
501                float3 TT = Hit( Pos, T, g_samplerRoomCubeMapColorDistanceMap );
502                hitSurf = texCUBE( g_samplerRoomCubeMapUVMap, TT );
503                // There is a Hit.
504                a = 1;
505        }
506        return float4( hitSurf.xyz, a );
507}
508//*****************************************************************************
509
510
511//-----------------------------------------------------------------------------
512// RenderPhotonUVMapClassic
513// This shader creates the PhotonUVTexture with classical method.
514// It is a 2D map with the photon hits. It is created from the position of the light source.
515// Every pixel stores the UV, and ObjectID information.
516// The result is stored in a 2D map.
517//-----------------------------------------------------------------------------
518void RenderPhotonUVMapClassicVS(
519        float4 Pos     : POSITION,
520        float3 Normal  : NORMAL,
521        float3 Tex         : TEXCOORD0,
522    out float4 outhPos : POSITION,
523    out float3 outTex  : TEXCOORD0,
524    out float3 outPos  : TEXCOORD1,
525    out float3 outN    : TEXCOORD2,
526    out float3 outL    : TEXCOORD3)
527{
528        // The offset (+float4...) is needed to move the Center Object into the center of the PhotonMap.
529    outhPos = mul( Pos, g_mWorldViewProjection ) + float4( -4.0f / (float)g_iPhotonMapSize , 4.0f / (float)g_iPhotonMapSize , 0, 0 );
530   
531    outTex  = Tex;
532    outPos  = mul( Pos, g_mWorldCenterObject );
533    outN    = Normal;   
534    outL    = normalize( outPos - g_vLightPos3f );
535}
536// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
537
538float4 RenderPhotonUVMapClassicPS(
539        float3 Tex : TEXCOORD0,
540        float3 Pos : TEXCOORD1,
541        float3 N   : TEXCOORD2,
542        float3 L   : TEXCOORD3 ) : COLOR
543{
544        // By default, there is no Hit.
545        float a = -1;
546        float4 hitSurf = float4(0,0,0,0);
547       
548        if ( dot( L, N ) < -0.1 ){
549                L = normalize( L );
550                N = normalize( N );
551                float3 TT = normalize( refract( L, N, g_fRefractionIndex ) );
552               
553                hitSurf = texCUBE( g_samplerRoomCubeMapUVMap, TT );
554                // There is a Hit.
555                a = 1;
556        }
557        return float4( hitSurf.xyz, a );
558}
559//*****************************************************************************
560
561 
562       
563//-----------------------------------------------------------------------------
564// RenderPhotonHit
565// This shader uses the PhotonUVTexture as an input.
566// It renders as many snippets to the Object's atlas as many pixels the PhotonUVTexture has.
567// If a pixel in the PhotonUVTexture is not connected to the actual Object (not the same ObjectID),
568// it will not be rendered.
569// The result is blended into the Object's Atlas.
570//-----------------------------------------------------------------------------
571void RenderPhotonHitVS(
572        float4 Pos          : POSITION,
573        float4 Color        : COLOR0,
574        float2 Tex                  : TEXCOORD0,
575    out float4 outhPos      : POSITION,
576    //out float2 outTex     : TEXCOORD0,
577    out float2 outTexFilter : TEXCOORD1,
578    out float  outPower     : TEXCOORD2,
579    out float4 outPos       : TEXCOORD3,
580    out float4 outColor     : TEXCOORD4 )
581{
582        // Reads back the PhotonHit's UV position and the ObjectID
583        float4 ph = tex2Dlod(g_samplerPhotonUVMap, float4( Tex, 0, 0));
584
585        // This enlarges the snippet's size if it is rendered into a Column's atlas.
586        // It is needed because the Column's atlas contains more elements,
587        // and the same distance (in world space) is bigger in the Column's atlas.
588        if ( g_iObjectID > -0.5 ) Pos *= 3.3f;
589
590        // Adds the snippet's offset to the UV coordinates
591        outPos.x  = ph.x + Pos.x / 2.0;
592        outPos.y  = ph.y - Pos.y / 2.0;
593        outPos.z = 0;
594        outPos.w = 1;
595
596        // Transforms the texture coordinates to screen coordinates
597        // and adds the snippet's offset to it
598    outhPos.x = ( ph.x * 2 ) - 1 + Pos.x + HALF;
599    outhPos.y = 1 - ph.y * 2     + Pos.y - HALF;
600   
601    // Is it a real Hit?
602    // and are we render the right Object?
603    if ( ( ph.a > 0 ) && ( ph.b == g_iObjectID ) )
604    {
605                // If YES
606                outhPos.z = 0;
607        }
608        // If NO
609        else outhPos.z = -100;
610       
611        outhPos.w = 1;
612               
613        //outTex = outhPos;
614        outTexFilter = Tex;
615        outPower = ph.w;
616        outColor = Color;
617}
618// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
619
620float4 RenderPhotonHitPS(
621                //float2 Tex       : TEXCOORD0,
622        float2 TexFilter : TEXCOORD1,
623        float  Power     : TEXCOORD2,
624        float4 Pos       : TEXCOORD3,
625        float4 Color     : TEXCOORD4 ) : COLOR
626{
627        float4 retColor;
628        float4 brdf = float4(0,0,0,0);
629       
630        // Room or Column?
631        if ( g_iObjectID == -1 ) brdf = tex2D(           g_samplerRoomMap, Pos);
632        else                                     brdf = tex2D( g_samplerColumnOriginalMap, Pos);
633       
634        // The Gausian weight of a snippet.
635        float4 w = tex2D( g_samplerPowerOfSnippetMap, Color.yz );
636        // The color of the snippet's pixel.
637        retColor = g_fPower * brdf * w * g_fCausticsIntensity;
638        return float4( retColor.xyz, 0 );
639}
640//*****************************************************************************
641
642   
643//-----------------------------------------------------------------------------
644// RenderRefractObjectScreen
645// This shader calculates the color of the CenterObject with Hit function.
646// It has reflection and refraction effect.
647// The result is displayed on the screen.
648//-----------------------------------------------------------------------------
649void RenderRefractObjectScreenVS(
650                float4 Pos     : POSITION,
651        float3 Normal  : NORMAL,
652        float2 Tex         : TEXCOORD0,
653    out float4 outhPos : POSITION,
654    out float3 outPos  : TEXCOORD1,
655    out float3 outN    : TEXCOORD2,
656    out float3 outV    : TEXCOORD3 )
657{
658    outhPos = mul( Pos, g_mWorldViewProjection );
659    outPos  = mul( Pos, g_mWorldCenterObject );
660    outN    = normalize( Normal );
661    outV    = normalize( outPos - g_vCameraPos3f );
662}
663// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
664
665float4 RenderRefractObjectScreenPS(
666        float3 p0 : TEXCOORD1,
667        float3 N  : TEXCOORD2,
668        float3 V  : TEXCOORD3 ) : COLOR
669{       
670        V = normalize( V );
671        N = normalize( N );
672
673          // Calculates reflection
674        float3 R = reflect( V, N );
675        float3 RR = Hit(p0, R, g_samplerRoomCubeMapColorDistanceMap);
676       
677          // Calculates refraction
678        float3 T = refract(V, N, g_fRefractionIndex);
679        float3 TT = Hit(p0, T, g_samplerRoomCubeMapColorDistanceMap);
680
681          // Calculates reflected color
682        float4 hitSurf = texCUBE( g_samplerRoomCubeMapUVMap_NO_FILTER, RR );    // UV position where the Atlas must be read
683        float4 reflectColor = float4( 1,0,0,0 );
684        if (hitSurf.b == -1) reflectColor = tex2D(    g_samplerRoomLastMap, hitSurf.xy );
685        if (hitSurf.b ==  0) reflectColor = tex2D( g_samplerColumnLast0Map, hitSurf.xy );
686        if (hitSurf.b ==  1) reflectColor = tex2D( g_samplerColumnLast1Map, hitSurf.xy );
687        if (hitSurf.b ==  2) reflectColor = tex2D( g_samplerColumnLast2Map, hitSurf.xy );
688        if (hitSurf.b ==  3) reflectColor = tex2D( g_samplerColumnLast3Map, hitSurf.xy );
689       
690       
691          // Calculates refracted color
692        hitSurf = texCUBE( g_samplerRoomCubeMapUVMap_NO_FILTER, TT );
693        float4 refractColor = float4( 1,0,0,0 );
694        if (hitSurf.b == -1) refractColor = tex2D(    g_samplerRoomLastMap, hitSurf.xy );
695        if (hitSurf.b ==  0) refractColor = tex2D( g_samplerColumnLast0Map, hitSurf.xy );
696        if (hitSurf.b ==  1) refractColor = tex2D( g_samplerColumnLast1Map, hitSurf.xy );
697        if (hitSurf.b ==  2) refractColor = tex2D( g_samplerColumnLast2Map, hitSurf.xy );
698        if (hitSurf.b ==  3) refractColor = tex2D( g_samplerColumnLast3Map, hitSurf.xy );
699       
700          // Calculates return color modified by Fresnel function.
701        float F = g_fFresnelFactor + ( 1 - g_fFresnelFactor ) * pow( 1 - dot( N, -V ), 5 );
702        return float4( F * reflectColor + ( 1 - F ) * refractColor );
703}
704
705//*****************************************************************************
706
707
708//-----------------------------------------------------------------------------
709// RenderRefractObjectScreenClassic
710// This shader calculates the color of the CenterObject with classical method.
711// It has reflection and refraction effect.
712// The result is displayed on the screen.
713//-----------------------------------------------------------------------------
714void RenderRefractObjectScreenClassicVS(
715                float4 Pos     : POSITION,
716        float3 Normal  : NORMAL,
717        float2 Tex         : TEXCOORD0,
718    out float4 outhPos : POSITION,
719    out float3 outPos  : TEXCOORD1,
720    out float3 outN    : TEXCOORD2,
721    out float3 outV    : TEXCOORD3 )
722{
723    outhPos = mul( Pos, g_mWorldViewProjection );
724    outPos  = mul( Pos, g_mWorldCenterObject );
725    outN    = normalize( Normal );
726    outV    = normalize( outPos - g_vCameraPos3f );
727}
728// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
729
730float4 RenderRefractObjectScreenClassicPS(
731        float3 p0 : TEXCOORD1,
732        float3 N  : TEXCOORD2,
733        float3 V  : TEXCOORD3 ) : COLOR
734{       
735        V = normalize( V );
736        N = normalize( N );
737
738          // Calculates reflection
739        float3 RR = reflect( V, N );
740       
741          // Calculates refraction
742        float3 TT = refract(V, N, g_fRefractionIndex);
743
744
745          // Calculates reflected color
746        float4 hitSurf = texCUBE( g_samplerRoomCubeMapUVMap_NO_FILTER, RR );    // UV position where the Atlas must be read
747        float3 reflectColor = float4(0,0,0,0);
748        if (hitSurf.b == -1) reflectColor = tex2D(    g_samplerRoomLastMap, hitSurf.xy );
749        if (hitSurf.b ==  0) reflectColor = tex2D( g_samplerColumnLast0Map, hitSurf.xy );
750        if (hitSurf.b ==  1) reflectColor = tex2D( g_samplerColumnLast1Map, hitSurf.xy );
751        if (hitSurf.b ==  2) reflectColor = tex2D( g_samplerColumnLast2Map, hitSurf.xy );
752        if (hitSurf.b ==  3) reflectColor = tex2D( g_samplerColumnLast3Map, hitSurf.xy );
753       
754          // Calculates refracted color
755        hitSurf = texCUBE( g_samplerRoomCubeMapUVMap_NO_FILTER, TT );
756        float3 refractColor = float4(0,0,0,0);
757        if (hitSurf.b == -1) refractColor = tex2D(    g_samplerRoomLastMap, hitSurf.xy );
758        if (hitSurf.b ==  0) refractColor = tex2D( g_samplerColumnLast0Map, hitSurf.xy );
759        if (hitSurf.b ==  1) refractColor = tex2D( g_samplerColumnLast1Map, hitSurf.xy );
760        if (hitSurf.b ==  2) refractColor = tex2D( g_samplerColumnLast2Map, hitSurf.xy );
761        if (hitSurf.b ==  3) refractColor = tex2D( g_samplerColumnLast3Map, hitSurf.xy );
762       
763          // Calculates return color modified by Fresnel function.
764        float F = g_fFresnelFactor + ( 1 - g_fFresnelFactor ) * pow( 1 - dot( N, -V ), 5 );
765        return float4( F * reflectColor + ( 1 - F ) * refractColor, 1 );
766}
767
768//*****************************************************************************
769
770
771
772//-----------------------------------------------------------------------------
773// RenderLightScreen
774// This shader renders the light source's object to the screen with a diffuse color.
775//-----------------------------------------------------------------------------
776void RenderLightScreenVS(
777        float4 Pos    : POSITION,
778    out float4 outPos : POSITION )
779{
780    outPos = mul( Pos, g_mWorldViewProjection );
781}
782
783// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
784
785float4 RenderLightScreenPS() : COLOR
786{
787    return float4( 1, 1, 0.7f, 1 );
788}
789//*****************************************************************************
790
791
792//-----------------------------------------------------------------------------
793// RenderShadow
794// This shader calculates the ShadowMapTexture's depth values.
795// The result is stored in g_pShadowMapTexture.
796//-----------------------------------------------------------------------------
797void RenderShadowVS(
798                float4 Pos     : POSITION,
799    out float4 outhPos : POSITION,
800    out float2 outPos  : TEXCOORD1 )
801{
802    outhPos = mul( Pos, g_mWorldViewProjection );
803    outPos  = mul( Pos, g_mLightViewTexBias ).zw;
804}
805// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
806
807float4 RenderShadowPS(
808                float2 Pos : TEXCOORD1 ) : COLOR
809{
810    return float4(Pos.x, 0, 0, 1);
811}
812//*****************************************************************************
813
814
815//-----------------------------------------------------------------------------
816// FullScreenQuad
817// This shader modifies the atlases of Room and Columns.
818// It removes the black edges from the atlases.
819// The result is stored in the Object's atlas.
820//-----------------------------------------------------------------------------
821void FullScreenQuadVS(
822                float4 Pos     : POSITION,
823    out float4 outhPos : POSITION,
824    out float2 outTex  : TEXCOORD0 )
825{
826        outhPos = float4( Pos.xy, 0, 1 );
827    outTex.x = Pos.x * 0.5f + 0.5f;
828    outTex.y = -Pos.y * 0.5f + 0.5f;
829}
830// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
831
832float4 FullScreenQuadPS(
833                float2 Tex : TEXCOORD0 ) : COLOR
834{
835    float4 preVal = float4(1,1,1,1);
836   
837    if( g_iObjectID ==-1 ) preVal = FlowLight(    g_samplerRoomModifyMap, Tex );
838    if( g_iObjectID == 0 ) preVal = FlowLight( g_samplerColumnModify0Map, Tex );
839    if( g_iObjectID == 1 ) preVal = FlowLight( g_samplerColumnModify1Map, Tex );
840    if( g_iObjectID == 2 ) preVal = FlowLight( g_samplerColumnModify2Map, Tex );
841    if( g_iObjectID == 3 ) preVal = FlowLight( g_samplerColumnModify3Map, Tex );
842       
843        return preVal; 
844}
845//*****************************************************************************
846
847
848//-----------------------------------------------------------------------------
849// PhotonMapScreen
850// This shader renders the PhotonMap texture (g_txPhotonUVMap) into the screen lower left corner.
851// This texture contains the PhotonHits positions and ObjectIDs.
852// The result is displayed on the screen.
853//-----------------------------------------------------------------------------
854
855void PhotonMapScreenVS(
856                float4 Pos     : POSITION,
857    out float4 outhPos : POSITION,
858    out float2 outTex  : TEXCOORD0 )
859{
860        outhPos = float4( Pos.xy * 0.2f + float2( -0.7f, -0.7f ), 0, 1 );
861    outTex.x = Pos.x * 0.5f + 0.5f;
862    outTex.y = -Pos.y * 0.5f + 0.5f;
863}
864// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
865
866float4 PhotonMapScreenPS(
867                float2 Tex : TEXCOORD0 ) : COLOR
868{
869        return tex2D( g_samplerPhotonUVMap, Tex );     
870}
871//*****************************************************************************
872
873
874
875
876
877//-----------------------------------------------------------------------------
878// TECHNIQUES
879// Here are the different Techniques.
880//-----------------------------------------------------------------------------
881
882// Technique(AnyRenderPass):
883//              VertexShader = compile vs_3_0 AnyRenderPassVS()
884//              PixelShader  = compile vs_3_0 AnyRenderPassPS()
885
886// if using the name convention above to name vertex/pixel shaders,
887// you can use the following macro definition to quickly define a technique:
888
889// note: ## stands for concatenation
890
891#define Technique(name);                                                                \
892        technique name                                                                          \
893        {                                                                                                       \
894            pass p0                                                                                     \
895            {                                                                                           \
896                    VertexShader = compile vs_3_0 name##VS();   \
897                    PixelShader  = compile ps_3_0 name##PS();   \
898                }                                                                                               \
899        }
900       
901Technique( RenderRoomColorDistance );
902Technique( RenderRoomUV );
903Technique( RenderUmbra );
904Technique( RenderRoomAndColumnsScreen );
905Technique( RenderLightScreen );
906Technique( RenderPhotonUVMap );
907Technique( RenderPhotonUVMapClassic );
908Technique( RenderPhotonHit );
909Technique( RenderRefractObjectScreen );
910Technique( RenderRefractObjectScreenClassic );
911Technique( RenderShadow );
912Technique( FullScreenQuad );
913Technique( PhotonMapScreen );
Note: See TracBrowser for help on using the repository browser.