//=================================================================== // Environment Fence Diffuse Normal Specular Ambient Occlusion Shader // @author jboldiga // Has transparency and uses light maps and fake specular lighting //=================================================================== #ifdef _MAX_ // Max includes are in ../3dsMax/MaterialLibraries/ #include "../KFF_Macros" #include "lighting.fx" #else #include "c:/_project/Fatburger/Assets/Shaders/lighting.fx" #include "c:/_project/Fatburger/Assets/Shaders/KFF_Macros" #endif float OpacityLevel < string UIType = "FloatSpinner"; float UIMin = -999999.0; float UIMax = 999999.0; float UIStep = 0.1; string UIName = "Opacity Level"; > = 1.0; float PixelDepthPower < string UIType = "FloatSpinner"; float UIMin = -99999.0; float UIMax = 99999.0; float UIStep = 1.0; string UIName = "PixelDepthPower"; > = -128.0; float MinOpacityLevel < string UIType = "FloatSpinner"; float UIMin = -999999.0; float UIMax = 999999.0; float UIStep = 0.001; string UIName = "Min Opacity Level"; > = 0.01; texture diffuseTexture : Diffuse < string Name = "EUPHBARK.TGA"; string UIName = "Diffuse Map"; string ResourceType = "2D"; >; sampler2D diffuseTextureSampler = sampler_state { Texture = ; MinFilter = LINEAR; MagFilter = LINEAR; MipFilter = LINEAR; AddressU = WRAP; AddressV = WRAP; }; texture LightMapAOTexture < string Name = "d_leaves1.dds"; string UIName = "AO Map"; string ResourceType = "2D"; >; sampler2D LightMapAOSampler = sampler_state { Texture = ; MinFilter = LINEAR; MagFilter = LINEAR; MipFilter = LINEAR; AddressU = WRAP; AddressV = WRAP; }; texture NormalMap : NormalMap < string Name = "C:\\_project\\Fatburger\\Assets\\Characters\\_Common\\Common_flat_N.dds"; string UIName = "Normal Map"; string ResourceType = "2D"; >; sampler2D NormalMapSampler = sampler_state { Texture = ; MinFilter = LINEAR; MagFilter = LINEAR; MipFilter = LINEAR; AddressU = WRAP; AddressV = WRAP; }; texture contactShadowTexture < string Name = "d_leaves1.dds"; string UIName = "Contact Shadow Map"; string ResourceType = "2D"; >; sampler2D contactShadowSampler = sampler_state { Texture = ; MinFilter = LINEAR; MagFilter = LINEAR; MipFilter = LINEAR; AddressU = WRAP; AddressV = WRAP; }; #ifdef _MAX_ // this is so that in max the channels match correctly int texcoord0 : Texcoord < int Texcoord = 0; int MapChannel = 1; string UIType = "None"; >; int texcoord1 : Texcoord < int Texcoord = 1; int MapChannel = 2; string UIType = "None"; >; int texcoord2 : Texcoord < int Texcoord = 2; int MapChannel = 3; string UIType = "None"; >; #endif float4 CSMask < string UIWidget = "Color"; string UIName = "Contact Shadow Mask"; > = {1.0f, 0.0f, 0.0f, 0.0f}; float3 AmbientColor : Ambient < string UIName = "Ambient Light"; string UIWidget = "Color"; > = {0.0f,0.0f,0.0f}; float4 SpecColor < string UIWidget = "Color"; string UIName = "Specular Color"; > = {1.0f, 1.0f, 1.0f, 1.0f}; float SpecPower < string UIWidget = "Slider"; float UIMin = 0.0; float UIMax = 100.0; float UIStep = 0.01; string UIName = "Specular Power"; > = 0.5; float SpecGloss < string UIWidget = "Slider"; float UIMin = 0.0; float UIMax = 100.0; float UIStep = 0.01; string UIName = "Specular Glossiness"; > = 15.0; float LowExposure < string UIWidget = "slider"; string UIName = "Low Exposure"; float UIMin = -100.0; float UIMax = 0.0; float UIStep = 0.5; > = -2.0f; float HighExposure < string UIWidget = "slider"; string UIName = "High Exposure"; float UIMin = 0.0; float UIMax = 100.0; float UIStep = 0.5; > = 2.0f; float Threshold < string UIWidget = "slider"; string UIName = "Threshold"; float UIMin = 0.0; float UIMax = 100.0; float UIStep = 0.1; > = 50.0f; // matrices float4x4 wvp : WorldViewProjection < string UIType = "None"; >; float4x4 worldI : WorldInverse < string UIType = "None"; >; float4x4 worldIT : WorldInverseTranspose < string UIType = "None"; >; float4x4 viewInv : ViewInverse < string UIType = "None"; >; float4x4 world : World < string UIType = "None"; >; // input from application struct a2v { float4 position : POSITION; float2 texCoord : TEXCOORD0; float2 texCoord1 : TEXCOORD1; float2 texCoord2 : TEXCOORD2; float4 tangent : TANGENT; float4 binormal : BINORMAL; float4 normal : NORMAL; }; // output to fragment program struct v2f { float4 position : POSITION; float3 worldNormal : TEXCOORD0; float2 texCoord : TEXCOORD1; float4 texCoord1 : TEXCOORD2; float3 eyeVec : TEXCOORD3; float3 worldTangent : TEXCOORD4; float3 worldBinormal : TEXCOORD5; float3 worldPosition : TEXCOORD6; float2 pixelDepth : TEXCOORD7; }; //Vertex Shader v2f av(a2v In) { v2f Out = (v2f)0; float3 worldSpacePos = matmul(In.position, world).xyz; //world space position Out.texCoord = In.texCoord; //pass through texture coordinates from channel 1 Out.texCoord1.xy = In.texCoord1; //pass through texture coordinates from channel 2 into xy Out.texCoord1.zw = In.texCoord2; //pass through texture coordinates from channel 3 into zw float4 osIPos = mattrans(viewInv); // world space eye position float3 osIVec = osIPos.xyz - worldSpacePos; // world space eye vector Out.eyeVec = osIVec; Out.position = matmul(In.position, wvp); //transform vert position to homogeneous clip space Out.worldNormal = matmul(In.normal, worldIT).xyz; //compute world space normal Out.worldBinormal = matmul(In.binormal, worldIT).xyz; //compute world space binormal Out.worldTangent = matmul(In.tangent, worldIT).xyz; //compute world space tangent Out.worldPosition = worldSpacePos; Out.pixelDepth.xy = Out.position.zw; //take Z and W component from clip space vertex position return Out; } //Pixel Shader float4 af(v2f In) : COLOR { float3 V = normalize(In.eyeVec); //creating the eye vector float4 diffuseTex = tex2D(diffuseTextureSampler, In.texCoord.xy); float4 lightMapAOTex = tex2D(LightMapAOSampler, In.texCoord1.xy); float4 NormalMap = tex2D(NormalMapSampler, In.texCoord.xy); float4 contactShadowMap = tex2D(contactShadowSampler, In.texCoord1.zw); float contactShadow = contactShadowMap * CSMask; float3 lightMapHDR = CalculateLightMapHDR(lightMapAOTex.rgb, LowExposure, HighExposure, Threshold); //expand to -1 to 1 range NormalMap.xyz = NormalMap.xyz * 2.0f - 1.0f; NormalMap.rgb = normalize(NormalMap.rgb); float3 N = NormalMap.rgb; float3 Nn = normalize(In.worldNormal); //input the vectors required for tangent to world space transform float3 Tn = normalize(In.worldTangent); float3 Bn = normalize(In.worldBinormal); float3 Nws = (Nn * N.z) + (N.x * Tn) + (N.y * -Bn); Nws = normalize(Nws); // aoLightMap contains the LightMap in the RGB channels and the AO map in the A channel float3 diffuseContribution = lightMapHDR.rgb; float3 ambientContribution = AmbientColor * lightMapAOTex.a; float4 specularContribution = SpecularFrontOn(Nws, V, V, SpecPower, SpecGloss) * SpecColor * NormalMap.a; float3 Material = (((ambientContribution + diffuseContribution) * diffuseTex.rgb) + specularContribution) * contactShadow; //------------------------------------------------------------------------------------------------------------------------ // OPACITY CODE SNIPPET. Function: Does a pixel-based alpha fade on objects of interest, so Camera can see our fighters. // Details: game sets opacity parameter to NOT = 1.0f. This triggers pixel-based fading. //------------------------------------------------------------------------------------------------------------------------- float constant_ONE = 1.0f; float final_opacity; if (constant_ONE == OpacityLevel) final_opacity = constant_ONE; // don't fade if calling code sets param to 1.0f. else { float distanceMod = 0.005; // this "works" For el matador. was 0.000005 float pixelDepth = (In.pixelDepth.x / In.pixelDepth.y); // divide Z and W from clip space vertex pos, to get depth per pixel if (pixelDepth < 0.0f) pixelDepth = 0.00001f; float pixelPower = pow(pixelDepth, PixelDepthPower); // opt this math float a = pixelPower * distanceMod; a = clamp(a, 0, 1); float pixel_alpha = 1.0f - a; //Subtract the input from one final_opacity = clamp(pixel_alpha, MinOpacityLevel, constant_ONE); //used at END of program.. //useful, found a bug with this :) //if (pixelDepth < 0.0f) //{ // Material.x = 1.0f; //RED if lt 0 // Material.y = 0.0f; // Material.z = 0.0f; //} } //------------------------------------------ END OPACITY CODE SNIPPET ----------------------------------------------------- return float4(Material, final_opacity); } technique Complete { pass ambient { VertexShader = compile vs_3_0 av(); ZEnable = true; ZWriteEnable = true; CullMode = cw; ShadeMode = Gouraud; AlphaBlendEnable = true; AlphaTestEnable = false; PixelShader = compile ps_3_0 af(); } }