/*============================================================================= Fog.cg: Scene fogging pixel shaders. Copyright ?2004-2005 Epic Games, Inc. All Rights Reserved. * Modified by Huang Chen, for atmospheric effect, [1/4/2007] =============================================================================*/ #include "Common.cg" #include "UserShader.cg" float3 FogColor; float2 FogDensity; // R: density, G: attenuation ratio static const float DepthEpsilon = 0.0001f; // +HC float4 MinZ_MaxZValue; // near plane and ratio of far plane to z range = MaxZ/(MaxZ-MinZ) = ProjM[2][2] #if XBOX bool AtmosphereEnabled; #else float AtmosphereEnabled; #endif float4 CameraPosition; // : register(CAMERA_POSITION_REG); float4x4 ScreenToWorldMatrix; float3 SunDirection; float4 SunColor_FogAffector; float3 MultRayleighMie; float3 gHG; sampler ShadowTexture; float4x4 ScreenToLight; sampler IndirectLightingTexture; //+ Bli: use definitions for those //float InvIndirectColorScale = 1.0f; //float InvSkyColorScale = 1.0f; //float InvOpacityColorScale = 1.0f; //float InvLogSkyColorScale = 0.0f; //float InvLogOpacityColorScale = 0.0f; //- Bli struct vertexShaderOutput { float4 Position : POSITION; float4 TexCoord : TEXCOORD0; float2 TexCoordInterp : TEXCOORD1; float4 ScreenLightVec : TEXCOORD2; float4 ScreenLightOff : TEXCOORD3; float3 CurrentFogColor : TEXCOORD4; }; float SD_Distance; float4 DesaturateColor; float ContrastMul_Value; struct PS_OUTPUT { float4 Color : COLOR0; float Z : DEPTH; }; PS_OUTPUT fogscene(vertexShaderOutput Input) { PS_OUTPUT output; const float3 depthFactor = float3( 65536.0/16777215.0, 256.0/16777215.0, 1.0/16777215.0 ); half4 DepthStencil1 = x4tex2D(DepthBufferTexture, Input.TexCoord.xy); DepthStencil1.rgb = round(DepthStencil1.rgb * 255.0); half4 DepthStencil2 = x4tex2D(DepthBufferTexture, Input.TexCoord.zw); DepthStencil2.rgb = round(DepthStencil2.rgb * 255.0); float z1 = dot(DepthStencil1.rgb, depthFactor ); float z2 = dot(DepthStencil2.rgb, depthFactor ); half3 DepthStencilValue = (z1 < z2) ? DepthStencil1 : DepthStencil2; output.Z = (z1 > z2) ? z1 : z2; float SceneDepth = 1.f / dot(MinZ_MaxZRatio, float4(DepthStencilValue.rgb, 1)); half4 LitColor1 = x4tex2D(PreviousLightingTexture, Input.TexCoord.xy); half4 LitColor2 = x4tex2D(PreviousLightingTexture, Input.TexCoord.zw); half2 testStencil = half2(DepthStencil1.a, DepthStencil2.a); half4 stencilMask = saturate(testStencil.xyxy - half4(0.9, 0.9, 0.504, 0.504)); half isSky = (stencilMask.x + stencilMask.y); half2 isSkyOrCircle = half2(stencilMask.z, stencilMask.w) > 0; half logScale = isSky ? InvLogSkyColorScale : InvLogOpacityColorScale; half3 SkyCircleLogColor = isSkyOrCircle.y ? LitColor2 : LitColor1; //half4 PrevColor = isSkyOrCircle.y ? LitColor1 : LitColor2; half4 PrevColor = isSkyOrCircle.y ? LitColor1 : (LitColor2 + LitColor1)/2; isSkyOrCircle.x = (isSkyOrCircle.x + isSkyOrCircle.y) / 2; // Calculate Fog Effect half LerpRatio = clamp(FogDensity.r * abs(SceneDepth) , 0, 1); //LerpRatio = exp(FogDensity.g * log(LerpRatio)); LerpRatio = pow(FOGCOLOR_POWER,FogDensity.g * log(LerpRatio)) ; //*** decrease the pow number to make it looks has linear fog half ShadowValue = PrevColor.a; #if LIGHT_FUNC half3 LightPosition = Input.ScreenLightVec * SceneDepth + Input.ScreenLightOff; userShaderInput UserShaderInput; #if NUM_USER_TEXCOORDS for(int CoordinateIndex = 0;CoordinateIndex < NUM_USER_TEXCOORDS;CoordinateIndex++) UserShaderInput.UserTexCoords[CoordinateIndex] = float4(LightPosition.xy, 0, 0); #endif UserShaderInput.UserVertexColor = 1; UserShaderInput.TangentCameraVector = UserShaderInput.TangentLightVector = LightPosition; UserShaderInput.TangentSkyVector = UserShaderInput.TangentNormal = UserShaderInput.TangentReflectionVector = UserShaderInput.ScreenPosition = 0; #if WORLD_SPACE_VECTOR UserShaderInput.WorldReflectionVector = UserShaderInput.WorldCameraVector = 0; #endif ShadowValue *= userEmissive(UserShaderInput).x; #endif //LIGHT_FUNC #if !USE_LOG_EXP Error: not surpported! #endif half3 IndirectColor = x4tex2D(IndirectLightingTexture,Input.TexCoordInterp) * InvIndirectColorScale; PrevColor.rgb = (saturate(PrevColor.rgb * InvOpacityColorScale - IndirectColor.rgb)) * saturate(ShadowValue) + IndirectColor.rgb; //*** Lerp with fog color PrevColor.rgb = lerp(PrevColor,Input.CurrentFogColor,LerpRatio); half DepthValue = saturate(SceneDepth*SD_Distance)*ContrastMul_Value; //*** Desaturation color PrevColor.rgb = Desaturation(PrevColor.rgb ,DesaturateColor,DepthValue); //*** Using log(color) to make the small value color don't lose too much precision #if 1//USE_SEPERATE_LOG_IN_FOGPASS // log first then lerp, more expensive PrevColor.rgb = log(saturate(PrevColor) + LOG_BIAS); output.Color.rgb = log(saturate(SkyCircleLogColor) + LOG_BIAS) + logScale; output.Color.rgb = lerp(PrevColor, output.Color.rgb, isSkyOrCircle.x) * LOG_SCALE_SHIFT; #else //lerp first and log later PrevColor.rgb = lerp(PrevColor.rgb, SkyCircleLogColor, isSkyOrCircle.x); output.Color.rgb = (log(saturate(PrevColor) + LOG_BIAS) + isSkyOrCircle.x * logScale) * LOG_SCALE_SHIFT; #endif output.Color.a = 0; return output; } vertexShaderOutput vertexshader(float4 Position : POSITION,float2 TexCoord : TEXCOORD0) { vertexShaderOutput Result; Result.Position = Position; Result.TexCoord.xy = TexCoord - float2(0.25 / FRAME_BUFFER_WIDTH, 0); Result.TexCoord.zw = TexCoord + float2(0.25 / FRAME_BUFFER_WIDTH, 0); Result.TexCoordInterp = TexCoord; float4 posNear = _mul(ScreenToWorldMatrix, float4(Position.xy, 0, 1)); float4 posFar = _mul(ScreenToWorldMatrix, float4(Position.xy, 1, 1)); posNear /= posNear.w; posFar /= posFar.w; float3 CameraDirection = normalize(posNear - posFar); float BlendWeight = saturate(dot(CameraDirection,SunDirection.xyz)*SunColor_FogAffector.w); Result.CurrentFogColor = lerp(FogColor * FOGCOLOR_SCALE,SunColor_FogAffector.rgb,BlendWeight); float4 lightNear = _mul(ScreenToLight, float4(0, 0, 0, 1)); float4 lightFar = _mul(ScreenToLight, float4(Position.xy, 1, 1)); Result.ScreenLightVec = lightFar - lightNear; Result.ScreenLightOff = lightNear; return Result; }