#version 450 core //! type fragment //! include chunks/raymarcher.glsl //! include lib/shading-pbr.glsl //! include lib/shading-blinnphong.glsl //! include lib/fog.glsl T_BPMaterial BPMaterials[1] = { { vec3( .95 , .8 , 1 ) , vec3( 1 ) , 1 , 5 } }; T_PBRMaterial PBRMaterials[1] = { { // Albedo color vec3( .8 , 1 , .8 ) , // Roughness , metallic , subsurface , anisotropy .2 , .7 , .7 , .7 , // 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; } float box( vec3 p , vec3 sz ) { vec3 n = abs( p ) - sz; return max( max( n.x , n.y ) , n.z ); } vec2 RM_Map( vec3 pos ) { pos = pos - vec3( 0 , 0 , 0 ); vec3 q = pos; q.xy = mod( q.xy + 4. , 8. ) - 4.; q.xz *= mat2( cos( 1 ) , -sin(1) , sin( 1 ) , cos( 1 ) ); return vec2( mix( box( q , vec3( 1.5 ) ) , length( q ) - 1.5 , .5 ) , step( 0. , 1.9 - length( pos.xy ) ) ); } vec3 pointlight( in T_PBRMaterial 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( .01 , dot( lightRay , lightRay ) ); if ( lPwr < .05 ) { return vec3( 0 ); } return lightColor * lPwr * PBR_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 ); vec3 r = RM_Advanced( u_CamPos , rayDir , int( u_Render.x ) , u_Render.y , u_Render.z , .001 , u_Render.w ); vec3 hitPos = u_CamPos + rayDir * r.x; const vec3 background = vec3( .005 ); vec3 bc = background; if ( r.y >= 0. ) { const int midx = int( r.y ); const vec3 normal = RM_GetNormal( hitPos ); 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; // Remap materials through mapMaterials int mtype , mtidx , glowidx; mapMaterial( midx , mtype , mtidx , glowidx ); if ( mtype == 0 ) { bc = sunlightColor * BP_Shade( BPMaterials[ mtidx ] , -rayDir , normal , sunlightDir ); } else { T_PBRMaterial mat = PBRMaterials[ mtidx ]; bc = sunlightColor * PBR_Shade( mat , -rayDir , normal , sunlightDir ); bc += pointlight( mat , hitPos , rayDir , normal , pl1Pos , pl1Color , pl1Power ); bc += pointlight( mat , hitPos , rayDir , normal , pl2Pos , pl2Color , pl2Power ); } if ( glowidx >= 0 ) { bc += Glow[ glowidx ]; } bc = FOG_Apply( bc , r.x , u_FogAttenuation , background ); } o_Color = bc; o_Z = r.x; }