string description = "Basic Material"; /************* UN-TWEAKABLES **************/ float4x4 vP : ViewProjection < string UIWidget="None"; >; float4x4 vIT : ViewInverseTranspose < string UIWidget="None"; >; float4x4 wM : World < string UIWidget="None"; >; #include "y:\\art\\shaders\\Basic_Utils.cgfx" /*********** Tweakables **********************/ float3 lightPosition : Position < string Object = "PointLight0"; string Space = "World"; > = {100.0f, 100.0f, 100.0f}; ///// float3 lightColour : Diffuse < string UIWidget = "Color"; > = {1.0f, 1.0f, 1.0f}; ///// float3 ambientColour : Ambient < string UIWidget = "Color"; > = {0.42f, 0.42f, 0.42f}; ///// 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 bumpScale < string UIWidget = "slider"; float UIMin = 0.0; float UIMax = 1.0; float UIStep = 0.001; > = 0.020; ////// 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; /************* 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; }; texture envMap : Environment < string ResourceType = "Cube"; >; samplerCUBE cubeMap= sampler_state { Texture = ; MinFilter = LinearMipMapLinear; MagFilter = Linear; WrapS = ClampToEdge; WrapT = ClampToEdge; }; /************* DATA STRUCTS **************/ /* data from application vertex buffer */ struct appdata { float4 position : POSITION; float2 texCoord0 : TEXCOORD0; float4 tangent : TEXCOORD1; float3 binormal : TEXCOORD2; float3 normal : NORMAL; float4 colour : COLOR0; }; /* data passed from vertex shader to pixel shader */ struct vertexOutput { float4 oPosition : POSITION; float4 oTexCoord : TEXCOORD0; half4 TSRow1 : TEXCOORD1; half4 TSRow2 : TEXCOORD2; half4 TSRow3 : TEXCOORD3; float4 worldPos : TEXCOORD4; float depth : TEXCOORD6; float3 tanView : TEXCOORD7; float4 oColour : COLOR0; }; /*********** vertex program ******/ vertexOutput simpleVP(appdata IN) { vertexOutput OUT = (vertexOutput)0; //constants OUT.oColour = IN.colour; OUT.oTexCoord.xy = IN.texCoord0 * uvTileBase; OUT.oTexCoord.zw = IN.texCoord0 * uvTileLayer1; //postion and depth OUT.worldPos = mul(wM, IN.position); OUT.oPosition = mul(vP, OUT.worldPos); OUT.depth = OUT.oPosition.z; //compute the transform from tangent space to object space float3 vtan = IN.tangent; #ifdef _PRAETORIAN_ half3 vbinorm = GetBiNormal(IN.tangent.xyz, IN.normal) * IN.tangent.w; #else half3 vbinorm = IN.binormal; #endif float3 vnorm = IN.normal; getTSRows ( vtan, vbinorm, vnorm, wM, OUT.TSRow1, OUT.TSRow2, OUT.TSRow3); OUT.worldPos = mul(wM, IN.position); float4 eyeVector = OUT.worldPos - vIT[3]; OUT.TSRow1.w = eyeVector.x; OUT.TSRow2.w = eyeVector.y; OUT.TSRow3.w = eyeVector.z; OUT.tanView = float3( dot(float3(OUT.TSRow1.x, OUT.TSRow2.x, OUT.TSRow3.x), eyeVector.xyz), dot(float3(OUT.TSRow1.y, OUT.TSRow2.y, OUT.TSRow3.y), eyeVector.xyz), dot(float3(OUT.TSRow1.z, OUT.TSRow2.z, OUT.TSRow3.z), eyeVector.xyz)); return OUT; } /********* fragment program ********/ void simpleFP(vertexOutput IN, out float4 colour : COLOR0, out float4 normal : COLOR1 ) { //final colour, textures and composite lighting contributions half4 baseColour = tex2D(baseColourMap, IN.oTexCoord.xy); float blend = saturate(smoothstep(baseColour.a, saturate(baseColour.a+IN.oColour.g), IN.oColour.r));//saturate(pow(IN.oColour.r, (baseColour.a+0.5f)*2.f)); half4 texColour = lerp (baseColour, tex2D(layer1ColourMap, IN.oTexCoord.zw), blend) ; float3 tanView = normalize(IN.tanView); float numSteps = lerp(128, 8, abs(tanView.z)); float steps = 1.0/numSteps; float height = 1.0; float2 delta; delta.x = (((-IN.tanView.x) * bumpScale) / (IN.tanView.z * numSteps)); #ifdef _PRAETORIAN_ delta.y = (((IN.tanView.y) * bumpScale) / (IN.tanView.z * numSteps)); #else delta.y = (((-IN.tanView.y) * bumpScale) / (IN.tanView.z * numSteps)); #endif for ( int i =0; i < numSteps; i++) { if (texColour.a < height) { height -= steps; IN.oTexCoord += delta.xyxy; baseColour = tex2D(baseColourMap, IN.oTexCoord.xy); blend = saturate(smoothstep(baseColour.a, saturate(baseColour.a+IN.oColour.g), IN.oColour.r));//saturate(pow(IN.oColour.r, (baseColour.a+0.5f)*2.f)); texColour = lerp (baseColour, tex2D(layer1ColourMap, IN.oTexCoord.zw), blend) ; } } float3 lightDir = normalize(lightPosition - IN.worldPos.xyz); float4 lColour = float4(lightColour, 1.0); float4 aColour = float4(ambientColour, 1.0); float4 nM = lerp (tex2D(baseNormalMap, IN.oTexCoord.xy), tex2D(layer1NormalMap, IN.oTexCoord.zw), blend); float4 blendMask = lerp (tex2D(baseBlendMap, IN.oTexCoord.xy), tex2D(layer1BlendMap, IN.oTexCoord.zw), blend); //generate normal and world normal float3 worldNorm = getWorldNormal(IN.TSRow1, IN.TSRow2, IN.TSRow3, nM); //generate eye vector, reflected diffuse and envornment contributions float3 eyeVector = normalize(float3(IN.TSRow1.w, IN.TSRow2.w, IN.TSRow3.w)); float3 reflection = reflect(eyeVector, worldNorm); half4 envMap = texCUBElod(cubeMap, float4(reflection.xyz, 7.f * (1.f-(specularExp/96.f)))); envMap.rgb += envMap.rgb * envMap.a; half4 diffEnvMap = texCUBElod(cubeMap, float4(worldNorm.xyz, 6.f)); diffEnvMap.rgb += diffEnvMap.rgb * diffEnvMap.a; #ifdef _PRAETORIAN_ half4 litRelPos = getLitRelPos(IN.worldPos); #else half4 litRelPos; #endif colour.xyz = getFinalColourReflection( lightDir, worldNorm, eyeVector, specularExp, specularInt, specularInt, aColour, lColour, texColour, blendMask, envMap.rgb, diffEnvMap.rgb, litRelPos, IN.oColour.a); colour.a = 1; } /*************/ technique basic { pass p0 { VertexProgram = compile vp30 simpleVP(); FragmentProgram = compile fp40 simpleFP(); DepthTestEnable = true; DepthMask = true; BlendEnable = false; } } /***************************** eof ***/