string description = "Basic Material Static"; /************* UN-TWEAKABLES **************/ float4x4 wM; float4x4 vP; float4 inverseParams; float3 inverseOffset; float3 inverseScale; #include "y:\\art\\shaders\\Basic_Utils.cgfx" #include "StippledAlpha_Utils.cgfx" /*********** Tweakables **********************/ int channelID < string UIWidget = "slider"; int UIMin = 0; int UIMax = 3; int UIStep = 1; > = 0; int baseID < string UIWidget = "slider"; int UIMin = 0; int UIMax = 3; int UIStep = 1; > = 15; int2 tileDimensions = int2(4, 4); ////// float uvTileBase < string UIWidget = "slider"; float UIMin = 0.0; float UIMax = 5.0; float UIStep = 0.01; > = 1.0; /************* Textures ******************/ texture cm : Diffuse < string ResourceType = "2D"; >; sampler2D baseColourMap = sampler_state { Texture = ; MinFilter = LinearMipMapLinear; MagFilter = Linear; }; texture sm < string ResourceType = "2D"; >; sampler2D screenTexture = sampler_state { Texture = ; MinFilter = LinearMipMapLinear; MagFilter = Linear; }; texture plm : Diffuse < string ResourceType = "2D"; >; sampler2D colourTexture = sampler_state { Texture = ; MinFilter = LinearMipMapLinear; MagFilter = Linear; }; texture ldm : Diffuse < string ResourceType = "2D"; >; sampler2D normalTexture = sampler_state { Texture = ; MinFilter = LinearMipMapLinear; MagFilter = Linear; }; sampler2D GBufferStoreTexture; /************* DATA STRUCTS **************/ /* data from application vertex buffer */ struct appdata { float4 position : POSITION; half2 texCoord0 : TEXCOORD0; half4 colour : COLOR0; }; /* data passed from vertex shader to pixel shader */ struct vertexOutput { float4 position : POSITION; half3 texCoord : TEXCOORD0; float4 screen : TEXCOORD4; float4 colour : COLOR0; }; /*********** vertex program ******/ vertexOutput simpleVP(appdata IN) { vertexOutput OUT = (vertexOutput)0; OUT.colour = IN.colour; OUT.texCoord.xy = IN.texCoord0 * uvTileBase; //compute matrices float4x4 wVP = mul(vP, wM); //postion and depth OUT.position = mul(wVP, float4(IN.position.xyz, 1.f)); OUT.screen = OUT.position; return OUT; } /********* fragment program ********/ void baseColourFP(vertexOutput IN, out float4 colour : COLOR0, out float depth : DEPTH) { float2 texCoord = ((IN.screen.xy/IN.screen.w) + 1.f.xx) * 0.5h.xx; float4 texColour = tex2D(GBufferStoreTexture, texCoord.xy).rgba; float4 sampleCoords = GenerateSampleCoords(tileDimensions, baseID, texCoord); float2 ratio = float2(texCoord - sampleCoords.xy)/float2(sampleCoords.zw - sampleCoords.xy); colour = Relight4x4Sample(ratio, sampleCoords, texColour, colourTexture, screenTexture); // NOTE: discontinuities when depth precision is too low, might need to bias slightly float viewDepth = -abs(tex2D(normalTexture, sampleCoords.xy).a); float objectDepth = inverseParams.w / ((IN.screen.z/IN.screen.w) + inverseParams.z); if(objectDepth <= viewDepth) discard; depth = (inverseParams.w / viewDepth) - inverseParams.z; depth = ((depth)/inverseScale.z)-inverseOffset.z; depth = saturate(depth); } void simpleFP(vertexOutput IN, out float4 colour : COLOR0) { half4 texColour = tex2D(baseColourMap, IN.texCoord.xy).rgba; if(texColour.a == 0.f) discard; float2 texCoord = ((IN.screen.xy/IN.screen.w) + 1.f.xx) * 0.5h.xx; float4 sampleCoords = GenerateSampleCoords(tileDimensions, channelID, texCoord); float2 ratio = float2(texCoord - sampleCoords.xy)/float2(sampleCoords.zw - sampleCoords.xy); colour = Relight4x4Sample(ratio, sampleCoords, texColour, colourTexture, screenTexture); colour /= colour.a; colour.a = texColour.a; colour.rgb *= colour.a; } void backFaceFP(vertexOutput IN, out float4 colour : COLOR0) { half4 texColour = tex2D(baseColourMap, IN.texCoord.xy).rgba; if(texColour.a == 0.f) discard; float2 texCoord = ((IN.screen.xy/IN.screen.w) + 1.f.xx) * 0.5h.xx; float4 sampleCoords = GenerateSampleCoords(tileDimensions, channelID+1, texCoord); float2 ratio = float2(texCoord - sampleCoords.xy)/float2(sampleCoords.zw - sampleCoords.xy); colour = Relight4x4Sample(ratio, sampleCoords, texColour, colourTexture, screenTexture); colour /= colour.a; colour.a = texColour.a; colour.rgb *= colour.a; } /*************/ technique basic < string layer = "SampleLitAlpha" ; > { pass p0 { VertexProgram = compile vp30 simpleVP(); FragmentProgram = compile glslf baseColourFP(); DepthTestEnable = true; DepthFunc = Always ; DepthMask = true; CullFaceEnable = false; StencilTestEnable = true; StencilFunc = int3( Equal , 0 , 1 ); StencilOp = int3( Keep , Keep , Incr ); } pass p1 { VertexProgram = compile vp30 simpleVP(); FragmentProgram = compile fp40 backFaceFP(); DepthTestEnable = true; BlendEnable = true; BlendFuncSeparate = int4( DstAlpha , OneMinusSrcAlpha , Zero , One ) ; AlphaTestEnable = false; CullFaceEnable = true; CullFace = Front ; } pass p2 { VertexProgram = compile vp30 simpleVP(); FragmentProgram = compile fp40 simpleFP(); DepthTestEnable = true; BlendEnable = true; BlendFuncSeparate = int4( DstAlpha , OneMinusSrcAlpha , Zero , One ) ; AlphaTestEnable = false; CullFaceEnable = true; CullFace = Back ; } } /***************************** eof ***/