Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: data/contentcreation/pps/MarkusWegmann/Godray Shader Proof of Concept/Godrays_blur.cgfx @ 9158

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

My Stuff

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