[1378] | 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 MatIP; // Inverse Projection Matrix
|
---|
| 7 |
|
---|
| 8 | //float3 ParticlePosition; // Particle's center in world coordinates
|
---|
| 9 | //float2 CurrentPattern; // Current pattern offset.
|
---|
| 10 | //float2 PatternSize; // Pattern size.
|
---|
| 11 | float ParticleSize; // Particle's size in world units.
|
---|
| 12 | float ReciprocalParticleSize; // 1/Particle size.
|
---|
| 13 | //float4 ParticleColor; // Particle color and opacity.
|
---|
| 14 | //float ScreenSpaceRotation; // Rotation of particle in radiants.
|
---|
| 15 | float NearPlaneDistance; // Distance of the near plane.
|
---|
| 16 | float FarPlaneMinusNearPlane; // Distance of the far plane minus distance of the near plane.
|
---|
| 17 |
|
---|
| 18 | texture g_txCurrentTexture;
|
---|
| 19 | texture g_txDepthBuffer;
|
---|
| 20 |
|
---|
| 21 |
|
---|
| 22 | //------------------------------------
|
---|
| 23 | struct vertexInput {
|
---|
| 24 | float3 position : POSITION;
|
---|
| 25 | float4 texCoord : TEXCOORD0;
|
---|
| 26 | };
|
---|
| 27 |
|
---|
| 28 | struct vertexOutput {
|
---|
| 29 | float4 HPosition : POSITION;
|
---|
| 30 | float4 ColorTextureCoord : TEXCOORD0;
|
---|
| 31 | float2 DepthBufferCoord : TEXCOORD1;
|
---|
| 32 | float ParticleDepth : TEXCOORD2;
|
---|
| 33 | };
|
---|
| 34 |
|
---|
| 35 | struct pixelOutput
|
---|
| 36 | {
|
---|
| 37 | float4 color : COLOR;
|
---|
| 38 | };
|
---|
| 39 |
|
---|
| 40 |
|
---|
| 41 | struct depthVertexInput {
|
---|
| 42 | float4 position : POSITION;
|
---|
| 43 | float4 texCoord : TEXCOORD0;
|
---|
| 44 | };
|
---|
| 45 |
|
---|
| 46 | struct depthVertexOutput {
|
---|
| 47 | float4 HPosition : POSITION;
|
---|
| 48 | float4 texCoord : TEXCOORD0;
|
---|
| 49 | };
|
---|
| 50 |
|
---|
| 51 | //------------------------------------
|
---|
| 52 | #define SAMPLER_LINEAR(g_samplerMap, g_txMap); \
|
---|
| 53 | sampler2D g_samplerMap = sampler_state { \
|
---|
| 54 | Texture = <g_txMap>; \
|
---|
| 55 | MinFilter = Linear; \
|
---|
| 56 | MagFilter = Linear; \
|
---|
| 57 | MipFilter = Linear; \
|
---|
| 58 | AddressU = BORDER; \
|
---|
| 59 | AddressV = BORDER; \
|
---|
| 60 | };
|
---|
| 61 |
|
---|
| 62 | #define SAMPLER_POINT(g_samplerMap, g_txMap); \
|
---|
| 63 | sampler2D g_samplerMap = sampler_state { \
|
---|
| 64 | Texture = <g_txMap>; \
|
---|
| 65 | MinFilter = Point; \
|
---|
| 66 | MagFilter = Point; \
|
---|
| 67 | MipFilter = Point; \
|
---|
| 68 | AddressU = BORDER; \
|
---|
| 69 | AddressV = BORDER; \
|
---|
| 70 | };
|
---|
| 71 |
|
---|
| 72 | SAMPLER_LINEAR(g_samplerCurrentTexture, g_txCurrentTexture);
|
---|
| 73 | //SAMPLER_POINT(g_samplerDepthBuffer, g_txDepthBuffer);
|
---|
| 74 | SAMPLER_LINEAR(g_samplerDepthBuffer, g_txDepthBuffer);
|
---|
| 75 | //sampler DepthBuffer;
|
---|
| 76 |
|
---|
| 77 | //----------------------------------------------
|
---|
| 78 | depthVertexOutput DepthPassVS(depthVertexInput IN)
|
---|
| 79 | {
|
---|
| 80 | depthVertexOutput OUT;
|
---|
| 81 | float4 vertexpos = float4(IN.position.xyz, 1.0);
|
---|
| 82 | float4 eyespace = mul(vertexpos, g_mWorldViewProjection);
|
---|
| 83 | OUT.HPosition = eyespace;
|
---|
| 84 | float tempDepth = saturate(eyespace.z/FarPlaneMinusNearPlane);
|
---|
| 85 | IN.texCoord.a = tempDepth;
|
---|
| 86 | OUT.texCoord = IN.texCoord;
|
---|
| 87 | return OUT;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | pixelOutput DepthPassPS(depthVertexOutput IN)
|
---|
| 91 | {
|
---|
| 92 | pixelOutput OUT;
|
---|
| 93 | float depth = IN.texCoord.a;
|
---|
| 94 | OUT.color = float4(depth, depth, depth, depth);
|
---|
| 95 | return OUT;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | //------------------------------------
|
---|
| 99 | vertexOutput DepthImposterVS(vertexInput IN)
|
---|
| 100 | {
|
---|
| 101 | vertexOutput OUT;
|
---|
| 102 |
|
---|
| 103 | // Calculate particle's center position in eye space.
|
---|
| 104 | //float4 vertexpos = float4(ParticlePosition.xyz, 1.0);
|
---|
| 105 | float4 vertexpos = float4(IN.position.xyz, 1.0);
|
---|
| 106 | float4 eyespace = mul(vertexpos, g_mWorldView);
|
---|
| 107 |
|
---|
| 108 | // Calculate offset and size for this vertex depending on input position.
|
---|
| 109 | /*float2 offset = float2(IN.position.xy) * ParticleSize;
|
---|
| 110 |
|
---|
| 111 | // Include screen space rotation.
|
---|
| 112 | float anglesin = sin(ScreenSpaceRotation);
|
---|
| 113 | float anglecos = cos(ScreenSpaceRotation);
|
---|
| 114 | float2x2 rotmatrix;
|
---|
| 115 | rotmatrix[0][0] = anglecos;
|
---|
| 116 | rotmatrix[1][0] = -anglesin;
|
---|
| 117 | rotmatrix[0][1] = anglesin;
|
---|
| 118 | rotmatrix[1][1] = anglecos;
|
---|
| 119 | float2 rotate = mul(rotmatrix, offset);
|
---|
| 120 | eyespace += float4(rotate.xy, -ParticleSize, 0.0);*/
|
---|
| 121 | eyespace.z += ParticleSize;
|
---|
| 122 | // Write linear depth of this vertex to the output.
|
---|
| 123 | //OUT.ParticleDepth = (eyespace.z);
|
---|
| 124 |
|
---|
| 125 | // Multiplicate with projection matrix to get final vertex position.
|
---|
| 126 | float4 finalpos = mul(eyespace, g_mProj);
|
---|
| 127 | OUT.HPosition = finalpos;
|
---|
| 128 |
|
---|
| 129 | OUT.ParticleDepth = finalpos.z/FarPlaneMinusNearPlane; //Dividieren durch zfar usw
|
---|
| 130 |
|
---|
| 131 | // Apply w-division to get normalized device coordinates [-1,1].
|
---|
| 132 | float2 devicecoords = finalpos.xy / finalpos.w;
|
---|
| 133 |
|
---|
| 134 | // No transform them into texture coordinates [0,1].
|
---|
| 135 | devicecoords = devicecoords * 0.5 + 0.5;
|
---|
| 136 | devicecoords.y = 1.0 - devicecoords.y;
|
---|
| 137 | OUT.DepthBufferCoord = devicecoords;
|
---|
| 138 |
|
---|
| 139 | // Texture coordinate depends on current pattern index and pattern size.
|
---|
| 140 | /*OUT.ColorTextureCoord = IN.texCoord * float4(PatternSize.xy, 0.0, 1.0)
|
---|
| 141 | + float4(CurrentPattern.xy, 0.0, 1.0);*/
|
---|
| 142 | OUT.ColorTextureCoord = IN.texCoord;
|
---|
| 143 |
|
---|
| 144 | return OUT;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 |
|
---|
| 148 | //------------------------------------
|
---|
| 149 | pixelOutput DepthImposterPS(vertexOutput IN)
|
---|
| 150 | {
|
---|
| 151 | pixelOutput OUT;
|
---|
| 152 |
|
---|
| 153 | // Do a lookup in the depth buffer.
|
---|
| 154 | float4 lookup = float4(IN.DepthBufferCoord.xy, 0.0, 1.0);
|
---|
| 155 | float depth = tex2D(g_samplerDepthBuffer, lookup).a;
|
---|
| 156 |
|
---|
| 157 | // Calculate linear depth
|
---|
| 158 | /*float4 transform;
|
---|
| 159 | transform.x = IN.DepthBufferCoord.x * 2.0 - 1.0;
|
---|
| 160 | transform.y = ((1.0 - IN.DepthBufferCoord.y) * 2.0 - 1.0);
|
---|
| 161 | transform.z = depth;
|
---|
| 162 | transform.w = 1.0;
|
---|
| 163 | float4 aftertransform = mul(transform, MatIP);
|
---|
| 164 | aftertransform.xyz /= aftertransform.w;*/
|
---|
| 165 |
|
---|
| 166 | // Calculate visible range of the particle.
|
---|
| 167 | //float difference = abs(aftertransform.z - IN.ParticleDepth);
|
---|
| 168 | float difference = abs(depth - IN.ParticleDepth);
|
---|
| 169 | difference *= ReciprocalParticleSize;
|
---|
| 170 |
|
---|
| 171 | difference = min(difference, 1.0);
|
---|
| 172 | // Multiply texel by input color.
|
---|
| 173 | float4 texturecolor = tex2D(g_samplerCurrentTexture, IN.ColorTextureCoord);
|
---|
| 174 | texturecolor.a *= difference;
|
---|
| 175 | //texturecolor.rgb *= difference;
|
---|
| 176 | //texturecolor.rgb = float3(1, 0, 0);
|
---|
| 177 | OUT.color = texturecolor;// * ParticleColor;
|
---|
| 178 |
|
---|
| 179 | return OUT;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 |
|
---|
| 183 |
|
---|
| 184 | //
|
---|
| 185 | // Effect
|
---|
| 186 | //
|
---|
| 187 |
|
---|
| 188 | #define Technique(name); \
|
---|
| 189 | technique name \
|
---|
| 190 | { \
|
---|
| 191 | pass p0 \
|
---|
| 192 | { \
|
---|
| 193 | VertexShader = compile vs_3_0 name##VS(); \
|
---|
| 194 | PixelShader = compile ps_3_0 name##PS(); \
|
---|
| 195 | } \
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | Technique( DepthImposter );
|
---|
| 199 | Technique( DepthPass );
|
---|