11import tgpu , { d } from 'typegpu' ;
2- import { add , dot , floor , fract , mul , sub } from 'typegpu/std' ;
2+ import { dot , floor , fract } from 'typegpu/std' ;
33import { randOnUnitCircle , randSeed2 } from '../random.ts' ;
4- import { quinticDerivative2 , quinticInterpolation2 } from '../utils.ts' ;
4+ import { quinticDerivative , quinticInterpolation } from '../utils.ts' ;
55
66export const computeJunctionGradient = tgpu . fn (
77 [ d . vec2i ] ,
88 d . vec2f ,
99) ( ( pos ) => {
10- randSeed2 ( mul ( 0.001 , d . vec2f ( pos ) ) ) ;
10+ 'use gpu' ;
11+ randSeed2 ( 0.001 * d . vec2f ( pos ) ) ;
1112 return randOnUnitCircle ( ) ;
1213} ) ;
1314
@@ -20,22 +21,23 @@ export const sample = tgpu.fn(
2021 [ d . vec2f ] ,
2122 d . f32 ,
2223) ( ( pos ) => {
24+ 'use gpu' ;
2325 // Reference: https://iquilezles.org/articles/gradientnoise/
2426
2527 const i = d . vec2i ( floor ( pos ) ) ;
2628 const f = fract ( pos ) ;
2729
28- const u = quinticInterpolation2 ( f ) ;
30+ const u = quinticInterpolation ( f ) ;
2931
3032 const ga = getJunctionGradientSlot . $ ( i ) ;
31- const gb = getJunctionGradientSlot . $ ( add ( i , d . vec2i ( 1 , 0 ) ) ) ;
32- const gc = getJunctionGradientSlot . $ ( add ( i , d . vec2i ( 0 , 1 ) ) ) ;
33- const gd = getJunctionGradientSlot . $ ( add ( i , d . vec2i ( 1 , 1 ) ) ) ;
33+ const gb = getJunctionGradientSlot . $ ( i + d . vec2i ( 1 , 0 ) ) ;
34+ const gc = getJunctionGradientSlot . $ ( i + d . vec2i ( 0 , 1 ) ) ;
35+ const gd = getJunctionGradientSlot . $ ( i + d . vec2i ( 1 , 1 ) ) ;
3436
35- const va = dot ( ga , sub ( f , d . vec2f ( 0 , 0 ) ) ) ;
36- const vb = dot ( gb , sub ( f , d . vec2f ( 1 , 0 ) ) ) ;
37- const vc = dot ( gc , sub ( f , d . vec2f ( 0 , 1 ) ) ) ;
38- const vd = dot ( gd , sub ( f , d . vec2f ( 1 , 1 ) ) ) ;
37+ const va = dot ( ga , f - d . vec2f ( 0 , 0 ) ) ;
38+ const vb = dot ( gb , f - d . vec2f ( 1 , 0 ) ) ;
39+ const vc = dot ( gc , f - d . vec2f ( 0 , 1 ) ) ;
40+ const vd = dot ( gd , f - d . vec2f ( 1 , 1 ) ) ;
3941
4042 const noise = va + u . x * ( vb - va ) + u . y * ( vc - va ) + u . x * u . y * ( va - vb - vc + vd ) ;
4143
@@ -50,37 +52,33 @@ export const sampleWithGradient = tgpu.fn(
5052 [ d . vec2f ] ,
5153 d . vec3f ,
5254) ( ( pos ) => {
55+ 'use gpu' ;
5356 // Reference: https://iquilezles.org/articles/gradientnoise/
5457
5558 const i = d . vec2i ( floor ( pos ) ) ;
5659 const f = fract ( pos ) ;
5760
58- const u = quinticInterpolation2 ( f ) ;
59- const du = quinticDerivative2 ( f ) ;
61+ const u = quinticInterpolation ( f ) ;
62+ const du = quinticDerivative ( f ) ;
6063
6164 const ga = getJunctionGradientSlot . $ ( i ) ;
62- const gb = getJunctionGradientSlot . $ ( add ( i , d . vec2i ( 1 , 0 ) ) ) ;
63- const gc = getJunctionGradientSlot . $ ( add ( i , d . vec2i ( 0 , 1 ) ) ) ;
64- const gd = getJunctionGradientSlot . $ ( add ( i , d . vec2i ( 1 , 1 ) ) ) ;
65+ const gb = getJunctionGradientSlot . $ ( i + d . vec2i ( 1 , 0 ) ) ;
66+ const gc = getJunctionGradientSlot . $ ( i + d . vec2i ( 0 , 1 ) ) ;
67+ const gd = getJunctionGradientSlot . $ ( i + d . vec2i ( 1 , 1 ) ) ;
6568
66- const va = dot ( ga , sub ( f , d . vec2f ( 0 , 0 ) ) ) ;
67- const vb = dot ( gb , sub ( f , d . vec2f ( 1 , 0 ) ) ) ;
68- const vc = dot ( gc , sub ( f , d . vec2f ( 0 , 1 ) ) ) ;
69- const vd = dot ( gd , sub ( f , d . vec2f ( 1 , 1 ) ) ) ;
69+ const va = dot ( ga , f - d . vec2f ( 0 , 0 ) ) ;
70+ const vb = dot ( gb , f - d . vec2f ( 1 , 0 ) ) ;
71+ const vc = dot ( gc , f - d . vec2f ( 0 , 1 ) ) ;
72+ const vd = dot ( gd , f - d . vec2f ( 1 , 1 ) ) ;
7073
7174 const noise = va + u . x * ( vb - va ) + u . y * ( vc - va ) + u . x * u . y * ( va - vb - vc + vd ) ;
7275
73- // ga + u.x*(gb-ga) + u.y*(gc-ga) + u.x*u.y*(ga-gb-gc+gd) + du * (u.yx*(va-vb-vc+vd) + vec2(vb,vc) - va))
74- const grad = add (
75- ga ,
76- add (
77- add (
78- add ( mul ( u . x , sub ( gb , ga ) ) , mul ( u . y , sub ( gc , ga ) ) ) ,
79- mul ( u . x , mul ( u . y , add ( sub ( sub ( ga , gb ) , gc ) , gd ) ) ) ,
80- ) ,
81- mul ( du , sub ( add ( mul ( u . yx , va - vb - vc + vd ) , d . vec2f ( vb , vc ) ) , va ) ) ,
82- ) ,
83- ) ;
76+ const grad =
77+ ga +
78+ u . x * ( gb - ga ) +
79+ u . y * ( gc - ga ) +
80+ u . x * u . y * ( ga - gb - gc + gd ) +
81+ du * ( u . yx * ( va - vb - vc + vd ) + d . vec2f ( vb , vc ) - va ) ;
8482
8583 return d . vec3f ( noise , grad ) ;
8684} ) ;
0 commit comments