//=================================================================== // Audience shader with lighting based on the origin of the scene // @author jboldiga //=================================================================== #ifdef _MAX_ // Max includes are in ../3dsMax/MaterialLibraries/ #include "../KFF_Macros" #else #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 TextureMap < string Name = "C:\\_project\\Fatburger\\Assets\\Characters\\Audience\\Textures\\aud_02_M_00_D.dds"; string UIName = "Diffuse Map"; string ResourceType = "2D"; >; sampler2D TextureMapSampler = sampler_state { Texture = ; MinFilter = LINEAR; MagFilter = LINEAR; MipFilter = LINEAR; AddressU = WRAP; AddressV = WRAP; }; float audienceFalloff : FallOff < string UIWidget = "slider"; float UIMin = 0.0f; float UIMax = 100.0f; float UIStep = 0.01f; string UIName = "Falloff Value"; > = 1.0f; float intensity : Intensity < string UIWidget = "slider"; float UIMin = 0.0f; float UIMax = 100.0f; float UIStep = 0.01f; string UIName = "Brightness"; > = 1.0f; float audienceVerticalFalloff : VerticalFallOff < string UIWidget = "slider"; float UIMin = 0.0f; float UIMax = 100.0f; float UIStep = 0.01f; string UIName = "Vertical Falloff"; > = 0.0f; float audienceVerticalFalloffOffset : VerticalFallOffOffset < string UIWidget = "slider"; float UIMin = 0.0f; float UIMax = 100.0f; float UIStep = 0.01f; string UIName = "Vertical Falloff Offset"; > = 1.0f; float audienceGlobalBrightness : GlobalBrightness < string UIWidget = "slider"; float UIMin = 0.0f; float UIMax = 100.0f; float UIStep = 0.01f; string UIName = "Global Brightness"; > = 1.0f; float brightnessUpperClamp : BrightnessUpperClamp < string UIWidget = "slider"; float UIMin = 0.0f; float UIMax = 100.0f; float UIStep = 0.01f; string UIName = "Brightness Upper Clamp"; > = 1.1f; float brightnessLowerClamp : BrightnessLowerClamp < string UIWidget = "slider"; float UIMin = 0.0f; float UIMax = 100.0f; float UIStep = 0.01f; string UIName = "Brightness Lower Clamp"; > = 0.16f; float3 audienceAmbientColor < string UIName = "Audience Ambient Color"; string UIWidget = "Color"; > = {1.0, 1.0, 1.0}; float4x4 wvp : WorldViewProjection < string UIWidget = "None"; >; float4x4 model : Model < string UIWidget = "None"; >; float4x4 worldI : WorldInverse < string UIWidget = "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 { float3 position : POSITION; float3 normal : NORMAL; float2 texCoord : TEXCOORD0; #ifdef _XBOX float4 boneIndices : BLENDINDICES; float4 boneWeights : BLENDWEIGHT; #endif }; // output to fragment program struct V2F { float4 position : POSITION; float2 texCoord : TEXCOORD0; float2 brightness : TEXCOORD1; }; // Ambient Pass Vertex Shader: V2F av(a2v In) { V2F Out = (V2F)0; float4 position, normal; position = float4(In.position, 1.0f); normal = float4(In.normal, 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( position, LocalToWorldMatrix ), 1.0f); normal = float4(mul( normal, LocalToWorldMatrix ), 0.0f); } //position = matmul(position, worldI); #endif float4 worldPos = matmul(position, model); //transform vert position to world space Out.position = matmul(position, wvp); //transform vert position to homogeneous clip space Out.texCoord = In.texCoord; //pass through texture coordinates from channel 1 float len = length(worldPos) / 5.0f; intensity = dot(normalize(-worldPos), matmul(normal, model)) / len; Out.brightness.x = (clamp((intensity + 0.2f/(len * len * audienceFalloff)) * (audienceVerticalFalloffOffset + audienceVerticalFalloff * position.z), brightnessLowerClamp, brightnessUpperClamp)) * audienceGlobalBrightness; return Out; } // Ambient Pass Pixel Shader: float4 af(V2F In) : COLOR { float4 TextureMap = tex2D(TextureMapSampler, In.texCoord.xy); TextureMap.rgb *= audienceAmbientColor; return float4 ((TextureMap * In.brightness.x).xyz, 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(); } }