Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: data/branches/environment/programs/Godrays_blur.cg @ 8258

Last change on this file since 8258 was 8258, checked in by marwegma, 13 years ago

Added Godray Shader Cg-Programs to Programs-Folder.

File size: 5.4 KB
Line 
1/**
2*
3* @brief Godrays_*.cg is a radial blur based shader implementation of the natural effects of godrays (ray light scattering).
4*
5* @author Markus Wegmann
6*
7**/
8
9
10struct OneTexelVertex {
11    float4 Position     : POSITION;
12    float2 UV           : TEXCOORD0;
13};
14
15//// Vertex Shader ////
16
17OneTexelVertex ScreenQuadVS(
18                float3 Position : POSITION,
19                float2 UV       : TEXCOORD0
20) {
21    OneTexelVertex OUT = (OneTexelVertex)0;
22    OUT.Position = float4(Position, 1);
23    OUT.UV = float2(UV.xy);
24    return OUT;
25}
26
27OneTexelVertex ScreenQuadVSWithLightPosition(
28                float3 Position : POSITION,
29                float2 UV       : TEXCOORD0,
30               
31                uniform float4x4 viewProj,
32                uniform float4 sunLightPosition,
33               
34                out float2 projSunLightPosition : TEXCOORD1
35) {
36    OneTexelVertex OUT = (OneTexelVertex)0;
37    OUT.Position = float4(Position, 1);
38    OUT.UV = float2(UV.xy);
39       
40        projSunLightPosition = (mul(viewProj, float4(0,0,0,1))).xy;
41        projSunLightPosition = float2(0.5, 0.5) + projSunLightPosition / 35;
42       
43    return OUT;
44}
45
46//// Fragment Shader ////
47
48void godray_blur(
49        OneTexelVertex IN,
50
51                float2 projSunLightPosition : TEXCOORD1,
52               
53        out float4 oColor: COLOR,
54       
55        uniform float exposure,
56        uniform float decay,
57        uniform float density,
58        uniform sampler2D decal)
59
60{
61        const int NUM_SAMPLES = 36;
62       
63        float2 texCoord = IN.UV;
64       
65       
66        oColor = float4(0,0,0,1);
67       
68    float2 deltaTextCoord = IN.UV - projSunLightPosition;
69    deltaTextCoord *= 1.0f / NUM_SAMPLES * density;
70
71        float illuminationDecay = 1.0f;
72
73    for(int i=0; i < NUM_SAMPLES; i++)
74    {
75        texCoord -= deltaTextCoord;
76        float4 sample = tex2D(decal, texCoord);
77               
78                oColor += sample;
79        sample *= illuminationDecay;
80
81        illuminationDecay *= decay;
82    }
83
84    oColor *= exposure / NUM_SAMPLES;
85}
86
87
88void combineRenderAndGodrays(
89        OneTexelVertex IN,
90
91    out float4 color : COLOR,
92
93    uniform sampler2D renderDecal,
94    uniform sampler2D godraysDecal)
95{
96    color = tex2D(renderDecal, IN.UV) + tex2D(godraysDecal, IN.UV);
97}
98
99
100
101
102/* CgFx based code
103
104//// Variables ////
105/////////////////////////////////////////////////////////////////////////////////////
106
107float Script : STANDARDSGLOBAL <
108    string UIWidget = "none";
109    string ScriptClass = "scene";
110    string ScriptOrder = "postprocess";
111    string ScriptOutput = "color";
112    string Script = "Technique=Main;";
113> = 0.8;
114
115float4 SunLightPosition : Position <
116    string UIName =  "Sun Light Position";
117    string Space = "World";
118> = {0, 0, 0, 1};
119
120float4 SkyColor <
121    string UIName =  "Sky Color";
122        string UIWidget = "Color";
123> = {0f,0f,0f,1.0f};
124
125float GodrayExposure = 1f;
126float GodrayDecay = 0.1f;
127float GodrayDensity = 0.7f;
128
129texture ScnTarget : RenderColorTarget <
130        float2 ViewPortRatio = {1,1};
131    int MipLevels = 1;
132    string Format = "X8R8G8B8" ;
133    string UIWidget = "None";
134>;
135
136sampler2D ScnDecal = sampler_state {
137    Texture = <ScnTarget>;
138        WrapS = Repeat;
139    WrapT = Repeat;
140    MinFilter = Linear;
141    MagFilter = Linear;
142};
143
144texture GodrayTarget : RenderColorTarget <
145        float2 ViewPortRatio = {1,1};
146    int MipLevels = 1;
147    string Format = "X8R8G8B8" ;
148    string UIWidget = "None";
149>;
150
151sampler2D GodrayDecal = sampler_state {
152    Texture = <GodrayTarget>;
153        WrapS = ClampToEdge;
154    WrapT = ClampToEdge;
155    MinFilter = Linear;
156    MagFilter = Linear;
157};
158
159texture DepthBuffer : RENDERDEPTHSTENCILTARGET <
160    float2 ViewPortRatio = {1,1};
161    string Format = "D24S8";
162    string UIWidget = "None";
163>;
164
165texture LowDepthBuffer : RENDERDEPTHSTENCILTARGET <
166    float2 ViewPortRatio = {1,1};
167    string Format = "D24S8";
168    string UIWidget = "None";
169>;
170//// UN-TWEAKABLES - AUTOMATICALLY-TRACKED TRANSFORMS ////////////////
171
172float4x4 WorldITXf : WorldInverseTranspose < string UIWidget="None"; >;
173float4x4 WvpXf : WorldViewProjection < string UIWidget="None"; >;
174float4x4 VpXf : ViewProjection < string UIWidget="None"; >;
175float4x4 WorldXf : World < string UIWidget="None"; >;
176float4x4 ViewIXf : ViewInverse < string UIWidget="None"; >;
177
178// Standard full-screen imaging value
179
180float ClearDepth <
181    string UIWidget = "None";
182> = 1.0;
183
184float2 ViewportSize : VIEWPORTPIXELSIZE <
185    string UIName="Screen Size";
186    string UIWidget="None";
187>;
188
189technique Main <
190        string Script =
191        "RenderColorTarget0=ScnTarget;"
192        "RenderDepthStencilTarget=DepthBuffer;"
193                "ClearSetColor=SkyColor;"
194                "ClearSetDepth=ClearDepth;"
195                "Clear=Color;"
196                "Clear=Depth;"
197            "ScriptExternal=color;"
198        "Pass=BlurRaw;"
199        "Pass=CombineRender;";
200>
201{
202
203        pass BlurRaw
204        <
205                string Script = "RenderColorTarget=GodrayTarget;"
206                        "RenderDepthStencilTarget=LowDepthBuffer;"
207                        "Draw=Buffer;";
208    >
209        {
210                DepthTestEnable = false;
211                DepthMask = false;
212                BlendEnable = false;
213               
214                VertexShader = compile vp40 ScreenQuadVSWithLightPosition(VpXf, SunLightPosition);
215        FragmentProgram = compile fp40 godray_blur(
216                        GodrayExposure,
217                        GodrayDecay,
218                        GodrayDensity,
219                        ScnDecal);
220        }
221       
222        pass CombineRender
223        <
224                string Script = "RenderColorTarget0=;"
225                        "RenderDepthStencilTarget=;"
226                        "Draw=Buffer;";
227    >
228        {
229                DepthTestEnable = false;
230                DepthMask = false;
231                BlendEnable = false;
232               
233                VertexShader = compile vp40 ScreenQuadVS();
234        FragmentProgram = compile fp40 combineRenderAndGodrays(
235                        ScnDecal,
236                        GodrayDecal);
237        }
238}
239
240*/
Note: See TracBrowser for help on using the repository browser.