//------------------------------------------------------------------------------------------ // Light helper //------------------------------------------------------------------------------------------ float4 createLight(float3 Normal, float3 WorldPos, int LightType, float3 LightPos, float3 LightDir, float4 LightDiffuse, float Attenuation, float LightPhi, float LightTheta, float LightFalloff, bool LightNormalShadow ) { float fDistance = distance( LightPos, WorldPos ); float fAttenuation = 1 - saturate(fDistance / Attenuation); fAttenuation = pow( fAttenuation, 2); float3 vLight = normalize( LightPos - WorldPos ); float angle = acos( dot ( -vLight, normalize( LightDir ))); float fSpotAtten = 0.0f; if ( angle > LightPhi ) fSpotAtten = 0.0f; else if ( angle < LightTheta) fSpotAtten = 1.0f; else fSpotAtten = pow( smoothstep( LightPhi, LightTheta, angle ), LightFalloff ); if (LightType==2) fAttenuation *= fSpotAtten; float NdotL = saturate( max( 0.0f, dot( Normal , vLight ) )); if (LightNormalShadow) return NdotL * fAttenuation * LightDiffuse; else return fAttenuation * LightDiffuse ; } //------------------------------------------------------------------------------------------ // - Some states for Diffuse //------------------------------------------------------------------------------------------ int gLighting < string renderState="LIGHTING"; >; int gDiffuseMaterialSource < string renderState="DIFFUSEMATERIALSOURCE"; >; int gAmbientMaterialSource < string renderState="AMBIENTMATERIALSOURCE"; >; int gEmissiveMaterialSource < string renderState="EMISSIVEMATERIALSOURCE"; >; float4 gGlobalAmbient < string renderState="AMBIENT"; >; float4 gMaterialAmbient < string materialState="Ambient"; >; float4 gMaterialDiffuse < string materialState="Diffuse"; >; float4 gMaterialEmissive < string materialState="Emissive"; >; int sLight1Enable < string lightEnableState="1,Enable"; >; int sLight2Enable < string lightEnableState="2,Enable"; >; int sLight3Enable < string lightEnableState="3,Enable"; >; int sLight4Enable < string lightEnableState="4,Enable"; >; float4 sLight1Diffuse < string lightState="1,Diffuse"; >; float3 sLight1Direction < string lightState="1,Direction"; >; float4 sLight2Diffuse < string lightState="2,Diffuse"; >; float3 sLight2Direction < string lightState="2,Direction"; >; float4 sLight3Diffuse < string lightState="3,Diffuse"; >; float3 sLight3Direction < string lightState="3,Direction"; >; float4 sLight4Diffuse < string lightState="4,Diffuse"; >; float3 sLight4Direction < string lightState="4,Direction"; >; float3 sCameraDirection : CAMERADIRECTION; float3 sLightDirection : LIGHTDIRECTION; float4 sLightAmbient : LIGHTAMBIENT; float4 sMaterialSpecular < string materialState="Specular"; >; float sMaterialSpecPower < string materialState="Power"; >; //------------------------------------------------------------------------------------------ // Specular helpers //------------------------------------------------------------------------------------------ float MTACalculateSpecular( float3 CamDir, float3 LightDir, float3 SurfNormal, float SpecPower ) { LightDir = normalize(LightDir); SurfNormal = normalize(SurfNormal); float3 halfAngle = normalize(-CamDir - LightDir); float r = dot(halfAngle, SurfNormal); return pow(saturate(r), SpecPower); } float4 MTACalculateVehicleSpecular(float3 SurfNormal ) { SurfNormal = normalize(SurfNormal); float3 halfAngle = normalize(-sCameraDirection - sLightDirection); float r = saturate(dot(halfAngle, SurfNormal)); float spec = pow(r, sMaterialSpecPower); return sMaterialSpecular * spec; } //------------------------------------------------------------------------------------------ // Diffuse calc (vehicle / ped / building / complete) //------------------------------------------------------------------------------------------ float4 MTACalcGTAVehicleDiffuse( float3 WorldNormal, float4 InDiffuse ) { float4 ambient = gAmbientMaterialSource == 0 ? gMaterialAmbient : InDiffuse; float4 diffuse = gDiffuseMaterialSource == 0 ? gMaterialDiffuse : InDiffuse; float4 emissive = gEmissiveMaterialSource == 0 ? gMaterialEmissive : InDiffuse; float4 TotalAmbient = ambient * ( gGlobalAmbient + sLightAmbient ) ; float DirectionFactor=0; float4 TotalDiffuse=0; if (sLight1Enable) { DirectionFactor = max(0,dot(WorldNormal, -sLight1Direction )); TotalDiffuse += ( sLight1Diffuse * DirectionFactor ); } if (sLight2Enable) { DirectionFactor = max(0,dot(WorldNormal, -sLight2Direction )); TotalDiffuse += ( sLight2Diffuse * DirectionFactor ); } if (sLight3Enable) { DirectionFactor = max(0,dot(WorldNormal, -sLight3Direction )); TotalDiffuse += ( sLight3Diffuse * DirectionFactor ); } if (sLight4Enable) { DirectionFactor = max(0,dot(WorldNormal, -sLight4Direction )); TotalDiffuse += ( sLight4Diffuse * DirectionFactor ); } TotalDiffuse *= diffuse; float4 OutDiffuse = saturate(TotalDiffuse + TotalAmbient + emissive); OutDiffuse.a *= diffuse.a; return OutDiffuse; } float4 MTACalcGTAPedDiffuse( float3 WorldNormal, float4 InDiffuse ) { float4 ambient = gAmbientMaterialSource == 0 ? gMaterialAmbient : InDiffuse; float4 diffuse = gDiffuseMaterialSource == 0 ? gMaterialDiffuse : InDiffuse; float4 emissive = gEmissiveMaterialSource == 0 ? gMaterialEmissive : InDiffuse; float4 TotalAmbient = ambient * ( gGlobalAmbient ); float DirectionFactor=0; float4 TotalDiffuse=0; if (sLight1Enable) { DirectionFactor = max(0,dot(WorldNormal, -sLight1Direction )); TotalDiffuse += ( sLight1Diffuse * DirectionFactor ); } if (sLight2Enable) { DirectionFactor = max(0,dot(WorldNormal, -sLight2Direction )); TotalDiffuse += ( sLight2Diffuse * DirectionFactor ); } if (sLight3Enable) { DirectionFactor = max(0,dot(WorldNormal, -sLight3Direction )); TotalDiffuse += ( sLight3Diffuse * DirectionFactor ); } if (sLight4Enable) { DirectionFactor = max(0,dot(WorldNormal, -sLight4Direction )); TotalDiffuse += ( sLight4Diffuse * DirectionFactor ); } TotalDiffuse *= diffuse; float4 OutDiffuse = saturate(TotalDiffuse + TotalAmbient + emissive); OutDiffuse.a *= diffuse.a; return OutDiffuse; } float4 MTACalcGTABuildingDiffuse( float4 InDiffuse ) { float4 OutDiffuse; if ( !gLighting ) { OutDiffuse = InDiffuse; } else { float4 ambient = gAmbientMaterialSource == 0 ? gMaterialAmbient : InDiffuse; float4 diffuse = gDiffuseMaterialSource == 0 ? gMaterialDiffuse : InDiffuse; float4 emissive = gEmissiveMaterialSource == 0 ? gMaterialEmissive : InDiffuse; OutDiffuse = gGlobalAmbient * saturate( ambient + emissive ); OutDiffuse.a *= diffuse.a; } return OutDiffuse; } float4 MTACalcGTACompleteDiffuse( float3 WorldNormal, float4 InDiffuse ) { float4 ambient = gAmbientMaterialSource == 0 ? gMaterialAmbient : InDiffuse; float4 diffuse = gDiffuseMaterialSource == 0 ? gMaterialDiffuse : InDiffuse; float4 emissive = gEmissiveMaterialSource == 0 ? gMaterialEmissive : InDiffuse; float4 TotalAmbient = ambient * gGlobalAmbient ; float DirectionFactor=0; float4 TotalDiffuse=0; if (sLight1Enable) { DirectionFactor = max(0,dot(WorldNormal, -sLight1Direction )); TotalDiffuse += ( sLight1Diffuse * DirectionFactor ); } if (sLight2Enable) { DirectionFactor = max(0,dot(WorldNormal, -sLight2Direction )); TotalDiffuse += ( sLight2Diffuse * DirectionFactor ); } if (sLight3Enable) { DirectionFactor = max(0,dot(WorldNormal, -sLight3Direction )); TotalDiffuse += ( sLight3Diffuse * DirectionFactor ); } if (sLight4Enable) { DirectionFactor = max(0,dot(WorldNormal, -sLight4Direction )); TotalDiffuse += ( sLight4Diffuse * DirectionFactor ); } TotalDiffuse *= diffuse * 0.9f; float4 OutDiffuse = saturate(TotalDiffuse + TotalAmbient + emissive); OutDiffuse.a *= diffuse.a; return OutDiffuse; } //------------------------------------------------------------------------------------------ // Directional light //------------------------------------------------------------------------------------------ float4 createDirLight(float3 Normal, float3 LightDir, float4 LightDiffuse) { float3 vLight = normalize( LightDir ); float NdotL = saturate( max( 0.0f, dot( Normal, -vLight ))); return NdotL * LightDiffuse; } //------------------------------------------------------------------------------------------ // Unlerp + vertex point light //------------------------------------------------------------------------------------------ float MTAUnlerp( float from, float to, float pos ) { if ( from == to ) return 1.0; else return ( pos - from ) / ( to - from ); } float createVertexLightPoint(float3 WorldPos, float3 LightPos, float Attenuation ) { float fDistance = distance( LightPos, WorldPos ); float fAttenuation = MTAUnlerp( Attenuation, Attenuation/2, fDistance ); return saturate( fAttenuation ); } //------------------------------------------------------------------------------------------ // Fog //------------------------------------------------------------------------------------------ int gFogEnable < string renderState="FOGENABLE"; >; float4 gFogColor < string renderState="FOGCOLOR"; >; float gFogStart < string renderState="FOGSTART"; >; float gFogEnd < string renderState="FOGEND"; >; float3 MTAApplyFog( float3 texel, float3 worldPos ) { if ( !gFogEnable ) return texel; float DistanceFromCamera = distance( gCameraPosition, worldPos ); float FogAmount = ( DistanceFromCamera - gFogStart )/( gFogEnd - gFogStart ); texel.rgb = lerp(texel.rgb, gFogColor, saturate( FogAmount ) ); return texel; } //------------------------------------------------------------------------------------------ // ComputeNormalsPS //------------------------------------------------------------------------------------------ float3 ComputeNormalsPS(sampler2D sample, float2 texCoord, float4 lightness, float tSize) { float off = 1.0 / tSize; float4 s00 = tex2D(sample, texCoord + float2(-off, -off)); float4 s01 = tex2D(sample, texCoord + float2( 0, -off)); float4 s02 = tex2D(sample, texCoord + float2( off, -off)); float4 s10 = tex2D(sample, texCoord + float2(-off, 0)); float4 s12 = tex2D(sample, texCoord + float2( off, 0)); float4 s20 = tex2D(sample, texCoord + float2(-off, off)); float4 s21 = tex2D(sample, texCoord + float2( 0, off)); float4 s22 = tex2D(sample, texCoord + float2( off, off)); float4 sobelX = s00 + 2 * s10 + s20 - s02 - 2 * s12 - s22; float4 sobelY = s00 + 2 * s01 + s02 - s20 - 2 * s21 - s22; float sx = dot(sobelX, lightness); float sy = dot(sobelY, lightness); float3 normal = normalize(float3(sx, sy, 1)); return float3(normal * 0.5 + 0.5); } //------------------------------------------------------------------------------------------ // Depth / layered depth //------------------------------------------------------------------------------------------ float depthCalcBias = 1.00004; float depthBias = -0.00014f; float depthPlanularBias = 1.0f; float depthDensityStabilize = 50; float calculateLayeredDepth(float4 ViewPos) { float depth = ViewPos.z / ViewPos.w; return depth * pow(depthCalcBias, depth - (0.25 + (1 - depth * (depth * depthPlanularBias)) * depthDensityStabilize)) + depthBias; } int CUSTOMFLAGS ;