/************* 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; samplerCUBE decalTexture; texture bm : Diffuse < string ResourceType = "2D"; >; //sampler2D baseBlendMap = sampler_state samplerCUBE baseBlendMap = sampler_state { Texture = ; MinFilter = LinearMipMapLinear; MagFilter = Linear; }; sampler2D baseBlendMap2; 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; half3 worldPos : TEXCOORD5; half3 centrePosWS : TEXCOORD6; }; /*********** vertex program ******/ vertexOutput pointLightSphereVP(appdata IN) { vertexOutput OUT = (vertexOutput)0; OUT.oViewPos = mul(wM, float4(IN.position.xyz, 1.f)); OUT.worldPos = OUT.oViewPos; 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; // Get the vector from the centre of the sphere to the pixel in view space half3 diff = IN.centrePos - viewPos; half dist = length(diff); float4x4 iwM = transpose(wM); float4 worldDiff = mul(ivM, float4(diff, 0.f)); float3 texCoords = mul(iwM, float4(worldDiff.xyz, 0.f)).xyz; colour.rgb *= TintColour; // The colour texture should not scale with the size of the mesh... float3 colourTexCoords = texCoords / (scale*2.0f); colourTexCoords.z += 0.1f; // Offset the z so we're not in teh middle of the cubemap // Get the blend from the cube map // This is the normals and specular only float3 blend1Coords = colourTexCoords; half4 normalBlend = texCUBE(baseBlendMap, blend1Coords.xyz); // Get the mask from the cube map float3 blend2Coords = texCoords; blend2Coords /= 5.0f; blend2Coords.z += 0.5f; if(blend2Coords.z > 0.5f) // This stops some concave anomolies blend2Coords.z = 1.0f - blend2Coords.z; half4 blend2 = texCUBE(baseBlendMap, blend2Coords.xyz); // Get the mask from the 2D texture // This scales with the size of the sphere float3 blend3Coords = texCoords; blend3Coords /= scale; blend3Coords.xy /= 2.0f; // Centre the texture blend3Coords.xy += 0.5h; blend3Coords.x = clamp(blend3Coords.x, 0.0f, 1.0f); blend3Coords.y = clamp(blend3Coords.y, 0.0f, 1.0f); half4 blend3 = tex2D(baseBlendMap2, blend3Coords.xy); // Multiply the 2 masks together blend2.g *= blend3.g; colour.rgb *= blend2.g; if(blend2.g <= 0.1h) discard; if(dist > (0.1h * scale)) discard; 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(half3(colour), blend2.g * reflectivity); normal = half4(nM.x, nM.y, clamp(blendMask.r * specularInt, 0, 0.95h) + ceil(specularExp), sign(IN.normal.z) * viewPos.z); colour = float4(texCUBE(decalTexture, colourTexCoords.xyz).rgb, blend2.g); //colour = float4(blend2Coords, 0.0f); //colour = float4(texCoords.xyz, 0.0f); //normal = half4(0.0f, 0.0f, 0.0f, 0.0f); } /*************/ technique basic < string layer = "LitBullets" ; > { pass p0 { VertexProgram = compile vp30 pointLightSphereVP(); FragmentProgram = compile fp40 pointLightFP(); DepthTestEnable = false; DepthMask = false; BlendEnable = false; } }