#version 450 core //! type fragment //! include /lib/raymarcher.glsl //! include /lib/hg_sdf.glsl //! include /lib/shading-pbr.glsl //! include /lib/shading-blinnphong.glsl //! include /lib/fog.glsl T_BPMaterial BPMaterials[1] = { { vec3( .95 , .8 , 1 ) * .2 , vec3( 10 ) , 800 , 0 } }; T_PBRMaterial PBRMaterials[1] = { { // Albedo color vec3( .9 , 1 , .9 ) , // Roughness , metallic , subsurface , anisotropy .15 , .5 , .9 , .9 , // Specular strength / tint% .5 , .5 } }; vec3 Glow[1] = { vec3( .95 , .8 , 1 ) * 5 }; void mapMaterial( in int matIndex , out int type , out int tIndex , out int glowIndex ) { if ( matIndex == 0 ) { type = 1; glowIndex = -1; } else { type = 0; glowIndex = 0; } tIndex = 0; } vec2 RM_Map( vec3 pos ) { vec3 q = pos; HG_pMod2( q.xy , vec2( 8. ) ); HG_pR( q.xz , 1 ); return vec2( HG_fOpUnionStairs( HG_fBox( q , vec3( 2.3 ) ) , HG_fSphere( q , 2.6 ) , 1. , 4 ) , step( 0. , -HG_fSphere( pos , 3 ) ) ); } vec3 pointlight( in T_PBRMaterial material , in T_PBRPrecomputed pre , 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( .001 , dot( lightRay , lightRay ) ); if ( lPwr < .00005 ) { return vec3( 0 ); } 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 ); } 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 ); vec3 r = RM_Advanced( u_CamPos , rayDir , int( u_Render.x ) , u_Render.y , tanPhi , .01 , u_Render.w ); const vec3 background = vec3( .005 ); vec3 bc = background; if ( r.y >= 0. ) { const vec3 hitPos = RM_ReduceDiscontinuity( u_CamPos , rayDir , r.x , tanPhi , u_Correction ); const vec3 normal = RM_GetNormal( hitPos ); const int midx = int( r.y ); const vec3 sunlightDir = normalize( vec3( 0 , 1 , 1 ) ); const vec3 sunlightColor = vec3( 1 , 1 , .99 ) * .05; 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; // Remap materials through mapMaterials int mtype , mtidx , glowidx; mapMaterial( midx , mtype , mtidx , glowidx ); if ( mtype == 0 ) { T_BPMaterial mat = BPMaterials[ mtidx ]; bc = sunlightColor * BP_Shade( mat , -rayDir , normal , sunlightDir ); bc += pointlight( mat , hitPos , rayDir , normal , pl1Pos , pl1Color , pl1Power ); bc += pointlight( mat , hitPos , rayDir , normal , pl2Pos , pl2Color , pl2Power ); } else { T_PBRMaterial mat = PBRMaterials[ mtidx ]; T_PBRPrecomputed pre = PBR_Precompute( mat , -rayDir , normal ); bc = sunlightColor * PBR_Shade( mat , pre , -rayDir , normal , sunlightDir ); bc += pointlight( mat , pre , hitPos , rayDir , normal , pl1Pos , pl1Color , pl1Power ); bc += pointlight( mat , pre , hitPos , rayDir , normal , pl2Pos , pl2Color , pl2Power ); } if ( glowidx >= 0 ) { bc += Glow[ glowidx ]; } bc = FOG_Apply( bc , length( u_CamPos - hitPos ) , u_FogAttenuation , background ); o_Z = r.x; } else { o_Z = u_Render.w; } o_Color = bc; }