demotool/shaders/scene.f.glsl

96 lines
2 KiB
Text
Raw Normal View History

#version 450 core
//! type fragment
2017-10-04 19:06:50 +02:00
//! include chunks/raymarcher.glsl
//! 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] = {
{ vec3( 1 , 1 , .4 ) * .1 , vec3( 1 , 1 , .4 ) , 40 , .1 }
};
T_PBRMaterial PBRMaterials[1] = {
{
// Albedo color
2017-10-05 11:47:33 +02:00
vec3( 1 , 1 , 1 ) ,
2017-10-04 19:06:50 +02:00
// Roughness , metallic , subsurface , anisotropy
2017-10-05 11:47:33 +02:00
.5 , .5 , 0 , 0 ,
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 ) {
glowIndex = -1;
} else {
//type = 1;
glowIndex = 0;
}
type = 1;
tIndex = 0;
}
vec2 RM_Map( vec3 pos )
{
pos = pos - vec3( 0 , 0 , 0 );
vec3 q = pos;
q.xy = mod( q.xy + 4. , 8. ) - 4.;
return vec2( length( q ) - 1.8 , step( 0. , 1.9 - length( pos.xy ) ) );
}
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;
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 int midx = int( r.y );
const vec3 normal = RM_GetNormal( hitPos );
const vec3 lightDir = normalize( -u_LightDir );
// Remap materials through mapMaterials
int mtype , mtidx , glowidx;
mapMaterial( midx , mtype , mtidx , glowidx );
if ( mtype == 0 ) {
bc = BP_Shade( BPMaterials[ mtidx ] ,
-rayDir , normal , lightDir );
} else {
bc = PBR_Shade( PBRMaterials[ mtidx ] ,
-rayDir , normal , lightDir );
}
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;
}