Skip to content

Commit

Permalink
$mol_3d_sdf - signed distance field functions library
Browse files Browse the repository at this point in the history
  • Loading branch information
jin committed Dec 29, 2024
1 parent 44ddc01 commit 16487a5
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions 3d/sdf/sdf.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// min## = union
// max## = joint
// -# = inverse
// #+# = scale

float mol_3d_sdf_joint( float a, float b ) {
return max( a, b );
}
float mol_3d_sdf_joint( float a, float b, float c ) {
return max( max( a, b ), c );
}
float mol_3d_sdf_joint( float a, float b, float c, float d ) {
return max( max( a, b ), max( c, d ) );
}
float mol_3d_sdf_joint( vec3 point ) {
return mol_3d_sdf_joint( point.x, point.y, point.z );
}

float mol_3d_sdf_union( float a, float b ) {
return min( a, b );
}
float mol_3d_sdf_union( float a, float b, float c ) {
return min( min( a, b ), c );
}
float mol_3d_sdf_union( float a, float b, float c, float d ) {
return min( min( a, b ), min( c, d ) );
}
float mol_3d_sdf_union( vec3 point ) {
return mol_3d_sdf_union( point.x, point.y, point.z );
}

float mol_3d_sdf_union_smooth( float d1, float d2, float k ) {
float h = clamp( 0.5 + 0.5*(d2-d1)/k, 0.0, 1.0 );
return mix( d2, d1, h ) - k*h*(1.0-h);
}

float mol_3d_sdf_sphere( vec3 point, float size ) {
return length(point) - size;
}

float mol_3d_sdf_plane( vec3 point, vec3 normal ) {
return dot( point, normal );
}

float mol_3d_sdf_box( vec3 point, vec3 size, float round ) {
vec3 d = abs(point) - size + round;
return min( mol_3d_sdf_joint( d ), 0.0 )
+ length( max( d, 0.0 ) )
- round;
}

0 comments on commit 16487a5

Please sign in to comment.