/************* 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 radius < string UIWidget = "slider"; float UIMin = 0.0; float UIMax = 100.0; float UIStep = 0.1; > = 1.0; float intensity < string UIWidget = "slider"; float UIMin = 0.0; float UIMax = 5.0; float UIStep = 0.1; > = 1.0; half3 maxColour < string UIWidget = "Color"; > = {1.0f, 1.0f, 1.0f}; half3 minColour < string UIWidget = "Color"; > = {0.0f, 0.0f, 0.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; //half2 oTexCoord : TEXCOORD0; half4 oScreenPos : TEXCOORD0; half4 oViewPos : TEXCOORD1; half4 lightPos : TEXCOORD2; }; /*********** vertex program ******/ vertexOutput pointLightSphereVP(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); wM = transpose(wM); OUT.lightPos = float4(wM[3].xyz, 1.f); OUT.lightPos = mul(vM, OUT.lightPos); return OUT; } /********* fragment program ********/ void pointLightFP(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; half4 nm = h4tex2D(normalTexture, texCoord); half3 view = normalize(IN.oViewPos.xyz); // If you want to use nm.a then uncomment below (we do!) nm.a = -abs(nm.a); half3 viewPos; viewPos.z = nm.a; viewPos.xy = IN.oScreenPos.xy *inverseParams.xy*-nm.aa; half3 lightDir = IN.lightPos.xyz - viewPos.xyz; half lightDist = length(lightDir); half3 normal = DecodeNormal(nm.xy); normal.z *= (sign(nm.a) * -1.0h); lightDir = normalize(lightDir); half3 texColour = h3tex2D(colourTexture, texCoord).rgb; half specularExp; half specularInt = modf(nm.b, specularExp); half atten = 1.h-(lightDist/radius); intensity *= step(0.h, atten); half3 litVec = getLitVec(lightDir, normal, view, specularExp); half3 diffuse = litVec.y * lerp(minColour, maxColour, atten); half specColour = litVec.z * specularInt * atten; colour = half4(((diffuse * texColour) + specColour.xxx) * intensity.xxx, 1.0h); // 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 half4 colour : COLOR0) { colour = half4(1.h); } /*************/ 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( Equal , 1 , 1 ); StencilOp = int3( Zero , Zero , Replace ); ClearStencil = 1 ; } 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 pointLightSphereVP(); FragmentProgram = compile fp40 pointLightFP(); DepthTestEnable = false; DepthMask = false; CullFaceEnable = true; CullFace = Front ; BlendEnable = true; BlendFuncSeparate = int4( DstAlpha , SrcAlpha , DstAlpha , Zero ) ; StencilTestEnable = true; StencilFunc = int3( Equal , 1 , 1 ); StencilMask = 0 ; } }