-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
$mol_3d_sdf - signed distance field functions library
- Loading branch information
jin
committed
Dec 29, 2024
1 parent
44ddc01
commit 16487a5
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |