/************* UN-TWEAKABLES **************/ float4x4 wM : World < string UIWidget="None"; >; float4x4 vM; float4x4 pM; float4x4 vP; half4 inverseParams; float3 inverseOffset; float3 inverseScale; #include "Basic_Utils.cgfx" /*********** Tweakables **********************/ float intensity < string UIWidget = "slider"; float UIMin = 0.0; float UIMax = 50.0; float UIStep = 0.1; > = 1.0; float OffsetU < string UIWidget = "slider"; float UIMin = 0.0; float UIMax = 1.0; float UIStep = 0.01; > = 0.0; float OffsetV < string UIWidget = "slider"; float UIMin = 0.0; float UIMax = 1.0; float UIStep = 0.01; > = 0.0; float ScaleU < string UIWidget = "slider"; float UIMin = -1.0; float UIMax = 2.0; float UIStep = 0.01; > = 1.0; float ScaleV < string UIWidget = "slider"; float UIMin = -1.0; float UIMax = 2.0; float UIStep = 0.01; > = 1.0; /************* Textures ******************/ texture cm : Diffuse < string ResourceType = "2D"; >; sampler2D screenTexture = sampler_state { Texture = ; MinFilter = LinearMipMapLinear; MagFilter = Linear; }; sampler2D depthTexture; texture nm : Normal < string ResourceType = "2D"; >; sampler2D baseBlendMap = sampler_state { Texture = ; MinFilter = LinearMipMapLinear; MagFilter = Linear; }; /************* DATA STRUCTS **************/ /* data from application vertex buffer */ struct appdata { float4 position : POSITION; half2 texCoord0 : TEXCOORD0; half4 tangent : TEXCOORD1; half3 normal : NORMAL; }; /* data passed from vertex shader to pixel shader */ struct vertexOutput { float4 position : POSITION; half4 screenPos : TEXCOORD0; half2 texCoord : TEXCOORD1; half3 normal : TEXCOORD2; half4 tangent : TEXCOORD3; half4 viewPos : TEXCOORD4; }; /*********** vertex program ******/ vertexOutput distortVP(appdata IN) { vertexOutput OUT = (vertexOutput)0; //constants #ifdef _PRAETORIAN_ OUT.texCoord = IN.texCoord0 * float2(ScaleU, ScaleV) + float2(OffsetU, -OffsetV); #else OUT.texCoord = IN.texCoord0 * float2(ScaleU, ScaleV) + float2(OffsetU, OffsetV); #endif //compute matrices float4x4 wvM = mul(vM, wM); float4x4 wVP = mul(pM, wvM); //postion and depth OUT.position = mul(wVP, float4(IN.position.xyz, 1.f)); // BM: Transform normal and tangent into object space, calculate biNormal OUT.normal = mul(wvM, float4(IN.normal, 0.f)).xyz; OUT.tangent.xyz = mul(wvM, float4(IN.tangent.xyz, 0.f)).xyz; OUT.tangent.w = IN.tangent.w; OUT.viewPos = mul(wvM, float4(IN.position.xyz, 1.f)); // we never use blue anyway... OUT.screenPos = OUT.position; // clamp z values of pixels to far plane to prevent clipping OUT.position.z = min(OUT.position.z, OUT.position.w); return OUT; } /********* fragment program ********/ void distortFP(vertexOutput IN, out half4 colour : COLOR0) { IN.screenPos /= IN.screenPos.w; half2 screenCoord = (IN.screenPos.xy + 1.f.xx) * 0.5h.xx; IN.screenPos.y = -IN.screenPos.y; half3 view = normalize(IN.viewPos.xyz); // Get Depth of scene // const float3 depthFactor = float3(65536.0f/16777215.0f, 256.0f/16777215.0f, 1.0f/16777215.0f ); // float4 RawDepth = tex2D(depthTexture, IN.texCoord); // float Depth = dot(round( RawDepth.xyz * 255.0f.xxx ), depthFactor.xyz); // float ScreenDepth = (Depth + inverseOffset.z) * inverseScale.z; // half viewDepth = -inverseParams.w / (ScreenDepth + inverseParams.z); // viewDepth = IN.viewPos.z - viewDepth; // Get Normal half4 normalBlend = tex2D(baseBlendMap, IN.texCoord.xy); half3 nM; nM.xy = normalBlend.zw; half3 binormal = GetBiNormal(IN.tangent.xyz, IN.normal) * IN.tangent.w; //generate normal and world normal nM.xy -= 0.5f.xx; nM.xy *= 2.f.xx; nM = DecodeNormal(nM.xy); nM = normalize(nM.x * IN.tangent.xyz + nM.y * binormal + nM.z*IN.normal); // half3 reflection = normalize(refract(view, nM, 0.1f)); //http://developer.nvidia.com/object/cube_map_ogl_tutorial.html // float2 cameraCoord = float2((-reflection.x/abs(reflection.z) +1)/2, (-reflection.y/abs(reflection.z) +1)/2); //cameraCoord.x = 1.f - cameraCoord.x; //colour = tex2D(screenTexture, screenCoord - (cameraCoord.xy * 5.f * float2(1.f/1280.f, 1.f/720.f))); colour = tex2D(screenTexture, screenCoord - (nM.xy * intensity * float2(1.f/1280.f, 1.f/720.f))); //float4 refractCoord = float4(IN.viewPos + (reflection.xyz * viewDepth * 0.1f), 1.f); //refractCoord = mul(pM, float4(refractCoord.xyz, 1.f)); //refractCoord /= refractCoord.w; //refractCoord.xy = (refractCoord.xy + 1.f.xx) * 0.5h.xx; //colour = tex2D(screenTexture, refractCoord.xy); } technique basic < string layer = "Distortion" ; > { pass p2 { VertexProgram = compile vp30 distortVP(); FragmentProgram = compile fp40 distortFP(); DepthTestEnable = true; DepthMask = false; //CullFaceEnable = true; //CullFace = Front ; } }