/************* UN-TWEAKABLES **************/ float4x4 vP : ViewProjection < string UIWidget="None"; >; float4x4 wM : World; float4x4 vIT : ViewInverseTranspose; float4x4 vM : View; float4x4 pM : Projection; float4 inverseParams; #include "Basic_Utils.cgfx" /*********** Tweakables **********************/ float refractInd < string UIWidget = "slider"; float UIMin = 0.0; float UIMax = 1.0; float UIStep = 0.01; > = 0.9; float bumpScale < string UIWidget = "slider"; float UIMin = 0.0; float UIMax = 5.0; float UIStep = 0.01; > = 1; float uvTileBase < string UIWidget = "slider"; float UIMin = 0; float UIMax = 8.0; float UIStep = 0.10; > = 1.0; /************ Animation ***************/ /************ 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; float2 texCoord : TEXCOORD0; float3 normal : NORMAL0; float4 tangent : TEXCOORD1; }; /* data passed from vertex shader to pixel shader */ struct vertexOutput { float4 position : POSITION; half4 screenPos : TEXCOORD0; half3 texCoord : TEXCOORD1; half3 normal : TEXCOORD2; half4 tangent : TEXCOORD3; half4 viewPos : TEXCOORD4; }; /*********** vertex program ******/ vertexOutput simpleVP(appdata IN) { vertexOutput OUT = (vertexOutput)0; OUT.texCoord.xy = IN.texCoord * uvTileBase; //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 simpleFP(vertexOutput IN, out float4 colour : COLOR0) { IN.screenPos /= IN.screenPos.w; half2 screenCoord = (IN.screenPos.xy + 1.f.xx) * 0.5h.xx; IN.screenPos.y = -IN.screenPos.y; float viewDepth = inverseParams.w / ((IN.screenPos.z/IN.screenPos.w) + inverseParams.z); half3 view = normalize(IN.viewPos.xyz); 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); float sampleDepth = -abs(tex2D(baseBlendMap, screenCoord).a); float refractDist = -(sampleDepth - viewDepth); half3 refraction = normalize(refract(view, nM, refractInd)); float4 refractCoord = float4(IN.viewPos.xyz + (refraction.xyz * refractDist), 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);; float sampleDepth2 = -abs(tex2D(baseBlendMap, refractCoord.xy).a); //if(abs(viewDepth) > abs(sampleDepth2)) // colour = tex2D(screenTexture, screenCoord.xy); } /*************/ technique main < string layer = "Distortion" ; > { pass p0 { VertexProgram = compile vp30 simpleVP(); FragmentProgram = compile fp40 simpleFP(); DepthTestEnable = true; DepthMask = false; BlendEnable = false; } } /***************************** eof ***/