Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 12033 was 8357, checked in by marwegma, 14 years ago

Godrays.compositor added, extended Godrays.material, unified the Godrays code a bit.

File size: 4.9 KB
RevLine 
[8258]1/**
2*
[8357]3* \brief Godrays_*.cg is a radial blur based shader implementation of the natural effects of godrays (ray light scattering). The vertex function ScreenQuadVS() skips the default wvp-transform, what is irrelevant in Ogre when doing a "render_quad"-pass... just to say!
[8258]4*
[8346]5* \author Markus Wegmann
[8258]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
27//// Fragment Shader ////
28
[8357]29void godrays_blur(
[8258]30        OneTexelVertex IN,
31               
32        out float4 oColor: COLOR,
33       
[8346]34        uniform float2 projSunLightPosition,
[8258]35        uniform float exposure,
36        uniform float decay,
37        uniform float density,
38        uniform sampler2D decal)
39
40{
[8346]41        const int NUM_SAMPLES = 35;
[8258]42       
43        float2 texCoord = IN.UV;
44       
45       
46        oColor = float4(0,0,0,1);
47       
48    float2 deltaTextCoord = IN.UV - projSunLightPosition;
49    deltaTextCoord *= 1.0f / NUM_SAMPLES * density;
50
51        float illuminationDecay = 1.0f;
52
53    for(int i=0; i < NUM_SAMPLES; i++)
54    {
55        texCoord -= deltaTextCoord;
56        float4 sample = tex2D(decal, texCoord);
57               
58                oColor += sample;
59        sample *= illuminationDecay;
60
61        illuminationDecay *= decay;
62    }
63
64    oColor *= exposure / NUM_SAMPLES;
65}
66
67
[8357]68void godrays_combine(
[8258]69        OneTexelVertex IN,
70
71    out float4 color : COLOR,
72
73    uniform sampler2D renderDecal,
74    uniform sampler2D godraysDecal)
75{
76    color = tex2D(renderDecal, IN.UV) + tex2D(godraysDecal, IN.UV);
77}
78
79
80
81
82/* CgFx based code
83
84//// Variables ////
85/////////////////////////////////////////////////////////////////////////////////////
86
87float Script : STANDARDSGLOBAL <
88    string UIWidget = "none";
89    string ScriptClass = "scene";
90    string ScriptOrder = "postprocess";
91    string ScriptOutput = "color";
92    string Script = "Technique=Main;";
93> = 0.8;
94
[8346]95float2 ProjSunLightPosition : Position <
[8258]96    string UIName =  "Sun Light Position";
97    string Space = "World";
[8346]98> = {0, 0};
[8258]99
100float4 SkyColor <
101    string UIName =  "Sky Color";
102        string UIWidget = "Color";
103> = {0f,0f,0f,1.0f};
104
[8357]105float GodraysExposure = 1f;
106float GodraysDecay = 0.1f;
107float GodraysDensity = 0.7f;
[8258]108
109texture ScnTarget : RenderColorTarget <
110        float2 ViewPortRatio = {1,1};
111    int MipLevels = 1;
112    string Format = "X8R8G8B8" ;
113    string UIWidget = "None";
114>;
115
116sampler2D ScnDecal = sampler_state {
117    Texture = <ScnTarget>;
118        WrapS = Repeat;
119    WrapT = Repeat;
120    MinFilter = Linear;
121    MagFilter = Linear;
122};
123
[8357]124texture GodraysTarget : RenderColorTarget <
[8258]125        float2 ViewPortRatio = {1,1};
126    int MipLevels = 1;
127    string Format = "X8R8G8B8" ;
128    string UIWidget = "None";
129>;
130
[8357]131sampler2D GodraysDecal = sampler_state {
132    Texture = <GodraysTarget>;
[8258]133        WrapS = ClampToEdge;
134    WrapT = ClampToEdge;
135    MinFilter = Linear;
136    MagFilter = Linear;
137};
138
139texture DepthBuffer : RENDERDEPTHSTENCILTARGET <
140    float2 ViewPortRatio = {1,1};
141    string Format = "D24S8";
142    string UIWidget = "None";
143>;
144
145texture LowDepthBuffer : RENDERDEPTHSTENCILTARGET <
146    float2 ViewPortRatio = {1,1};
147    string Format = "D24S8";
148    string UIWidget = "None";
149>;
150//// UN-TWEAKABLES - AUTOMATICALLY-TRACKED TRANSFORMS ////////////////
151
152float4x4 WorldITXf : WorldInverseTranspose < string UIWidget="None"; >;
153float4x4 WvpXf : WorldViewProjection < string UIWidget="None"; >;
154float4x4 VpXf : ViewProjection < string UIWidget="None"; >;
155float4x4 WorldXf : World < string UIWidget="None"; >;
156float4x4 ViewIXf : ViewInverse < string UIWidget="None"; >;
157
158// Standard full-screen imaging value
159
160float ClearDepth <
161    string UIWidget = "None";
162> = 1.0;
163
164technique Main <
165        string Script =
166        "RenderColorTarget0=ScnTarget;"
167        "RenderDepthStencilTarget=DepthBuffer;"
168                "ClearSetColor=SkyColor;"
169                "ClearSetDepth=ClearDepth;"
170                "Clear=Color;"
171                "Clear=Depth;"
172            "ScriptExternal=color;"
173        "Pass=BlurRaw;"
174        "Pass=CombineRender;";
175>
176{
177
178        pass BlurRaw
179        <
180                string Script = "RenderColorTarget=GodrayTarget;"
181                        "RenderDepthStencilTarget=LowDepthBuffer;"
182                        "Draw=Buffer;";
183    >
184        {
185                DepthTestEnable = false;
186                DepthMask = false;
187                BlendEnable = false;
188               
[8346]189                VertexShader = compile vp40 ScreenQuadVS();
[8357]190        FragmentProgram = compile fp40 godrays_blur(
[8346]191            ProjSunLightPosition,
[8357]192                        GodraysExposure,
193                        GodraysDecay,
194                        GodraysDensity,
[8258]195                        ScnDecal);
196        }
197       
198        pass CombineRender
199        <
200                string Script = "RenderColorTarget0=;"
201                        "RenderDepthStencilTarget=;"
202                        "Draw=Buffer;";
203    >
204        {
205                DepthTestEnable = false;
206                DepthMask = false;
207                BlendEnable = false;
208               
209                VertexShader = compile vp40 ScreenQuadVS();
[8357]210        FragmentProgram = compile fp40 godrays_combine(
[8258]211                        ScnDecal,
[8357]212                        GodraysDecal);
[8258]213        }
214}
215
216*/
Note: See TracBrowser for help on using the repository browser.