Skip to content

Commit 04f2cb3

Browse files
committed
added trefoilKnotSdf
close #1285
1 parent b714296 commit 04f2cb3

File tree

11 files changed

+162
-0
lines changed

11 files changed

+162
-0
lines changed
Loading

src/operators/sdf/index.tox

360 Bytes
Binary file not shown.

src/operators/sdf/trefoilKnotSdf.glsl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
ReturnT thismap(CoordT p, ContextT ctx) {
2+
CoordT p0 = p;
3+
switch (THIS_Axis) {
4+
case THISTYPE_Axis_x: p = p.zyx; break;
5+
case THISTYPE_Axis_y: p = p.xzy; break;
6+
case THISTYPE_Axis_z: p = p.yxz; break;
7+
}
8+
9+
#ifdef THIS_EXPOSE_angle
10+
THIS_angle = degrees(atan(p.y, p.x)) + 180.;
11+
#endif
12+
#ifdef THIS_EXPOSE_normangle
13+
THIS_normangle = atan(p.y, p.x)/TAU + .5;
14+
#endif
15+
16+
#ifdef THIS_HAS_INPUT_radiusField
17+
float r = inputOp_radiusField(p0, ctx);
18+
#else
19+
float r = THIS_Radius;
20+
#endif
21+
#ifdef THIS_HAS_INPUT_thicknessField
22+
float th = inputOp_thicknessField(p0, ctx);
23+
#else
24+
float th = THIS_Thickness;
25+
#endif
26+
return createSdf(sdTrefoilKnot(p, r, th));
27+
}

src/operators/sdf/trefoilKnotSdf.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Trefoil knot SDF.
2+
3+
Based on [Trefoil Knot Explained](https://www.shadertoy.com/view/M3tXW4) by ruudhelderman.
4+
5+
## Parameters
6+
7+
* `Axis`
8+
* `x`
9+
* `y`
10+
* `z`
11+
* `Radius`
12+
* `Thickness`
13+
14+
## Inputs
15+
16+
* `radiusField`:
17+
* `thicknessField`:
18+
19+
## Variables
20+
21+
* `angle`:
22+
* `normangle`:

src/operators/sdf/trefoilKnotSdf.tox

30.5 KB
Binary file not shown.

src/operators/sdf/trefoilKnotSdf.yaml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
!rop
2+
meta: !meta
3+
opType: raytk.operators.sdf.trefoilKnotSdf
4+
opVersion: '0'
5+
opStatus: beta
6+
opDef: !def
7+
enable: true
8+
typeSpec: !ropTypes
9+
coordType: !coordT
10+
Coordtypevec3: true
11+
contextType: !contextT
12+
Allcontexttype: true
13+
returnType: !returnT
14+
Returntypesdf: true
15+
function: !text
16+
file: src/operators/sdf/trefoilKnotSdf.glsl
17+
name: function
18+
paramGroupTable: eval_params
19+
libraryNames: !expr
20+
expr: op('trefoilKnotSdf_lib')
21+
variableTable: !table
22+
file: src/operators/sdf/trefoilKnotSdf_variables.txt
23+
name: variable_exprs
24+
evaluate: true
25+
evalOpts: !evalOpts
26+
excludeFirstRow: true
27+
cols: enable
28+
help: !text
29+
file: src/operators/sdf/trefoilKnotSdf.md
30+
name: help
31+
inputs:
32+
- !input
33+
Localalias: radiusField
34+
Variables: angle normangle
35+
coordType: !coordT
36+
Coordtypevec3: true
37+
contextType: !contextT
38+
Allcontexttype: true
39+
returnType: !returnT
40+
Returntypefloat: true
41+
- !input
42+
Localalias: thicknessField
43+
Variables: angle normangle
44+
Variableinputs: radiusField
45+
coordType: !coordT
46+
Coordtypevec3: true
47+
contextType: !contextT
48+
Allcontexttype: true
49+
returnType: !returnT
50+
Returntypefloat: true
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// https://www.shadertoy.com/view/M3tXW4
2+
// Trefoil Knot Explained by ruudhelderman
3+
4+
// Parametric equation of the Trefoil Knot.
5+
// https://en.wikipedia.org/wiki/Trefoil_knot
6+
vec3 trf_pareq(in float t)
7+
{
8+
return vec3(
9+
sin(t) + 2.0 * sin(2.0 * t),
10+
cos(t) - 2.0 * cos(2.0 * t),
11+
-sin(3.0 * t)
12+
);
13+
}
14+
15+
// First derivative of the parametric equation.
16+
vec3 trf_pareq_derived(in float t)
17+
{
18+
return vec3(
19+
cos(t) + 4.0 * cos(2.0 * t),
20+
-sin(t) + 4.0 * sin(2.0 * t),
21+
-3.0 * cos(3.0 * t)
22+
);
23+
}
24+
25+
float trf_dist_origin_line(in vec3 p, in vec3 d)
26+
{
27+
return length(cross(p, p + normalize(d)));
28+
}
29+
30+
float trf_dist_point_curve(in vec3 p, in float s, in float t) {
31+
return trf_dist_origin_line(p - s * trf_pareq(t), trf_pareq_derived(t));
32+
}
33+
34+
float trf_rough_inverse(in vec3 p) {
35+
return 0.5 * atan(p.x, -p.y);
36+
}
37+
38+
float trf_improve_inverse(in vec3 p, in float s, in float t) {
39+
vec3 d = trf_pareq_derived(t);
40+
return -t - atan(-d.y, d.x);
41+
}
42+
43+
// SDF for Trefoil Knot.
44+
// s = scale (size of the shape)
45+
// r = radius (thickness of the wire)
46+
float sdTrefoilKnot(in vec3 p, in float s, in float r)
47+
{
48+
float t = trf_rough_inverse(p);
49+
50+
// There are 2 points on the curve that match t; calculate both.
51+
float d1 = trf_dist_point_curve(p, s, trf_improve_inverse(p, s, t));
52+
float d2 = trf_dist_point_curve(p, s, trf_improve_inverse(p, s, t + PI));
53+
54+
// Take whichever is closest to p.
55+
return min(d1, d2) - r;
56+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
names source handling readOnlyHandling conversion enable
2+
Radius param runtime macro op('radius_definition').numRows < 2
3+
Thickness param runtime macro op('thickness_definition').numRows < 2
4+
Axis param constant 1
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name label dataType enable macros category
2+
angle Angle (0-360) float 1
3+
normangle Normalized Angle (0-1) float 1
Loading

0 commit comments

Comments
 (0)