/************* UN-TWEAKABLES **************/ float4x4 wM : World < string UIWidget="None"; >; float4x4 vM; float4x4 pM; float4x4 ivM; half4 inverseParams; float3 inverseOffset; float3 inverseScale; float2 textureOffset = float2(0.f, 0.f); float3 TintColour; #include "..\\Basic_Utils.cgfx" /*********** Tweakables **********************/ /************* Textures ******************/ float scale = 1.0f; sampler2D decalTexture; 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 centrePos : TEXCOORD2; // half2 texCoords : TEXCOORD3; }; /*********** vertex program ******/ vertexOutput pointLightSphereVP(appdata IN) { vertexOutput OUT = (vertexOutput)0; // OUT.texCoords = mul(wM, float4(IN.position.xyz, 0.f)).xy; // OUT.texCoords -= 0.1h; // OUT.texCoords /= -0.2h; 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.centrePos = mul(wM, float4(0.f, 0.f, 0.f, 1.f)); OUT.centrePos = mul(vM, OUT.centrePos); return OUT; } /********* fragment program ********/ void pointLightFP(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); half3 viewPos; viewPos.z = nm.a; viewPos.xy = IN.oScreenPos.xy *inverseParams.xy*-viewPos.zz; half3 diff = IN.centrePos - viewPos; half dist = length(diff); //diff = normalize(diff); half ratio = 1.h;//1.h - (dist/0.1h); // 0.1h = radius of sphere float4x4 iwM = transpose(wM); float4 worldDiff = mul(ivM, float4(diff, 0.f)); half3 texCoords = mul(iwM, float4(worldDiff.xyz, 0.f)).xyz; texCoords /= (scale*scale); texCoords /= 0.2h; texCoords /= 2.h; texCoords += 0.25h; //colour = half4(texCoords, 1.h); //colour = half4(texCoords.x, texCoords.y, 0.h, 1.h); colour = tex2D(decalTexture, texCoords.xy + textureOffset); colour.rgb = TintColour; colour.a *= ratio; colour.rgb *= colour.a; } /*************/ 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 < string layer = "BulletHoles" ; > { 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 pointLightSphereVP(); FragmentProgram = compile fp40 pointLightFP(); DepthTestEnable = false; DepthMask = false; CullFaceEnable = true; CullFace = Front ; BlendEnable = true; BlendFuncSeparate = int4( DstAlpha , OneMinusSrcAlpha , Zero , One ) ; StencilTestEnable = true; StencilFunc = int3( Equal , 1 , 1 ); } }