/************* 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 = float3(1.0f, 1.0f, 1.0f); #include "..\\Basic_Utils.cgfx" /*********** Tweakables **********************/ /************* Textures ******************/ float scale = 1.0f; sampler2D decalTexture; texture bm : Diffuse < string ResourceType = "2D"; >; sampler2D baseBlendMap = sampler_state { Texture = ; MinFilter = LinearMipMapLinear; MagFilter = Linear; }; float specularInt < string UIWidget = "slider"; float UIMin = 0.0; float UIMax = 5.0; float UIStep = 0.01; > = 1.0; float specularExp < string UIWidget = "slider"; float UIMin = 1.0; float UIMax = 128.0; float UIStep = 1.0; > = 32.0; ////// float reflectivity < string UIWidget = "slider"; float UIMin = 0.0; float UIMax = 1.0; float UIStep = 0.01; > = 0.0; 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; half4 normal : TEXCOORD3; half4 tangent : TEXCOORD4; }; /*********** 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); float4 normal = mul(wM, float4(0.f, 0.f, -1.f, 0.f)); OUT.normal = mul(vM, normal); OUT.normal = normalize(OUT.normal); OUT.tangent = mul(wM, float4(-1.f, 0.f, 0.f, 0.f)); OUT.tangent = mul(vM, OUT.tangent); return OUT; } /********* fragment program ********/ void pointLightFP(vertexOutput IN, out float4 colour : COLOR0, out float4 normal : COLOR1 ) { IN.oScreenPos /= IN.oScreenPos.w; half2 texCoord = (IN.oScreenPos.xy + 1.f.xx) * 0.5h.xx; IN.oScreenPos.y = -IN.oScreenPos.y; half3 view = normalize(IN.oViewPos.xyz); half3 viewPos; const float3 depthFactor = float3(65536.0f/16777215.0f, 256.0f/16777215.0f, 1.0f/16777215.0f ); float4 RawDepth = tex2D(depthTexture, texCoord); float Depth = dot(round( RawDepth.xyz * 255.0f.xxx ), depthFactor.xyz); float ScreenDepth = (Depth + inverseOffset.z) * inverseScale.z; viewPos.z = inverseParams.w / (ScreenDepth + inverseParams.z); viewPos.xy = IN.oScreenPos.xy *inverseParams.xy*-viewPos.zz; half3 diff = IN.centrePos - viewPos; half dist = length(diff); 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; half alpha = h4tex2D(decalTexture, texCoords.xy + textureOffset).a; if(dist > (0.1h * scale)) discard; half4 normalBlend = h4tex2D(baseBlendMap, texCoords.xy + textureOffset); half2 blendMask = normalBlend.xy; half3 nM; nM.xy = normalBlend.zw; half3 binormal = GetBiNormal(IN.tangent, IN.normal); //generate normal and world normal nM.xy -= 0.5f.xx; nM.xy *= 2.f.xx; nM = DecodeNormal(nM.xy); nM = normalize(nM.x * IN.tangent + nM.y * binormal + nM.z*IN.normal); colour = half4(0.h); normal = half4(nM.x, nM.y, 0, alpha); } /*************/ technique basic < string layer = "LitBullets" ; > { pass p0 { ColorMask = bool4( true , true , false , false ); VertexProgram = compile vp30 pointLightSphereVP(); FragmentProgram = compile fp40 pointLightFP(); DepthTestEnable = false; DepthMask = false; BlendEnable = false; BlendEnable = true; BlendFuncSeparate = int4( SrcAlpha , OneMinusSrcAlpha , Zero , One ) ; } }