/************* UN-TWEAKABLES **************/ float4x4 wM : World < string UIWidget="None"; >; float4x4 vM; float4x4 pM; float4x4 vP; half4 inverseParams; float3 inverseOffset; float3 inverseScale; #include "y:\\art\\shaders\\Basic_Utils.cgfx" #include "LightUtils.cgfx" /*********** Tweakables **********************/ float intensity < string UIWidget = "slider"; float UIMin = 0.0; float UIMax = 5.0; float UIStep = 0.1; > = 1.0; 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; }; /* 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); return OUT; } /********* fragment program ********/ void lightMeshDirectionalFP(vertexOutput IN, out half4 colour : COLOR0) { IN.oScreenPos /= IN.oScreenPos.w; half2 texCoord = (IN.oScreenPos.xy + 1.f.xx) * 0.5h.xx; IN.oScreenPos.y = -IN.oScreenPos.y; half3 nm = tex2D(normalTexture, texCoord).rgb; // If you want to use nm.a then uncomment below //nm.a = abs(nm.a); //nm.a *= -1.0h; half3 view = normalize(IN.oViewPos.xyz); half3 normal = DecodeNormal(nm.xy); half3 texColour = tex2D(colourTexture, texCoord).rgb; half specularExp; half specularInt = modf(nm.b, specularExp); half3 litVec = getLitVec(-IN.lightDir, normal, view, specularExp); half3 diffuse = litVec.y * lightColour * texColour; half specColour = litVec.z * specularInt; colour = half4((diffuse + specColour.xxx), 1.f); // Encode to RGBdiv8 colour = colour/max(max(colour.r, colour.g), max(colour.b, 1.1f)); } /*************/ 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 { pass p0 { VertexProgram = compile vp20 emptyVP(); FragmentProgram = compile fp20 emptyFP(); DepthTestEnable = true; DepthMask = false; ColorMask = bool4( false , false , false , false ); DepthFunc = GEqual ; CullFaceEnable = true; CullFace = Front ; BlendEnable = false; StencilTestEnable = true; StencilFunc = int3( Always , 1 , 1 ); StencilOp = int3( Zero , Zero , Replace ); } pass p1 { VertexProgram = compile vp20 emptyVP(); FragmentProgram = compile fp20 emptyFP(); DepthTestEnable = true; DepthMask = false; ColorMask = bool4( false , false , false , false ); DepthFunc = LEqual ; CullFaceEnable = true; CullFace = Back ; BlendEnable = false; StencilTestEnable = true; StencilFunc = int3( Equal , 1 , 1 ); StencilOp = int3( Zero , Zero , Keep ); } pass p2 { VertexProgram = compile vp30 lightMeshDirectionalVP(); FragmentProgram = compile fp40 lightMeshDirectionalFP(); DepthTestEnable = false; DepthMask = false; CullFaceEnable = true; CullFace = Front ; BlendEnable = true; BlendFuncSeparate = int4( DstAlpha , SrcAlpha , DstAlpha , Zero ) ; StencilTestEnable = true; StencilFunc = int3( Equal , 1 , 1 ); } }