glre 0.38.0 → 0.40.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (295) hide show
  1. package/README.md +8 -4
  2. package/dist/addons.cjs +2 -0
  3. package/dist/addons.cjs.map +1 -0
  4. package/dist/addons.d.ts +1287 -0
  5. package/dist/addons.js +2 -0
  6. package/dist/addons.js.map +1 -0
  7. package/dist/index.cjs +31 -43
  8. package/dist/index.cjs.map +1 -1
  9. package/dist/index.d.ts +187 -323
  10. package/dist/index.js +31 -43
  11. package/dist/index.js.map +1 -1
  12. package/dist/native.cjs +1 -48
  13. package/dist/native.cjs.map +1 -1
  14. package/dist/native.d.ts +536 -10
  15. package/dist/native.js +1 -48
  16. package/dist/native.js.map +1 -1
  17. package/dist/node.cjs +69 -0
  18. package/dist/node.cjs.map +1 -0
  19. package/dist/node.d.ts +626 -0
  20. package/dist/node.js +69 -0
  21. package/dist/node.js.map +1 -0
  22. package/dist/react.cjs +1 -48
  23. package/dist/react.cjs.map +1 -1
  24. package/dist/react.d.ts +524 -4
  25. package/dist/react.js +1 -48
  26. package/dist/react.js.map +1 -1
  27. package/dist/solid.cjs +1 -48
  28. package/dist/solid.cjs.map +1 -1
  29. package/dist/solid.d.ts +524 -4
  30. package/dist/solid.js +1 -48
  31. package/dist/solid.js.map +1 -1
  32. package/package.json +64 -11
  33. package/src/addons/animation/easing/backIn.ts +10 -0
  34. package/src/addons/animation/easing/backInOut.ts +12 -0
  35. package/src/addons/animation/easing/backOut.ts +10 -0
  36. package/src/addons/animation/easing/bounceIn.ts +10 -0
  37. package/src/addons/animation/easing/bounceInOut.ts +13 -0
  38. package/src/addons/animation/easing/bounceOut.ts +30 -0
  39. package/src/addons/animation/easing/circularIn.ts +9 -0
  40. package/src/addons/animation/easing/circularInOut.ts +11 -0
  41. package/src/addons/animation/easing/circularOut.ts +9 -0
  42. package/src/addons/animation/easing/cubicIn.ts +9 -0
  43. package/src/addons/animation/easing/cubicInOut.ts +11 -0
  44. package/src/addons/animation/easing/cubicOut.ts +10 -0
  45. package/src/addons/animation/easing/elasticIn.ts +10 -0
  46. package/src/addons/animation/easing/elasticInOut.ts +21 -0
  47. package/src/addons/animation/easing/elasticOut.ts +12 -0
  48. package/src/addons/animation/easing/exponentialIn.ts +9 -0
  49. package/src/addons/animation/easing/exponentialInOut.ts +13 -0
  50. package/src/addons/animation/easing/exponentialOut.ts +9 -0
  51. package/src/addons/animation/easing/index.ts +33 -0
  52. package/src/addons/animation/easing/linearIn.ts +9 -0
  53. package/src/addons/animation/easing/linearInOut.ts +9 -0
  54. package/src/addons/animation/easing/linearOut.ts +9 -0
  55. package/src/addons/animation/easing/quadraticIn.ts +9 -0
  56. package/src/addons/animation/easing/quadraticInOut.ts +10 -0
  57. package/src/addons/animation/easing/quadraticOut.ts +9 -0
  58. package/src/addons/animation/easing/quarticIn.ts +9 -0
  59. package/src/addons/animation/easing/quarticInOut.ts +11 -0
  60. package/src/addons/animation/easing/quarticOut.ts +10 -0
  61. package/src/addons/animation/easing/quinticIn.ts +9 -0
  62. package/src/addons/animation/easing/quinticInOut.ts +11 -0
  63. package/src/addons/animation/easing/quinticOut.ts +9 -0
  64. package/src/addons/animation/easing/sineIn.ts +10 -0
  65. package/src/addons/animation/easing/sineInOut.ts +10 -0
  66. package/src/addons/animation/easing/sineOut.ts +10 -0
  67. package/src/addons/color/palette/macbeth.ts +42 -0
  68. package/src/addons/color/space/cmyk2rgb.ts +12 -0
  69. package/src/addons/color/space/gamma2linear.ts +19 -0
  70. package/src/addons/color/space/hsl2rgb.ts +20 -0
  71. package/src/addons/color/space/hsv2rgb.ts +18 -0
  72. package/src/addons/color/space/hue2rgb.ts +12 -0
  73. package/src/addons/color/space/index.ts +29 -0
  74. package/src/addons/color/space/lab2lch.ts +22 -0
  75. package/src/addons/color/space/lab2rgb.ts +19 -0
  76. package/src/addons/color/space/lab2xyz.ts +32 -0
  77. package/src/addons/color/space/lch2lab.ts +21 -0
  78. package/src/addons/color/space/lch2rgb.ts +22 -0
  79. package/src/addons/color/space/linear2gamma.ts +19 -0
  80. package/src/addons/color/space/oklab2rgb.ts +35 -0
  81. package/src/addons/color/space/rgb2cmyk.ts +13 -0
  82. package/src/addons/color/space/rgb2hcv.ts +29 -0
  83. package/src/addons/color/space/rgb2hsl.ts +23 -0
  84. package/src/addons/color/space/rgb2hsv.ts +36 -0
  85. package/src/addons/color/space/rgb2hue.ts +29 -0
  86. package/src/addons/color/space/rgb2lab.ts +19 -0
  87. package/src/addons/color/space/rgb2lch.ts +22 -0
  88. package/src/addons/color/space/rgb2oklab.ts +45 -0
  89. package/src/addons/color/space/rgb2srgb.ts +34 -0
  90. package/src/addons/color/space/rgb2xyz.ts +20 -0
  91. package/src/addons/color/space/rgb2yiq.ts +22 -0
  92. package/src/addons/color/space/rgb2yuv.ts +32 -0
  93. package/src/addons/color/space/srgb2rgb.ts +34 -0
  94. package/src/addons/color/space/xyz2lab.ts +27 -0
  95. package/src/addons/color/space/xyz2rgb.ts +30 -0
  96. package/src/addons/color/space/yiq2rgb.ts +22 -0
  97. package/src/addons/color/space/yuv2rgb.ts +32 -0
  98. package/src/addons/draw/arrows.ts +75 -0
  99. package/src/addons/draw/axis.ts +57 -0
  100. package/src/addons/draw/bridge.ts +81 -0
  101. package/src/addons/draw/char.ts +30 -0
  102. package/src/addons/draw/circle.ts +29 -0
  103. package/src/addons/draw/fill.ts +25 -0
  104. package/src/addons/draw/flip.ts +45 -0
  105. package/src/addons/draw/hex.ts +29 -0
  106. package/src/addons/draw/index.ts +13 -0
  107. package/src/addons/draw/line.ts +16 -0
  108. package/src/addons/draw/point.ts +30 -0
  109. package/src/addons/draw/rect.ts +52 -0
  110. package/src/addons/draw/stroke.ts +31 -0
  111. package/src/addons/draw/tri.ts +29 -0
  112. package/src/addons/generative/cnoise.ts +239 -0
  113. package/src/addons/generative/curl.ts +64 -0
  114. package/src/addons/generative/fbm.ts +69 -0
  115. package/src/addons/generative/gerstnerWave.ts +21 -0
  116. package/src/addons/generative/gnoise.ts +113 -0
  117. package/src/addons/generative/index.ts +15 -0
  118. package/src/addons/generative/noised.ts +139 -0
  119. package/src/addons/generative/pnoise.ts +249 -0
  120. package/src/addons/generative/psrdnoise.ts +277 -0
  121. package/src/addons/generative/random.ts +136 -0
  122. package/src/addons/generative/snoise.ts +199 -0
  123. package/src/addons/generative/srandom.ts +90 -0
  124. package/src/addons/generative/voronoi.ts +134 -0
  125. package/src/addons/generative/voronoise.ts +69 -0
  126. package/src/addons/generative/wavelet.ts +77 -0
  127. package/src/addons/generative/worley.ts +99 -0
  128. package/src/addons/geometry/aabb/aabb.ts +8 -0
  129. package/src/addons/geometry/aabb/centroid.ts +10 -0
  130. package/src/addons/geometry/aabb/contain.ts +19 -0
  131. package/src/addons/geometry/aabb/diagonal.ts +10 -0
  132. package/src/addons/geometry/aabb/expand.ts +16 -0
  133. package/src/addons/geometry/aabb/index.ts +7 -0
  134. package/src/addons/geometry/aabb/intersect.ts +20 -0
  135. package/src/addons/geometry/aabb/square.ts +17 -0
  136. package/src/addons/geometry/index.ts +2 -0
  137. package/src/addons/geometry/triangle/area.ts +10 -0
  138. package/src/addons/geometry/triangle/barycentric.ts +50 -0
  139. package/src/addons/geometry/triangle/centroid.ts +10 -0
  140. package/src/addons/geometry/triangle/closestPoint.ts +85 -0
  141. package/src/addons/geometry/triangle/contain.ts +19 -0
  142. package/src/addons/geometry/triangle/distanceSq.ts +38 -0
  143. package/src/addons/geometry/triangle/index.ts +10 -0
  144. package/src/addons/geometry/triangle/intersect.ts +49 -0
  145. package/src/addons/geometry/triangle/normal.ts +12 -0
  146. package/src/addons/geometry/triangle/signedDistance.ts +31 -0
  147. package/src/addons/geometry/triangle/triangle.ts +9 -0
  148. package/src/addons/index.ts +8 -0
  149. package/src/addons/lighting/ray.ts +8 -0
  150. package/src/addons/math/aafloor.ts +13 -0
  151. package/src/addons/math/aafract.ts +38 -0
  152. package/src/addons/math/aamirror.ts +12 -0
  153. package/src/addons/math/aastep.ts +14 -0
  154. package/src/addons/math/absi.ts +9 -0
  155. package/src/addons/math/adaptiveThreshold.ts +24 -0
  156. package/src/addons/math/bump.ts +20 -0
  157. package/src/addons/math/const.ts +19 -0
  158. package/src/addons/math/cubic.ts +101 -0
  159. package/src/addons/math/cubicMix.ts +49 -0
  160. package/src/addons/math/decimate.ts +12 -0
  161. package/src/addons/math/dist.ts +143 -0
  162. package/src/addons/math/fcos.ts +11 -0
  163. package/src/addons/math/frac.ts +9 -0
  164. package/src/addons/math/gain.ts +14 -0
  165. package/src/addons/math/gaussian.ts +14 -0
  166. package/src/addons/math/grad4.ts +19 -0
  167. package/src/addons/math/hammersley.ts +54 -0
  168. package/src/addons/math/highPass.ts +12 -0
  169. package/src/addons/math/index.ts +63 -0
  170. package/src/addons/math/inside.ts +68 -0
  171. package/src/addons/math/invCubic.ts +9 -0
  172. package/src/addons/math/invQuartic.ts +9 -0
  173. package/src/addons/math/inverse.ts +9 -0
  174. package/src/addons/math/lengthSq.ts +10 -0
  175. package/src/addons/math/map.ts +27 -0
  176. package/src/addons/math/mirror.ts +12 -0
  177. package/src/addons/math/mmax.ts +27 -0
  178. package/src/addons/math/mmin.ts +28 -0
  179. package/src/addons/math/mmix.ts +47 -0
  180. package/src/addons/math/mod2.ts +44 -0
  181. package/src/addons/math/mod289.ts +46 -0
  182. package/src/addons/math/modi.ts +15 -0
  183. package/src/addons/math/nyquist.ts +15 -0
  184. package/src/addons/math/pack.ts +15 -0
  185. package/src/addons/math/parabola.ts +12 -0
  186. package/src/addons/math/permute.ts +42 -0
  187. package/src/addons/math/pow2.ts +10 -0
  188. package/src/addons/math/pow3.ts +9 -0
  189. package/src/addons/math/pow5.ts +10 -0
  190. package/src/addons/math/pow7.ts +12 -0
  191. package/src/addons/math/powFast.ts +18 -0
  192. package/src/addons/math/quartic.ts +15 -0
  193. package/src/addons/math/quat/index.ts +14 -0
  194. package/src/addons/math/quat/quat2mat3.ts +28 -0
  195. package/src/addons/math/quat/quat2mat4.ts +16 -0
  196. package/src/addons/math/quat/quatAdd.ts +18 -0
  197. package/src/addons/math/quat/quatConj.ts +14 -0
  198. package/src/addons/math/quat/quatDiv.ts +18 -0
  199. package/src/addons/math/quat/quatIdentity.ts +9 -0
  200. package/src/addons/math/quat/quatInverse.ts +17 -0
  201. package/src/addons/math/quat/quatLength.ts +15 -0
  202. package/src/addons/math/quat/quatLengthSq.ts +14 -0
  203. package/src/addons/math/quat/quatLerp.ts +40 -0
  204. package/src/addons/math/quat/quatMul.ts +38 -0
  205. package/src/addons/math/quat/quatNeg.ts +14 -0
  206. package/src/addons/math/quat/quatNorm.ts +16 -0
  207. package/src/addons/math/quat/quatSub.ts +18 -0
  208. package/src/addons/math/quintic.ts +16 -0
  209. package/src/addons/math/rotate2d.ts +16 -0
  210. package/src/addons/math/rotate3d.ts +37 -0
  211. package/src/addons/math/rotate3dX.ts +20 -0
  212. package/src/addons/math/rotate3dY.ts +20 -0
  213. package/src/addons/math/rotate3dZ.ts +20 -0
  214. package/src/addons/math/rotate4d.ts +41 -0
  215. package/src/addons/math/rotate4dX.ts +21 -0
  216. package/src/addons/math/rotate4dY.ts +16 -0
  217. package/src/addons/math/rotate4dZ.ts +21 -0
  218. package/src/addons/math/saturateMediump.ts +11 -0
  219. package/src/addons/math/scale2d.ts +44 -0
  220. package/src/addons/math/scale3d.ts +17 -0
  221. package/src/addons/math/scale4d.ts +50 -0
  222. package/src/addons/math/smootherstep.ts +16 -0
  223. package/src/addons/math/taylorInvSqrt.ts +9 -0
  224. package/src/addons/math/toMat3.ts +14 -0
  225. package/src/addons/math/toMat4.ts +14 -0
  226. package/src/addons/math/translate4d.ts +31 -0
  227. package/src/addons/math/unpack.ts +88 -0
  228. package/src/addons/sdf/boxSDF.ts +24 -0
  229. package/src/addons/sdf/circleSDF.ts +20 -0
  230. package/src/addons/sdf/crossSDF.ts +17 -0
  231. package/src/addons/sdf/hexSDF.ts +18 -0
  232. package/src/addons/sdf/index.ts +7 -0
  233. package/src/addons/sdf/lineSDF.ts +33 -0
  234. package/src/addons/sdf/rectSDF.ts +46 -0
  235. package/src/addons/sdf/sphereSDF.ts +20 -0
  236. package/src/addons/sdf/triSDF.ts +14 -0
  237. package/src/addons/space/aspect.ts +14 -0
  238. package/src/addons/space/bracketing.ts +44 -0
  239. package/src/addons/space/brickTile.ts +44 -0
  240. package/src/addons/space/cart2polar.ts +20 -0
  241. package/src/addons/space/center.ts +32 -0
  242. package/src/addons/space/checkerTile.ts +41 -0
  243. package/src/addons/space/depth2viewZ.ts +43 -0
  244. package/src/addons/space/displace.ts +55 -0
  245. package/src/addons/space/equirect2xyz.ts +17 -0
  246. package/src/addons/space/eulerView.ts +19 -0
  247. package/src/addons/space/fisheye2xyz.ts +18 -0
  248. package/src/addons/space/flipY.ts +25 -0
  249. package/src/addons/space/hexTile.ts +18 -0
  250. package/src/addons/space/index.ts +38 -0
  251. package/src/addons/space/kaleidoscope.ts +48 -0
  252. package/src/addons/space/linearizeDepth.ts +17 -0
  253. package/src/addons/space/lookAt.ts +49 -0
  254. package/src/addons/space/lookAtView.ts +40 -0
  255. package/src/addons/space/mirrorTile.ts +73 -0
  256. package/src/addons/space/nearest.ts +13 -0
  257. package/src/addons/space/orthographic.ts +25 -0
  258. package/src/addons/space/parallaxMapping.ts +149 -0
  259. package/src/addons/space/perspective.ts +24 -0
  260. package/src/addons/space/polar2cart.ts +24 -0
  261. package/src/addons/space/ratio.ts +14 -0
  262. package/src/addons/space/rotate.ts +37 -0
  263. package/src/addons/space/rotateX.ts +54 -0
  264. package/src/addons/space/rotateY.ts +54 -0
  265. package/src/addons/space/rotateZ.ts +54 -0
  266. package/src/addons/space/scale.ts +13 -0
  267. package/src/addons/space/sprite.ts +16 -0
  268. package/src/addons/space/sqTile.ts +20 -0
  269. package/src/addons/space/tbn.ts +26 -0
  270. package/src/addons/space/translate.ts +12 -0
  271. package/src/addons/space/triTile.ts +32 -0
  272. package/src/addons/space/uncenter.ts +32 -0
  273. package/src/addons/space/unratio.ts +12 -0
  274. package/src/addons/space/viewZ2depth.ts +25 -0
  275. package/src/addons/space/windmillTile.ts +58 -0
  276. package/src/addons/space/xyz2equirect.ts +10 -0
  277. package/src/index.ts +10 -23
  278. package/src/node/build.ts +127 -0
  279. package/src/node/create.ts +76 -0
  280. package/src/node/index.ts +64 -49
  281. package/src/node/scope.ts +66 -62
  282. package/src/node/types.ts +221 -187
  283. package/src/node/utils/const.ts +74 -13
  284. package/src/node/utils/index.ts +19 -14
  285. package/src/node/utils/infer.ts +32 -34
  286. package/src/node/utils/parse.ts +48 -24
  287. package/src/node/utils/utils.ts +52 -29
  288. package/src/types.ts +11 -6
  289. package/src/utils/helpers.ts +56 -0
  290. package/src/utils/pipeline.ts +24 -8
  291. package/src/utils/program.ts +62 -38
  292. package/src/{webgl.ts → utils/webgl.ts} +46 -30
  293. package/src/{webgpu.ts → utils/webgpu.ts} +22 -17
  294. package/src/node/core.ts +0 -121
  295. package/src/node/node.ts +0 -65
package/src/node/index.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import { hex2rgb } from './utils'
2
- import { builtin as b, conversion as c, function_ as f, uniform as u } from './node'
2
+ import { builtin as b, conversion as c, function_ as f, uniform as u } from './create'
3
3
  import { is } from '../utils/helpers'
4
- import type { Constants as C, X, NodeProxy } from './types'
5
- export * from './core'
6
- export * from './node'
4
+ import type { Constants as C, Float, X, Y } from './types'
5
+ export * from './build'
6
+ export * from './create'
7
7
  export * from './scope'
8
8
  export * from './types'
9
9
 
@@ -16,7 +16,7 @@ export const fragDepth = b<'float'>('frag_depth')
16
16
  export const sampleIndex = b<'uint'>('sample_index')
17
17
  export const sampleMask = b<'uint'>('sample_mask')
18
18
  export const pointCoord = b<'vec2'>('point_coord')
19
- export const id = b<'vec3'>('global_invocation_id')
19
+ export const id = b<'uvec3'>('global_invocation_id')
20
20
 
21
21
  // TSL Compatible Builtin Variables
22
22
  export const positionLocal = b<'vec3'>('position')
@@ -29,28 +29,28 @@ export const screenCoordinate = b<'vec2'>('screenCoordinate')
29
29
  export const screenUV = b<'vec2'>('screenUV')
30
30
 
31
31
  // Type constructors with proper type inference
32
- export const float = (x?: X) => c('float', x)
33
- export const int = (x?: X) => c('int', x)
34
- export const uint = (x?: X) => c('uint', x)
35
- export const bool = (x?: X) => c('bool', x)
36
- export const vec2 = (x?: X, y?: X) => c('vec2', x, y)
37
- export const vec3 = (x?: X, y?: X, z?: X) => c('vec3', x, y, z)
38
- export const vec4 = (x?: X, y?: X, z?: X, w?: X) => c('vec4', x, y, z, w)
39
- export const mat2 = (...args: X[]) => c('mat2', ...args)
40
- export const mat3 = (...args: X[]) => c('mat3', ...args)
41
- export const mat4 = (...args: X[]) => c('mat4', ...args)
42
- export const ivec2 = (x?: X, y?: X) => c('ivec2', x, y)
43
- export const ivec3 = (x?: X, y?: X, z?: X) => c('ivec3', x, y, z)
44
- export const ivec4 = (x?: X, y?: X, z?: X, w?: X) => c('ivec4', x, y, z, w)
45
- export const uvec2 = (x?: X, y?: X) => c('uvec2', x, y)
46
- export const uvec3 = (x?: X, y?: X, z?: X) => c('uvec3', x, y, z)
47
- export const uvec4 = (x?: X, y?: X, z?: X, w?: X) => c('uvec4', x, y, z, w)
48
- export const bvec2 = (x?: X, y?: X) => c('bvec2', x, y)
49
- export const bvec3 = (x?: X, y?: X, z?: X) => c('bvec3', x, y, z)
50
- export const bvec4 = (x?: X, y?: X, z?: X, w?: X) => c('bvec4', x, y, z, w)
51
- export const texture2D = (x?: X) => c('texture', x)
32
+ export const float = (x?: Y) => c('float', x)
33
+ export const int = (x?: Y) => c('int', x)
34
+ export const uint = (x?: Y) => c('uint', x)
35
+ export const bool = (x?: Y) => c('bool', x)
36
+ export const vec2 = (x?: Y, y?: Y) => c('vec2', x, y)
37
+ export const vec3 = (x?: Y, y?: Y, z?: Y) => c('vec3', x, y, z)
38
+ export const vec4 = (x?: Y, y?: Y, z?: Y, w?: Y) => c('vec4', x, y, z, w)
39
+ export const mat2 = (...args: Y[]) => c('mat2', ...args)
40
+ export const mat3 = (...args: Y[]) => c('mat3', ...args)
41
+ export const mat4 = (...args: Y[]) => c('mat4', ...args)
42
+ export const ivec2 = (x?: Y, y?: Y) => c('ivec2', x, y)
43
+ export const ivec3 = (x?: Y, y?: Y, z?: Y) => c('ivec3', x, y, z)
44
+ export const ivec4 = (x?: Y, y?: Y, z?: Y, w?: Y) => c('ivec4', x, y, z, w)
45
+ export const uvec2 = (x?: Y, y?: Y) => c('uvec2', x, y)
46
+ export const uvec3 = (x?: Y, y?: Y, z?: Y) => c('uvec3', x, y, z)
47
+ export const uvec4 = (x?: Y, y?: Y, z?: Y, w?: Y) => c('uvec4', x, y, z, w)
48
+ export const bvec2 = (x?: Y, y?: Y) => c('bvec2', x, y)
49
+ export const bvec3 = (x?: Y, y?: Y, z?: Y) => c('bvec3', x, y, z)
50
+ export const bvec4 = (x?: Y, y?: Y, z?: Y, w?: Y) => c('bvec4', x, y, z, w)
51
+ export const texture2D = (x?: Y) => c('texture', x)
52
52
  export const sampler2D = () => c('sampler2D')
53
- export const color = (r?: X, g?: X, b?: X) => {
53
+ export const color = (r?: Y, g?: Y, b?: Y) => {
54
54
  if (is.num(r) && is.und(g) && is.und(b)) return vec3(...hex2rgb(r))
55
55
  return vec3(r, g, b)
56
56
  }
@@ -64,20 +64,26 @@ export const uv = position.xy.div(iResolution)
64
64
  /**
65
65
  * 1.1. unified with:
66
66
  * 2.1. const.ts BUILTIN_VARIABLES and
67
- * 3.1. types.ts BaseNodeProxy
67
+ * 3.1. types.ts _N
68
68
  */
69
69
  // 0. Always return bool
70
70
  export const all = <T extends C>(x: X<T>) => f<'bool'>('all', x)
71
71
  export const any = <T extends C>(x: X<T>) => f<'bool'>('any', x)
72
72
 
73
- // 2. Always return float
74
- export const length = (x: X) => f<'float'>('length', x)
73
+ // 2. Always return float with WGSL-compliant type constraints and unified parameter types
74
+ export const determinant = <T extends 'mat2' | 'mat3' | 'mat4'>(x: X<T>) => f<'float'>('determinant', x)
75
+ export const distance = <T extends 'vec2' | 'vec3' | 'vec4', U extends C>(x: X<T>, y: number | X<U>) =>
76
+ f<'float'>('distance', x, y)
77
+ export const dot = <T extends 'vec2' | 'vec3' | 'vec4' | 'ivec2' | 'ivec3' | 'ivec4', U extends C>(
78
+ x: X<T>,
79
+ y: number | X<U>
80
+ ) => f<T extends `ivec${string}` ? 'int' : 'float'>('dot', x, y)
81
+ export const length = <T extends 'vec2' | 'vec3' | 'vec4'>(x: X<T>) => f<'float'>('length', x)
75
82
  export const lengthSq = (x: X) => f<'float'>('lengthSq', x)
76
- export const distance = (x: X, y: X) => f<'float'>('distance', x, y)
77
- export const dot = (x: X, y: X) => f<'float'>('dot', x, y)
83
+ export const luminance = (x: X) => f<'float'>('luminance', x)
78
84
 
79
- // 3. Always return vec3
80
- export const cross = (x: X<'vec3'>, y: X<'vec3'>) => f<'vec3'>('cross', x, y)
85
+ // 3. Always return vec3 with vec3 constraint and unified parameter types
86
+ export const cross = <U extends C = 'vec3'>(x: X<'vec3'>, y: number | X<U>) => f<'vec3'>('cross', x, y)
81
87
 
82
88
  // 4. Always return vec4
83
89
  export const cubeTexture = (x: X, y: X, z?: X) => f<'vec4'>('cubeTexture', x, y, z)
@@ -88,7 +94,7 @@ export const textureLod = (x: X, y: X, z?: X) => f<'vec4'>('textureLod', x, y, z
88
94
  /**
89
95
  * 1.2. unified with:
90
96
  * 2.2. const.ts FUNCTIONS and
91
- * 3.2. types.ts BaseNodeProxy
97
+ * 3.2. types.ts _N
92
98
  */
93
99
  // 0. Component-wise functions
94
100
  export const abs = <T extends C>(x: X<T>) => f<T>('abs', x)
@@ -113,12 +119,11 @@ export const inverseSqrt = <T extends C>(x: X<T>) => f<T>('inverseSqrt', x)
113
119
  export const log = <T extends C>(x: X<T>) => f<T>('log', x)
114
120
  export const log2 = <T extends C>(x: X<T>) => f<T>('log2', x)
115
121
  export const negate = <T extends C>(x: X<T>) => f<T>('negate', x)
116
- export const normalize = <T extends C>(x: X<T>) => f<T>('normalize', x)
122
+ export const normalize = <T extends 'vec2' | 'vec3' | 'vec4'>(x: X<T>) => f<T>('normalize', x)
117
123
  export const oneMinus = <T extends C>(x: X<T>) => f<T>('oneMinus', x)
118
124
  export const radians = <T extends C>(x: X<T>) => f<T>('radians', x)
119
125
  export const reciprocal = <T extends C>(x: X<T>) => f<T>('reciprocal', x)
120
126
  export const round = <T extends C>(x: X<T>) => f<T>('round', x)
121
- export const saturate = <T extends C>(x: X<T>) => f<T>('saturate', x)
122
127
  export const sign = <T extends C>(x: X<T>) => f<T>('sign', x)
123
128
  export const sin = <T extends C>(x: X<T>) => f<T>('sin', x)
124
129
  export const sinh = <T extends C>(x: X<T>) => f<T>('sinh', x)
@@ -127,17 +132,27 @@ export const tan = <T extends C>(x: X<T>) => f<T>('tan', x)
127
132
  export const tanh = <T extends C>(x: X<T>) => f<T>('tanh', x)
128
133
  export const trunc = <T extends C>(x: X<T>) => f<T>('trunc', x)
129
134
 
130
- // 1. Functions where first argument determines return type
131
- export const atan2 = <T extends C>(x: X<T>, y: X) => f<T>('atan2', x, y)
132
- export const clamp = <T extends C>(x: X<T>, y: X, z: X) => f<T>('clamp', x, y, z)
133
- export const max = <T extends C>(x: X<T>, y: X) => f<T>('max', x, y)
134
- export const min = <T extends C>(x: X<T>, y: X) => f<T>('min', x, y)
135
- export const mix = <T extends C>(x: X<T>, y: X, a: X) => f<T>('mix', x, y, a)
136
- export const pow = <T extends C>(x: X<T>, y: X) => f<T>('pow', x, y)
137
- export const reflect = <T extends C>(I: X<T>, N: X) => f<T>('reflect', I, N)
138
- export const refract = <T extends C>(I: X<T>, N: X, eta: X) => f<T>('refract', I, N, eta)
135
+ // 1. Functions where first argument determines return type with unified parameter types
136
+ export const atan2 = <T extends C, U extends C>(x: X<T>, y: number | X<U>) => f<T>('atan2', x, y)
137
+ export const clamp = <T extends C, U extends C>(x: X<T>, min: number | X<U>, max: number | X<U>) =>
138
+ f<T>('clamp', x, min, max)
139
+ export const max = <T extends C, U extends C>(x: X<T>, y: number | X<U>) => f<T>('max', x, y)
140
+ export const min = <T extends C, U extends C>(x: X<T>, y: number | X<U>) => f<T>('min', x, y)
141
+ export const mix = <T extends C, U extends C>(x: X<T>, y: number | X<U>, a: number | Float | X<U>) =>
142
+ f<T>('mix', x, y, a)
143
+ export const pow = <T extends C, U extends C>(x: X<T>, y: number | X<U>) => f<T>('pow', x, y)
144
+ export const reflect = <T extends 'vec2' | 'vec3' | 'vec4', U extends C>(I: X<T>, N: number | X<U>) =>
145
+ f<T>('reflect', I, N)
146
+ export const refract = <T extends 'vec2' | 'vec3' | 'vec4', U extends C>(
147
+ I: X<T>,
148
+ N: number | X<U>,
149
+ eta: number | Float
150
+ ) => f<T>('refract', I, N, eta)
139
151
 
140
- // 2. Functions where not first argument determines return type
141
- export const smoothstep = <T extends C>(e0: X, e1: X, x: X<T>) => f<T>('smoothstep', e0, e1, x)
142
- export const step = <T extends C>(edge: X, x: X<T>) => f<T>('step', edge, x)
143
- export const mod = <T extends C>(x: NodeProxy<T>, y: X<T>) => x.sub(x.div(y).toFloat().floor().mul(y))
152
+ // 2. Functions where not first argument determines return type with unified parameter types
153
+ export const smoothstep = <T extends C, U extends C>(e0: number | X<U>, e1: number | X<U>, x: X<T>) =>
154
+ f<T>('smoothstep', e0, e1, x)
155
+ export const step = <T extends C, U extends C>(edge: number | X<U>, x: X<T>) => f<T>('step', edge, x)
156
+ export const mod = <T extends C, U extends C>(x: X<T>, y: number | X<U>) => {
157
+ return (x as any).sub((x as any).div(y).floor().mul(y)) as X<T>
158
+ }
package/src/node/scope.ts CHANGED
@@ -1,62 +1,69 @@
1
1
  import { getId } from './utils'
2
- import { conversion, node } from './node'
2
+ import { conversion, create } from './create'
3
3
  import type {
4
4
  FnLayout,
5
5
  FnType,
6
- NodeProps,
7
- NodeProxy,
8
- X,
9
- Constants,
6
+ Constants as C,
10
7
  Int,
8
+ NodeProps,
9
+ Struct,
11
10
  StructFactory,
12
- StructNode,
13
11
  StructFields,
12
+ X,
13
+ Y,
14
14
  } from './types'
15
15
 
16
- let scope: NodeProxy | null = null
17
- let define: NodeProxy | null = null
16
+ let scope: X | null = null
17
+ let define: X | null = null
18
18
 
19
- const addToScope = (x: NodeProxy) => {
19
+ export const addToScope = <T extends C>(x: X<T>) => {
20
20
  if (!scope) return
21
21
  if (!scope.props.children) scope.props.children = []
22
22
  scope.props.children.push(x)
23
- if (x.type !== 'return' || !define) return
23
+ if (x.type !== 'return' || !define) return x
24
24
  const { props } = define
25
25
  if (!props.inferFrom) props.inferFrom = []
26
26
  props.inferFrom.push(x)
27
+ return x
27
28
  }
28
29
 
29
- export function toVar<T extends StructFields>(x: StructNode<T>, id?: string): StructNode<T>
30
- export function toVar<T extends Constants>(x: NodeProxy<T>, id?: string): NodeProxy<T> {
30
+ export function toVar<T extends StructFields>(x: Struct<T>, id?: string): Struct<T>
31
+ export function toVar<T extends C>(x: X<T>, id?: string): X<T> {
31
32
  if (!id) id = getId()
32
- const y = node<T>('variable', { id, inferFrom: [x] })
33
- const z = node<T>('declare', null, x as NodeProxy, y)
33
+ const y = create<T>('variable', { id, inferFrom: [x] })
34
+ const z = create<T>('declare', null, x as X, y)
34
35
  addToScope(z)
35
36
  return y
36
37
  }
37
38
 
38
- export const assign = <T extends Constants>(x: NodeProxy<T>, isScatter = false, y: X<T>): X<T> => {
39
- const z = node(isScatter ? 'scatter' : 'assign', null, x, y)
39
+ export const assign = <T extends C>(x: X<T>, isScatter = false, y: Y<T>): X<T> => {
40
+ const z = create(isScatter ? 'scatter' : 'assign', null, x, y)
40
41
  addToScope(z)
41
42
  return x
42
43
  }
43
44
 
44
- export const Return = <T extends Constants>(x: X<T>): NodeProxy<T> => {
45
- const y = node<T>('return', { inferFrom: [x] }, x)
46
- addToScope(y)
47
- return y
45
+ export const Return = <T extends C>(x?: Y<T>): Y<T> => {
46
+ return addToScope(create<T>('return', { inferFrom: [x] }, x))
47
+ }
48
+
49
+ export const Break = (): Y => {
50
+ return addToScope(create('break'))
51
+ }
52
+
53
+ export const Continue = (): Y => {
54
+ return addToScope(create('continue'))
48
55
  }
49
56
 
50
57
  export const struct = <T extends StructFields>(fields: T, id = getId()): StructFactory<T> => {
51
58
  return (initialValues: StructFields = {}, instanceId = getId()) => {
52
- const x = node('variable', { id: instanceId, inferFrom: [id] })
53
- const y = node('struct', { id, fields, initialValues }, x)
59
+ const x = create('variable', { id: instanceId, inferFrom: [id] })
60
+ const y = create('struct', { id, fields, initialValues }, x)
54
61
  addToScope(y)
55
- return x as StructNode<T>
62
+ return x as Struct<T>
56
63
  }
57
64
  }
58
65
 
59
- const scoped = (x: NodeProxy, fun: () => NodeProxy | void, y = define) => {
66
+ export const scoped = (x: X, fun: () => X | void, y = define) => {
60
67
  const [_scope, _define] = [scope, define]
61
68
  ;[scope, define] = [x, y]
62
69
  const z = fun()
@@ -64,81 +71,78 @@ const scoped = (x: NodeProxy, fun: () => NodeProxy | void, y = define) => {
64
71
  ;[scope, define] = [_scope, _define]
65
72
  }
66
73
 
67
- export const If = (x: NodeProxy, fun: () => void) => {
68
- const y = node('scope')
69
- scoped(y, fun)
70
- const ifNode = node('if', null, x, y)
74
+ export const Scope = <T extends X>(fun: () => T | void) => {
75
+ const scope = create('scope')
76
+ scoped(scope, fun)
77
+ return scope as T
78
+ }
79
+
80
+ export const If = (x: Y, fun: () => void) => {
81
+ const y = Scope(fun)
82
+ const ifNode = create('if', null, x, y)
71
83
  addToScope(ifNode)
72
84
  const ret = () => ({
73
85
  ElseIf: (_x: X, _fun: () => void) => {
74
- const _y = node('scope')
75
- scoped(_y, _fun)
86
+ const _y = Scope(_fun)
76
87
  ifNode.props.children!.push(_x, _y)
77
88
  return ret()
78
89
  },
79
90
  Else: (_fun: () => void) => {
80
- const _x = node('scope')
81
- scoped(_x, _fun)
91
+ const _x = Scope(_fun)
82
92
  ifNode.props.children!.push(_x)
83
93
  },
84
94
  })
85
95
  return ret()
86
96
  }
87
97
 
88
- export const Loop = (x: NodeProxy, fun: (params: { i: Int }) => void) => {
89
- const y = node('scope')
98
+ export const Loop = (x: Y, fun: (params: { i: Int }) => void) => {
90
99
  const id = getId()
91
- scoped(y, () => fun({ i: node<'int'>('variable', { id, inferFrom: [conversion('int', 0)] }) }))
92
- const ret = node('loop', { id }, x, y)
93
- addToScope(ret)
94
- return ret
100
+ const y = Scope(() => fun({ i: create<'int'>('variable', { id, inferFrom: [conversion('int', 0)] }) }))
101
+ const ret = create('loop', { id }, x, y)
102
+ return addToScope(ret)
95
103
  }
96
104
 
97
- export const Switch = (x: NodeProxy) => {
98
- const switchNode = node('switch', null, x)
105
+ export const Switch = (x: Y) => {
106
+ const switchNode = create('switch', null, x)
99
107
  addToScope(switchNode)
100
108
  const ret = () => ({
101
109
  Case: (...values: X[]) => {
102
110
  return (fun: () => void) => {
103
- const y = node('scope')
104
- scoped(y, fun)
111
+ const y = Scope(fun)
105
112
  for (const _x of values) switchNode.props.children!.push(_x, y)
106
113
  return ret()
107
114
  }
108
115
  },
109
116
  Default: (fun: () => void) => {
110
- const scope = node('scope')
111
- scoped(scope, fun)
117
+ const scope = Scope(fun)
112
118
  switchNode.props.children!.push(scope)
113
119
  },
114
120
  })
115
121
  return ret()
116
122
  }
117
123
 
118
- export function Fn<T extends NodeProxy | StructNode | void, Args extends any[]>(
119
- fun: (args: Args) => T,
120
- defaultId = getId()
121
- ) {
122
- let layout: FnLayout
124
+ export function Fn<T extends X | Struct | void, Args extends any[]>(fun: (args: Args) => T, layout?: FnLayout) {
123
125
  const ret = (...args: any[]) => {
124
- const id = layout?.name || defaultId
125
- const x = node('scope')
126
- const paramVars: NodeProxy[] = []
126
+ const id = layout?.name || getId()
127
+ const paramVars: X[] = []
127
128
  const paramDefs: NodeProps[] = []
128
- if (layout?.inputs) {
129
- for (const input of layout.inputs) {
130
- paramDefs.push({ id: input.name, inferFrom: [conversion(input.type)] })
131
- }
132
- } else {
133
- for (let i = 0; i < args.length; i++) {
134
- paramDefs.push({ id: `p${i}`, inferFrom: [args[i]] })
135
- }
129
+ for (let i = 0; i < args.length; i++) {
130
+ const input = layout?.inputs?.[i]
131
+ if (!input) paramDefs.push({ id: `p${i}`, inferFrom: [args[i]] })
132
+ else
133
+ paramDefs.push({
134
+ id: input.name,
135
+ inferFrom:
136
+ input.type === 'auto' ? [args[i]] : [conversion(input.type, args[i])],
137
+ })
136
138
  }
137
- for (const props of paramDefs) paramVars.push(node('variable', props))
138
- const y = node('define', { id, layout }, x, ...args)
139
+ for (const props of paramDefs) paramVars.push(create('variable', props))
140
+ const x = create('scope')
141
+ const y = create('define', { id, layout }, x, ...args)
139
142
  scoped(x, () => fun(paramVars as Args) as any, y)
140
143
  return y
141
144
  }
145
+ ret.getLayout = () => layout
142
146
  ret.setLayout = (_layout: FnLayout) => {
143
147
  layout = _layout
144
148
  return ret