Skip to content

Commit

Permalink
rough cut of cook-torrence from the old PBS fork, mostly courtesy of …
Browse files Browse the repository at this point in the history
…andrewmac strips the heavier wieght normalmapped ambient in favor of GarageGames#1027
  • Loading branch information
Azaezel committed Jan 12, 2015
1 parent 10c934b commit 21a036c
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 54 deletions.
77 changes: 59 additions & 18 deletions Templates/Full/game/shaders/common/lighting.hlsl
Expand Up @@ -37,6 +37,7 @@ uniform float4 inLightColor[4];
#endif

uniform float4 ambient;
#define ambientCameraFactor 0.3
uniform float specularPower;
uniform float4 specularColor;

Expand Down Expand Up @@ -205,13 +206,62 @@ void compute4Lights( float3 wsView,
/// @param toEye The normalized vector representing direction from the pixel
/// being lit to the camera.
///
float AL_CalcSpecular( float3 toLight, float3 normal, float3 toEye )
float3 AL_CalcSpecular( float3 baseColor, float3 lightColor, float3 toLight, float3 normal, float3 toEye, float roughness, float metallic )
{
// (R.V)^c
float specVal = dot( normalize( -reflect( toLight, normal ) ), toEye );
float PI = 3.14159265898793f;

// Return the specular factor.
return pow( max( specVal, 0.00001f ), AL_ConstantSpecularPower );
float nDotL = saturate( dot( normal, toLight ) );

if ( nDotL == 0 )
return float3( 0.0f, 0.0f, 0.0f );

float3 V = -toEye;
float3 H = normalize( toLight + V );

float nDotH = saturate( dot( normal, H ) );
float nDotV = saturate( dot( normal, V ) );
float HDotV = saturate( dot( H, V ) );

//
// Microfacet Specular Cook-Torrance
//

float alphaSqr = pow( roughness, 4 );

float D = alphaSqr / ( PI * pow( (pow( nDotH, 2 ) * ( alphaSqr - 1.0f ) + 1.0f ), 2 ) );

//
// G( l, v, h ) ==> Geometric attenution, basicly a visibility term
//


float k = pow( ( roughness + 1.0f ), 2 ) / 8;

float G = ( nDotL * ( 1.0f - k ) + k ) * ( nDotV * ( 1.0f - k ) + k );
G = 1.0f / G;

//
// F( v, h ) ==> frensel term, slicks approach.
//

float Fc = pow( 1.0f - HDotV, 5.0f );

float Fdielectric = 0.04 + ( 1.0 - 0.04 ) * Fc;

float3 Fconductor = baseColor + ( float3( 1.0, 1.0, 1.0 ) - baseColor ) * Fc;

//
// Evalute based on the metallic property
//

float fDielectric = ( ( 1.0f - metallic ) * D * G * Fdielectric ) / 4.0f ;
float3 fConductor = ( metallic * D * G * Fconductor ) / 4.0f ;

//
// Specular color
//

return ( fDielectric + fConductor ) * nDotL * lightColor;
}

/// The output for Deferred Lighting
Expand All @@ -227,22 +277,13 @@ float AL_CalcSpecular( float3 toLight, float3 normal, float3 toEye )
float4 AL_DeferredOutput(
float3 lightColor,
float3 diffuseColor,
float4 matInfo,
float4 ambient,
float specular,
float shadowAttenuation)
{
float3 specularColor = float3(specular, specular, specular);
bool metalness = getFlag(matInfo.r, 3);
if ( metalness )
{
specularColor = 0.04 * (1 - specular) + diffuseColor * specular;
}

//specular = color * map * spec^gloss
float specularOut = (specularColor * matInfo.b * min(pow(specular, max(( matInfo.a/ AL_ConstantSpecularPower),1.0f)),matInfo.a)).r;

lightColor *= shadowAttenuation;
lightColor += ambient.rgb;
return float4(lightColor.rgb, specularOut);

diffuseColor = saturate(diffuseColor);
lightColor *= diffuseColor.rgb;
return float4(lightColor.rgb, 0.0f);
}
Expand Up @@ -35,13 +35,6 @@ float4 main( PFXVertToPix IN,
float4 matInfo = tex2D( matInfoTex, IN.uv0 );
float specular = saturate(lightBuffer.a);

// Diffuse Color Altered by Metalness
bool metalness = getFlag(matInfo.r, 3);
if ( metalness )
{
colorBuffer *= (1.0 - colorBuffer.a);
}

colorBuffer *= float4(lightBuffer.rgb, 1.0);
colorBuffer += float4(specular, specular, specular, 1.0);

Expand Down
Expand Up @@ -224,12 +224,18 @@ float4 main( ConvexConnectP IN,
// cause the hardware occlusion query to disable the shadow.

// Specular term
float specular = AL_CalcSpecular( lightVec,
normal,
normalize( -eyeRay ) ) * lightBrightness * atten * shadowed;

float4 colorSample = tex2D( colorBuffer, uvScene );
float specular = 0;
float3 real_specular = AL_CalcSpecular( colorSample.rgb,
lightColor.rgb,
lightVec,
normal,
viewSpacePos,
matInfo.b,
matInfo.a );

float Sat_NL_Att = saturate( nDotL * atten * shadowed ) * lightBrightness;
float3 lightColorOut = lightMapParams.rgb * lightColor.rgb;
float3 lightColorOut = (lightColor.rgb + real_specular) * lightBrightness * shadowed * atten;
float4 addToResult = 0.0;

// TODO: This needs to be removed when lightmapping is disabled
Expand Down Expand Up @@ -264,6 +270,5 @@ float4 main( ConvexConnectP IN,
addToResult = lightColor * float4( fLT, 0.0);
}

float4 colorSample = tex2D( colorBuffer, uvScene );
return AL_DeferredOutput(lightColorOut, colorSample.rgb, matInfo, addToResult, specular, Sat_NL_Att);
return AL_DeferredOutput(lightColorOut, colorSample.rgb, addToResult, Sat_NL_Att);
}
Expand Up @@ -152,12 +152,20 @@ float4 main( ConvexConnectP IN,
// cause the hardware occlusion query to disable the shadow.

// Specular term
float specular = AL_CalcSpecular( -lightToPxlVec,
normal,
normalize( -eyeRay ) ) * lightBrightness * atten * shadowed;
float4 colorSample = tex2D( colorBuffer, uvScene );
float specular = 0;

float3 lightVec = lightPosition - viewSpacePos;
float3 real_specular = AL_CalcSpecular( colorSample.rgb,
lightColor.rgb,
lightVec,
normal,
viewSpacePos,
matInfo.b,
matInfo.a );

float Sat_NL_Att = saturate( nDotL * atten * shadowed ) * lightBrightness;
float3 lightColorOut = lightMapParams.rgb * lightColor.rgb;
float3 lightColorOut = (lightColor.rgb + real_specular) * lightBrightness * shadowed * atten;
float4 addToResult = 0.0;

// TODO: This needs to be removed when lightmapping is disabled
Expand All @@ -176,6 +184,5 @@ float4 main( ConvexConnectP IN,
addToResult = ( 1.0 - shadowed ) * abs(lightMapParams);
}

float4 colorSample = tex2D( colorBuffer, uvScene );
return AL_DeferredOutput(lightColorOut, colorSample.rgb, matInfo, addToResult, specular, Sat_NL_Att);
return AL_DeferredOutput(lightColorOut, colorSample.rgb, addToResult, Sat_NL_Att);
}
Expand Up @@ -209,22 +209,20 @@ float4 main( FarFrustumQuadConnectP IN,
#endif // !NO_SHADOW

// Specular term
float specular = AL_CalcSpecular( -lightDirection,
normal,
normalize(-IN.vsEyeRay) ) * lightBrightness * shadowed;
float4 colorSample = tex2D( colorBuffer, IN.uv0 );
float specular = 0;
float3 real_specular = AL_CalcSpecular( colorSample.rgb,
lightColor.rgb,
normalize( -lightDirection ),
normal,
IN.vsEyeRay * depth,
matInfo.b,
matInfo.a );

float Sat_NL_Att = saturate( dotNL * shadowed ) * lightBrightness;
float3 lightColorOut = lightMapParams.rgb * lightColor.rgb;

// Felix' Normal Mapped Ambient.
float ambientBrightness = (float)lightAmbient;
float3 worldNormal = normalize(mul(eyeMat, float4(normal,1.0))).xyz;
float ambientContrast = 0.5;
float4 upAmbient = lerp( 1 - lightAmbient * 0.65, lightAmbient, 1-ambientBrightness*ambientContrast );
float4 lightAmbientTwoTone = lerp( lightAmbient * 0.8 , upAmbient , worldNormal.b );
float4 addToResult = lightAmbientTwoTone + dotNL * lightColor * ambientBrightness * 0.25;

//float4 addToResult = lightAmbient;
float3 lightColorOut = (lightColor.rgb + real_specular) * lightBrightness * shadowed;

float4 addToResult = (lightAmbient * (1 - ambientCameraFactor)) + ( lightAmbient * ambientCameraFactor * saturate(dot(normalize(-IN.vsEyeRay), normal)) );

// TODO: This needs to be removed when lightmapping is disabled
// as its extra work per-pixel on dynamic lit scenes.
Expand Down Expand Up @@ -267,6 +265,5 @@ float4 main( FarFrustumQuadConnectP IN,
addToResult = lightColor * float4( fLT, 0.0);
}

float4 colorSample = tex2D( colorBuffer, IN.uv0 );
return AL_DeferredOutput(lightColorOut, colorSample.rgb, matInfo, addToResult, specular, Sat_NL_Att);
return AL_DeferredOutput(lightColorOut, colorSample.rgb, addToResult, Sat_NL_Att);
}

0 comments on commit 21a036c

Please sign in to comment.