PBR fixing, episode 572

This commit is contained in:
Emmanuel BENOîT 2017-10-06 09:36:49 +02:00
parent f141a948ac
commit dbac828891
3 changed files with 51 additions and 24 deletions

View file

@ -24,9 +24,8 @@ struct T_PBRPrecomputed
float PBR_SchlickFresnel( float PBR_SchlickFresnel(
in float u ) in float u )
{ {
const float m = clamp( 1 - u , 0 , 1 ) , const float m = 1 - u , m2 = M_Sqr( m );
m2 = m * m; return M_Sqr( m2 ) * m;
return m2 * m2 * m;
} }
float PBR_GTR2Aniso( float PBR_GTR2Aniso(
@ -38,7 +37,7 @@ float PBR_GTR2Aniso(
{ {
float x = hDotX / ax , y = hDotY / ay , float x = hDotX / ax , y = hDotY / ay ,
p = x * x + y * y + nDotH * nDotH; p = x * x + y * y + nDotH * nDotH;
return 1 / ( PI * ax * ay * p * p ); return 1 / ( ax * ay * p * p );
} }
float PBR_SmithGGXAniso( float PBR_SmithGGXAniso(
@ -59,7 +58,7 @@ T_PBRPrecomputed PBR_Precompute(
{ {
T_PBRPrecomputed rv; T_PBRPrecomputed rv;
rv.nDotV = dot( normal , camDir ); rv.nDotV = abs( dot( normal , camDir ) ) + 1e-5;
rv.fv = PBR_SchlickFresnel( rv.nDotV ); rv.fv = PBR_SchlickFresnel( rv.nDotV );
float tDir = step( .99 , abs( normal.z ) ) , float tDir = step( .99 , abs( normal.z ) ) ,
@ -93,19 +92,21 @@ vec3 PBR_Shade(
in vec3 lightDir ) in vec3 lightDir )
{ {
float nDotL = dot( normal , lightDir ); float nDotL = dot( normal , lightDir );
if ( nDotL < 0 || pre.nDotV < 0 ) { if ( nDotL < 0 ) {
return vec3( 0 ); return vec3( 0 );
} }
vec3 halfVec = normalize( lightDir + camDir ) ; vec3 halfVec = normalize( lightDir + camDir ) ;
float nDotH = dot( normal , halfVec ) , float nDotH = M_Saturate( dot( normal , halfVec ) ) ,
lDotH = dot( lightDir , halfVec ) , lDotH = M_Saturate( dot( lightDir , halfVec ) ) ,
// Diffuse fresnel - go from 1 at normal incidence to .5 at grazing // Diffuse fresnel - go from 1 at normal incidence to .5 at grazing
// and mix in diffuse retro-reflection based on roughness // and mix in diffuse retro-reflection based on roughness
FL = PBR_SchlickFresnel( nDotL ) , FL = PBR_SchlickFresnel( nDotL ) ,
Fd90 = 0.5 + 2 * lDotH * lDotH * material.roughness , Fd90 = ( 0.5 + 2 * lDotH * lDotH ) * material.roughness ,
Fd = mix( 1 , Fd90 , FL ) * mix( 1 , Fd90 , pre.fv ) , Fd = ( 1 + ( Fd90 - 1 ) * FL )
* ( 1 + ( Fd90 - 1 ) * pre.fv )
* ( 1 - 0.51 * material.roughness / 1.51 ) ,
// Based on Hanrahan-Krueger brdf approximation of isotropic bssrdf // Based on Hanrahan-Krueger brdf approximation of isotropic bssrdf
// 1.25 scale is used to (roughly) preserve albedo // 1.25 scale is used to (roughly) preserve albedo
@ -124,11 +125,10 @@ vec3 PBR_Shade(
pre.ax , pre.ay ) * pre.vgs; pre.ax , pre.ay ) * pre.vgs;
vec3 Fs = mix( pre.cSpecular , vec3(1) , FH ); vec3 Fs = mix( pre.cSpecular , vec3(1) , FH );
return nDotL * ( ( ( 1 / PI ) return nDotL * ( mix( Fd , ss , material.subsurface )
* mix( Fd , ss , material.subsurface ) * material.cAlbedo
* material.cAlbedo /* + Fsheen */) * ( 1 - material.metallic )
* pow( 1 - material.metallic , 3 ) + Gs * Fs * Ds ) / PI;
+ Gs * Fs * Ds );
} }
vec3 PBR_Shade( vec3 PBR_Shade(

View file

@ -36,7 +36,7 @@ vec3 M_NormalizeColor(
float M_Saturate( float M_Saturate(
in float x ) in float x )
{ {
return clamp( x , 0 , 1 ); return clamp( x , 0. , 1. );
} }
float M_LengthSqr( float M_LengthSqr(

View file

@ -10,15 +10,15 @@
T_BPMaterial BPMaterials[1] = { T_BPMaterial BPMaterials[1] = {
{ vec3( .95 , .8 , 1 ) , vec3( 1 ) , 1 , 5 } { vec3( .95 , .8 , 1 ) * .2 , vec3( 10 ) , 800 , 0 }
}; };
T_PBRMaterial PBRMaterials[1] = { T_PBRMaterial PBRMaterials[1] = {
{ {
// Albedo color // Albedo color
vec3( .8 , 1 , .8 ) , vec3( .9 , 1 , .9 ) ,
// Roughness , metallic , subsurface , anisotropy // Roughness , metallic , subsurface , anisotropy
.3 , .8 , .8 , .9 , .25 , .5 , .9 , .9 ,
// Specular strength / tint% // Specular strength / tint%
.5 , .5 .5 , .5
} }
@ -68,13 +68,33 @@ vec3 pointlight(
in float lightIntensity ) in float lightIntensity )
{ {
vec3 lightRay = lightPos - hitPos; vec3 lightRay = lightPos - hitPos;
float lPwr = lightIntensity / max( .01 , dot( lightRay , lightRay ) ); float lPwr = lightIntensity / max( .001 , dot( lightRay , lightRay ) );
if ( lPwr < .05 ) { if ( lPwr < .00005 ) {
return vec3( 0 ); return vec3( 0 );
} }
return lightColor * lPwr * PBR_Shade( return lightColor * lPwr * PBR_Shade( material , pre ,
material , pre , -rayDir , normal , lightRay ); -rayDir , normal , normalize( lightRay ) );
}
vec3 pointlight(
in T_BPMaterial material ,
in vec3 hitPos ,
in vec3 rayDir ,
in vec3 normal ,
in vec3 lightPos ,
in vec3 lightColor ,
in float lightIntensity )
{
vec3 lightRay = lightPos - hitPos;
float lPwr = lightIntensity / max( .1 , dot( lightRay , lightRay ) );
if ( lPwr < .00005 ) {
return vec3( 0 );
}
return lightColor * lPwr * BP_Shade(
material , -rayDir , normal , lightRay );
} }
@ -116,8 +136,15 @@ void main( )
int mtype , mtidx , glowidx; int mtype , mtidx , glowidx;
mapMaterial( midx , mtype , mtidx , glowidx ); mapMaterial( midx , mtype , mtidx , glowidx );
if ( mtype == 0 ) { if ( mtype == 0 ) {
bc = sunlightColor * BP_Shade( BPMaterials[ mtidx ] , T_BPMaterial mat = BPMaterials[ mtidx ];
bc = sunlightColor * BP_Shade( mat ,
-rayDir , normal , sunlightDir ); -rayDir , normal , sunlightDir );
bc += pointlight( mat ,
hitPos , rayDir , normal ,
pl1Pos , pl1Color , pl1Power );
bc += pointlight( mat ,
hitPos , rayDir , normal ,
pl2Pos , pl2Color , pl2Power );
} else { } else {
T_PBRMaterial mat = PBRMaterials[ mtidx ]; T_PBRMaterial mat = PBRMaterials[ mtidx ];
T_PBRPrecomputed pre = PBR_Precompute( T_PBRPrecomputed pre = PBR_Precompute(