/************* UN-TWEAKABLES **************/ float4x4 wVP : WorldViewProjection < string UIWidget="None"; >; float4x4 vIT : ViewInverseTranspose < string UIWidget="None"; >; float4x4 wM : World < string UIWidget="None"; >; float4x4 ipM; float4x4 ivM; float4x4 v2l; float4x4 pM; float4x4 vM; float4 inverseParams; float3 inverseOffset; float3 inverseScale; #include "y:\\art\\shaders\\Basic_Utils.cgfx" #include "LightUtils.cgfx" /*********** Tweakables **********************/ float3 lightPosition : Position < string Object = "PointLight0"; 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; }; /*********** 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.oScreenPos.xy *= -inverseParams.xy; OUT.oScreenPos.zw = IN.position.xy; return OUT; } /********* fragment program ********/ void simpleFP(vertexOutput IN, out float4 colour : COLOR0) { half4 nm = tex2D(normalTexture, IN.oScreenPos.zw); float3 view = normalize(IN.oViewPos.xyz); 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 = mul(v2l, float4(viewPos, 1.f)); half shadowed = GetShadow(litRelPos); half4 lightPos = mul(vM, float4(lightPosition, 1.f)); half3 lightDir = normalize(lightPos.xyz - viewPos.xyz); half3 texColour = tex2D(colourTexture, IN.oScreenPos.zw).rgb; half specularExp; half specularInt = modf(nm.b, specularExp); half3 litVec = getLitVec(lightDir, 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.1f)); } /*************/ technique basic { pass p0 { VertexProgram = compile vp30 simpleVP(); FragmentProgram = compile fp40 simpleFP(); DepthTestEnable = false; DepthMask = false; BlendEnable = true; BlendFuncSeparate = int4( DstAlpha , SrcAlpha , DstAlpha , Zero ) ; } } /***************************** eof ***/