/************* UN-TWEAKABLES **************/ float4x4 ipM; float4x4 v2l; float4x4 v2l1; float4x4 v2l2; float4x4 v2l3; float3 planeLimits; half4 inverseParams; #include "..\\Basic_Utils.cgfx" #include "LightUtils.cgfx" /*********** Tweakables **********************/ float3 lightDirection : Direction < string Object = "DirectionalLight0"; string Space = "World"; > = {100.0f, 100.0f, 100.0f}; float3 lightColour : Diffuse < string UIWidget = "Color"; > = {1.0f, 1.0f, 1.0f}; /************* Textures ******************/ texture cm : Diffuse < string ResourceType = "2D"; >; sampler2D colourTexture = sampler_state { Texture = ; MinFilter = LinearMipMapLinear; MagFilter = Linear; }; texture nm : Normal < string ResourceType = "2D"; >; sampler2D normalTexture = sampler_state { Texture = ; MinFilter = LinearMipMapLinear; MagFilter = Linear; }; texture dm : Diffuse < string ResourceType = "2D"; >; sampler2D depthTexture = sampler_state { Texture = ; MinFilter = LinearMipMapLinear; MagFilter = Linear; }; /************* DATA STRUCTS **************/ /* data from application vertex buffer */ struct appdata { float4 position : POSITION; half2 texCoord0 : TEXCOORD0; }; /* data passed from vertex shader to pixel shader */ struct vertexOutput { float4 oPosition : POSITION; //half2 oTexCoord : TEXCOORD0; half4 oScreenPos : TEXCOORD0; half3 oViewPos : TEXCOORD1; half3 oViewPosNorm: TEXCOORD2; }; /*********** vertex program ******/ vertexOutput simpleVP(appdata IN) { vertexOutput OUT = (vertexOutput)0; OUT.oPosition = float4((IN.position.xy - 0.5f.xx) * 2.f.xx, -1.f, 1.f); OUT.oScreenPos.x = OUT.oPosition.x; OUT.oScreenPos.y = -OUT.oPosition.y; half4 viewPos = mul(ipM, OUT.oPosition); viewPos /= viewPos.w; OUT.oViewPos = viewPos.xyz; OUT.oViewPosNorm = normalize(viewPos.xyz); OUT.oScreenPos.xy *= -inverseParams.xy; OUT.oScreenPos.zw = IN.position.xy; return OUT; } /********* fragment program ********/ void simpleFP(vertexOutput IN, out half4 colour : COLOR0) { half4 nm = h4tex2D(normalTexture, IN.oScreenPos.zw); half3 view = IN.oViewPosNorm; half3 normal = DecodeNormal(nm.xy); normal.z *= (sign(nm.a) * -1.0h); // If you want to use nm.a then uncomment below (we do!) nm.a = abs(nm.a); nm.a *= -1.0h; half3 viewPos = half3(IN.oScreenPos.x, IN.oScreenPos.y, 1.f) * nm.a; //half4 litRelPos = mul(v2l, half4(viewPos, 1.h)); half4 litRelPos = GetShadowCoord4Cascades(planeLimits, v2l, v2l1, v2l2, v2l3, float4(viewPos, 1.f)); half shadowed = GetShadow(litRelPos); half3 texColour = h3tex2D(colourTexture, IN.oScreenPos.zw).rgb; half specularExp; half specularInt = modf(nm.b, specularExp); half3 litVec = getLitVec(-lightDirection, normal, view, specularExp); half3 diffuse = litVec.y * lightColour * texColour.rgb; half specColour = litVec.z * specularInt; colour = half4((diffuse + specColour.xxx) * shadowed.xxx, 1.f); // Encode to RGBdiv8 colour = colour/max(max(colour.r, colour.g), max(colour.b, 1.1h)); } void simpleFP_2x2PCF(vertexOutput IN, out half4 colour : COLOR0) { half4 nm = h4tex2D(normalTexture, IN.oScreenPos.zw); half3 view = IN.oViewPosNorm; half3 normal = DecodeNormal(nm.xy); // If you want to use nm.a then uncomment below (we do!) nm.a = abs(nm.a); nm.a *= -1.0h; half3 viewPos = half3(IN.oScreenPos.x, IN.oScreenPos.y, 1.f) * nm.a; half4 litRelPos = GetShadowCoord4Cascades(planeLimits, v2l, v2l1, v2l2, v2l3, half4(viewPos, 1.f)); half shadowed = GetShadow_2x2PCF(litRelPos); half3 texColour = h3tex2D(colourTexture, IN.oScreenPos.zw).rgb; half specularExp; half specularInt = modf(nm.b, specularExp); half3 litVec = getLitVec(-lightDirection, normal, view, specularExp); half3 diffuse = litVec.y * lightColour * texColour.rgb; half specColour = litVec.z * specularInt; colour = half4((diffuse + specColour.xxx) * shadowed.xxx, 1.f); // Encode to RGBdiv8 colour = colour/max(max(colour.r, colour.g), max(colour.b, 1.1h)); } void simpleFP_3x3PCF(vertexOutput IN, out half4 colour : COLOR0) { half4 nm = h4tex2D(normalTexture, IN.oScreenPos.zw); half3 view = IN.oViewPosNorm; half3 normal = DecodeNormal(nm.xy); // If you want to use nm.a then uncomment below (we do!) nm.a = abs(nm.a); nm.a *= -1.0h; half3 viewPos = half3(IN.oScreenPos.x, IN.oScreenPos.y, 1.f) * nm.a; half4 litRelPos = GetShadowCoord4Cascades(planeLimits, v2l, v2l1, v2l2, v2l3, half4(viewPos, 1.f)); half shadowed = GetShadow_3x3PCF(litRelPos); half3 texColour = h3tex2D(colourTexture, IN.oScreenPos.zw).rgb; half specularExp; half specularInt = modf(nm.b, specularExp); half3 litVec = getLitVec(-lightDirection, normal, view, specularExp); half3 diffuse = litVec.y * lightColour * texColour.rgb; half specColour = litVec.z * specularInt; colour = half4((diffuse + specColour.xxx) * shadowed.xxx, 1.f); // Encode to RGBdiv8 colour = colour/max(max(colour.r, colour.g), max(colour.b, 1.1h)); } /*************/ technique basic { pass p0 { VertexProgram = compile vp30 simpleVP(); FragmentProgram = compile fp40 simpleFP(); DepthTestEnable = false; DepthMask = false; BlendEnable = true; BlendFuncSeparate = int4( DstAlpha , SrcAlpha , DstAlpha , Zero ); } } technique PCF2x2 { pass p0 { VertexProgram = compile vp30 simpleVP(); FragmentProgram = compile fp40 simpleFP_2x2PCF(); DepthTestEnable = false; DepthMask = false; BlendEnable = true; BlendFuncSeparate = int4( DstAlpha , SrcAlpha , DstAlpha , Zero ); } } technique PCF3x3 { pass p0 { VertexProgram = compile vp30 simpleVP(); FragmentProgram = compile fp40 simpleFP_3x3PCF(); DepthTestEnable = false; DepthMask = false; BlendEnable = true; BlendFuncSeparate = int4( DstAlpha , SrcAlpha , DstAlpha , Zero ); } } /***************************** eof ***/