string description = "Basic Material Static"; /************* UN-TWEAKABLES **************/ float4x4 wM; float4x4 vM; float4x4 pM; float4x4 vP; float4 inverseParams; #include "y:\\art\\shaders\\Basic_Utils.cgfx" #include "y:\\art\\shaders\\Basic_Depth.cgfx" /*********** Tweakables **********************/ 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; ////// float uvTileBase < string UIWidget = "slider"; float UIMin = 0.0; float UIMax = 5.0; float UIStep = 0.01; > = 1.0; float uvTileLayer1 < string UIWidget = "slider"; float UIMin = 0.0; float UIMax = 5.0; float UIStep = 0.01; > = 1.0; float blendWeight < 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 nm : Normal < string ResourceType = "2D"; >; sampler2D baseNormalMap = sampler_state { Texture = ; MinFilter = LinearMipMapLinear; MagFilter = Linear; }; texture bm : Diffuse < string ResourceType = "2D"; >; sampler2D baseBlendMap = sampler_state { Texture = ; MinFilter = LinearMipMapLinear; MagFilter = Linear; }; texture l1cm : Diffuse < string ResourceType = "2D"; >; sampler2D layer1ColourMap = sampler_state { Texture = ; MinFilter = LinearMipMapLinear; MagFilter = Linear; }; texture l1nm : Normal < string ResourceType = "2D"; >; sampler2D layer1NormalMap = sampler_state { Texture = ; MinFilter = LinearMipMapLinear; MagFilter = Linear; }; texture l1bm : Diffuse < string ResourceType = "2D"; >; sampler2D layer1BlendMap = sampler_state { Texture = ; MinFilter = LinearMipMapLinear; MagFilter = Linear; }; /************* DATA STRUCTS **************/ /* data from application vertex buffer */ struct appdata { float4 position : POSITION; half2 texCoord0 : TEXCOORD0; half4 tangent : TEXCOORD1; half3 normal : NORMAL; half4 colour : COLOR0; }; /* data passed from vertex shader to pixel shader */ struct vertexOutput { float4 position : POSITION; half4 texCoord : TEXCOORD0; half3 normal : TEXCOORD1; half3 tangent : TEXCOORD2; half3 binormal : TEXCOORD3; float2 screen : TEXCOORD4; half4 colour : COLOR0; // z = screenDepth }; /*********** vertex program ******/ vertexOutput simpleVP(appdata IN) { vertexOutput OUT = (vertexOutput)0; IN.colour.g = 1.f- IN.colour.g; OUT.colour = IN.colour * float4(blendWeight.r, 1.f.rrr); OUT.texCoord.xy = IN.texCoord0 * uvTileBase; OUT.texCoord.zw = IN.texCoord0 * uvTileLayer1; //postion and depth OUT.position = mul(wM, float4(IN.position.xyz, 1.f)); OUT.position = mul(vP, OUT.position); // BM: Transform normal and tangent into object space, calculate biNormal OUT.normal = mul(wM, half4(IN.normal, 0.h)).xyz; OUT.tangent = mul(wM, half4(IN.tangent.xyz, 0.h)).xyz; OUT.normal = mul(vM, half4(OUT.normal, 0.h)).xyz; OUT.tangent = mul(vM, half4(OUT.tangent.xyz, 0.h)).xyz; OUT.binormal = GetBiNormal(OUT.tangent, OUT.normal) * IN.tangent.w; // we never use blue anyway... OUT.screen.xy = OUT.position.zw; return OUT; } /********* fragment program ********/ void simpleFP(vertexOutput IN, out half4 colour : COLOR0, out half4 normal : COLOR1 ) { //final colour, textures and composite lighting contributions half4 baseColour = tex2D(baseColourMap, IN.texCoord.xy); baseColour.a *= 0.999f; //float blend = step(baseColour.a, IN.colour.r) * (IN.colour.g); float blend = saturate(smoothstep(baseColour.a, saturate(baseColour.a+IN.colour.g), IN.colour.r)); half3 texColour = lerp (baseColour.rgb, h3tex2D(layer1ColourMap, IN.texCoord.zw), blend).xyz; half4 normalBlend = lerp (h4tex2D(baseBlendMap, IN.texCoord.xy), h4tex2D(layer1BlendMap, IN.texCoord.zw), blend); half2 blendMask = normalBlend.xy; half3 nM; nM.xy = normalBlend.zw; //generate normal and world normal nM.xy -= 0.5h.xx; nM.xy *= 2.h.xx; nM = DecodeNormal(nM.xy); // nM.xy += nM.xy * 0.25f; // exagerate normals // nM = normalize(nM); nM = normalize(nM.x * IN.tangent + nM.y * IN.binormal + nM.z*IN.normal); float viewDepth = inverseParams.w / ((IN.screen.x/IN.screen.y) + inverseParams.z); colour = half4(half3(texColour * IN.colour.a), blendMask.g * reflectivity); normal = half4(nM.x, nM.y, clamp(blendMask.r * specularInt, 0, 0.95h) + ceil(specularExp), sign(nM.z) * viewDepth); } void simpleDSFP(vertexOutput IN, in float face : FACE, out half4 colour : COLOR0, out half4 normal : COLOR1 ) { IN.tangent *= face; IN.normal *= face; simpleFP(IN, colour, normal); } /*************/ technique basic < string layer = "3DScene" ; > { pass p0 { VertexProgram = compile vp30 simpleVP(); FragmentProgram = compile fp40 simpleFP(); DepthTestEnable = true; DepthMask = true; BlendEnable = false; } pass shadowPass < string layer = "Shadow" ; string excludeData = "NoShadowCast" ; > { VertexProgram = compile vp20 basicDepthVP(); FragmentProgram = compile fp20 basicDepthFP(); DepthTestEnable = true; DepthMask = true; ColorMask = bool4( false , false , false , false ); BlendEnable = false; AlphaTest = false; CullFaceEnable = false; } } technique doubleSided < string layer = "3DScene" ; > { pass p0 { VertexProgram = compile vp30 simpleVP(); FragmentProgram = compile fp40 simpleDSFP(); DepthTestEnable = true; DepthMask = true; BlendEnable = false; AlphaTestEnable = false; CullFaceEnable = false; } pass shadowPass < string layer = "Shadow" ; string excludeData = "NoShadowCast" ; > { VertexProgram = compile vp20 basicDepthVP(); FragmentProgram = compile fp20 basicDepthFP(); DepthTestEnable = true; DepthMask = true; ColorMask = bool4( false , false , false , false ); BlendEnable = false; AlphaTest = false; CullFaceEnable = false; } } /***************************** eof ***/