2017-10-04 17:29:48 +02:00
|
|
|
#version 450 core
|
|
|
|
//! type fragment
|
2017-10-04 19:06:50 +02:00
|
|
|
|
|
|
|
//! include chunks/raymarcher.glsl
|
2017-10-05 20:29:39 +02:00
|
|
|
//! include lib/hg_sdf.glsl
|
|
|
|
|
2017-10-04 19:06:50 +02:00
|
|
|
//! include lib/shading-pbr.glsl
|
|
|
|
//! include lib/shading-blinnphong.glsl
|
2017-10-05 09:07:37 +02:00
|
|
|
//! include lib/fog.glsl
|
2017-10-04 19:06:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
T_BPMaterial BPMaterials[1] = {
|
2017-10-05 15:05:05 +02:00
|
|
|
{ vec3( .95 , .8 , 1 ) , vec3( 1 ) , 1 , 5 }
|
2017-10-04 19:06:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
T_PBRMaterial PBRMaterials[1] = {
|
|
|
|
{
|
|
|
|
// Albedo color
|
2017-10-05 15:05:05 +02:00
|
|
|
vec3( .8 , 1 , .8 ) ,
|
2017-10-04 19:06:50 +02:00
|
|
|
// Roughness , metallic , subsurface , anisotropy
|
2017-10-05 15:05:05 +02:00
|
|
|
.2 , .7 , .7 , .7 ,
|
2017-10-04 19:06:50 +02:00
|
|
|
// Specular strength / tint%
|
|
|
|
.5 , .5
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
vec3 Glow[1] = {
|
2017-10-05 11:47:33 +02:00
|
|
|
vec3( .95 , .8 , 1 ) * 5
|
2017-10-04 19:06:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
void mapMaterial(
|
|
|
|
in int matIndex ,
|
|
|
|
out int type ,
|
|
|
|
out int tIndex ,
|
|
|
|
out int glowIndex )
|
|
|
|
{
|
|
|
|
if ( matIndex == 0 ) {
|
2017-10-05 15:05:05 +02:00
|
|
|
type = 1;
|
2017-10-04 19:06:50 +02:00
|
|
|
glowIndex = -1;
|
|
|
|
} else {
|
2017-10-05 15:05:05 +02:00
|
|
|
type = 0;
|
2017-10-04 19:06:50 +02:00
|
|
|
glowIndex = 0;
|
|
|
|
}
|
|
|
|
tIndex = 0;
|
|
|
|
}
|
|
|
|
|
2017-10-05 15:05:05 +02:00
|
|
|
float box( vec3 p , vec3 sz )
|
|
|
|
{
|
|
|
|
vec3 n = abs( p ) - sz;
|
|
|
|
return max( max( n.x , n.y ) , n.z );
|
|
|
|
}
|
|
|
|
|
2017-10-04 19:06:50 +02:00
|
|
|
vec2 RM_Map( vec3 pos )
|
|
|
|
{
|
|
|
|
pos = pos - vec3( 0 , 0 , 0 );
|
|
|
|
vec3 q = pos;
|
|
|
|
q.xy = mod( q.xy + 4. , 8. ) - 4.;
|
2017-10-05 15:05:05 +02:00
|
|
|
q.xz *= mat2(
|
|
|
|
cos( 1 ) , -sin(1) ,
|
|
|
|
sin( 1 ) , cos( 1 ) );
|
|
|
|
return vec2( mix( box( q , vec3( 1.5 ) ) ,
|
|
|
|
length( q ) - 1.5 ,
|
2017-10-05 17:13:58 +02:00
|
|
|
.35 ) ,
|
|
|
|
step( 0. , 3 - length( pos.xy ) ) );
|
2017-10-05 15:05:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
vec3 pointlight(
|
|
|
|
in T_PBRMaterial material ,
|
2017-10-05 15:28:33 +02:00
|
|
|
in T_PBRPrecomputed pre ,
|
2017-10-05 15:05:05 +02:00
|
|
|
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( .01 , dot( lightRay , lightRay ) );
|
|
|
|
if ( lPwr < .05 ) {
|
|
|
|
return vec3( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
return lightColor * lPwr * PBR_Shade(
|
2017-10-05 15:28:33 +02:00
|
|
|
material , pre , -rayDir , normal , lightRay );
|
2017-10-04 19:06:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void main( )
|
|
|
|
{
|
|
|
|
vec2 uv = ( gl_FragCoord.xy / u_Resolution ) * 2 - 1;
|
|
|
|
vec3 camDir = normalize( u_LookAt - u_CamPos );
|
|
|
|
vec3 side = normalize( cross( u_CamUp , camDir ) );
|
|
|
|
vec3 up = normalize( cross( camDir , side ) );
|
|
|
|
vec3 rayDir = normalize( camDir * u_NearPlane
|
|
|
|
+ uv.x * side * u_Resolution.x / u_Resolution.y
|
|
|
|
+ uv.y * up );
|
|
|
|
|
2017-10-05 17:03:31 +02:00
|
|
|
float tanPhi = RM_TanPhi( uv , u_Render.z );
|
|
|
|
|
2017-10-04 19:06:50 +02:00
|
|
|
vec3 r = RM_Advanced( u_CamPos , rayDir ,
|
|
|
|
int( u_Render.x ) , u_Render.y ,
|
2017-10-05 17:03:31 +02:00
|
|
|
/*u_Render.z */ tanPhi , .001 , u_Render.w );
|
2017-10-05 11:47:33 +02:00
|
|
|
const vec3 background = vec3( .005 );
|
2017-10-04 19:06:50 +02:00
|
|
|
|
2017-10-05 09:07:37 +02:00
|
|
|
vec3 bc = background;
|
2017-10-04 19:06:50 +02:00
|
|
|
if ( r.y >= 0. ) {
|
2017-10-05 17:03:31 +02:00
|
|
|
const vec3 hitPos = RM_ReduceDiscontinuity(
|
|
|
|
u_CamPos , rayDir , r.x ,
|
2017-10-05 17:13:58 +02:00
|
|
|
tanPhi , u_Correction );
|
2017-10-04 19:06:50 +02:00
|
|
|
const vec3 normal = RM_GetNormal( hitPos );
|
2017-10-05 15:05:05 +02:00
|
|
|
|
2017-10-05 17:03:31 +02:00
|
|
|
const int midx = int( r.y );
|
2017-10-05 15:05:05 +02:00
|
|
|
const vec3 sunlightDir = normalize( vec3( 0 , 1 , 1 ) );
|
|
|
|
const vec3 sunlightColor = vec3( 1 , 1 , .99 ) * .1;
|
|
|
|
const vec3 pl1Pos = vec3( 3 , 3 , 5 );
|
|
|
|
const vec3 pl1Color = vec3( 1 , .7 , .8 );
|
|
|
|
const float pl1Power = 20;
|
|
|
|
const vec3 pl2Pos = vec3( -3 , -3 , 5 );
|
|
|
|
const vec3 pl2Color = vec3( .6 , 1 , .8 );
|
|
|
|
const float pl2Power = 20;
|
2017-10-04 19:06:50 +02:00
|
|
|
|
|
|
|
// Remap materials through mapMaterials
|
|
|
|
int mtype , mtidx , glowidx;
|
|
|
|
mapMaterial( midx , mtype , mtidx , glowidx );
|
|
|
|
if ( mtype == 0 ) {
|
2017-10-05 15:05:05 +02:00
|
|
|
bc = sunlightColor * BP_Shade( BPMaterials[ mtidx ] ,
|
|
|
|
-rayDir , normal , sunlightDir );
|
2017-10-04 19:06:50 +02:00
|
|
|
} else {
|
2017-10-05 15:05:05 +02:00
|
|
|
T_PBRMaterial mat = PBRMaterials[ mtidx ];
|
2017-10-05 15:28:33 +02:00
|
|
|
T_PBRPrecomputed pre = PBR_Precompute(
|
|
|
|
mat , -rayDir , normal );
|
|
|
|
bc = sunlightColor * PBR_Shade( mat , pre ,
|
2017-10-05 15:05:05 +02:00
|
|
|
-rayDir , normal , sunlightDir );
|
2017-10-05 15:28:33 +02:00
|
|
|
bc += pointlight( mat , pre ,
|
|
|
|
hitPos , rayDir , normal ,
|
2017-10-05 15:05:05 +02:00
|
|
|
pl1Pos , pl1Color , pl1Power );
|
2017-10-05 15:28:33 +02:00
|
|
|
bc += pointlight( mat , pre ,
|
|
|
|
hitPos , rayDir , normal ,
|
2017-10-05 15:05:05 +02:00
|
|
|
pl2Pos , pl2Color , pl2Power );
|
2017-10-04 19:06:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( glowidx >= 0 ) {
|
|
|
|
bc += Glow[ glowidx ];
|
|
|
|
}
|
2017-10-05 09:07:37 +02:00
|
|
|
bc = FOG_Apply( bc , r.x , u_FogAttenuation , background );
|
2017-10-04 19:06:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
o_Color = bc;
|
|
|
|
o_Z = r.x;
|
|
|
|
}
|