//=================================================================== // Environment Diffuse Normal Specular LightMap Ambient Occlusion Shader // @author jboldiga // No dynamic lighting. Uses HDR lightmaps 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 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 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; }; #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"; >; #endif 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; 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; float4 tangent : TANGENT; float4 binormal : BINORMAL; float4 normal : NORMAL; }; // output to fragment program struct v2f { float4 position : POSITION; float3 worldNormal : TEXCOORD0; float2 texCoord0 : TEXCOORD1; float3 eyeVec : TEXCOORD3; float3 worldTangent : TEXCOORD4; float3 worldBinormal : TEXCOORD5; }; //Vertex Shader v2f av(a2v In) { v2f Out = (v2f)0; float3 worldSpacePos = matmul(In.position, world).xyz; //world space position Out.texCoord0 = In.texCoord0; //pass through texture coordinates from channel 1 float4 osIPos = mattrans(viewinverse); // 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 return Out; } //Pixel Shader float4 af(v2f In) : COLOR { float3 V = normalize(In.eyeVec); //creating the eye vector float4 diffuseTex = tex2D(diffuseTextureSampler, In.texCoord0.xy); float4 NormalMap = tex2D(NormalMapSampler, In.texCoord0.xy); //expand to -1 to 1 range NormalMap.xyz = NormalMap.xyz * 2.0f - 1.0f; float3 N = normalize(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); float4 specularContribution = SpecularFrontOn(Nws, V, V, SpecPower, SpecGloss) * SpecColor * NormalMap.a; float3 Material = (AmbientColor * diffuseTex.rgb) + diffuseTex.rgb + specularContribution.rgb; 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(); } }