/************* UN-TWEAKABLES **************/ float4x4 wM : World < string UIWidget="None"; >; float4x4 vM; float4x4 pM; float4x4 iwM; float4x4 ivM; 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 = 50.0; float UIStep = 0.1; > = 1.0; float3 lightColour < string UIWidget = "Color"; > = {1.0f, 1.0f, 1.0f}; float uvTileBase < string UIWidget = "slider"; float UIMin = 0.0; float UIMax = 5.0; float UIStep = 0.01; > = 1.0; float uvOffsetX < string UIWidget = "slider"; float UIMin = 0.0; float UIMax = 1.0; float UIStep = 0.01; > = 0.0f; float uvOffsetY < string UIWidget = "slider"; float UIMin = 0.0; float UIMax = 1.0; float UIStep = 0.01; > = 0.0f; float cellsX < string UIWidget = "slider"; float UIMin = 0.0; float UIMax = 64.0; float UIStep = 1.0; > = 1.0f; float cellsY < string UIWidget = "slider"; float UIMin = 0.0; float UIMax = 64.0; float UIStep = 1.0; > = 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; }; texture lightMap : Diffuse < string ResourceType = "2D"; >; sampler2D lightTexture = 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 lightMeshDirectionaProjectiveVP(appdata IN) { vertexOutput OUT = (vertexOutput)0; float4x4 wvM; 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 if(OUT.oPosition.z > OUT.oPosition.w) OUT.oPosition.z = OUT.oPosition.w; OUT.lightDir = mul(wM, float4(0.f, 0.f, 1.f, 0.f)); OUT.lightDir = normalize(OUT.lightDir); return OUT; } /********* fragment program ********/ void lightMeshDirectionaProjectiveFP(vertexOutput IN, out float4 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 = tex2D(normalTexture, texCoord); half3 view = normalize(IN.oViewPos.xyz); float4 normal; normal.xyz = DecodeNormal(nm.xy); normal.z *= (sign(nm.a) * -1.0h); nm.a = abs(nm.a); nm.a *= -1.0h; half3 viewPos; viewPos.z = nm.a; viewPos.xy = IN.oScreenPos.xy *inverseParams.xy*-viewPos.zz; half3 texColour = tex2D(colourTexture, texCoord).rgb; half specularExp; half specularInt = modf(nm.b, specularExp); half3 lightLocalPos; // Create the inverse worldview matrix and transform the viewPos back into world space float4x4 iwvM = mul(iwM, ivM); lightLocalPos = mul(iwvM, float4(viewPos, 1)).xyz; normal = mul(ivM, normal); // Multiply by 4 and subtract 0.5 to bring it to the range of 0-1 //lightLocalPos *= 0.5h;//4.0f; lightLocalPos.xy += 0.5h;//half2(0.5h); // Pos was negative so make it positive... lightLocalPos.xy *= -1.0f; half2 cellMod = half2(1.0f / cellsX, 1.0f / cellsY); // Move it so we only display one of the 64 frames in the sphere // this data will need to come into the shader lightLocalPos.xy *= cellMod; // Flip the x-coord as it was back to front lightLocalPos.x *= -1.0f; lightLocalPos.x += cellMod.x; // Apply any animated offsets... lightLocalPos.xy += half2(uvOffsetX, uvOffsetY); // Get our colour from the light texture to be projected half4 lightTexCol = tex2D(lightTexture, lightLocalPos.xy); half3 litVec = getLitVec(IN.lightDir, normal.xyz, view, specularExp); half3 diffuse = litVec.y * lightColour * lightTexCol.rgb; half specColour = litVec.z * specularInt * lightTexCol.rgb; colour = half4(((diffuse * texColour) + specColour.xxx) * intensity.xxx, 1.0h); //colour.rgb = diffuse.rgb; colour.a = 1.0h; // Uncomment for texcoords colours //colour.rgb = lightLocalPos.xyz; //colour.b = 0.0f; // Encode to RGBdiv8 //colour = half4(normalize(normal.xyz), 1.f); colour = colour/max(max(colour.r, colour.g), max(colour.b, 1.1f)); //colour = half4(texCoord.xy, 0, 1); } /*************/ struct simpleVertexOutput { float4 position : POSITION; }; simpleVertexOutput emptyVP(appdata IN) { simpleVertexOutput OUT = (simpleVertexOutput)0; OUT.position = mul(wM, IN.position); OUT.position = mul(vM, OUT.position); OUT.position = mul(pM, OUT.position); // clamp z values of pixels to far plane to prevent clipping if(OUT.position.z > OUT.position.w) 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 lightMeshDirectionaProjectiveVP(); FragmentProgram = compile fp40 lightMeshDirectionaProjectiveFP(); DepthTestEnable = false; DepthMask = false; CullFaceEnable = true; CullFace = Front ; BlendEnable = true; BlendFuncSeparate = int4( DstAlpha , SrcAlpha , DstAlpha , Zero ) ; StencilTestEnable = true; StencilFunc = int3( Equal , 1 , 1 ); } }