/*============================================================================= Common.cg: Common shader code. Copyright ?2003-2005 Epic Games, Inc. All Rights Reserved. =============================================================================*/ #include "Definitions.cg" #if NUM_USER_VECTOR_INPUTS float4 UserVectorInputs[NUM_USER_VECTOR_INPUTS]; #endif #if NUM_USER_SCALAR_INPUTS float4 UserScalarInputs[(NUM_USER_SCALAR_INPUTS + 3) / 4]; #endif #if NUM_USER_TEXTURES sampler UserTextures[NUM_USER_TEXTURES]; // We share our high level material compiler across the Cg and HLSL backends and Cg doesn't allow late binding of sampler types which // is why we cannot use a generic array of samplers. The below hack allows us to emit code that works with both Cg and HLSL and maps // the samplers back into an array for HLSL. #define UserTextures0 UserTextures[0] #define UserTextures1 UserTextures[1] #define UserTextures2 UserTextures[2] #define UserTextures3 UserTextures[3] #define UserTextures4 UserTextures[4] #define UserTextures5 UserTextures[5] #define UserTextures6 UserTextures[6] #define UserTextures7 UserTextures[7] #define UserTextures8 UserTextures[8] #define UserTextures9 UserTextures[9] #define UserTextures10 UserTextures[10] #define UserTextures11 UserTextures[11] #define UserTextures12 UserTextures[12] #define UserTextures13 UserTextures[13] #define UserTextures14 UserTextures[14] #define UserTextures15 UserTextures[15] float4 UserTexturesMins[NUM_USER_TEXTURES]; float4 UserTexturesMaxMinusMins[NUM_USER_TEXTURES]; #endif float SceneTime; float4 UserMeshEmitterVertexColor; struct userShaderInput { #if NUM_USER_TEXCOORDS float4 UserTexCoords[NUM_USER_TEXCOORDS]; #endif float4 UserVertexColor; float3 TangentNormal, TangentReflectionVector, TangentCameraVector; #if WORLD_SPACE_VECTOR float3 WorldReflectionVector, WorldCameraVector; #endif half3 TangentLightVector; half3 TangentSkyVector; // WWL: Sin(SlopeAngle) = dot(TangentSkyVector, (0,0,1)) float4 ScreenPosition; }; float2 userTexCoordTransform(userShaderInput Input, int n); half3 userNormal(userShaderInput Input); half3 userEmissive(userShaderInput Input); half3 userDiffuseColor(userShaderInput Input); half3 userSpecularColor(userShaderInput Input); half userSpecularPower(userShaderInput Input); half userOpacity(userShaderInput Input); float userMask(userShaderInput Input); float2 userDistortion(userShaderInput Input); float3 userTwoSidedLightingMask(userShaderInput Input); float3 userCustomLighting(userShaderInput Input); float3 safeNormalize(float3 V) { return V / sqrt(max(dot(V,V),0.01)); } float square(float A) { return A * A; } float4 expandRGBE( float4 RGBE ) { return float4( ldexp( RGBE.xyz, 255.0 * RGBE.w - 128.0 ), 1.0 ); } float4 expandCompressedRGBE( float4 RGBE ) { return float4( ldexp( RGBE.xyz, 255.0 / 16.0 * RGBE.w - 8.0 ), 1.0 ); } float4 gammaCorrect( float4 Color ) { return float4( pow( Color.xyz, 2.2 ), Color.w ); } #define PI 3.141592653 half3 pointLightPhong(half3 DiffuseColor,half3 SpecularColor,half SpecularPower, half3 L, float3 E, half3 N, float3 R, float SpecularAttenuation) { half3 DiffuseLighting = saturate(dot(N,L)), SpecularLighting = pow(saturate(dot(R,L)),max(SpecularPower, 0.0001)); return DiffuseColor * DiffuseLighting + SpecularLighting * SpecularColor * saturate(SpecularAttenuation); } half3 pointLightPhongNoSpecular(half3 DiffuseColor, half3 L, float3 E, half3 N, float3 R) { half3 DiffuseLighting = saturate(dot(N,L)); return DiffuseColor * DiffuseLighting; } half3 hemisphereLightPhong(half3 DiffuseColor, half3 S, half3 N) { return DiffuseColor * square(0.5 + 0.5 * dot(N,S)); } sampler PreviousLightingTexture; float4 ScreenPositionScaleBias; float radialAttenuation(float3 WorldLightVector) { return square(saturate(1.0f - dot(WorldLightVector,WorldLightVector))); } sampler LightTexture; half3 lightFunction(float4 ScreenPosition) { return 1; //float2 TexCoord = ScreenPosition.xy / ScreenPosition.w * ScreenPositionScaleBias.xy + ScreenPositionScaleBias.wz; //return half3(tex2D(LightTexture,TexCoord).xyz); } sampler DepthBufferTexture; float4 MinZ_MaxZRatio; #if !XBOX&&!PS3 float calcSceneDepth( float2 ScreenUV ) { return tex2D(PreviousLightingTexture, ScreenUV).a * SCENE_DEPTH_SCALAR_INV; } #elif XBOX float calcSceneDepth( float2 ScreenUV ) { // get depth buffer z value half DeviceZ = tex2D(DepthBufferTexture, ScreenUV).r; // convert it to clip space w half ClipSpaceW = MinZ_MaxZRatio[0] / (1.0 - (DeviceZ / MinZ_MaxZRatio[1])); if(ClipSpaceW<0) ClipSpaceW = 6550000+1; return ClipSpaceW; } #elif PS3 float calcSceneDepth( float2 ScreenUV ) { // get depth buffer z value float3 depthColor = round(tex2D(DepthBufferTexture, ScreenUV).rgb * 255.0); float ClipSpaceW = 1.f / dot(MinZ_MaxZRatio, float4(depthColor, 1)); return ClipSpaceW > 0 ? ClipSpaceW : 6550000; // for the sky depth issue } #endif float previousDepth(float2 ScreenPosition) { ScreenPosition *= float2(1.f / FRAME_BUFFER_WIDTH, -1.f / FRAME_BUFFER_HEIGHT); return calcSceneDepth(ScreenPosition + float2(0.5f / (FRAME_BUFFER_WIDTH*2.0f), 1)); } #define FLOAT16_EPSILON 4.8875809e-4f double getDepthCompareEpsilon(double Depth) { #if 1 // This is an approximate, much faster way to calculate the epsilon. return Depth * FLOAT16_EPSILON * 2.0; #else // This is the slower way to calculate the epsilon. return exp2(ceil(log2(Depth))) * FLOAT16_EPSILON; #endif } /** * aligns the clip space position so that it can be used as a texture coordinate * to properly align in screen space */ float4 screenAlignedPosition( float4 ScreenPosition ) { float4 Result = ScreenPosition; Result.xy = (ScreenPosition.xy / ScreenPosition.w) * 0.5 + 0.5; Result.y = 1 - Result.y; return Result; } /* Bilinear interpolation for floating-point texture */ float4 tex2D_bilinear( sampler2D tex, float2 t, float4 textureInfo ) // textureInfo[x,y,z,w]: texSizeX, texSizeY, 1.0/texSizeX, 1.0/texSizeY { float2 f = frac( t.xy * textureInfo.xy ); float4 t00 = tex2D( tex, t ); float4 t10 = tex2D( tex, t + float2( textureInfo.z, 0.0f ) ); float4 tA = lerp( t00, t10, f.x ); float4 t01 = tex2D( tex, t + float2( 0.0f, textureInfo.w ) ); float4 t11 = tex2D( tex, t + textureInfo.zw ); float4 tB = lerp( t01, t11, f.x ); return lerp( tA, tB, f.y ); } //+WbXie //Function for wang tiles float4 WangTile(float2 TexUV,float2 InTileSize, float2 OutTileSize,sampler tileTexture, sampler indexTexture) { float4 result; float4 whileTile = tex2D(indexTexture,TexUV); float2 localUV = frac(TexUV.xy*OutTileSize.xy)/InTileSize.xy; float2 tileScale = TexUV*OutTileSize/InTileSize; result = tex2D(tileTexture, whileTile.rg+localUV.xy,ddx(tileScale),ddy(tileScale)); return result; } //-WbXie //+WM //Function for CharTable Sample Param float4 CharTableSample( float TableRange, float CharIndex, float InputCharNum, float Offset, sampler tex, float2 TexUV ) { float4 result; float2 ClipedTexUV; //ClipedTexUV.r = (TexUV.r + CharIndex)/TableRange; ClipedTexUV.r = (TexUV.r*InputCharNum - Offset + CharIndex)/TableRange; ClipedTexUV.g = TexUV.g; //(TexUV.r >= Offset/InputCharNum ) ? ( TexUV.r < (Offset+1)/InputCharNum ? ( result = tex2D( tex, ClipedTexUV ) ) : (result = float4(0.0f,0.0f,0.0f,1.0f)) ) : (result = float4(0.0f,0.0f,0.0f,1.0f)); if(TexUV.r >= Offset/InputCharNum && TexUV.r < (Offset+1)/InputCharNum) result = tex2D( tex, ClipedTexUV ); else result = float4(0.0f, 0.0f, 0.0f, 1.0f); return result; } //-WM //+WbXie float4 MagmaTexture(float2 UV) { return tex2D(LightTexture, UV); } float3 Desaturation(float3 SceneColor,float3 Desaturate,float DepthValue) { float3 DestDesaturateColor = dot(SceneColor.rgb,float3(0.3,0.59,0.11))*Desaturate.rgb; float3 FinalColor = lerp(SceneColor.rgb, DestDesaturateColor, DepthValue); return FinalColor; } //-WbXie half3 Polarization(half3 SceneColor,half PolarizedMul,half PolarOpacity, half PolarMin, half GradientStart,half4 Polarized_Color, half YCoord) { half Gradient = saturate(1.0f-GradientStart-YCoord)/(1.01f-GradientStart); half SmoothGradient = lerp(PolarMin,1.0f,sqrt(1-Gradient*Gradient)); half Intensity = dot(half3(0.3,0.59,0.11),SceneColor); half3 WhitePolarizedColor = lerp(SceneColor,Polarized_Color,PolarOpacity*(1-SmoothGradient));//lerp(Polarized_Color,SceneColor,saturate(Gradient*(1.0f-Polarized_Opacity)))*(saturate(Gradient*(PolarizedMul+1.0f))+Intensity*(PolarizedMul)); return WhitePolarizedColor; } float3 userPointLightTransfer(userShaderInput Input,float RadialAttenuation,float3 UserDiffuseColor,half3 UserSpecularColor, half UserSpecularPower) { float3 Lighting = 0; #if SHADER_LIGHTINGMODEL_NONDIRECTIONAL Lighting = UserDiffuseColor * RadialAttenuation; #elif SHADER_LIGHTINGMODEL_PHONG Lighting = pointLightPhong( UserDiffuseColor, UserSpecularColor, UserSpecularPower, Input.TangentLightVector, Input.TangentCameraVector, Input.TangentNormal, Input.TangentReflectionVector, 1 // no specular attenuation ) * RadialAttenuation; #elif SHADER_LIGHTINGMODEL_CUSTOM Lighting = userCustomLighting(Input) * RadialAttenuation; #endif return Lighting; } //for a better data sharing, we add more input parameter instead of calculate in function. float3 userDirectionalLightTransfer(float3 UserDiffuseColor, userShaderInput Input, float SpecularAttenuation,float3 UserSpecularColor,half UserSpecularPower) { float3 Lighting = 0; #if SHADER_LIGHTINGMODEL_NONDIRECTIONAL Lighting = UserDiffuseColor; #elif SHADER_LIGHTINGMODEL_PHONG #ifdef TERRAIN_RENDERING_MULTI_LAYERED float3 L0 = pointLightPhong(UserDiffuseColor, UserSpecularColor, UserSpecularPower, Input.TangentLightVector, Input.TangentCameraVector, Input.TangentNormal, Input.TangentReflectionVector, SpecularAttenuation); float3 L1 = pointLightPhong(UserDiffuseColor, UserSpecularColor, UserSpecularPower, Input.TangentLightVector, Input.TangentCameraVector, float3(0, 0, 1), Input.TangentCameraVector * float3(-1, -1, 1), SpecularAttenuation); Lighting = lerp(L1, L0, userOpacity(Input)); #else Lighting = pointLightPhong(UserDiffuseColor, UserSpecularColor, UserSpecularPower, Input.TangentLightVector, Input.TangentCameraVector, Input.TangentNormal, Input.TangentReflectionVector, SpecularAttenuation); #endif //TERRAIN_RENDERING_MULTI_LAYERED #elif SHADER_LIGHTINGMODEL_CUSTOM Lighting = userCustomLighting(Input); #endif return Lighting; } //for a better data sharing, we add more input parameter instead of calculate in function. float3 userDirectionalLightTransferNoSpecular(float3 UserDiffuseColor, userShaderInput Input) { float3 Lighting = 0; #if SHADER_LIGHTINGMODEL_NONDIRECTIONAL Lighting = UserDiffuseColor; #elif SHADER_LIGHTINGMODEL_PHONG Lighting = pointLightPhongNoSpecular( UserDiffuseColor, Input.TangentLightVector, Input.TangentCameraVector, Input.TangentNormal, Input.TangentReflectionVector ); #elif SHADER_LIGHTINGMODEL_CUSTOM Lighting = userCustomLighting(Input); #endif return Lighting; } float3 userHemisphereLightTransfer( float3 UserDiffuseColor, userShaderInput Input, float3 SkyVector) { float3 Lighting = 0; #if SHADER_LIGHTINGMODEL_NONDIRECTIONAL Lighting = UserDiffuseColor; #elif SHADER_LIGHTINGMODEL_PHONG #ifdef TERRAIN_RENDERING_MULTI_LAYERED float3 L0 = hemisphereLightPhong(UserDiffuseColor,SkyVector,Input.TangentNormal); float3 L1 = hemisphereLightPhong(UserDiffuseColor,SkyVector,float3(0, 0, 1)); Lighting = lerp(L1, L0, userOpacity(Input)); #else Lighting = hemisphereLightPhong(UserDiffuseColor,SkyVector,Input.TangentNormal); #endif //TERRAIN_RENDERING_MULTI_LAYERED #elif SHADER_LIGHTINGMODEL_CUSTOM Lighting = 0; #endif return Lighting; } float userClipping(userShaderInput Input) #if SHADERBLENDING_MASKED #if XBOX { return (userMask(Input) + SHADER_OPACITY_CLIP_OFFSET) * SHADER_OPACITY_CLIP_SCALE; } #else { clip(userMask(Input)); return 1; } #endif #elif SHADERBLENDING_ADDITIVE || SHADERBLENDING_MODULATE { float clipValue = userOpacity(Input) - 1.0 / 255.0; clip(clipValue); return 1;} #else { return 1; } #endif #define initUserVectors(UserShaderInput) \ { \ UserShaderInput.UserVertexColor = 1; \ UserShaderInput.TangentNormal = 0; \ UserShaderInput.TangentCameraVector = 0; \ UserShaderInput.TangentReflectionVector = 0;\ UserShaderInput.TangentLightVector = 0; \ UserShaderInput.TangentSkyVector = 0; \ UserShaderInput.ScreenPosition = 0; \ } \ void calcUserVectors(in out userShaderInput UserShaderInput,float3 CameraVector,float4 ScreenPosition,half3 LightVector = 0,half3 SkyVector = half3(0,0,1)) { UserShaderInput.ScreenPosition = ScreenPosition; UserShaderInput.TangentCameraVector = normalize(CameraVector); UserShaderInput.TangentLightVector = normalize((half3)LightVector); UserShaderInput.TangentNormal = normalize(userNormal(UserShaderInput)); UserShaderInput.TangentReflectionVector = -UserShaderInput.TangentCameraVector + UserShaderInput.TangentNormal * dot(UserShaderInput.TangentNormal,UserShaderInput.TangentCameraVector) * 2.0; UserShaderInput.TangentSkyVector = SkyVector; //MT: moved normalize up, check sunlight.hlsl } void calcWorldVector(out float3 WorldCameraVec, out float3 WorldReflectionVec,float4 CameraPos,float4 WorldPos, float3 WorldNormal) { WorldCameraVec = CameraPos - WorldPos; WorldCameraVec = normalize(WorldCameraVec); //WorldReflectionVec = -WorldCameraVec+WorldNormal*(dot(WorldNormal,WorldCameraVec))*2.0f; WorldReflectionVec = normalize(-WorldCameraVec+WorldNormal*(dot(normalize(WorldNormal),WorldCameraVec))*2.0f); } float4 ScaleVector(float a) { return float4(a,0,0,0); } float4 ScaleVector(float2 a) { return float4(a.xy,0,0); } float4 ScaleVector(float3 a) { return float4(a.xyz,0); } float4 ScaleVector(float4 a) { return a; } //*** Distortion post process float DistortionOffsetScale; sampler DistortionTexture; sampler LightingTexture; float4 disortionImage(float2 texcoord) { float4 lightColor = 0.0f; float4 distInfo = tex2D(DistortionTexture, texcoord); //*** Just detect Alpha value is more fast //if (dot(distInfo, 1) > 0) if(distInfo.a > 0.0f) { float distInfo0 = distInfo.a; float distInfo1 = tex2D(DistortionTexture, float2(texcoord + float2( 0, 0.002))).a; float distInfo2 = tex2D(DistortionTexture, float2(texcoord + float2(0.002, 0))).a; float distInfo3 = tex2D(DistortionTexture, float2(texcoord + float2(0.002, 0.002))).a; float2 offset = clamp(float2(distInfo2 - distInfo0 + distInfo3 - distInfo1, distInfo1 - distInfo0 + distInfo3 - distInfo2) * DistortionOffsetScale, -0.01, 0.01); float lenOffset = length(offset) * 0.05 + distInfo.a * 0.01; #if USE_LOG_EXP float3 tempColor; tempColor = tex2D(LightingTexture, texcoord + offset + float2( -0.75f , 0.75f) * lenOffset).rgb; lightColor.rgb += exp(tempColor * EXP_SCALE_SHIFT); tempColor = tex2D(LightingTexture, texcoord + offset + float2( 0.25f , 0.25f) * lenOffset).rgb; lightColor.rgb += exp(tempColor * EXP_SCALE_SHIFT); tempColor = tex2D(LightingTexture, texcoord + offset + float2( 0.75f ,-0.25f) * lenOffset).rgb; lightColor.rgb += exp(tempColor * EXP_SCALE_SHIFT); tempColor = tex2D(LightingTexture, texcoord + offset + float2( -0.25f ,-0.75f) * lenOffset).rgb; lightColor.rgb += exp(tempColor * EXP_SCALE_SHIFT); #else lightColor.rgb += tex2D(LightingTexture, texcoord + offset + float2( -0.75f , 0.75f) * lenOffset).rgb; lightColor.rgb += tex2D(LightingTexture, texcoord + offset + float2( 0.25f , 0.25f) * lenOffset).rgb; lightColor.rgb += tex2D(LightingTexture, texcoord + offset + float2( 0.75f ,-0.25f) * lenOffset).rgb; lightColor.rgb += tex2D(LightingTexture, texcoord + offset + float2( -0.25f ,-0.75f) * lenOffset).rgb; #endif lightColor.rgb = lightColor.rgb * 0.25 + distInfo.rgb; } else { lightColor.rgb = tex2D(LightingTexture, texcoord).rgb; #if USE_LOG_EXP lightColor.rgb = exp(lightColor.rgb * EXP_SCALE_SHIFT); #endif } lightColor.a = distInfo.a; return lightColor; }