/************* UN-TWEAKABLES **************/ float4x4 wM : World < string UIWidget="None"; >; float4x4 vM; float4x4 pM; float4x4 vP; float4x4 ivM; // cascade matrices float4x4 v2l; float3 planeLimits; half4 inverseParams; float3 inverseOffset; float3 inverseScale; #include "y:\\art\\shaders\\Basic_Utils.cgfx" #include "LightUtils.cgfx" /*********** Tweakables **********************/ half ambientIntensity < string UIWidget = "slider"; float UIMin = 0.0; float UIMax = 5.0; float UIStep = 0.1; > = 1.h; half3 ambientColour : Ambient < string UIWidget = "Color"; > = {0.42h, 0.42h, 0.42h}; half directionalLightIntensity < string UIWidget = "slider"; float UIMin = 0.0; float UIMax = 5.0; float UIStep = 0.1; > = 1.0h; half3 directionalLightColour : Diffuse < string UIWidget = "Color"; > = {1.0h, 1.0h, 1.0h}; half shadowDensity < string UIWidget = "slider"; float UIMin = 0.0; float UIMax = 5.0; float UIStep = 0.1; > = 1.0h; half3 shadowColour : Diffuse < string UIWidget = "Color"; > = {1.0h, 1.0h, 1.0h}; /************* 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 envMap : Environment < string ResourceType = "Cube"; >; samplerCUBE cubeMap = sampler_state { Texture = ; MinFilter = LinearMipMapLinear; MagFilter = Linear; WrapS = ClampToEdge; WrapT = ClampToEdge; }; texture diffEnvMap : Environment < string ResourceType = "Cube"; >; samplerCUBE diffCubeMap = sampler_state { Texture = ; MinFilter = LinearMipMapLinear; MagFilter = Linear; WrapS = ClampToEdge; WrapT = ClampToEdge; }; /************* DATA STRUCTS **************/ /* data from application vertex buffer */ struct appdata { float4 position : POSITION; }; /* data passed from vertex shader to pixel shader */ struct vertexOutput { float4 oPosition : POSITION; half4 oScreenPos : TEXCOORD0; half4 oViewPos : TEXCOORD1; half4 lightDir : TEXCOORD2; }; /*********** vertex program ******/ vertexOutput lightMeshDirectionalVP(appdata IN) { vertexOutput OUT = (vertexOutput)0; OUT.oViewPos = mul(wM, float4(IN.position.xyz, 1.f)); OUT.oViewPos = mul(vM, OUT.oViewPos); OUT.oPosition = mul(pM, OUT.oViewPos); OUT.oScreenPos = OUT.oPosition; // clamp z values of pixels to far plane to prevent clipping OUT.oPosition.z = min(OUT.oPosition.z, OUT.oPosition.w); OUT.lightDir = mul(wM, float4(0.f, 0.f, 1.f, 0.f)); OUT.lightDir = mul(vM, OUT.lightDir); OUT.lightDir = normalize(OUT.lightDir); return OUT; } /********* fragment program ********/ void lightMeshDirectionalFP(vertexOutput IN, out half4 colour : COLOR0) { // Unpack screen space position and texture coordinates IN.oScreenPos /= IN.oScreenPos.w; half2 texCoord = (IN.oScreenPos.xy + 1.f.xx) * 0.5h.xx; IN.oScreenPos.y = -IN.oScreenPos.y; // Calculate normal half4 nm = h4tex2D(normalTexture, texCoord); nm.a = abs(nm.a); nm.a *= -1.0h; half3 normal = DecodeNormal(nm.xy); // Calculate view vector and view position half3 view = normalize(IN.oViewPos.xyz); half3 viewPos; viewPos.z = nm.a; viewPos.xy = IN.oScreenPos.xy *inverseParams.xy*-viewPos.zz; // Sample unlit colour half4 cm = h4tex2D(colourTexture, texCoord); half3 texColour = cm.rgb; half reflectMask = cm.a; // Apply ambient term half3 reflection = normalize(reflect(view, normal)); reflection = mul(ivM, half4(reflection, 0.h)).xyz; half3 envMap = h3texCUBE(cubeMap, reflection).xyz; half3 diffEnvMap = h3texCUBE(diffCubeMap, reflection).xyz; colour.rgb = texColour * ambientColour * diffEnvMap * ambientIntensity * 2.h; colour.rgb = lerp(colour.rgb, envMap, reflectMask); colour.a = 1.h; // Get shadow half4 litRelPos = mul(v2l, float4(viewPos, 1.f)); litRelPos = saturate(litRelPos); half shadowed = GetShadow(litRelPos); // Unpack specular half specularExp; half specularInt = modf(nm.b, specularExp); // Apply directional light term half3 litVec = getLitVec(IN.lightDir, normal, view, specularExp); half3 diffuse = litVec.y * directionalLightColour * texColour; half specColour = litVec.z * specularInt; colour.rgb += half3((diffuse + specColour.xxx) * shadowed.xxx * directionalLightIntensity); // Apply shadow colour colour.rgb = lerp(colour.rgb, shadowColour, (1.h - shadowed) * shadowDensity); // Encode to RGBdiv8 colour = colour/max(max(colour.r, colour.g), max(colour.b, 1.1h)); } /*************/ struct simpleVertexOutput { float4 position : POSITION; }; simpleVertexOutput emptyVP(appdata IN) { simpleVertexOutput OUT = (simpleVertexOutput)0; OUT.position = mul(wM, IN.position); OUT.position = mul(vP, 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; } void emptyFP(simpleVertexOutput IN, out float4 colour : COLOR0) { colour = float4(1.f); } /*************/ technique basic < string layer = "AmbientLight" ; > { pass p2 { VertexProgram = compile vp30 lightMeshDirectionalVP(); FragmentProgram = compile fp40 lightMeshDirectionalFP(); DepthTestEnable = false; DepthMask = false; BlendEnable = false; CullFace = Front ; BlendEnable = true; } }