demotool/test/scene.f.glsl

174 lines
4 KiB
Text
Raw Normal View History

#version 450 core
//! type fragment
2017-10-04 19:06:50 +02:00
//! include /lib/raymarcher.glsl
//! include /lib/hg_sdf.glsl
2017-10-05 20:29:39 +02:00
//! include /lib/shading-pbr.glsl
//! include /lib/shading-blinnphong.glsl
//! include /lib/fog.glsl
2017-10-04 19:06:50 +02:00
T_BPMaterial BPMaterials[1] = {
2017-10-06 09:36:49 +02:00
{ vec3( .95 , .8 , 1 ) * .2 , vec3( 10 ) , 800 , 0 }
2017-10-04 19:06:50 +02:00
};
T_PBRMaterial PBRMaterials[1] = {
{
// Albedo color
2017-10-06 09:36:49 +02:00
vec3( .9 , 1 , .9 ) ,
2017-10-04 19:06:50 +02:00
// Roughness , metallic , subsurface , anisotropy
2017-10-06 10:30:06 +02:00
.15 , .5 , .9 , .9 ,
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;
}
vec2 RM_Map( vec3 pos )
{
vec3 q = pos;
2017-10-05 20:41:57 +02:00
HG_pMod2( q.xy , vec2( 8. ) );
HG_pR( q.xz , 1 );
2017-10-05 22:28:04 +02:00
return vec2( HG_fOpUnionStairs(
HG_fBox( q , vec3( 2.3 ) ) ,
HG_fSphere( q , 2.6 ) ,
1. , 4 ) ,
step( 0. , -HG_fSphere( pos , 3 ) ) );
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;
2017-10-06 09:36:49 +02:00
float lPwr = lightIntensity / max( .001 , dot( lightRay , lightRay ) );
if ( lPwr < .00005 ) {
2017-10-05 15:05:05 +02:00
return vec3( 0 );
}
2017-10-06 09:36:49 +02:00
return lightColor * lPwr * PBR_Shade( material , pre ,
-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 );
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 );
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 22:28:04 +02:00
tanPhi , .01 , 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. ) {
const vec3 hitPos = RM_ReduceDiscontinuity(
u_CamPos , rayDir , r.x ,
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
const int midx = int( r.y );
2017-10-05 15:05:05 +02:00
const vec3 sunlightDir = normalize( vec3( 0 , 1 , 1 ) );
2017-10-06 10:30:06 +02:00
const vec3 sunlightColor = vec3( 1 , 1 , .99 ) * .05;
2017-10-05 15:05:05 +02:00
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-06 09:36:49 +02:00
T_BPMaterial mat = BPMaterials[ mtidx ];
bc = sunlightColor * BP_Shade( mat ,
2017-10-05 15:05:05 +02:00
-rayDir , normal , sunlightDir );
2017-10-06 09:36:49 +02:00
bc += pointlight( mat ,
hitPos , rayDir , normal ,
pl1Pos , pl1Color , pl1Power );
bc += pointlight( mat ,
hitPos , rayDir , normal ,
pl2Pos , pl2Color , pl2Power );
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 22:28:04 +02:00
bc = FOG_Apply( bc , length( u_CamPos - hitPos ) ,
u_FogAttenuation , background );
o_Z = r.x;
} else {
o_Z = u_Render.w;
2017-10-04 19:06:50 +02:00
}
o_Color = bc;
}