[2376] | 1 | struct SBB_VS_OUT
|
---|
| 2 | {
|
---|
| 3 | float4 hPosition : POSITION;
|
---|
| 4 | float4 texCoord : TEXCOORD0;
|
---|
| 5 | float3 P : TEXCOORD1;
|
---|
| 6 | float3 Q : TEXCOORD2;
|
---|
| 7 | float r : TEXCOORD3;
|
---|
| 8 | float2 screenCoord : TEXCOORD4;
|
---|
| 9 | float4 color : COLOR0;
|
---|
| 10 | };
|
---|
| 11 |
|
---|
| 12 | SBB_VS_OUT MoriaDust_VS (float4 position : POSITION,
|
---|
| 13 | float4 texCoord : TEXCOORD0,
|
---|
| 14 | float4 color : COLOR,
|
---|
| 15 | uniform float width,
|
---|
| 16 | uniform float height,
|
---|
| 17 | uniform float4x4 worldView,
|
---|
| 18 | uniform float4x4 Proj)
|
---|
| 19 | {
|
---|
| 20 | SBB_VS_OUT OUT;
|
---|
| 21 | ///transform to camera space and create a sprite with vertex offset
|
---|
| 22 | float2 offset = texCoord.zw;
|
---|
| 23 | float4 cPosition;
|
---|
| 24 | float4 wPosition = position;
|
---|
| 25 | cPosition = mul(worldView, wPosition);
|
---|
| 26 | /// P is the particle sphere center
|
---|
| 27 | OUT.P = cPosition.xyz;
|
---|
| 28 | OUT.P.z = - 1 * OUT.P.z;
|
---|
| 29 | cPosition.xy += offset;
|
---|
| 30 | /// Q is the shaded point (it is moved backwards to avoid unwanted frontplane clipping)
|
---|
| 31 | OUT.Q = cPosition.xyz;
|
---|
| 32 | OUT.Q.z = OUT.P.z;
|
---|
| 33 | OUT.r = abs(texCoord.z);
|
---|
| 34 | /// calculate screen space position
|
---|
| 35 | OUT.hPosition = mul( Proj, cPosition );
|
---|
| 36 | OUT.screenCoord = (OUT.hPosition.xy / OUT.hPosition.w + 1.0) / 2.0;
|
---|
| 37 | OUT.screenCoord.y = 1.0 - OUT.screenCoord.y;
|
---|
| 38 | OUT.screenCoord += float2(0.5/width, 0.5/height);
|
---|
| 39 |
|
---|
| 40 | OUT.texCoord = texCoord;
|
---|
| 41 | OUT.color = color;
|
---|
| 42 |
|
---|
| 43 | return OUT;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 |
|
---|
| 47 | float4 MoriaDust_PS(SBB_VS_OUT IN ,
|
---|
| 48 | // in screenCoord : VPOS,
|
---|
| 49 | uniform float nearplane,
|
---|
| 50 | uniform float farplane,
|
---|
| 51 | uniform float4 lightCPos,
|
---|
| 52 | uniform float4 color,
|
---|
| 53 | uniform float symmetry,
|
---|
| 54 | uniform sampler2D colorTexture : register(s0),
|
---|
| 55 | uniform sampler2D DepthMap : register(s1),
|
---|
| 56 | uniform sampler2D PhaseMap : register(s2)
|
---|
| 57 | ) : COLOR
|
---|
| 58 | {
|
---|
| 59 | float4 Color = IN.color * color;
|
---|
| 60 | float alpha = 0;
|
---|
| 61 |
|
---|
| 62 | /// get the depth values from the depthMap and calculate ray length in sphere
|
---|
| 63 | float d = length( IN.Q - IN.P );
|
---|
| 64 | float Zs;
|
---|
| 65 | if( d < IN.r )
|
---|
| 66 | {
|
---|
| 67 |
|
---|
| 68 | float w = sqrt( IN.r * IN.r - d * d );
|
---|
| 69 | alpha = w / IN.r;
|
---|
| 70 | alpha *= pow( (IN.r-d) / IN.r , 2);
|
---|
| 71 |
|
---|
| 72 | float F = IN.Q.z - w;
|
---|
| 73 | float B = IN.Q.z + w;
|
---|
| 74 |
|
---|
| 75 |
|
---|
| 76 | Zs = tex2D( DepthMap, IN.screenCoord ).r;
|
---|
| 77 | if(Zs == 0) Zs = farplane;
|
---|
| 78 | float ds = min( Zs, B ) - max( nearplane, F );
|
---|
| 79 | // float ds = min( Zs, B ) - F;
|
---|
| 80 | alpha *= ds / w * 0.5;
|
---|
| 81 | }
|
---|
| 82 | /// fetch opacity from a texture
|
---|
| 83 | Color.a *= tex2D( colorTexture, IN.texCoord.xy).r;
|
---|
| 84 | Color.a *= alpha;
|
---|
| 85 |
|
---|
| 86 | //illumination
|
---|
| 87 | float3 L = normalize(lightCPos - IN.Q * lightCPos.w);
|
---|
| 88 | float3 V = normalize(-IN.Q);
|
---|
| 89 | float phase = tex2D(PhaseMap, float2(symmetry, dot(L,V)) * 0.5 + 0.5).r;
|
---|
| 90 | // float phase = (dot(L, V) + 1.0) * 0.5;
|
---|
| 91 | Color.rgb *= phase;
|
---|
| 92 | return Color;
|
---|
| 93 | }
|
---|