//=================================================================== // Environment Floor Shader // @author jboldiga // Unlit shader, uses 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 #ifndef _MAX_ sampler2D bloodSampler; #endif 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; MaxAnisotropy = 16; }; #ifdef _MAX_ int texcoord0 : Texcoord < int Texcoord = 0; int MapChannel = 1; string UIType = "None"; >; int texcoord1 : Texcoord < int Texcoord = 1; int MapChannel = 2; string UIType = "None"; >; #endif 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; MaxAnisotropy = 16; }; 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; }; float3 AmbientColor : Ambient < string UIName = "Ambient Light"; string UIWidget = "Color"; > = {1.0f, 1.0f, 1.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 BloodGloss < string UIType = "FloatSpinner"; float UIMin = 1.0; float UIMax = 999999.0; float UIStep = 0.01; string UIName = "Blood Glossiness"; > = 32.0; float BloodSpec < string UIType = "FloatSpinner"; float UIMin = 1.0; float UIMax = 999999.0; float UIStep = 0.01; string UIName = "Blood Specular"; > = 20.0; float LowExposure < string UIWidget = "slider"; string UIName = "Low Exposure"; float UIMin = -100.0; float UIMax = 0.0; float UIStep = 0.5; > = -2.0; float HighExposure < string UIWidget = "slider"; string UIName = "High Exposure"; float UIMin = 0.0; float UIMax = 100.0; float UIStep = 0.5; > = 2.0; float Threshold < string UIWidget = "slider"; string UIName = "Threshold"; float UIMin = 0.0; float UIMax = 100.0; float UIStep = 0.1; > = 50.0f; float AODiffuseBlend < float UIMin = 0.0; float UIMax = 1.0; float UIStep = 0.1; string UIName = "AO Influence"; > = 0.5f; float4 WetBloodColor < string UIWidget = "Color"; string UIName = "Wet Blood Color"; > = {0.30f, 0.039f, 0.068f, 1.0f}; float4x4 wvp : WorldViewProjection < string UIType = "None"; >; float4x4 worldI : WorldInverse < string UIType = "None"; >; float4x4 worldIT : WorldInverseTranspose < string UIType = "None"; >; float4x4 viewinverse : ViewInverse < string UIType = "None"; >; float4x4 world : World < string UIType = "None"; >; // input from application struct a2v { float4 position : POSITION; float2 texCoord0 : TEXCOORD0; float2 texCoord1 : TEXCOORD1; float4 tangent : TANGENT; float4 binormal : BINORMAL; float4 normal : NORMAL; }; // output to fragment program struct v2f { float4 position : POSITION; float3 worldNormal : TEXCOORD0; float4 texCoord : TEXCOORD1; float3 lightVec : TEXCOORD2; float4 uv : TEXCOORD3; float3 eyeVec : TEXCOORD4; float3 worldTangent : TEXCOORD5; float3 worldBinormal : TEXCOORD6; float3 worldPosition : TEXCOORD7; }; //Vertex Shader v2f av(a2v In) { v2f Out = (v2f)0; float3 worldSpacePos = matmul(In.position, world).xyz; //world space position Out.texCoord.xy = In.texCoord0.xy; //pass through texture coordinates from channel 1 Out.texCoord.zw = In.texCoord1.xy; //pass through texture coordinates from channel 2 float4 osIPos = mattrans(viewinverse); // world space eye position float3 osIVec = osIPos.xyz - worldSpacePos; // world space eye vector Out.eyeVec = osIVec; float4 osLPos = float4(-osIPos.x, -osIPos.y, osIPos.z, 1.0); // Out.lightVec = osLPos.xyz - worldSpacePos; Out.lightVec = float3(6.8, 0, 1.18) - worldSpacePos; // shadow and blood buffer UV's float4 uvs = float4(1 - (worldSpacePos.y * 0.1 + 0.5), worldSpacePos.x * 0.1 + 0.5, 0.0f, 1.0f); Out.uv.xy = uvs.xy; Out.uv.zw = mul(shadowDownscaleMatrix, uvs).xy; //jboldiga - purposely did NOT use matmul... #ifdef _XBOX Out.uv.w = 1 - Out.uv.w; #endif 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; 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.texCoord.zw); float4 NormalMap = tex2D(NormalMapSampler, In.texCoord.xy); float4 ProjectedShadowColor = float4(0.5f, 0.5f, 0.5f, 0.0f); #ifdef _MAX_ float4 blood_color = float4(0.0f, 0.0f, 0.0f, 0.0f); float4 shadow_color = float4(1.0f, 1.0f, 1.0f, 1.0f) + ProjectedShadowColor; #else float4 blood_color = tex2D(bloodSampler, In.uv.xy); float4 shadow_color = tex2D(shadowdiffuseSampler, In.uv.zw) + ProjectedShadowColor; #endif float3 lightMapHDR = CalculateLightMapHDR(lightMapAOTex.rgb, LowExposure, HighExposure, Threshold); //expand to -1 to 1 range NormalMap.rgb = lerp( NormalMap.rgb , blood_color.rgb, blood_color.a) ; NormalMap.xyz = NormalMap.xyz * 2.0f - 1.0f; NormalMap.rgb = normalize(NormalMap.rgb); float3 N = NormalMap.rgb; float3 Nn = normalize(In.worldNormal); float3 Tn = normalize(In.worldTangent); float3 Bn = normalize(In.worldBinormal); float3 Nws = (Nn * N.z) + (N.x * Tn) + (N.y * -Bn); Nws = normalize(Nws); // blood spec and gloss SpecGloss = lerp(SpecGloss, BloodGloss, blood_color.a); SpecColor = SpecColor * (1.0f - blood_color.a) + BloodSpec * blood_color.a; // Shadow and Blood color float4 diffuseColor = lerp( diffuseTex, WetBloodColor, blood_color.a) ; float3 diffuseContribution = lightMapHDR.rgb; float3 ambientContribution = AmbientColor * lightMapAOTex.a; // modulate ambient light by AO map // Calculate dot products float3 L = normalize(In.lightVec); float3 H = normalize((L+V)/2); float NdotL = dot(Nws, L); float NdotH = dot(Nws, H); // Phong shading half4 light = lit(NdotL, NdotH, SpecGloss); half3 specularContribution = light.zzz*NormalMap.a* SpecColor; // finalize material color float3 Material = (ambientContribution * diffuseColor.rgb) + (diffuseContribution * diffuseColor.rgb * saturate(lerp(1.0, lightMapAOTex.a, AODiffuseBlend))) + specularContribution; Material = Material* shadow_color; return float4(Material, 1.0f); } technique Complete { pass ambient { VertexShader = compile vs_3_0 av(); ZEnable = true; ZWriteEnable = true; CullMode = cw; ShadeMode = Gouraud; AlphaBlendEnable = false; AlphaTestEnable = false; PixelShader = compile ps_3_0 af(); } }