@@ -21,44 +21,44 @@ import {
2121
2222/**
2323 * Signed distance function for a disk (filled circle)
24- * @param p Point to evaluate
24+ * @param point Point to evaluate
2525 * @param radius Radius of the disk
2626 */
27- export const sdDisk = tgpu . fn ( [ vec2f , f32 ] , f32 ) ( ( p , radius ) => {
28- return length ( p ) - radius ;
27+ export const sdDisk = tgpu . fn ( [ vec2f , f32 ] , f32 ) ( ( point , radius ) => {
28+ return length ( point ) - radius ;
2929} ) ;
3030
3131/**
32- * Signed distance function for a 2d box
33- * @param p Point to evaluate
32+ * Signed distance function for a 2d box (rectangle)
33+ * @param point Point to evaluate
3434 * @param size Half-dimensions of the box
3535 */
36- export const sdBox2d = tgpu . fn ( [ vec2f , vec2f ] , f32 ) ( ( p , size ) => {
37- const d = sub ( abs ( p ) , size ) ;
36+ export const sdBox2d = tgpu . fn ( [ vec2f , vec2f ] , f32 ) ( ( point , size ) => {
37+ const d = sub ( abs ( point ) , size ) ;
3838 return length ( max ( d , vec2f ( 0 ) ) ) + min ( max ( d . x , d . y ) , 0 ) ;
3939} ) ;
4040
4141/**
4242 * Signed distance function for a rounded 2d box
43- * @param p Point to evaluate
43+ * @param point Point to evaluate
4444 * @param size Half-dimensions of the box
4545 * @param cornerRadius Box corner radius
4646 */
4747export const sdRoundedBox2d = tgpu
48- . fn ( [ vec2f , vec2f , f32 ] , f32 ) ( ( p , size , cornerRadius ) => {
49- const d = add ( sub ( abs ( p ) , size ) , vec2f ( cornerRadius ) ) ;
48+ . fn ( [ vec2f , vec2f , f32 ] , f32 ) ( ( point , size , cornerRadius ) => {
49+ const d = add ( sub ( abs ( point ) , size ) , vec2f ( cornerRadius ) ) ;
5050 return length ( max ( d , vec2f ( 0 ) ) ) + min ( max ( d . x , d . y ) , 0 ) - cornerRadius ;
5151 } ) ;
5252
5353/**
5454 * Signed distance function for a line segment
55- * @param p Point to evaluate
56- * @param a First endpoint of the line
57- * @param b Second endpoint of the line
55+ * @param point Point to evaluate
56+ * @param A First endpoint of the line
57+ * @param B Second endpoint of the line
5858 */
59- export const sdLine = tgpu . fn ( [ vec2f , vec2f , vec2f ] , f32 ) ( ( p , a , b ) => {
60- const pa = sub ( p , a ) ;
61- const ba = sub ( b , a ) ;
59+ export const sdLine = tgpu . fn ( [ vec2f , vec2f , vec2f ] , f32 ) ( ( point , A , B ) => {
60+ const pa = sub ( point , A ) ;
61+ const ba = sub ( B , A ) ;
6262 const h = max ( 0 , min ( 1 , dot ( pa , ba ) / dot ( ba , ba ) ) ) ;
6363 return length ( sub ( pa , ba . mul ( h ) ) ) ;
6464} ) ;
@@ -70,17 +70,17 @@ const dot2 = (v: v2f) => {
7070
7171/**
7272 * Signed distance function for a quadratic Bezier curve
73- * @param pos Point to evaluate
73+ * @param point Point to evaluate
7474 * @param A First control point of the Bezier curve
7575 * @param B Second control point of the Bezier curve
7676 * @param C Third control point of the Bezier curve
7777 */
7878export const sdBezier = tgpu . fn ( [ vec2f , vec2f , vec2f , vec2f ] , f32 ) (
79- ( pos , A , B , C ) => {
79+ ( point , A , B , C ) => {
8080 const a = B . sub ( A ) ;
8181 const b = A . sub ( B . mul ( 2 ) ) . add ( C ) ;
8282 const c = a . mul ( f32 ( 2 ) ) ;
83- const d = A . sub ( pos ) ;
83+ const d = A . sub ( point ) ;
8484
8585 const dotB = max ( dot ( b , b ) , 0.0001 ) ;
8686 const kk = 1 / dotB ;
@@ -121,27 +121,34 @@ export const sdBezier = tgpu.fn([vec2f, vec2f, vec2f, vec2f], f32)(
121121 } ,
122122) ;
123123
124- const cro = ( a : v2f , b : v2f ) => {
124+ const cross = ( a : v2f , b : v2f ) => {
125125 'use gpu' ;
126126 return a . x * b . y - a . y * b . x ;
127127} ;
128128
129+ /**
130+ * A fast approximation of the signed distance function for a quadratic Bezier curve
131+ * @param point Point to evaluate
132+ * @param A First control point of the Bezier curve
133+ * @param B Second control point of the Bezier curve
134+ * @param C Third control point of the Bezier curve
135+ */
129136export const sdBezierApprox = tgpu . fn (
130137 [ vec2f , vec2f , vec2f , vec2f ] ,
131138 f32 ,
132- ) ( ( pos , A , B , C ) => {
139+ ) ( ( point , A , B , C ) => {
133140 const i = A . sub ( C ) ;
134141 const j = C . sub ( B ) ;
135142 const k = B . sub ( A ) ;
136143 const w = j . sub ( k ) ;
137144
138- const v0 = A . sub ( pos ) ;
139- const v1 = B . sub ( pos ) ;
140- const v2 = C . sub ( pos ) ;
145+ const v0 = A . sub ( point ) ;
146+ const v1 = B . sub ( point ) ;
147+ const v2 = C . sub ( point ) ;
141148
142- const x = cro ( v0 , v2 ) ;
143- const y = cro ( v1 , v0 ) ;
144- const z = cro ( v2 , v1 ) ;
149+ const x = cross ( v0 , v2 ) ;
150+ const y = cross ( v1 , v0 ) ;
151+ const z = cross ( v2 , v1 ) ;
145152
146153 const s = j . mul ( y ) . add ( k . mul ( z ) ) . mul ( 2 ) . sub ( i . mul ( x ) ) ;
147154
@@ -152,10 +159,17 @@ export const sdBezierApprox = tgpu.fn(
152159 return length ( d ) ;
153160} ) ;
154161
155- export const sdPie = tgpu . fn ( [ vec2f , vec2f , f32 ] , f32 ) ( ( p , c , r ) => {
156- const p_w = vec2f ( p ) ;
157- p_w . x = abs ( p . x ) ;
158- const l = length ( p_w ) - r ;
159- const m = length ( p_w . sub ( c . mul ( clamp ( dot ( p_w , c ) , 0 , r ) ) ) ) ;
160- return max ( l , m * sign ( c . y * p_w . x - c . x * p_w . y ) ) ;
162+ /**
163+ * Computes the signed distance field for a pie shape (circular sector).
164+ *
165+ * @param point - The point to evaluate, in 2D space
166+ * @param sc - The sine/cosine of the pie's half-angle (`d.vec2f(std.sin(angle/2), std.cos(angle/2))`)
167+ * @param radius - The radius of the pie
168+ */
169+ export const sdPie = tgpu . fn ( [ vec2f , vec2f , f32 ] , f32 ) ( ( point , sc , radius ) => {
170+ const p_w = vec2f ( point ) ;
171+ p_w . x = abs ( point . x ) ;
172+ const l = length ( p_w ) - radius ;
173+ const m = length ( p_w . sub ( sc . mul ( clamp ( dot ( p_w , sc ) , 0 , radius ) ) ) ) ;
174+ return max ( l , m * sign ( sc . y * p_w . x - sc . x * p_w . y ) ) ;
161175} ) ;
0 commit comments