[3255] | 1 | /////////
|
---|
| 2 | ///This example shader demonstrates how to use the sprite particle type added by the GTP to OGRE's particle types.
|
---|
| 3 | ///These sprites are quads with zero size, they should be scaled and rotated towards the camera by the vertex shader.
|
---|
| 4 | ///All four vertices of the quad are placed in the center of the particle, the z and w components of their texture coordinates
|
---|
| 5 | ///gives us the offset they should be moved in camera space (these components store the corner direction multiplied by the particle size).
|
---|
| 6 | ///The x and y components of the texture coordinates store the defult uv for texturing.
|
---|
| 7 | ////////
|
---|
| 8 | struct VS_OUT
|
---|
| 9 | {
|
---|
| 10 | float4 hPosition : POSITION;
|
---|
| 11 | float4 texCoord : TEXCOORD0;
|
---|
| 12 | float4 color : COLOR0;
|
---|
| 13 | };
|
---|
| 14 |
|
---|
| 15 | VS_OUT Sprite_VS(float4 position : POSITION,
|
---|
| 16 | float4 texCoord : TEXCOORD0,
|
---|
| 17 | float4 color : COLOR0,
|
---|
| 18 | uniform float4x4 worldView,
|
---|
| 19 | uniform float4x4 Proj)
|
---|
| 20 | {
|
---|
| 21 | VS_OUT OUT;
|
---|
| 22 | ///transform to camera space and create a sprite with vertex offset
|
---|
| 23 | //texCoord.y = 1.0 - texCoord.y;
|
---|
| 24 | float2 offset = texCoord.zw;
|
---|
| 25 | float4 cPosition = mul(worldView, position);
|
---|
| 26 | cPosition.xy += offset;
|
---|
| 27 |
|
---|
| 28 | // cPosition = float4(100*texCoord.xy,0,1);
|
---|
| 29 | ///projection
|
---|
| 30 | OUT.hPosition = mul( Proj, cPosition );
|
---|
| 31 | OUT.texCoord = texCoord;
|
---|
| 32 | // OUT.color = float4(offset,1,1.0);
|
---|
| 33 | OUT.color = color;
|
---|
| 34 |
|
---|
| 35 | return OUT;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | ///Simple texture mapping
|
---|
| 39 | float4 Sprite_PS(VS_OUT IN ,
|
---|
| 40 | uniform sampler2D colorTexture : register(s0)):COLOR
|
---|
| 41 | {
|
---|
| 42 | return 1;
|
---|
| 43 | return tex2D( colorTexture, IN.texCoord.xy) * IN.color;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | //////////
|
---|
| 47 | ///These shaders uses the sprite particle type. They demonstrate the "spherical billboards" (SBB) particle rendering method.
|
---|
| 48 | ///SBB alter the opacity of a particle according to the objects of the scene it intersects. To find out the correct opacity
|
---|
| 49 | ///we store scene depth values in a previous step, we assume that the particle is sphere shaped and find the length of the viewray
|
---|
| 50 | ///that travells inside a particle before reaching an object.
|
---|
| 51 | //////////
|
---|
| 52 | struct SBB_VS_OUT
|
---|
| 53 | {
|
---|
| 54 | float4 hPosition : POSITION;
|
---|
| 55 | float4 texCoord : TEXCOORD0;
|
---|
| 56 | float3 P : TEXCOORD1;
|
---|
| 57 | float3 Q : TEXCOORD2;
|
---|
| 58 | float r : TEXCOORD3;
|
---|
| 59 | float2 screenCoord : TEXCOORD4;
|
---|
| 60 | float4 color : COLOR0;
|
---|
| 61 | };
|
---|
| 62 |
|
---|
| 63 | SBB_VS_OUT SBB_Sprite_VS (float4 position : POSITION,
|
---|
| 64 | float4 texCoord : TEXCOORD0,
|
---|
| 65 | float4 color : COLOR,
|
---|
| 66 | uniform float width,
|
---|
| 67 | uniform float height,
|
---|
| 68 | uniform float4x4 worldView,
|
---|
| 69 | uniform float4x4 Proj)
|
---|
| 70 | {
|
---|
| 71 | SBB_VS_OUT OUT;
|
---|
| 72 | ///transform to camera space and create a sprite with vertex offset
|
---|
| 73 | float2 offset = texCoord.zw;
|
---|
| 74 | float4 cPosition;
|
---|
| 75 | float4 wPosition = position;
|
---|
| 76 | cPosition = mul(worldView, wPosition);
|
---|
| 77 | /// P is the particle sphere center
|
---|
| 78 | OUT.P = cPosition.xyz;
|
---|
| 79 | OUT.P.z = - 1 * OUT.P.z;
|
---|
| 80 | cPosition.xy += offset;
|
---|
| 81 | /// Q is the shaded point (it is moved backwards to avoid unwanted frontplane clipping)
|
---|
| 82 | OUT.Q = cPosition.xyz;
|
---|
| 83 | OUT.Q.z = OUT.P.z;
|
---|
| 84 | OUT.r = abs(texCoord.z);
|
---|
| 85 | /// calculate screen space position
|
---|
| 86 | OUT.hPosition = mul( Proj, cPosition );
|
---|
| 87 | OUT.screenCoord = (OUT.hPosition.xy / OUT.hPosition.w + 1.0) / 2.0;
|
---|
| 88 | OUT.screenCoord.y = 1.0 - OUT.screenCoord.y;
|
---|
| 89 | OUT.screenCoord += float2(0.5/width, 0.5/height);
|
---|
| 90 |
|
---|
| 91 | OUT.texCoord = texCoord;
|
---|
| 92 | OUT.color = color;
|
---|
| 93 |
|
---|
| 94 | return OUT;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 |
|
---|
| 98 | float4 SBB_Sprite_PS(SBB_VS_OUT IN ,
|
---|
| 99 | // in screenCoord : VPOS,
|
---|
| 100 | uniform float nearplane,
|
---|
| 101 | uniform float farplane,
|
---|
| 102 | uniform float4 color,
|
---|
| 103 | uniform sampler2D colorTexture : register(s0),
|
---|
| 104 | uniform sampler2D DepthMap : register(s1),
|
---|
| 105 | uniform sampler2D PlanckMap : register(s2)
|
---|
| 106 | ) : COLOR
|
---|
| 107 | {
|
---|
| 108 | float4 Color = IN.color;
|
---|
| 109 | float alpha = 0;
|
---|
| 110 |
|
---|
| 111 | /// get the depth values from the depthMap and calculate ray length in sphere
|
---|
| 112 | float d = length( IN.Q - IN.P );
|
---|
| 113 | float Zs;
|
---|
| 114 | if( d < IN.r )
|
---|
| 115 | {
|
---|
| 116 |
|
---|
| 117 | float w = sqrt( IN.r * IN.r - d * d );
|
---|
| 118 | alpha = w / IN.r;
|
---|
| 119 | alpha *= pow( (IN.r-d) / IN.r , 2);
|
---|
| 120 |
|
---|
| 121 | float F = IN.Q.z - w;
|
---|
| 122 | float B = IN.Q.z + w;
|
---|
| 123 |
|
---|
| 124 |
|
---|
| 125 | Zs = tex2D( DepthMap, IN.screenCoord ).r;
|
---|
| 126 | if(Zs == 0) Zs = farplane;
|
---|
| 127 | float ds = min( Zs, B ) - max( nearplane, F );
|
---|
| 128 | // float ds = min( Zs, B ) - F;
|
---|
| 129 | alpha *= ds / w * 0.5;
|
---|
| 130 |
|
---|
| 131 |
|
---|
| 132 | }
|
---|
| 133 | /// fetch opacity from a texture
|
---|
| 134 | Color.a *= tex2D( colorTexture, IN.texCoord.xy).r ;
|
---|
| 135 | //Color.a *= IN.color.a;
|
---|
| 136 | Color.a *= alpha;
|
---|
| 137 | /// address a color map (colors of fire eg.) with the alpha
|
---|
| 138 | //Color.rgb = tex2D( PlanckMap, Color.aa).rgb;
|
---|
| 139 |
|
---|
| 140 | return Color * color;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | struct SBB_ILLUM_VS_OUT
|
---|
| 144 | {
|
---|
| 145 | float4 hPosition : POSITION;
|
---|
| 146 | float4 texCoord : TEXCOORD0;
|
---|
| 147 | float3 P : TEXCOORD1;
|
---|
| 148 | float3 Q : TEXCOORD2;
|
---|
| 149 | float r : TEXCOORD3;
|
---|
| 150 | float2 screenCoord : TEXCOORD4;
|
---|
| 151 | float4 lightCoord : TEXCOORD5;
|
---|
| 152 | float4 color : COLOR0;
|
---|
| 153 | };
|
---|
| 154 |
|
---|
| 155 | SBB_ILLUM_VS_OUT SBB_Sprite_Illum_VS (float4 position : POSITION,
|
---|
| 156 | float4 texCoord : TEXCOORD0,
|
---|
| 157 | float4 color : COLOR,
|
---|
| 158 | uniform float width,
|
---|
| 159 | uniform float height,
|
---|
| 160 | uniform float4x4 worldView,
|
---|
| 161 | uniform float4x4 Proj,
|
---|
| 162 | uniform float4x4 worldViewInv,
|
---|
| 163 | uniform float4x4 lightViewProj)
|
---|
| 164 | {
|
---|
| 165 | SBB_ILLUM_VS_OUT OUT;
|
---|
| 166 | ///transform to camera space and create a sprite with vertex offset
|
---|
| 167 | float2 offset = texCoord.zw;
|
---|
| 168 | float4 cPosition;
|
---|
| 169 | float4 wPosition = position;
|
---|
| 170 | cPosition = mul(worldView, wPosition);
|
---|
| 171 | /// P is the particle sphere center
|
---|
| 172 | OUT.P = cPosition.xyz;
|
---|
| 173 | OUT.P.z = - 1 * OUT.P.z;
|
---|
| 174 | cPosition.xy += offset;
|
---|
| 175 | /// Q is the shaded point (it is moved backwards to avoid unwanted frontplane clipping)
|
---|
| 176 | OUT.Q = cPosition.xyz;
|
---|
| 177 | OUT.Q.z = OUT.P.z;
|
---|
| 178 | OUT.r = abs(texCoord.z);
|
---|
| 179 | /// calculate screen space position
|
---|
| 180 | OUT.hPosition = mul( Proj, cPosition );
|
---|
| 181 | OUT.screenCoord = (OUT.hPosition.xy / OUT.hPosition.w + 1.0) / 2.0;
|
---|
| 182 | OUT.screenCoord.y = 1.0 - OUT.screenCoord.y;
|
---|
| 183 | OUT.screenCoord += float2(0.5/width, 0.5/height);
|
---|
| 184 |
|
---|
| 185 | OUT.texCoord = texCoord;
|
---|
| 186 | OUT.color = color;
|
---|
| 187 |
|
---|
| 188 | wPosition = mul(worldViewInv, cPosition);
|
---|
| 189 | OUT.lightCoord = mul(lightViewProj, wPosition);
|
---|
| 190 |
|
---|
| 191 | return OUT;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 |
|
---|
| 195 | float4 SBB_Sprite_Illum_PS(SBB_ILLUM_VS_OUT IN ,
|
---|
| 196 | // in screenCoord : VPOS,
|
---|
| 197 | uniform float nearplane,
|
---|
| 198 | uniform float farplane,
|
---|
| 199 | uniform float4 color,
|
---|
| 200 | uniform sampler2D colorTexture : register(s0),
|
---|
| 201 | uniform sampler2D DepthMap : register(s1),
|
---|
| 202 | uniform sampler2D illumVolume : register(s2)
|
---|
| 203 | ) : COLOR
|
---|
| 204 | {
|
---|
| 205 | ///identify light volume slices and interpolation
|
---|
| 206 | float2 lightCoord;
|
---|
| 207 | lightCoord = (IN.lightCoord.xy + float2(1.0, 1.0)) / 2.0;
|
---|
| 208 | lightCoord.y = 1.0 - lightCoord.y;
|
---|
| 209 | float z = IN.lightCoord.z / IN.lightCoord.w;
|
---|
| 210 |
|
---|
| 211 | float4 extintion = tex2D(illumVolume, lightCoord);
|
---|
| 212 |
|
---|
| 213 | float intensities[5];
|
---|
| 214 | intensities[0] = 1.0;
|
---|
| 215 | intensities[1] = extintion.r;
|
---|
| 216 | intensities[2] = extintion.g;
|
---|
| 217 | intensities[3] = extintion.b;
|
---|
| 218 | intensities[4] = extintion.a;
|
---|
| 219 |
|
---|
| 220 | float3 start;
|
---|
| 221 | float3 end;
|
---|
| 222 | float3 temp = 1.0;
|
---|
| 223 | float t;
|
---|
| 224 |
|
---|
| 225 | float4 planes = float4(0.33, 0.5, 0.66, 1);
|
---|
| 226 | if(z < planes.x)
|
---|
| 227 | {
|
---|
| 228 | start = intensities[0];
|
---|
| 229 | end = intensities[1];
|
---|
| 230 | t = z / planes.x;
|
---|
| 231 | temp = lerp(start, end, t);
|
---|
| 232 | }
|
---|
| 233 | if(z > planes.x && z < planes.y)
|
---|
| 234 | {
|
---|
| 235 | start = intensities[1];
|
---|
| 236 | end = intensities[2];
|
---|
| 237 | t = (z - planes.x) / (planes.y - planes.x);
|
---|
| 238 | temp = lerp(start, end, t);
|
---|
| 239 | }
|
---|
| 240 | if(z > planes.y && z < planes.z)
|
---|
| 241 | {
|
---|
| 242 | start = intensities[2];
|
---|
| 243 | end = intensities[3];
|
---|
| 244 | t = (z - planes.y) / (planes.z - planes.y);
|
---|
| 245 | temp = lerp(start, end, t);
|
---|
| 246 | }
|
---|
| 247 | if(z > planes.z)
|
---|
| 248 | {
|
---|
| 249 | start = intensities[3];
|
---|
| 250 | end = intensities[4];
|
---|
| 251 | t = (z - planes.z) / (planes.a - planes.z);
|
---|
| 252 | temp = lerp(start, end, t);
|
---|
| 253 | }
|
---|
| 254 | IN.color.rgb *= temp;
|
---|
| 255 | ///////////////
|
---|
| 256 | float4 Color = IN.color;
|
---|
| 257 | float alpha = 0;
|
---|
| 258 |
|
---|
| 259 | /// get the depth values from the depthMap and calculate ray length in sphere
|
---|
| 260 | float d = length( IN.Q - IN.P );
|
---|
| 261 | float Zs;
|
---|
| 262 | if( d < IN.r )
|
---|
| 263 | {
|
---|
| 264 |
|
---|
| 265 | float w = sqrt( IN.r * IN.r - d * d );
|
---|
| 266 | alpha = w / IN.r;
|
---|
| 267 | alpha *= pow( (IN.r-d) / IN.r , 2);
|
---|
| 268 |
|
---|
| 269 | float F = IN.Q.z - w;
|
---|
| 270 | float B = IN.Q.z + w;
|
---|
| 271 |
|
---|
| 272 |
|
---|
| 273 | Zs = tex2D( DepthMap, IN.screenCoord ).r;
|
---|
| 274 | if(Zs == 0) Zs = farplane;
|
---|
| 275 | float ds = min( Zs, B ) - max( nearplane, F );
|
---|
| 276 | // float ds = min( Zs, B ) - F;
|
---|
| 277 | alpha *= ds / w * 0.5;
|
---|
| 278 |
|
---|
| 279 |
|
---|
| 280 | }
|
---|
| 281 | /// fetch opacity from a texture
|
---|
| 282 | Color.a *= tex2D( colorTexture, IN.texCoord.xy).r ;
|
---|
| 283 | //Color.a *= IN.color.a;
|
---|
| 284 | Color.a *= alpha;
|
---|
| 285 | /// address a color map (colors of fire eg.) with the alpha
|
---|
| 286 | //Color.rgb = tex2D( PlanckMap, Color.aa).rgb;
|
---|
| 287 |
|
---|
| 288 | return Color * color;
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 |
|
---|