demotool/shaders/scene.f.glsl

93 lines
1.9 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
T_BPMaterial BPMaterials[1] = {
{ vec3( 1 , 1 , .4 ) * .1 , vec3( 1 , 1 , .4 ) , 40 , .1 }
};
T_PBRMaterial PBRMaterials[1] = {
{
// Albedo color
vec3( 1 , 1 , .4 ) ,
// Roughness , metallic , subsurface , anisotropy
.4 , .7 , 1 , .1 ,
// Specular strength / tint%
.5 , .5
}
};
vec3 Glow[1] = {
vec3( 2 , .4 , 5 ) * 4
};
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;
vec3 bc = vec3( 0 );
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 ];
}
}
o_Color = bc;
o_Z = r.x;
}