Reorganised shaders

This commit is contained in:
Emmanuel BENOîT 2017-10-04 19:06:50 +02:00
parent b75cee8638
commit 5306ce4535
12 changed files with 318 additions and 472 deletions

View file

@ -0,0 +1,90 @@
//! type library
vec2 RM_Map( in vec3 pos );
// -----------------------------------------------------------------------------
vec3 RM_GetNormal(
in vec3 pos )
{
vec2 v = vec2( .0005 , 0 );
return normalize( vec3(
RM_Map( pos + v.xyy ).x - RM_Map( pos - v.xyy ).x ,
RM_Map( pos + v.yxy ).x - RM_Map( pos - v.yxy ).x ,
RM_Map( pos + v.yyx ).x - RM_Map( pos - v.yyx ).x ) );
}
// -----------------------------------------------------------------------------
vec3 RM_Basic(
in vec3 origin ,
in vec3 direction ,
in int steps ,
in float factor ,
in float epsilon ,
in float dMin ,
in float dMax )
{
int i = 0;
float dist = dMin , mat = -1;
for ( ; i < steps ; ++ i ) {
vec2 res = RM_Map( origin + direction * dist );
if ( abs( res.x ) < epsilon || dist > dMax ) {
break;
}
dist += res.x * factor;
mat = res.y;
}
return vec3( dist , dist >= dMax ? -1 : mat , i );
}
vec3 RM_Advanced(
in vec3 origin ,
in vec3 direction ,
in int steps ,
in float factor ,
in float epsilon ,
in float dMin ,
in float dMax )
{
int i = 0;
float dist = dMin ,
omega = factor ,
cError = 1 / 0. ,
cDist = dMin ,
pRad = 0 ,
sLen = 0;
for ( ; i < steps ; ++ i ) {
vec2 res = RM_Map( origin + direction * dist );
float rad = abs( res.x );
bool sorFail = omega > 1 && ( rad + pRad ) < sLen;
if ( sorFail ) {
sLen -= omega * sLen;
omega = 1;
} else {
sLen = res.x * omega;
}
pRad = rad;
float error = rad / dist;
if ( !sorFail && error < cError ) {
cError = error;
cDist = dist;
}
if ( !sorFail && error < epsilon || dist > dMax ) {
break;
}
dist += sLen;
}
if ( dist <= dMax && cError <= epsilon ) {
return vec3( cDist , RM_Map( origin + direction * cDist ).y , i );
}
return vec3( cDist , -1 , steps );
}

View file

@ -0,0 +1,23 @@
//! type library
struct T_BPMaterial
{
vec3 cAlbedo, cSpecular;
float specPower , ambient;
};
vec3 BP_Shade(
in T_BPMaterial material ,
in vec3 rayDir ,
in vec3 normal ,
in vec3 lightDir )
{
const vec3 halfVec = normalize( rayDir + lightDir );
const float nDotL = dot( normal , lightDir ) ,
nDotH = dot( normal , halfVec ) ,
si = pow( clamp( nDotH , 0 , 1 ) , material.specPower ) ,
di = material.ambient + ( 1 - material.ambient )
* clamp( nDotL , 0 , 1 );
return mix( material.cAlbedo * di , material.cSpecular , si );
}

View file

@ -0,0 +1,111 @@
//! type library
//! include lib/utils.glsl
struct T_PBRMaterial
{
vec3 cAlbedo;
float roughness;
float metallic;
float subsurface;
float anisotropy;
float specular; // Specular strengh for non-metals
float specularTint; // Albedo color% in specular tint (non-metals)
};
// -----------------------------------------------------------------------------
float PBR_SchlickFresnel(
in float u )
{
const float m = clamp( 1 - u , 0 , 1 ) ,
m2 = m * m;
return m2 * m2 * m;
}
float PBR_GTR2Aniso(
in float nDotH ,
in float hDotX ,
in float hDotY ,
in float ax ,
in float ay )
{
float x = hDotX / ax , y = hDotY / ay ,
p = x * x + y * y + nDotH * nDotH;
return 1 / ( PI * ax * ay * p * p );
}
float PBR_SmithGGXAniso(
in float nDotV ,
in float vDotX ,
in float vDotY ,
in float ax ,
in float ay )
{
float x = vDotX * ax , y = vDotY * ay;
return 1 / ( nDotV + sqrt( x * x + y * y + nDotV * nDotV ) );
}
vec3 PBR_Shade(
in T_PBRMaterial material ,
in vec3 camDir ,
in vec3 normal ,
in vec3 lightDir )
{
float nDotL = dot( normal , lightDir ) ,
nDotV = dot( normal , camDir );
if ( nDotL < 0 || nDotV < 0 ) {
return vec3( 0 );
}
vec3 tangent = cross( vec3( 0 , 1 , 0 ) , normal );
if ( length( tangent ) == 0 ) {
tangent = cross( vec3( 1 , 0 , 0 ) , normal );
}
tangent = normalize( tangent );
vec3 bitangent = normalize( cross( normal , tangent ) );
vec3 halfVec = normalize( lightDir + camDir ) ,
tint = M_NormalizeColor( material.cAlbedo ) ,
cSpecular = mix( material.specular * .08 * mix(
vec3( 1 ) , tint , material.specularTint ) ,
material.cAlbedo , material.metallic );
//vec3 Csheen = mix(vec3(1), Ctint, sheenTint);
float nDotH = dot( normal , halfVec ) ,
lDotH = dot( lightDir , halfVec ) ,
// Diffuse fresnel - go from 1 at normal incidence to .5 at grazing
// and mix in diffuse retro-reflection based on roughness
FL = PBR_SchlickFresnel( nDotL ) ,
FV = PBR_SchlickFresnel( nDotV ) ,
Fd90 = 0.5 + 2 * lDotH * lDotH * material.roughness ,
Fd = mix( 1 , Fd90 , FL ) * mix( 1 , Fd90 , FV ) ,
// Based on Hanrahan-Krueger brdf approximation of isotropic bssrdf
// 1.25 scale is used to (roughly) preserve albedo
// Fss90 used to "flatten" retroreflection based on roughness
Fss90 = lDotH * lDotH * material.roughness ,
Fss = mix( 1 , Fss90 , FL ) * mix( 1 , Fss90 , FV ) ,
ss = 1.25 * ( Fss * ( 1 / ( nDotL + nDotV ) - .5 ) + .5 ) ,
// Specular
aspect = sqrt( 1 - material.anisotropy * .9 ) ,
rsqr = material.roughness * material.roughness ,
ax = max( .001, rsqr / aspect ) ,
ay = max( .001, rsqr * aspect ) ,
Ds = PBR_GTR2Aniso( nDotH , dot( halfVec , tangent ) ,
dot( halfVec , bitangent ) , ax , ay ) ,
FH = PBR_SchlickFresnel( lDotH ) ,
Gs = PBR_SmithGGXAniso( nDotL , dot( lightDir , tangent ) ,
dot( lightDir , bitangent ) , ax , ay )
* PBR_SmithGGXAniso( nDotV , dot( camDir , tangent ) ,
dot( camDir , bitangent ) , ax , ay );
vec3 Fs = mix( cSpecular , vec3(1) , FH );
return nDotL * ( ( ( 1 / PI )
* mix( Fd , ss , material.subsurface )
* material.cAlbedo /* + Fsheen */)
* pow( 1 - material.metallic , 3 )
+ clamp( Gs , 0 , 1 ) * Fs * Ds );
}

View file

@ -1,11 +1,30 @@
//! type library
const float PI = 3.14159265;
// -----------------------------------------------------------------------------
float M_Hash( in vec2 p )
float M_Hash(
in vec2 p )
{
p = fract(p * vec2(5.3987, 5.4421));
p += dot(p.yx, p.xy + vec2(21.5351, 14.3137));
return fract(p.x * p.y * 95.4307);
}
// -----------------------------------------------------------------------------
float M_Luminosity(
in vec3 color )
{
return dot( color , vec3( .3 , .6 , .1 ) );
}
vec3 M_NormalizeColor(
in vec3 color )
{
const float l = M_Luminosity( color );
return l > 0 ? ( color / l ) : vec3( 1 );
}