source: GTP/trunk/App/Demos/Illum/Ogre/Media/materials/GTPParticles/GTP_Sprite.hlsl @ 2310

Revision 2310, 4.4 KB checked in by szirmay, 17 years ago (diff)
Line 
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////////
8struct VS_OUT
9{
10        float4 hPosition        : POSITION;
11        float4 texCoord         : TEXCOORD0;
12        float4 color            : COLOR0;
13};
14
15VS_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
39float4 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//////////
52struct 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
63SBB_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
98float4 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
Note: See TracBrowser for help on using the repository browser.