//=================================================================== // Hair shader // @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"; >; //jboldiga - No mip-mapping allows the hair to look better from a distance sampler2D diffuseTextureSampler = sampler_state { Texture = ; MinFilter = NONE; MagFilter = NONE; MipFilter = NONE; AddressU = WRAP; AddressV = WRAP; }; #ifndef _XBOX texture rampTexture < string Name = "EUPHBARK.TGA"; string UIName = "Anisotropic Ramp"; string ResourceType = "2D"; >; sampler2D rampMapSampler = sampler_state { Texture = ; MinFilter = LINEAR; MagFilter = LINEAR; MipFilter = LINEAR; AddressU = WRAP; AddressV = WRAP; }; #endif 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; }; float AnisotropicPower < float UIMin = -999.0; float UIMax = 999.0; float UIStep = 0.1; string UIName = "Anisotropic Power"; > = 1.8; float3 SpecularColor < string UIName = "Specular Color"; string Type = "Color"; > = {0.266667, 0.337255, 0.396078}; float FresnelPower < string UIType = "FloatSpinner"; float UIMin = 0.0; float UIMax = 100.0; float UIStep = 0.01; string UIName = "Fresnel Power"; > = 1.75; float FresnelScale < string UIType = "FloatSpinner"; float UIMin = 0.0; float UIMax = 100.0; float UIStep = 0.01; string UIName = "Fresnel Scale"; > = 1.02; float FresnelBias < string UIType = "FloatSpinner"; float UIMin = -100.0; float UIMax = 100.0; float UIStep = 0.01; string UIName = "Fresnel Bias"; > = 0.68; 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; }; //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; 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); float4 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); float NdotL1 = saturate(dot(LV1, Nws.rgb)); float NdotL2 = saturate(dot(LV2, Nws.rgb)); float NdotL3 = saturate(dot(LV3, Nws.rgb)); float3 LV1_Eye = normalize(LV1 + V); float3 LV2_Eye = normalize(LV2 + V); float3 LV3_Eye = normalize(LV3 + V); float NdotLEye1 = saturate(dot(LV1_Eye, Nws.rgb)); float NdotLEye2 = saturate(dot(LV2_Eye, Nws.rgb)); float NdotLEye3 = saturate(dot(LV3_Eye, Nws.rgb)); float2 RampCoord1 = float2(NdotL1, NdotLEye1); float2 RampCoord2 = float2(NdotL2, NdotLEye2); float2 RampCoord3 = float2(NdotL3, NdotLEye3); #ifdef _XBOX float val = 1.0f - (1.0f / ((1.0f-RampCoord1.x) + 1.0f)); float3 RampMap1 = float3(val, val, val); val = 1.0f - (1.0f / ((1.0f-RampCoord2.x) + 1.0f)); float3 RampMap2 = float3(val, val, val); val = 1.0f - (1.0f / ((1.0f-RampCoord3.x) + 1.0f)); float3 RampMap3 = float3(val, val, val); SpecularColor.rgb = SpecularColor.rgb * 0.5f; #else float4 RampMap1 = tex2D(rampMapSampler, RampCoord1.xy); float4 RampMap2 = tex2D(rampMapSampler, RampCoord2.xy); float4 RampMap3 = tex2D(rampMapSampler, RampCoord3.xy); #endif float NdotEye_fresnel = saturate(dot(V, Nws.rgb)); float Pow_NdotEye = pow(NdotEye_fresnel, FresnelPower); float3 black = float3(0.0, 0.0, 0.0); float3 ramp = lerp(SpecularColor.rgb, black, ((Pow_NdotEye * FresnelScale) + FresnelBias)); ramp = saturate(ramp); float3 outColor1 = (((((RampMap1.rgb * AnisotropicPower) * SpecularColor.rgb) * SpecMap.rgb) + diffuseTex.rgb) + ramp); float3 outColor2 = (((((RampMap2.rgb * AnisotropicPower) * SpecularColor.rgb) * SpecMap.rgb) + diffuseTex.rgb) + ramp); float3 outColor3 = (((((RampMap3.rgb * AnisotropicPower) * SpecularColor.rgb) * SpecMap.rgb) + diffuseTex.rgb) + ramp); outColor1 *= NdotL1; outColor2 *= NdotL2; outColor3 *= NdotL3; outColor1 *= lightClr1; outColor2 *= lightClr2; outColor3 *= lightClr3; float3 Material = outColor1 + outColor2 + outColor3; 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(); } }