/* Shader description: APPLYMODE = 1 WORLDPOSITION = 1 WORLDNORMAL = 1 WORLDNBT = 0 WORLDVIEW = 0 NORMALMAPTYPE = 0 PARALLAXMAPCOUNT = 0 BASEMAPCOUNT = 0 NORMALMAPCOUNT = 0 DARKMAPCOUNT = 0 DETAILMAPCOUNT = 0 BUMPMAPCOUNT = 0 GLOSSMAPCOUNT = 0 GLOWMAPCOUNT = 0 CUSTOMMAP00COUNT = 0 CUSTOMMAP01COUNT = 0 CUSTOMMAP02COUNT = 0 CUSTOMMAP03COUNT = 0 CUSTOMMAP04COUNT = 0 DECALMAPCOUNT = 0 FOGENABLED = 1 ENVMAPTYPE = 0 PROJLIGHTMAPCOUNT = 0 PROJLIGHTMAPTYPES = 0 PROJLIGHTMAPCLIPPED = 0 PROJSHADOWMAPCOUNT = 0 PROJSHADOWMAPTYPES = 0 PROJSHADOWMAPCLIPPED = 0 PERVERTEXLIGHTING = 0 UVSETFORMAP00 = 0 UVSETFORMAP01 = 0 UVSETFORMAP02 = 0 UVSETFORMAP03 = 0 UVSETFORMAP04 = 0 UVSETFORMAP05 = 0 UVSETFORMAP06 = 0 UVSETFORMAP07 = 0 UVSETFORMAP08 = 0 UVSETFORMAP09 = 0 UVSETFORMAP10 = 0 UVSETFORMAP11 = 0 POINTLIGHTCOUNT = 0 SPOTLIGHTCOUNT = 0 DIRLIGHTCOUNT = 2 SHADOWMAPFORLIGHT = 2 SPECULAR = 0 AMBDIFFEMISSIVE = 0 LIGHTINGMODE = 1 APPLYAMBIENT = 1 BASEMAPALPHAONLY = 0 APPLYEMISSIVE = 1 SHADOWTECHNIQUE = 0 ALPHATEST = 0 */ //--------------------------------------------------------------------------- // Types: #if defined(DIRECT3D) #define BONEMATRIX_TYPE float4x3 #else #define BONEMATRIX_TYPE float3x4 #endif //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- // Constant variables: //--------------------------------------------------------------------------- float4 g_AmbientLight; float4 g_MaterialEmissive; float4 g_MaterialDiffuse; float4 g_MaterialAmbient; float4 g_DirAmbient0; float4 g_DirDiffuse0; float4 g_DirSpecular0; float4 g_DirWorldPosition0; float4 g_DirWorldDirection0; float2 g_ShadowDirTexSizeShadow0; float g_ShadowDirShadowBias0; float4x4 g_ShadowDirWorldToShadowMapMatrix0; sampler2D DirShadow; float4 g_ShadowDirAmbient0; float4 g_ShadowDirDiffuse0; float4 g_ShadowDirSpecular0; float4 g_FogColor; //--------------------------------------------------------------------------- // Functions: //--------------------------------------------------------------------------- /* This fragment is responsible for normalizing a float3. */ void NormalizeFloat3(float3 VectorIn, out float3 VectorOut) { VectorOut = normalize(VectorIn); } //--------------------------------------------------------------------------- /* Separate a float4 into a float3 and a float. */ void SplitColorAndOpacity(float4 ColorAndOpacity, out float3 Color, out float Opacity) { Color.rgb = ColorAndOpacity.rgb; Opacity = ColorAndOpacity.a; } //--------------------------------------------------------------------------- /* This fragment is responsible for calculating the Shadow coefficient. */ void ShadowMap(float4 WorldPos, int LightType, sampler2D ShadowMap, float2 ShadowMapSize, float4x4 WorldToLightProjMat, float ShadowBias, out float ShadowOut) { if ( LightType != 0) { // This fragment only supports directional lights. ShadowOut = 0.0; }else{ float4 LightProjPos = mul(WorldPos, WorldToLightProjMat); float2 ShadowTexC = 0.5 * LightProjPos.xy / LightProjPos.w + float2( 0.5, 0.5 ); ShadowTexC.y = 1.0f - ShadowTexC.y; float2 lerps = frac( ShadowTexC * ShadowMapSize); float LightSpaceDepth = saturate(LightProjPos.z / LightProjPos.w); float2 borderTest = saturate(ShadowTexC) - ShadowTexC; if (any(borderTest)) { ShadowOut = 1.0; }else{ float ShadowMapSizeInverse = 1.0f / ShadowMapSize; LightSpaceDepth -= ShadowBias; float4 SourceVals; SourceVals.x = tex2D( ShadowMap, ShadowTexC ).r; ShadowTexC.x += ShadowMapSizeInverse; SourceVals.y = tex2D( ShadowMap, ShadowTexC).r; ShadowTexC.y += ShadowMapSizeInverse; SourceVals.w = tex2D( ShadowMap, ShadowTexC ).r; ShadowTexC.x -= ShadowMapSizeInverse; SourceVals.z = tex2D( ShadowMap, ShadowTexC).r; float4 Shade = (LightSpaceDepth <= SourceVals); ShadowOut = lerp(lerp(Shade.x, Shade.y, lerps.x), lerp(Shade.z, Shade.w, lerps.x), lerps.y ); } } } //--------------------------------------------------------------------------- /* This fragment is responsible for accumulating the effect of a light on the current pixel. LightType can be one of three values: 0 - Directional 1 - Point 2 - Spot Note that the LightType must be a compile-time variable, not a runtime constant/uniform variable on most Shader Model 2.0 cards. The compiler will optimize out any constants that aren't used. Attenuation is defined as (const, linear, quad, range). Range is not implemented at this time. SpotAttenuation is stored as (cos(theta/2), cos(phi/2), falloff) theta is the angle of the inner cone and phi is the angle of the outer cone in the traditional DX manner. Gamebryo only allows setting of phi, so cos(theta/2) will typically be cos(0) or 1. To disable spot effects entirely, set cos(theta/2) and cos(phi/2) to -1 or lower. */ void Light(float4 WorldPos, float3 WorldNrm, int LightType, bool SpecularEnable, float Shadow, float3 WorldViewVector, float4 LightPos, float3 LightAmbient, float3 LightDiffuse, float3 LightSpecular, float3 LightAttenuation, float3 LightSpotAttenuation, float3 LightDirection, float4 SpecularPower, float3 AmbientAccum, float3 DiffuseAccum, float3 SpecularAccum, out float3 AmbientAccumOut, out float3 DiffuseAccumOut, out float3 SpecularAccumOut) { // Get the world space light vector. float3 LightVector; float DistanceToLight; float DistanceToLightSquared; if (LightType == 0) { LightVector = -LightDirection; } else { LightVector = LightPos - WorldPos; DistanceToLightSquared = dot(LightVector, LightVector); DistanceToLight = length(LightVector); LightVector = normalize(LightVector); } // Take N dot L as intensity. float LightNDotL = dot(LightVector, WorldNrm); float LightIntensity = max(0, LightNDotL); float Attenuate = Shadow; if (LightType != 0) { // Attenuate Here Attenuate = LightAttenuation.x + LightAttenuation.y * DistanceToLight + LightAttenuation.z * DistanceToLightSquared; Attenuate = max(1.0, Attenuate); Attenuate = 1.0 / Attenuate; Attenuate *= Shadow; if (LightType == 2) { // Get intensity as cosine of light vector and direction. float CosAlpha = dot(-LightVector, LightDirection); // Factor in inner and outer cone angles. float AttenDiff = LightSpotAttenuation.x - LightSpotAttenuation.y; CosAlpha = saturate((CosAlpha - LightSpotAttenuation.y) / AttenDiff); // Power to falloff. // The pow() here can create a NaN if CosAlpha is 0 or less. // On some cards (GeForce 6800), the NaN will propagate through // a ternary instruction, so we need two to be safe. float origCosAlpha = CosAlpha; CosAlpha = origCosAlpha <= 0.0 ? 1.0 : CosAlpha; CosAlpha = pow(CosAlpha, LightSpotAttenuation.z); CosAlpha = origCosAlpha <= 0.0 ? 0.0 : CosAlpha; // Multiply the spot attenuation into the overall attenuation. Attenuate *= CosAlpha; } } // Determine the interaction of diffuse color of light and material. // Scale by the attenuated intensity. DiffuseAccumOut = DiffuseAccum; DiffuseAccumOut.rgb += LightDiffuse.rgb * LightIntensity * Attenuate; // Determine ambient contribution - Is affected by shadow AmbientAccumOut = AmbientAccum; AmbientAccumOut.rgb += LightAmbient.rgb * Attenuate; SpecularAccumOut = SpecularAccum; if (SpecularEnable) { // Get the half vector. float3 LightHalfVector = LightVector + WorldViewVector; LightHalfVector = normalize(LightHalfVector); // Determine specular intensity. float LightNDotH = max(0, dot(WorldNrm, LightHalfVector)); float LightSpecIntensity = pow(LightNDotH, SpecularPower.x); //if (LightNDotL < 0.0) // LightSpecIntensity = 0.0; // Must use the code below rather than code above. // Using previous lines will cause the compiler to generate incorrect // output. // Attenuate Here LightSpecIntensity = LightSpecIntensity * Attenuate; // Determine the interaction of specular color of light and material. // Scale by the attenuated intensity. SpecularAccumOut.rgb += LightSpecIntensity * LightSpecular; } } //--------------------------------------------------------------------------- /* XRD649 Shadow Light Pixel Shader */ void XRD649ShadowLight(float Shadow, float3 LightAmbient, float3 LightDiffuse, float3 LightSpecular, float3 AmbientAccum, float3 DiffuseAccum, float3 SpecularAccum, out float3 AmbientAccumOut, out float3 DiffuseAccumOut, out float3 SpecularAccumOut, out float AttenuateOut) { float Attenuate = saturate( LightAmbient.r * (((1.0/LightAmbient.r)-1.0) + Shadow)); DiffuseAccumOut = DiffuseAccum * Attenuate; AmbientAccumOut = AmbientAccum * Attenuate; SpecularAccumOut = SpecularAccum * Attenuate; AttenuateOut = Attenuate; } //--------------------------------------------------------------------------- /* This fragment is responsible for computing the coefficients for the following equations: Kdiffuse = MatEmissive + MatAmbient * Summation(0...N){LightAmbientContribution[N]} + MatDiffuse * Summation(0..N){LightDiffuseContribution[N]} Kspecular = MatSpecular * Summation(0..N){LightSpecularContribution[N]} */ void ComputeShadingCoefficients(float3 MatEmissive, float3 MatDiffuse, float3 MatAmbient, float3 MatSpecular, float3 LightSpecularAccum, float3 LightDiffuseAccum, float3 LightAmbientAccum, bool Saturate, out float3 Diffuse, out float3 Specular) { Diffuse = MatEmissive + MatAmbient * LightAmbientAccum + MatDiffuse * LightDiffuseAccum; Specular = MatSpecular * LightSpecularAccum; if (Saturate) { Diffuse = saturate(Diffuse); Specular = saturate(Specular); } } //--------------------------------------------------------------------------- /* This fragment is responsible for multiplying two float3's. */ void MultiplyFloat3(float3 V1, float3 V2, out float3 Output) { Output = V1 * V2; } //--------------------------------------------------------------------------- /* This fragment is responsible for computing the final RGB color. */ void CompositeFinalRGBColor(float3 DiffuseColor, float3 SpecularColor, out float3 OutputColor) { OutputColor.rgb = DiffuseColor.rgb + SpecularColor.rgb; } //--------------------------------------------------------------------------- /* This fragment is responsible for applying the fog based on the calculations in the vertex shader. */ void ApplyFog(float3 UnfoggedColor, float3 FogColor, float FogAmount, out float3 FoggedColor) { FoggedColor = lerp(FogColor, UnfoggedColor, FogAmount); } //--------------------------------------------------------------------------- /* This fragment is responsible for computing the final RGBA color. */ void CompositeFinalRGBAColor(float3 FinalColor, float FinalOpacity, out float4 OutputColor) { OutputColor.rgb = FinalColor.rgb; OutputColor.a = saturate(FinalOpacity); } //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- // Input: //--------------------------------------------------------------------------- struct Input { float4 PosProjected : POSITION0; float4 WorldPos : TEXCOORD0; float3 WorldNormal : TEXCOORD1; float FogDepth : FOG0; }; //--------------------------------------------------------------------------- // Output: //--------------------------------------------------------------------------- struct Output { float4 Color0 : COLOR0; }; //--------------------------------------------------------------------------- // Main(): //--------------------------------------------------------------------------- Output Main(Input In) { Output Out; // Function call #0 float3 VectorOut_CallOut0; NormalizeFloat3(In.WorldNormal, VectorOut_CallOut0); // Function call #1 float3 Color_CallOut1; float Opacity_CallOut1; SplitColorAndOpacity(g_MaterialDiffuse, Color_CallOut1, Opacity_CallOut1); // Function call #2 float ShadowOut_CallOut2; ShadowMap(In.WorldPos, int(0), DirShadow, g_ShadowDirTexSizeShadow0, g_ShadowDirWorldToShadowMapMatrix0, g_ShadowDirShadowBias0, ShadowOut_CallOut2); // Function call #3 float3 AmbientAccumOut_CallOut3; float3 DiffuseAccumOut_CallOut3; float3 SpecularAccumOut_CallOut3; Light(In.WorldPos, VectorOut_CallOut0, int(0), bool(false), float(1.0), float3(0.0, 0.0, 0.0), g_DirWorldPosition0, g_DirAmbient0, g_DirDiffuse0, g_DirSpecular0, float3(0.0, 1.0, 0.0), float3(-1.0, -1.0, 0.0), g_DirWorldDirection0, float4(1.0, 1.0, 1.0, 1.0), g_AmbientLight, float3(0.0, 0.0, 0.0), float3(0.0, 0.0, 0.0), AmbientAccumOut_CallOut3, DiffuseAccumOut_CallOut3, SpecularAccumOut_CallOut3); // Function call #4 float3 AmbientAccumOut_CallOut4; float3 DiffuseAccumOut_CallOut4; float3 SpecularAccumOut_CallOut4; float AttenuateOut_CallOut4; XRD649ShadowLight(ShadowOut_CallOut2, g_ShadowDirAmbient0, g_ShadowDirDiffuse0, g_ShadowDirSpecular0, AmbientAccumOut_CallOut3, DiffuseAccumOut_CallOut3, float3(0.0, 0.0, 0.0), AmbientAccumOut_CallOut4, DiffuseAccumOut_CallOut4, SpecularAccumOut_CallOut4, AttenuateOut_CallOut4); // Function call #5 float3 Diffuse_CallOut5; float3 Specular_CallOut5; ComputeShadingCoefficients(g_MaterialEmissive, Color_CallOut1, g_MaterialAmbient, float3(1.0, 1.0, 1.0), float3(0.0, 0.0, 0.0), DiffuseAccumOut_CallOut4, AmbientAccumOut_CallOut4, bool(false), Diffuse_CallOut5, Specular_CallOut5); // Function call #6 float3 Output_CallOut6; MultiplyFloat3(Diffuse_CallOut5, float3(1.0f, 1.0f, 1.0f), Output_CallOut6); // Function call #7 float3 OutputColor_CallOut7; CompositeFinalRGBColor(Output_CallOut6, Specular_CallOut5, OutputColor_CallOut7); // Function call #8 float3 FoggedColor_CallOut8; ApplyFog(OutputColor_CallOut7, g_FogColor, In.FogDepth, FoggedColor_CallOut8); // Function call #9 CompositeFinalRGBAColor(FoggedColor_CallOut8, Opacity_CallOut1, Out.Color0); return Out; }