//=================================================================== // Hair shader // @author jboldiga // The most expensive hair shader ever. //=================================================================== #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 SpecularColor < string UIName = "Specular Color"; string Type = "Color"; > = {0.5, 0.5, 0.5}; float SpecularPower < float UIMin = 1.0; float UIMax = 256.0; float UIStep = 0.1; string UIName = "Specular Power"; > = 30.0; float SpecSharpMarker < float UIMin = 0.0; float UIMax = 1; float UIStep = 0.1; string UIName = "Spec Sharpness Marker"; > = 0.72; float SpecSharpness < float UIMin = 0.0; float UIMax = 1; float UIStep = 0.1; string UIName = "Spec Edge Sharpness"; > = 0.85; float SpecFresnelExp < float UIMin = 0.0; float UIMax = 5.0; float UIStep = 0.1; string UIName = "Specular Fresnel Exponent"; > = 0.0; float SpecFresnelScale < float UIMin = 1.0; float UIMax = 15.0; float UIStep = 0.1; string UIName = "Specular Fresnel Multiplier"; > = 1.0; float SpecShift < float UIMin = -0.5; float UIMax = 1; float UIStep = 0.01; string UIName = "Specular Highlight Shift"; > = 0.0; float SpecNoiseScale < float UIMin = 0.0; float UIMax = 1.0; float UIStep = 0.1; string UIName = "Specular Noise Map Scale"; > = 1.0; 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"; >; #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 texCoord : 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 texCoord : TEXCOORD1; float3 eyeVec : TEXCOORD3; float3 worldTangent : TEXCOORD4; float3 worldBinormal : TEXCOORD5; float3 worldPosition : TEXCOORD6; }; // kajiyaKay specular lighting model based on tangent and binormal direction float3 kajiyaKay (float3 N, float3 Bn, float3 V, float3 L, float specNoise) { float3 B = normalize(Bn + N * specNoise); float3 H = normalize(L + V); return sqrt(1-pow(dot(B,H),2)); } float3 lambertHair (float3 L, float3 N) { return smoothstep(-0.1,0.75, lerp(0.25,1.0,dot(N,L)) ); } //Vertex Shader v2f av(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( normal, LocalToWorldMatrix ), 0.0f); tangent = float4(mul( tangent, LocalToWorldMatrix ), 0.0f); } binormal = float4(cross(normal.xyz, tangent.xyz), 0.0f); #else position = In.position; normal = In.normal; tangent = In.tangent; binormal = In.binormal; #endif float3 worldSpacePos = matmul(position, world).xyz; //world space position Out.texCoord = In.texCoord; //pass through texture coordinates float4 osIPos = mattrans(viewinverse); // 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, 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 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 NormalMap = tex2D(NormalMapSampler, In.texCoord.xy); float SpecMap = NormalMap.a; //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); #ifdef _MAX_ float4 lightClr1 = float4(0.5f, 0.5f, 0.5f, 0.0f); float4 lightClr2 = float4(0.5f, 0.5f, 0.5f, 0.0f); float4 lightClr3 = float4(0.5f, 0.5f, 0.5f, 0.0f); float4 lightPos1 = float4(1403.0f, 1441.0f, 1690.0f, 0.0f); float4 lightPos2 = float4(1403.0f, 1441.0f, 1690.0f, 0.0f); float4 lightPos3 = float4(1403.0f, 1441.0f, 1690.0f, 0.0f); #else float4 lightClr1 = lightColor0SL; float4 lightClr2 = lightColor1SL; float4 lightClr3 = lightColor2SL; float4 lightPos1 = lightPositionWS0SL; float4 lightPos2 = lightPositionWS1SL; float4 lightPos3 = lightPositionWS2SL; #endif float3 lightVec1 = lightPos1.xyz - In.worldPosition; float3 lightVec2 = lightPos2.xyz - In.worldPosition; float3 lightVec3 = lightPos3.xyz - In.worldPosition; float3 LV1 = normalize(lightVec1.xyz); float3 LV2 = normalize(lightVec2.xyz); float3 LV3 = normalize(lightVec3.xyz); // fresnel float fresnel = max(0, pow(abs(1.0 - dot(Nws, V)), SpecFresnelExp)) * SpecFresnelScale; // lambert hair float3 diffuse1 = lambertHair(LV1, Nws) * lightClr1; float3 diffuse2 = lambertHair(LV2, Nws) * lightClr2; float3 diffuse3 = lambertHair(LV3, Nws) * lightClr3; float specNoise = (((SpecMap * 2) - 1) * SpecNoiseScale) + SpecShift; // The first one is from light reflecting off the surface, and is shifted towards the tip of the hair float3 Spec1_1 = smoothstep(SpecSharpMarker - SpecSharpness, SpecSharpMarker + SpecSharpness, pow(kajiyaKay(Nws, Bn, V, LV1, specNoise), SpecularPower)); float3 Spec2_1 = smoothstep(SpecSharpMarker - SpecSharpness, SpecSharpMarker + SpecSharpness, pow(kajiyaKay(Nws, Bn, V, LV2, specNoise), SpecularPower)); float3 Spec3_1 = smoothstep(SpecSharpMarker - SpecSharpness, SpecSharpMarker + SpecSharpness, pow(kajiyaKay(Nws, Bn, V, LV3, specNoise), SpecularPower)); // The second highlight is from light that is transmitted into the hair strand and reflected back out towards the viewer. float3 Spec1_2 = pow(kajiyaKay(Nws, Bn, V, LV1, specNoise + 0.15), SpecularPower / 4) * diffuseTex.rgb; float3 Spec2_2 = pow(kajiyaKay(Nws, Bn, V, LV2, specNoise + 0.15), SpecularPower / 4) * diffuseTex.rgb; float3 Spec3_2 = pow(kajiyaKay(Nws, Bn, V, LV3, specNoise + 0.15), SpecularPower / 4) * diffuseTex.rgb; float3 diffuseContribution = (diffuse1 + diffuse2 + diffuse3) * diffuseTex.rgb; float3 Specular1 = smoothstep(0, 0.25, lambertHair(LV1, Nws)) * (Spec1_1 + Spec1_2) * lightClr1 * SpecularColor * fresnel; float3 Specular2 = smoothstep(0, 0.25, lambertHair(LV2, Nws)) * (Spec2_1 + Spec2_2) * lightClr2 * SpecularColor * fresnel; float3 Specular3 = smoothstep(0, 0.25, lambertHair(LV3, Nws)) * (Spec3_1 + Spec3_2) * lightClr3 * SpecularColor * fresnel; float3 Material = diffuseContribution + Specular1 + Specular2 + Specular3; return float4(Material, diffuseTex.a); } technique Complete { pass ambient { VertexShader = compile vs_3_0 av(); ZEnable = true; ZWriteEnable = true; CullMode = cw; ShadeMode = Gouraud; AlphaBlendEnable = true; SrcBlend = srcalpha; DestBlend = invsrcalpha; PixelShader = compile ps_3_0 af(); } }