//==============================Info================ ================ // Soft Light Skin Shader // @author jboldiga //================================================================== // includes #ifdef _MAX_ #include "../KFF_Macros" #include "lighting.fx" #else #include "c:/_project/Fatburger/Assets/Shaders/lighting.fx" #include "c:/_project/Fatburger/Assets/Shaders/KFF_Macros" #endif //===========================UI====================================== #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 DiffuseMap < string Name = "C:\\_project\\Fatburger\\Assets\\Characters\\_Common\\Common_Glove_DaS.dds"; string UIName = "Diffuse Map"; string type = "2D"; >; sampler2D DiffuseMapSampler = sampler_state { Texture = ; MinFilter = LINEAR; MagFilter = LINEAR; MipFilter = LINEAR; AddressU = WRAP; AddressV = WRAP; }; texture NormalMap < string Name = "C:\\_project\\Fatburger\\Assets\\Characters\\_Common\\Common_flat_N.dds"; string UIName = "Normal Map"; string type = "2D"; >; sampler2D NormalMapSampler = sampler_state { Texture = ; MinFilter = LINEAR; MagFilter = LINEAR; MipFilter = LINEAR; AddressU = CLAMP; AddressV = CLAMP; }; texture BloodPantsDecalTexture < string ResourceName = "C:\\_project\\Fatburger\\Assets\\Characters\\Fighters\\_CommonFighter\\black.dds"; string UIName = "Blood Pants Decal"; string ResourceType = "2D"; >; sampler2D BloodPantsDecal = sampler_state { Texture = ; MinFilter = LINEAR; MagFilter = LINEAR; MipFilter = LINEAR; AddressU = WRAP; AddressV = WRAP; }; float4 SpecColour < string UIWidget = "Color"; string UIName = "Specular Colour"; > = {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; float3 WetBloodColor < string UIName = "Wet Blood Color"; string UIWidget = "Color"; > = {0.30, 0.039, 0.068}; #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 //===========================Matrices================================ float4x4 wvp : WorldViewProjection < string UIType = "None"; >; float4x4 worldI : WorldInverse < string UIType = "None"; >; float4x4 worldinversetranspose : WorldInverseTranspose < string UIType = "None"; >; float4x4 viewinverse : ViewInverse < string UIType = "None"; >; float4x4 world : World < string UIType = "None"; >; float4x4 viewprojection : ViewProjection < string UIType = "None"; >; //================Specular component 1 (front on)======================= float4 FresnelMask( float3 Normals, float3 EyeVec) { float4 Fresnel = dot(EyeVec,Normals) * SpecFresnel; return saturate(Fresnel); } #ifdef _XBOX #define MAX_BONE_COUNT 129 //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 texCoord : TEXCOORD0; float2 bloodCoord : TEXCOORD1; #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 eyeVec : TEXCOORD0; float3 shadowUVZ : TEXCOORD1; float4 texCoord : TEXCOORD2; float3 worldTangent : TEXCOORD3; float3 worldBinormal : TEXCOORD4; float3 worldNormal : TEXCOORD5; float3 worldPosition : TEXCOORD6; float3 shadowLightVec : TEXCOORD7; }; //Vertex Shader v2f v(a2v In) { v2f Out = (v2f)0; float4 position, normal, tangent, binormal; normal = float4(In.normal.xyz, 0.0f); tangent = float4(In.tangent.xyz, 0.0f); #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); #else position = In.position; binormal = In.binormal; #endif float3 worldSpacePos = matmul(position, world).xyz; //world space position Out.eyeVec = mattrans(viewinverse) - worldSpacePos; Out.position = matmul(position, wvp); //transform vert position to homogeneous clip space Out.worldPosition = worldSpacePos; float3 light_vector = normalize(worldSpacePos - ShadowPos.xyz); float3 projectedPos = worldSpacePos+light_vector*worldSpacePos.z; // distance along light vector for self shadowing projectedPos.z = -worldSpacePos.z/light_vector.z; Out.shadowUVZ = EvaluateSelfShadowing(projectedPos); Out.shadowLightVec = light_vector; Out.texCoord.xy = In.texCoord; //pass through texture coordinates from channel 1 Out.texCoord.zw = In.bloodCoord; //pass the blood coordinates from channel 2 Out.worldNormal = matmul(normal, world).xyz; //compute world space normal Out.worldBinormal = matmul(binormal, world).xyz; //compute world space binormal Out.worldTangent = matmul(tangent, world).xyz; //compute world space tangent return Out; } //Pixel Shader float4 f(v2f In) : COLOR { float3 EV = normalize(In.eyeVec); float2 TexUV = In.texCoord.xy; float2 BloodUV = In.texCoord.zw; #ifdef _XBOX float4 DiffuseMap; asm { tfetch2D DiffuseMap, TexUV, DiffuseMapSampler, LODBias = -1.0 }; #else float4 DiffuseMap = tex2D(DiffuseMapSampler, TexUV); #endif float3 NormalMap = tex2D(NormalMapSampler, TexUV).xyz * 2.0f - 1.0f; // calculate blood normals and replace NormalMap if blood normals exist. Otherwise use NormalMap. float4 BloodDecalMap = tex2D(BloodPantsDecal, BloodUV); float hasBlood = BloodDecalMap.b; BloodDecalMap.rg = (BloodDecalMap.rg) * 2.0f - 1.0f; BloodDecalMap.b = sqrt(1.0f - saturate(BloodDecalMap.r * BloodDecalMap.r + BloodDecalMap.g * BloodDecalMap.g)); NormalMap = NormalMap * (1.0f - BloodDecalMap.a) + BloodDecalMap.rgb * BloodDecalMap.a; 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 N = Nn * NormalMap.z + NormalMap.x * Tn + NormalMap.y * Bn; //create a per-pixel normal for Y up NormalMap = normalize(N); //normalized the normal vector // Set diffuse to the blood color if blood exists in this fragment hasBlood = clamp(clamp(hasBlood, 0.0f, 1.0f) * 2.0f - 1.0f, 0.0f, 1.0f); float3 BloodColor = WetBloodColor * hasBlood; DiffuseMap.rgb = DiffuseMap.rgb * (1.0f - BloodDecalMap.a) + BloodColor * BloodDecalMap.a; // diffuse lighting #ifndef _XBOX float3 diffuseContribution = float3(0.0f, 0.0f, 0.0f); EvaluatePhong_DiffuseOnly(In.worldPosition, NormalMap, EV, diffuseContribution); // specular lighting float4 FresnelStrength = saturate(1.0f - FresnelMask(Nn,EV)) * FresnelPower + 1.0f; float4 FresnelGlossiness = FresnelMask(Nn,EV) * FresnelGloss + 1.0f; #endif #ifdef _XBOX float3 specularContribution; float3 diffuseContribution; EvaluatePhong(In.worldPosition, NormalMap, EV, SpecPower, SpecGloss, diffuseContribution, specularContribution); #else float3 specularContribution = SpecularFrontOn_MultiLight(NormalMap, EV, In.worldPosition, SpecPower * FresnelStrength, SpecGloss * FresnelGlossiness); #endif // Shadows float shadowValue = EvaluateShadow(In.shadowUVZ, NormalMap.rgb, -In.shadowLightVec); float4 shadowLightContribution = (float4(DiffuseMap.rgb, 1.0f) * shadowValue) * ShadowLightClr; float3 LightingOutput = (diffuseContribution * DiffuseMap.rgb) + shadowLightContribution.rgb + specularContribution; return float4(LightingOutput, 1.0f); } technique Complete { pass one { VertexShader = compile vs_3_0 v(); ZEnable = true; ZWriteEnable = true; CullMode = CW; ShadeMode = Gouraud; AlphaBlendEnable = false; AlphaTestEnable = FALSE; PixelShader = compile ps_3_0 f(); } }