/** * * \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! * * \author Markus Wegmann * **/ struct OneTexelVertex { float4 Position : POSITION; float2 UV : TEXCOORD0; }; //// Vertex Shader //// OneTexelVertex ScreenQuadVS( float3 Position : POSITION, float2 UV : TEXCOORD0 ) { OneTexelVertex OUT = (OneTexelVertex)0; OUT.Position = float4(Position, 1); OUT.UV = float2(UV.xy); return OUT; } //// Fragment Shader //// void godrays_blur( OneTexelVertex IN, out float4 oColor: COLOR, uniform float2 projSunLightPosition, uniform float exposure, uniform float decay, uniform float density, uniform sampler2D decal) { const int NUM_SAMPLES = 35; float2 texCoord = IN.UV; oColor = float4(0,0,0,1); float2 deltaTextCoord = IN.UV - projSunLightPosition; deltaTextCoord *= 1.0f / NUM_SAMPLES * density; float illuminationDecay = 1.0f; for(int i=0; i < NUM_SAMPLES; i++) { texCoord -= deltaTextCoord; float4 sample = tex2D(decal, texCoord); oColor += sample; sample *= illuminationDecay; illuminationDecay *= decay; } oColor *= exposure / NUM_SAMPLES; } void godrays_combine( OneTexelVertex IN, out float4 color : COLOR, uniform sampler2D renderDecal, uniform sampler2D godraysDecal) { color = tex2D(renderDecal, IN.UV) + tex2D(godraysDecal, IN.UV); } /* CgFx based code //// Variables //// ///////////////////////////////////////////////////////////////////////////////////// float Script : STANDARDSGLOBAL < string UIWidget = "none"; string ScriptClass = "scene"; string ScriptOrder = "postprocess"; string ScriptOutput = "color"; string Script = "Technique=Main;"; > = 0.8; float2 ProjSunLightPosition : Position < string UIName = "Sun Light Position"; string Space = "World"; > = {0, 0}; float4 SkyColor < string UIName = "Sky Color"; string UIWidget = "Color"; > = {0f,0f,0f,1.0f}; float GodraysExposure = 1f; float GodraysDecay = 0.1f; float GodraysDensity = 0.7f; texture ScnTarget : RenderColorTarget < float2 ViewPortRatio = {1,1}; int MipLevels = 1; string Format = "X8R8G8B8" ; string UIWidget = "None"; >; sampler2D ScnDecal = sampler_state { Texture = ; WrapS = Repeat; WrapT = Repeat; MinFilter = Linear; MagFilter = Linear; }; texture GodraysTarget : RenderColorTarget < float2 ViewPortRatio = {1,1}; int MipLevels = 1; string Format = "X8R8G8B8" ; string UIWidget = "None"; >; sampler2D GodraysDecal = sampler_state { Texture = ; WrapS = ClampToEdge; WrapT = ClampToEdge; MinFilter = Linear; MagFilter = Linear; }; texture DepthBuffer : RENDERDEPTHSTENCILTARGET < float2 ViewPortRatio = {1,1}; string Format = "D24S8"; string UIWidget = "None"; >; texture LowDepthBuffer : RENDERDEPTHSTENCILTARGET < float2 ViewPortRatio = {1,1}; string Format = "D24S8"; string UIWidget = "None"; >; //// UN-TWEAKABLES - AUTOMATICALLY-TRACKED TRANSFORMS //////////////// float4x4 WorldITXf : WorldInverseTranspose < string UIWidget="None"; >; float4x4 WvpXf : WorldViewProjection < string UIWidget="None"; >; float4x4 VpXf : ViewProjection < string UIWidget="None"; >; float4x4 WorldXf : World < string UIWidget="None"; >; float4x4 ViewIXf : ViewInverse < string UIWidget="None"; >; // Standard full-screen imaging value float ClearDepth < string UIWidget = "None"; > = 1.0; technique Main < string Script = "RenderColorTarget0=ScnTarget;" "RenderDepthStencilTarget=DepthBuffer;" "ClearSetColor=SkyColor;" "ClearSetDepth=ClearDepth;" "Clear=Color;" "Clear=Depth;" "ScriptExternal=color;" "Pass=BlurRaw;" "Pass=CombineRender;"; > { pass BlurRaw < string Script = "RenderColorTarget=GodrayTarget;" "RenderDepthStencilTarget=LowDepthBuffer;" "Draw=Buffer;"; > { DepthTestEnable = false; DepthMask = false; BlendEnable = false; VertexShader = compile vp40 ScreenQuadVS(); FragmentProgram = compile fp40 godrays_blur( ProjSunLightPosition, GodraysExposure, GodraysDecay, GodraysDensity, ScnDecal); } pass CombineRender < string Script = "RenderColorTarget0=;" "RenderDepthStencilTarget=;" "Draw=Buffer;"; > { DepthTestEnable = false; DepthMask = false; BlendEnable = false; VertexShader = compile vp40 ScreenQuadVS(); FragmentProgram = compile fp40 godrays_combine( ScnDecal, GodraysDecal); } } */