//=================================================================== // Fresnel Rim Lighting Audience Shader 2 Lights // @author jboldiga //=================================================================== #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 #ifdef _XBOX // Vertex shader texture sampler for the bone matrix palette. #define POINT_SAMPLER sampler_state { MipFilter = NONE; MinFilter = POINT; MagFilter = POINT; AddressU = CLAMP; AddressV = CLAMP; } sampler1D bone_palette_texture = POINT_SAMPLER; int bone_count; #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; }; float3 AmbientColor : Ambient < string UIName = "Ambient Light"; string UIWidget = "Color"; > = {0.07f,0.07f,0.07f}; float4 SpecColor < string UIWidget = "Color"; string UIName = "Specular Color"; > = {0.318f, 0.553f, 0.910f, 1.0f}; float SpecPower < string UIWidget = "Slider"; float UIMin = 0.0; float UIMax = 100.0; float UIStep = 0.01; string UIName = "Specular Power"; > = 0.899; float SpecGloss < string UIWidget = "Slider"; float UIMin = 0.0; float UIMax = 100.0; float UIStep = 0.01; string UIName = "Specular Glossiness"; > = 2.75; float SpecFresnel < string UIWidget = "Slider"; float UIMin = 0.0; float UIMax = 100.0; float UIStep = 0.01; string UIName = "Specular Fresnel"; > = 1.84; float FresnelPower < string UIWidget = "Slider"; float UIMin = 0.0; float UIMax = 100.0; float UIStep = 0.01; string UIName = "Specular Fresnel Power"; > = 3.88; float FresnelGloss < string UIWidget = "Slider"; float UIMin = 0.0; float UIMax = 100.0; float UIStep = 0.01; string UIName = "Specular Fresnel Gloss"; > = 2.61; 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"; >; #ifdef _XBOX //jboldiga 2011-01-27: Fetch bone from the bone pallete in the texture cache float4x3 TFetchBoneMatrix( float BoneIndex ) { float TexCoord = BoneIndex / bone_count; float4 Result1; float4 Result2; float4 Result3; asm { tfetch1D Result1, TexCoord, bone_palette_texture, UseComputedLOD = false, OffsetX = 0.5 tfetch1D Result2, TexCoord, bone_palette_texture, UseComputedLOD = false, OffsetX = 1.5 tfetch1D Result3, TexCoord, bone_palette_texture, UseComputedLOD = false, OffsetX = 2.5 }; float4x3 Result; Result._11_21_31_41 = Result1.xyzw; Result._12_22_32_42 = Result2.xyzw; Result._13_23_33_43 = Result3.xyzw; return Result; } #endif // input from application struct a2v { float4 position : POSITION; float2 texCoord0 : TEXCOORD0; #ifdef _XBOX float4 boneIndices : BLENDINDICES; float4 boneWeights : BLENDWEIGHT; float4 tangent : TANGENT; float4 normal : NORMAL; #else float4 tangent : TANGENT; float4 binormal : BINORMAL; float4 normal : NORMAL; #endif }; // output to fragment program struct v2f { float4 position : POSITION; float3 worldNormal : TEXCOORD0; float2 texCoord0 : TEXCOORD1; float3 eyeVec : TEXCOORD4; float3 worldTangent : TEXCOORD5; float3 worldBinormal : TEXCOORD6; float3 worldPosition : TEXCOORD7; }; //Vertex Shader v2f av(a2v In) { v2f Out = (v2f)0; float4 position, normal, tangent, binormal; #ifdef _XBOX float BoneWeights[4] = (float[4])In.boneWeights; int BoneIndices[4] = (int[4])In.boneIndices; float4x3 LocalToWorldMatrix = 0; for( int i = 3; i >= 0; --i ) { LocalToWorldMatrix += BoneWeights[i] * TFetchBoneMatrix( BoneIndices[i] ); } [isolate] { position = float4(mul( In.position, LocalToWorldMatrix ), 1.0f); normal = float4(mul( In.normal, LocalToWorldMatrix ), 0.0f); tangent = float4(mul( In.tangent, LocalToWorldMatrix ), 0.0f); } binormal = float4(cross(normal.xyz, tangent.xyz), 0.0f); //position = matmul(position, worldI); #else position = In.position; normal = In.normal; tangent = In.tangent; binormal = In.binormal; #endif float3 worldSpacePos = matmul(position, world).xyz; //world space position Out.texCoord0 = In.texCoord0; //pass through texture coordinates from channel 1 float4 osIPos = mattrans(viewInv); // world space eye position float3 osIVec = osIPos.xyz - worldSpacePos; // world space eye vector Out.eyeVec = osIVec; Out.position = matmul(position, wvp); //transform vert position to homogeneous clip space Out.worldNormal = matmul(normal, worldIT).xyz; //compute world space normal Out.worldBinormal = matmul(binormal, worldIT).xyz; //compute world space binormal Out.worldTangent = matmul(tangent, worldIT).xyz; //compute world space tangent Out.worldPosition = worldSpacePos; return Out; } //Pixel Shader float4 af(v2f In) : COLOR { float3 EV = 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; 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); #ifdef _MAX_ float4 lightPos1 = float4(1403.0f, 1441.0f, 1690.0f, 0.0f); float4 lightPos2 = float4(1403.0f, 1441.0f, 1690.0f, 0.0f); #else float4 lightPos1 = lightPositionWS0SL; float4 lightPos2 = lightPositionWS1SL; #endif float3 lightVec1 = lightPos1.xyz - In.worldPosition; float3 lightVec2 = lightPos2.xyz - In.worldPosition; float3 LV1 = normalize(lightVec1.xyz); float3 LV2 = normalize(lightVec2.xyz); // specular lighting float4 FresnelMask = dot(EV, Nn) * SpecFresnel; float4 FresnelStrength = saturate(1.0f - FresnelMask) * FresnelPower + 1.0f; float4 FresnelGlossiness = FresnelMask * FresnelGloss + 1.0f; float4 Spec1 = SpecularFrontOn(Nws, EV, LV1, SpecPower * FresnelStrength, SpecGloss * FresnelGlossiness) * SpecColor; float4 Spec2 = SpecularFrontOn(Nws, EV, LV2, SpecPower * FresnelStrength, SpecGloss * FresnelGlossiness) * SpecColor; // diffuse lighting float3 ambientContribution = AmbientColor; float3 diffuseContribution = float3(0.0f, 0.0f, 0.0f); EvaluatePhong_DiffuseOnly_SL_2Lights(In.worldPosition, Nws, EV, diffuseContribution); float3 Material = ((diffuseContribution + ambientContribution) * diffuseTex.rgb) + Spec1 + Spec2; 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(); } }