@asciiz/atoolkit 0.1.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 (343) hide show
  1. package/LICENSE +21 -0
  2. package/ReadMe.md +198 -0
  3. package/dist/acircuit/chip.d.ts +46 -0
  4. package/dist/acircuit/chip.d.ts.map +1 -0
  5. package/dist/acircuit/chip.js +104 -0
  6. package/dist/acircuit/chip.js.map +1 -0
  7. package/dist/acircuit/circuit.d.ts +86 -0
  8. package/dist/acircuit/circuit.d.ts.map +1 -0
  9. package/dist/acircuit/circuit.js +606 -0
  10. package/dist/acircuit/circuit.js.map +1 -0
  11. package/dist/acircuit/composite.d.ts +32 -0
  12. package/dist/acircuit/composite.d.ts.map +1 -0
  13. package/dist/acircuit/composite.js +88 -0
  14. package/dist/acircuit/composite.js.map +1 -0
  15. package/dist/acircuit/index.d.ts +7 -0
  16. package/dist/acircuit/index.d.ts.map +1 -0
  17. package/dist/acircuit/index.js +6 -0
  18. package/dist/acircuit/index.js.map +1 -0
  19. package/dist/acircuit/socket.d.ts +20 -0
  20. package/dist/acircuit/socket.d.ts.map +1 -0
  21. package/dist/acircuit/socket.js +23 -0
  22. package/dist/acircuit/socket.js.map +1 -0
  23. package/dist/acircuit/types.d.ts +94 -0
  24. package/dist/acircuit/types.d.ts.map +1 -0
  25. package/dist/acircuit/types.js +2 -0
  26. package/dist/acircuit/types.js.map +1 -0
  27. package/dist/acircuit/wire.d.ts +26 -0
  28. package/dist/acircuit/wire.d.ts.map +1 -0
  29. package/dist/acircuit/wire.js +23 -0
  30. package/dist/acircuit/wire.js.map +1 -0
  31. package/dist/adiag/bus.d.ts +65 -0
  32. package/dist/adiag/bus.d.ts.map +1 -0
  33. package/dist/adiag/bus.js +225 -0
  34. package/dist/adiag/bus.js.map +1 -0
  35. package/dist/adiag/index.d.ts +3 -0
  36. package/dist/adiag/index.d.ts.map +1 -0
  37. package/dist/adiag/index.js +2 -0
  38. package/dist/adiag/index.js.map +1 -0
  39. package/dist/aecs/ComponentSet.d.ts +33 -0
  40. package/dist/aecs/ComponentSet.d.ts.map +1 -0
  41. package/dist/aecs/ComponentSet.js +127 -0
  42. package/dist/aecs/ComponentSet.js.map +1 -0
  43. package/dist/aecs/Entity.d.ts +43 -0
  44. package/dist/aecs/Entity.d.ts.map +1 -0
  45. package/dist/aecs/Entity.js +82 -0
  46. package/dist/aecs/Entity.js.map +1 -0
  47. package/dist/aecs/FloatSet.d.ts +34 -0
  48. package/dist/aecs/FloatSet.d.ts.map +1 -0
  49. package/dist/aecs/FloatSet.js +147 -0
  50. package/dist/aecs/FloatSet.js.map +1 -0
  51. package/dist/aecs/Query.d.ts +90 -0
  52. package/dist/aecs/Query.d.ts.map +1 -0
  53. package/dist/aecs/Query.js +304 -0
  54. package/dist/aecs/Query.js.map +1 -0
  55. package/dist/aecs/index.d.ts +6 -0
  56. package/dist/aecs/index.d.ts.map +1 -0
  57. package/dist/aecs/index.js +6 -0
  58. package/dist/aecs/index.js.map +1 -0
  59. package/dist/aecs/join.d.ts +2 -0
  60. package/dist/aecs/join.d.ts.map +1 -0
  61. package/dist/aecs/join.js +2 -0
  62. package/dist/aecs/join.js.map +1 -0
  63. package/dist/alm/constants.d.ts +11 -0
  64. package/dist/alm/constants.d.ts.map +1 -0
  65. package/dist/alm/constants.js +17 -0
  66. package/dist/alm/constants.js.map +1 -0
  67. package/dist/alm/index.d.ts +9 -0
  68. package/dist/alm/index.d.ts.map +1 -0
  69. package/dist/alm/index.js +12 -0
  70. package/dist/alm/index.js.map +1 -0
  71. package/dist/alm/mat3.d.ts +63 -0
  72. package/dist/alm/mat3.d.ts.map +1 -0
  73. package/dist/alm/mat3.js +393 -0
  74. package/dist/alm/mat3.js.map +1 -0
  75. package/dist/alm/mat4.d.ts +157 -0
  76. package/dist/alm/mat4.d.ts.map +1 -0
  77. package/dist/alm/mat4.js +1448 -0
  78. package/dist/alm/mat4.js.map +1 -0
  79. package/dist/alm/quat.d.ts +89 -0
  80. package/dist/alm/quat.d.ts.map +1 -0
  81. package/dist/alm/quat.js +543 -0
  82. package/dist/alm/quat.js.map +1 -0
  83. package/dist/alm/spatial.d.ts +102 -0
  84. package/dist/alm/spatial.d.ts.map +1 -0
  85. package/dist/alm/spatial.js +389 -0
  86. package/dist/alm/spatial.js.map +1 -0
  87. package/dist/alm/vec2.d.ts +97 -0
  88. package/dist/alm/vec2.d.ts.map +1 -0
  89. package/dist/alm/vec2.js +369 -0
  90. package/dist/alm/vec2.js.map +1 -0
  91. package/dist/alm/vec3.d.ts +117 -0
  92. package/dist/alm/vec3.d.ts.map +1 -0
  93. package/dist/alm/vec3.js +547 -0
  94. package/dist/alm/vec3.js.map +1 -0
  95. package/dist/alm/vec4.d.ts +95 -0
  96. package/dist/alm/vec4.d.ts.map +1 -0
  97. package/dist/alm/vec4.js +452 -0
  98. package/dist/alm/vec4.js.map +1 -0
  99. package/dist/atempo/cadence.d.ts +58 -0
  100. package/dist/atempo/cadence.d.ts.map +1 -0
  101. package/dist/atempo/cadence.js +110 -0
  102. package/dist/atempo/cadence.js.map +1 -0
  103. package/dist/atempo/clip.d.ts +37 -0
  104. package/dist/atempo/clip.d.ts.map +1 -0
  105. package/dist/atempo/clip.js +92 -0
  106. package/dist/atempo/clip.js.map +1 -0
  107. package/dist/atempo/curve.d.ts +6 -0
  108. package/dist/atempo/curve.d.ts.map +1 -0
  109. package/dist/atempo/curve.js +2 -0
  110. package/dist/atempo/curve.js.map +1 -0
  111. package/dist/atempo/curves/bezier.d.ts +7 -0
  112. package/dist/atempo/curves/bezier.d.ts.map +1 -0
  113. package/dist/atempo/curves/bezier.js +46 -0
  114. package/dist/atempo/curves/bezier.js.map +1 -0
  115. package/dist/atempo/curves/bounce.d.ts +6 -0
  116. package/dist/atempo/curves/bounce.d.ts.map +1 -0
  117. package/dist/atempo/curves/bounce.js +22 -0
  118. package/dist/atempo/curves/bounce.js.map +1 -0
  119. package/dist/atempo/curves/cubic.d.ts +14 -0
  120. package/dist/atempo/curves/cubic.d.ts.map +1 -0
  121. package/dist/atempo/curves/cubic.js +22 -0
  122. package/dist/atempo/curves/cubic.js.map +1 -0
  123. package/dist/atempo/curves/elastic.d.ts +6 -0
  124. package/dist/atempo/curves/elastic.d.ts.map +1 -0
  125. package/dist/atempo/curves/elastic.js +12 -0
  126. package/dist/atempo/curves/elastic.js.map +1 -0
  127. package/dist/atempo/curves/expo.d.ts +14 -0
  128. package/dist/atempo/curves/expo.d.ts.map +1 -0
  129. package/dist/atempo/curves/expo.js +26 -0
  130. package/dist/atempo/curves/expo.js.map +1 -0
  131. package/dist/atempo/curves/holdSnap.d.ts +6 -0
  132. package/dist/atempo/curves/holdSnap.d.ts.map +1 -0
  133. package/dist/atempo/curves/holdSnap.js +15 -0
  134. package/dist/atempo/curves/holdSnap.js.map +1 -0
  135. package/dist/atempo/curves/index.d.ts +14 -0
  136. package/dist/atempo/curves/index.d.ts.map +1 -0
  137. package/dist/atempo/curves/index.js +14 -0
  138. package/dist/atempo/curves/index.js.map +1 -0
  139. package/dist/atempo/curves/linear.d.ts +6 -0
  140. package/dist/atempo/curves/linear.d.ts.map +1 -0
  141. package/dist/atempo/curves/linear.js +5 -0
  142. package/dist/atempo/curves/linear.js.map +1 -0
  143. package/dist/atempo/curves/overshoot.d.ts +6 -0
  144. package/dist/atempo/curves/overshoot.d.ts.map +1 -0
  145. package/dist/atempo/curves/overshoot.js +16 -0
  146. package/dist/atempo/curves/overshoot.js.map +1 -0
  147. package/dist/atempo/curves/quad.d.ts +14 -0
  148. package/dist/atempo/curves/quad.d.ts.map +1 -0
  149. package/dist/atempo/curves/quad.js +22 -0
  150. package/dist/atempo/curves/quad.js.map +1 -0
  151. package/dist/atempo/curves/step.d.ts +6 -0
  152. package/dist/atempo/curves/step.d.ts.map +1 -0
  153. package/dist/atempo/curves/step.js +11 -0
  154. package/dist/atempo/curves/step.js.map +1 -0
  155. package/dist/atempo/index.d.ts +17 -0
  156. package/dist/atempo/index.d.ts.map +1 -0
  157. package/dist/atempo/index.js +18 -0
  158. package/dist/atempo/index.js.map +1 -0
  159. package/dist/atempo/phase.d.ts +66 -0
  160. package/dist/atempo/phase.d.ts.map +1 -0
  161. package/dist/atempo/phase.js +121 -0
  162. package/dist/atempo/phase.js.map +1 -0
  163. package/dist/atempo/spring.d.ts +20 -0
  164. package/dist/atempo/spring.d.ts.map +1 -0
  165. package/dist/atempo/spring.js +71 -0
  166. package/dist/atempo/spring.js.map +1 -0
  167. package/dist/atempo/track.d.ts +69 -0
  168. package/dist/atempo/track.d.ts.map +1 -0
  169. package/dist/atempo/track.js +184 -0
  170. package/dist/atempo/track.js.map +1 -0
  171. package/dist/atempo/tracks/color.d.ts +20 -0
  172. package/dist/atempo/tracks/color.d.ts.map +1 -0
  173. package/dist/atempo/tracks/color.js +52 -0
  174. package/dist/atempo/tracks/color.js.map +1 -0
  175. package/dist/atempo/tracks/float.d.ts +9 -0
  176. package/dist/atempo/tracks/float.d.ts.map +1 -0
  177. package/dist/atempo/tracks/float.js +13 -0
  178. package/dist/atempo/tracks/float.js.map +1 -0
  179. package/dist/atempo/tracks/index.d.ts +8 -0
  180. package/dist/atempo/tracks/index.d.ts.map +1 -0
  181. package/dist/atempo/tracks/index.js +8 -0
  182. package/dist/atempo/tracks/index.js.map +1 -0
  183. package/dist/atempo/tracks/quat.d.ts +16 -0
  184. package/dist/atempo/tracks/quat.d.ts.map +1 -0
  185. package/dist/atempo/tracks/quat.js +72 -0
  186. package/dist/atempo/tracks/quat.js.map +1 -0
  187. package/dist/atempo/tracks/vec.d.ts +11 -0
  188. package/dist/atempo/tracks/vec.d.ts.map +1 -0
  189. package/dist/atempo/tracks/vec.js +27 -0
  190. package/dist/atempo/tracks/vec.js.map +1 -0
  191. package/dist/awasm/abi/index.d.ts +4 -0
  192. package/dist/awasm/abi/index.d.ts.map +1 -0
  193. package/dist/awasm/abi/index.js +4 -0
  194. package/dist/awasm/abi/index.js.map +1 -0
  195. package/dist/awasm/abi/layout.d.ts +40 -0
  196. package/dist/awasm/abi/layout.d.ts.map +1 -0
  197. package/dist/awasm/abi/layout.js +100 -0
  198. package/dist/awasm/abi/layout.js.map +1 -0
  199. package/dist/awasm/abi/slice.d.ts +28 -0
  200. package/dist/awasm/abi/slice.d.ts.map +1 -0
  201. package/dist/awasm/abi/slice.js +68 -0
  202. package/dist/awasm/abi/slice.js.map +1 -0
  203. package/dist/awasm/abi/string-buffer.d.ts +23 -0
  204. package/dist/awasm/abi/string-buffer.d.ts.map +1 -0
  205. package/dist/awasm/abi/string-buffer.js +52 -0
  206. package/dist/awasm/abi/string-buffer.js.map +1 -0
  207. package/dist/awasm/index.d.ts +5 -0
  208. package/dist/awasm/index.d.ts.map +1 -0
  209. package/dist/awasm/index.js +8 -0
  210. package/dist/awasm/index.js.map +1 -0
  211. package/dist/awasm/memory/block-pool.d.ts +31 -0
  212. package/dist/awasm/memory/block-pool.d.ts.map +1 -0
  213. package/dist/awasm/memory/block-pool.js +98 -0
  214. package/dist/awasm/memory/block-pool.js.map +1 -0
  215. package/dist/awasm/memory/index.d.ts +5 -0
  216. package/dist/awasm/memory/index.d.ts.map +1 -0
  217. package/dist/awasm/memory/index.js +5 -0
  218. package/dist/awasm/memory/index.js.map +1 -0
  219. package/dist/awasm/memory/linear-arena.d.ts +36 -0
  220. package/dist/awasm/memory/linear-arena.d.ts.map +1 -0
  221. package/dist/awasm/memory/linear-arena.js +94 -0
  222. package/dist/awasm/memory/linear-arena.js.map +1 -0
  223. package/dist/awasm/memory/memory-view.d.ts +51 -0
  224. package/dist/awasm/memory/memory-view.d.ts.map +1 -0
  225. package/dist/awasm/memory/memory-view.js +110 -0
  226. package/dist/awasm/memory/memory-view.js.map +1 -0
  227. package/dist/awasm/memory/wasm-memory.d.ts +40 -0
  228. package/dist/awasm/memory/wasm-memory.d.ts.map +1 -0
  229. package/dist/awasm/memory/wasm-memory.js +88 -0
  230. package/dist/awasm/memory/wasm-memory.js.map +1 -0
  231. package/dist/awasm/runtime/host-bridge.d.ts +30 -0
  232. package/dist/awasm/runtime/host-bridge.d.ts.map +1 -0
  233. package/dist/awasm/runtime/host-bridge.js +57 -0
  234. package/dist/awasm/runtime/host-bridge.js.map +1 -0
  235. package/dist/awasm/runtime/index.d.ts +5 -0
  236. package/dist/awasm/runtime/index.d.ts.map +1 -0
  237. package/dist/awasm/runtime/index.js +5 -0
  238. package/dist/awasm/runtime/index.js.map +1 -0
  239. package/dist/awasm/runtime/module-inspector.d.ts +32 -0
  240. package/dist/awasm/runtime/module-inspector.d.ts.map +1 -0
  241. package/dist/awasm/runtime/module-inspector.js +43 -0
  242. package/dist/awasm/runtime/module-inspector.js.map +1 -0
  243. package/dist/awasm/runtime/module-loader.d.ts +17 -0
  244. package/dist/awasm/runtime/module-loader.d.ts.map +1 -0
  245. package/dist/awasm/runtime/module-loader.js +99 -0
  246. package/dist/awasm/runtime/module-loader.js.map +1 -0
  247. package/dist/awasm/runtime/wasm-instance.d.ts +25 -0
  248. package/dist/awasm/runtime/wasm-instance.d.ts.map +1 -0
  249. package/dist/awasm/runtime/wasm-instance.js +60 -0
  250. package/dist/awasm/runtime/wasm-instance.js.map +1 -0
  251. package/dist/awasm/thread/atomic-queue.d.ts +33 -0
  252. package/dist/awasm/thread/atomic-queue.d.ts.map +1 -0
  253. package/dist/awasm/thread/atomic-queue.js +78 -0
  254. package/dist/awasm/thread/atomic-queue.js.map +1 -0
  255. package/dist/awasm/thread/index.d.ts +4 -0
  256. package/dist/awasm/thread/index.d.ts.map +1 -0
  257. package/dist/awasm/thread/index.js +4 -0
  258. package/dist/awasm/thread/index.js.map +1 -0
  259. package/dist/awasm/thread/shared-memory.d.ts +38 -0
  260. package/dist/awasm/thread/shared-memory.d.ts.map +1 -0
  261. package/dist/awasm/thread/shared-memory.js +74 -0
  262. package/dist/awasm/thread/shared-memory.js.map +1 -0
  263. package/dist/awasm/thread/worker-pool.d.ts +43 -0
  264. package/dist/awasm/thread/worker-pool.d.ts.map +1 -0
  265. package/dist/awasm/thread/worker-pool.js +118 -0
  266. package/dist/awasm/thread/worker-pool.js.map +1 -0
  267. package/dist/awgpu/binding.d.ts +108 -0
  268. package/dist/awgpu/binding.d.ts.map +1 -0
  269. package/dist/awgpu/binding.js +251 -0
  270. package/dist/awgpu/binding.js.map +1 -0
  271. package/dist/awgpu/device.d.ts +68 -0
  272. package/dist/awgpu/device.d.ts.map +1 -0
  273. package/dist/awgpu/device.js +150 -0
  274. package/dist/awgpu/device.js.map +1 -0
  275. package/dist/awgpu/graph.d.ts +77 -0
  276. package/dist/awgpu/graph.d.ts.map +1 -0
  277. package/dist/awgpu/graph.js +288 -0
  278. package/dist/awgpu/graph.js.map +1 -0
  279. package/dist/awgpu/index.d.ts +10 -0
  280. package/dist/awgpu/index.d.ts.map +1 -0
  281. package/dist/awgpu/index.js +19 -0
  282. package/dist/awgpu/index.js.map +1 -0
  283. package/dist/awgpu/memory.d.ts +231 -0
  284. package/dist/awgpu/memory.d.ts.map +1 -0
  285. package/dist/awgpu/memory.js +547 -0
  286. package/dist/awgpu/memory.js.map +1 -0
  287. package/dist/awgpu/pipeline.d.ts +114 -0
  288. package/dist/awgpu/pipeline.d.ts.map +1 -0
  289. package/dist/awgpu/pipeline.js +335 -0
  290. package/dist/awgpu/pipeline.js.map +1 -0
  291. package/dist/awgpu/sequence.d.ts +83 -0
  292. package/dist/awgpu/sequence.d.ts.map +1 -0
  293. package/dist/awgpu/sequence.js +273 -0
  294. package/dist/awgpu/sequence.js.map +1 -0
  295. package/dist/awgpu/state.d.ts +27 -0
  296. package/dist/awgpu/state.d.ts.map +1 -0
  297. package/dist/awgpu/state.js +43 -0
  298. package/dist/awgpu/state.js.map +1 -0
  299. package/dist/awgpu/stream.d.ts +57 -0
  300. package/dist/awgpu/stream.d.ts.map +1 -0
  301. package/dist/awgpu/stream.js +139 -0
  302. package/dist/awgpu/stream.js.map +1 -0
  303. package/dist/awgpu/target.d.ts +98 -0
  304. package/dist/awgpu/target.d.ts.map +1 -0
  305. package/dist/awgpu/target.js +357 -0
  306. package/dist/awgpu/target.js.map +1 -0
  307. package/dist/awgpu_old/buffer.d.ts +78 -0
  308. package/dist/awgpu_old/buffer.d.ts.map +1 -0
  309. package/dist/awgpu_old/buffer.js +220 -0
  310. package/dist/awgpu_old/buffer.js.map +1 -0
  311. package/dist/awgpu_old/device.d.ts +69 -0
  312. package/dist/awgpu_old/device.d.ts.map +1 -0
  313. package/dist/awgpu_old/device.js +121 -0
  314. package/dist/awgpu_old/device.js.map +1 -0
  315. package/dist/awgpu_old/frame.d.ts +17 -0
  316. package/dist/awgpu_old/frame.d.ts.map +1 -0
  317. package/dist/awgpu_old/frame.js +29 -0
  318. package/dist/awgpu_old/frame.js.map +1 -0
  319. package/dist/awgpu_old/index.d.ts +8 -0
  320. package/dist/awgpu_old/index.d.ts.map +1 -0
  321. package/dist/awgpu_old/index.js +18 -0
  322. package/dist/awgpu_old/index.js.map +1 -0
  323. package/dist/awgpu_old/layout.d.ts +70 -0
  324. package/dist/awgpu_old/layout.d.ts.map +1 -0
  325. package/dist/awgpu_old/layout.js +137 -0
  326. package/dist/awgpu_old/layout.js.map +1 -0
  327. package/dist/awgpu_old/pass.d.ts +97 -0
  328. package/dist/awgpu_old/pass.d.ts.map +1 -0
  329. package/dist/awgpu_old/pass.js +302 -0
  330. package/dist/awgpu_old/pass.js.map +1 -0
  331. package/dist/awgpu_old/pipeline.d.ts +73 -0
  332. package/dist/awgpu_old/pipeline.d.ts.map +1 -0
  333. package/dist/awgpu_old/pipeline.js +226 -0
  334. package/dist/awgpu_old/pipeline.js.map +1 -0
  335. package/dist/awgpu_old/target.d.ts +186 -0
  336. package/dist/awgpu_old/target.d.ts.map +1 -0
  337. package/dist/awgpu_old/target.js +428 -0
  338. package/dist/awgpu_old/target.js.map +1 -0
  339. package/dist/index.d.ts +10 -0
  340. package/dist/index.d.ts.map +1 -0
  341. package/dist/index.js +10 -0
  342. package/dist/index.js.map +1 -0
  343. package/package.json +106 -0
@@ -0,0 +1,1448 @@
1
+ import { EPSILON } from "./constants.js";
2
+ import { Vec2 } from "./vec2.js";
3
+ import { Vec3 } from "./vec3.js";
4
+ import { Vec4 } from "./vec4.js";
5
+ import { Quat } from "./quat.js";
6
+ /**
7
+ * 4x4 Matrix stored as column-major Float32Array[16].
8
+ *
9
+ * Index mapping:
10
+ * 0 4 8 12
11
+ * 1 5 9 13
12
+ * 2 6 10 14
13
+ * 3 7 11 15
14
+ */
15
+ export class _Mat4 extends Float32Array {
16
+ static get [Symbol.species]() {
17
+ return Float32Array;
18
+ }
19
+ static IDENTITY = new _Mat4([
20
+ 1, 0, 0, 0,
21
+ 0, 1, 0, 0,
22
+ 0, 0, 1, 0,
23
+ 0, 0, 0, 1,
24
+ ]);
25
+ constructor(a, b) {
26
+ if (a instanceof ArrayBuffer || (typeof SharedArrayBuffer !== "undefined" && a instanceof SharedArrayBuffer)) {
27
+ super(a, b ?? 0, 16);
28
+ }
29
+ else {
30
+ super(16);
31
+ if (a && "length" in a) {
32
+ const len = Math.min(a.length, 16);
33
+ for (let i = 0; i < len; i++) {
34
+ this[i] = a[i];
35
+ }
36
+ }
37
+ else {
38
+ this[0] = 1;
39
+ this[5] = 1;
40
+ this[10] = 1;
41
+ this[15] = 1;
42
+ }
43
+ }
44
+ }
45
+ static create() {
46
+ return new _Mat4();
47
+ }
48
+ static identity(out) {
49
+ out ??= new _Mat4();
50
+ out[0] = 1;
51
+ out[1] = 0;
52
+ out[2] = 0;
53
+ out[3] = 0;
54
+ out[4] = 0;
55
+ out[5] = 1;
56
+ out[6] = 0;
57
+ out[7] = 0;
58
+ out[8] = 0;
59
+ out[9] = 0;
60
+ out[10] = 1;
61
+ out[11] = 0;
62
+ out[12] = 0;
63
+ out[13] = 0;
64
+ out[14] = 0;
65
+ out[15] = 1;
66
+ return out;
67
+ }
68
+ static makeIdentity(out) {
69
+ return _Mat4.identity(out);
70
+ }
71
+ static view(buffer, byteOffset = 0) {
72
+ return new _Mat4(buffer, byteOffset);
73
+ }
74
+ static fromArray(array, offset = 0, out) {
75
+ out ??= new _Mat4();
76
+ for (let i = 0; i < 16; i++) {
77
+ out[i] = array[offset + i];
78
+ }
79
+ return out;
80
+ }
81
+ identity() {
82
+ this[0] = 1;
83
+ this[1] = 0;
84
+ this[2] = 0;
85
+ this[3] = 0;
86
+ this[4] = 0;
87
+ this[5] = 1;
88
+ this[6] = 0;
89
+ this[7] = 0;
90
+ this[8] = 0;
91
+ this[9] = 0;
92
+ this[10] = 1;
93
+ this[11] = 0;
94
+ this[12] = 0;
95
+ this[13] = 0;
96
+ this[14] = 0;
97
+ this[15] = 1;
98
+ return this;
99
+ }
100
+ copy(src) {
101
+ for (let i = 0; i < 16; i++) {
102
+ this[i] = src[i];
103
+ }
104
+ return this;
105
+ }
106
+ clone() {
107
+ return new _Mat4(this);
108
+ }
109
+ mul(b, out = this) {
110
+ const a00 = this[0], a01 = this[1], a02 = this[2], a03 = this[3];
111
+ const a10 = this[4], a11 = this[5], a12 = this[6], a13 = this[7];
112
+ const a20 = this[8], a21 = this[9], a22 = this[10], a23 = this[11];
113
+ const a30 = this[12], a31 = this[13], a32 = this[14], a33 = this[15];
114
+ let b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3];
115
+ out[0] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
116
+ out[1] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
117
+ out[2] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
118
+ out[3] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
119
+ b0 = b[4];
120
+ b1 = b[5];
121
+ b2 = b[6];
122
+ b3 = b[7];
123
+ out[4] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
124
+ out[5] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
125
+ out[6] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
126
+ out[7] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
127
+ b0 = b[8];
128
+ b1 = b[9];
129
+ b2 = b[10];
130
+ b3 = b[11];
131
+ out[8] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
132
+ out[9] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
133
+ out[10] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
134
+ out[11] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
135
+ b0 = b[12];
136
+ b1 = b[13];
137
+ b2 = b[14];
138
+ b3 = b[15];
139
+ out[12] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
140
+ out[13] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
141
+ out[14] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
142
+ out[15] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
143
+ return out;
144
+ }
145
+ multiply(b, out = this) {
146
+ return this.mul(b, out);
147
+ }
148
+ premul(a, out = this) {
149
+ return _Mat4.mul(a, this, out);
150
+ }
151
+ transpose(out = this) {
152
+ if (out === this) {
153
+ let t;
154
+ t = this[1];
155
+ this[1] = this[4];
156
+ this[4] = t;
157
+ t = this[2];
158
+ this[2] = this[8];
159
+ this[8] = t;
160
+ t = this[3];
161
+ this[3] = this[12];
162
+ this[12] = t;
163
+ t = this[6];
164
+ this[6] = this[9];
165
+ this[9] = t;
166
+ t = this[7];
167
+ this[7] = this[13];
168
+ this[13] = t;
169
+ t = this[11];
170
+ this[11] = this[14];
171
+ this[14] = t;
172
+ return this;
173
+ }
174
+ out[0] = this[0];
175
+ out[1] = this[4];
176
+ out[2] = this[8];
177
+ out[3] = this[12];
178
+ out[4] = this[1];
179
+ out[5] = this[5];
180
+ out[6] = this[9];
181
+ out[7] = this[13];
182
+ out[8] = this[2];
183
+ out[9] = this[6];
184
+ out[10] = this[10];
185
+ out[11] = this[14];
186
+ out[12] = this[3];
187
+ out[13] = this[7];
188
+ out[14] = this[11];
189
+ out[15] = this[15];
190
+ return out;
191
+ }
192
+ invert(out = this) {
193
+ const a00 = this[0], a01 = this[1], a02 = this[2], a03 = this[3];
194
+ const a10 = this[4], a11 = this[5], a12 = this[6], a13 = this[7];
195
+ const a20 = this[8], a21 = this[9], a22 = this[10], a23 = this[11];
196
+ const a30 = this[12], a31 = this[13], a32 = this[14], a33 = this[15];
197
+ const b00 = a00 * a11 - a01 * a10;
198
+ const b01 = a00 * a12 - a02 * a10;
199
+ const b02 = a00 * a13 - a03 * a10;
200
+ const b03 = a01 * a12 - a02 * a11;
201
+ const b04 = a01 * a13 - a03 * a11;
202
+ const b05 = a02 * a13 - a03 * a12;
203
+ const b06 = a20 * a31 - a21 * a30;
204
+ const b07 = a20 * a32 - a22 * a30;
205
+ const b08 = a20 * a33 - a23 * a30;
206
+ const b09 = a21 * a32 - a22 * a31;
207
+ const b10 = a21 * a33 - a23 * a31;
208
+ const b11 = a22 * a33 - a23 * a32;
209
+ let det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
210
+ if (Math.abs(det) <= EPSILON)
211
+ return null;
212
+ det = 1.0 / det;
213
+ out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
214
+ out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
215
+ out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
216
+ out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det;
217
+ out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
218
+ out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
219
+ out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
220
+ out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det;
221
+ out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
222
+ out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
223
+ out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
224
+ out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det;
225
+ out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det;
226
+ out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det;
227
+ out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det;
228
+ out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det;
229
+ return out;
230
+ }
231
+ /**
232
+ * Fast specialized inverse for affine/rigid transformation matrices (Rotation + Translation).
233
+ * ~8x faster than full general matrix inversion.
234
+ */
235
+ invertRigid(out = this) {
236
+ const r00 = this[0], r01 = this[4], r02 = this[8];
237
+ const r10 = this[1], r11 = this[5], r12 = this[9];
238
+ const r20 = this[2], r21 = this[6], r22 = this[10];
239
+ const tx = this[12], ty = this[13], tz = this[14];
240
+ out[0] = r00;
241
+ out[1] = r01;
242
+ out[2] = r02;
243
+ out[3] = 0;
244
+ out[4] = r10;
245
+ out[5] = r11;
246
+ out[6] = r12;
247
+ out[7] = 0;
248
+ out[8] = r20;
249
+ out[9] = r21;
250
+ out[10] = r22;
251
+ out[11] = 0;
252
+ out[12] = -(r00 * tx + r10 * ty + r20 * tz);
253
+ out[13] = -(r01 * tx + r11 * ty + r21 * tz);
254
+ out[14] = -(r02 * tx + r12 * ty + r22 * tz);
255
+ out[15] = 1;
256
+ return out;
257
+ }
258
+ /**
259
+ * Computes 3x3 normal matrix (transpose of inverse of model matrix) embedded into a Mat4.
260
+ * Essential for 3D lighting with non-uniform scale.
261
+ */
262
+ normalMatrix(out = this) {
263
+ const a00 = this[0], a01 = this[1], a02 = this[2];
264
+ const a10 = this[4], a11 = this[5], a12 = this[6];
265
+ const a20 = this[8], a21 = this[9], a22 = this[10];
266
+ const b01 = a22 * a11 - a12 * a21;
267
+ const b11 = -a22 * a10 + a12 * a20;
268
+ const b21 = a21 * a10 - a11 * a20;
269
+ let d = a00 * b01 + a01 * b11 + a02 * b21;
270
+ if (Math.abs(d) <= EPSILON)
271
+ return null;
272
+ d = 1.0 / d;
273
+ out[0] = b01 * d;
274
+ out[1] = (-a22 * a01 + a02 * a21) * d;
275
+ out[2] = (a12 * a01 - a02 * a11) * d;
276
+ out[3] = 0;
277
+ out[4] = b11 * d;
278
+ out[5] = (a22 * a00 - a02 * a20) * d;
279
+ out[6] = (-a12 * a00 + a02 * a10) * d;
280
+ out[7] = 0;
281
+ out[8] = b21 * d;
282
+ out[9] = (-a21 * a00 + a01 * a20) * d;
283
+ out[10] = (a11 * a00 - a01 * a10) * d;
284
+ out[11] = 0;
285
+ out[12] = 0;
286
+ out[13] = 0;
287
+ out[14] = 0;
288
+ out[15] = 1;
289
+ return out;
290
+ }
291
+ /**
292
+ * Computes determinant of this 4x4 matrix.
293
+ */
294
+ determinant() {
295
+ return _Mat4.determinant(this);
296
+ }
297
+ /**
298
+ * Decomposes matrix into translation, rotation quaternion, and scale.
299
+ */
300
+ decompose(outTranslation, outRotation, outScale) {
301
+ return _Mat4.decompose(this, outTranslation, outRotation, outScale);
302
+ }
303
+ translate(v, out = this) {
304
+ const x = v[0], y = v[1], z = v[2];
305
+ if (out !== this) {
306
+ out[0] = this[0];
307
+ out[1] = this[1];
308
+ out[2] = this[2];
309
+ out[3] = this[3];
310
+ out[4] = this[4];
311
+ out[5] = this[5];
312
+ out[6] = this[6];
313
+ out[7] = this[7];
314
+ out[8] = this[8];
315
+ out[9] = this[9];
316
+ out[10] = this[10];
317
+ out[11] = this[11];
318
+ }
319
+ out[12] = this[0] * x + this[4] * y + this[8] * z + this[12];
320
+ out[13] = this[1] * x + this[5] * y + this[9] * z + this[13];
321
+ out[14] = this[2] * x + this[6] * y + this[10] * z + this[14];
322
+ out[15] = this[3] * x + this[7] * y + this[11] * z + this[15];
323
+ return out;
324
+ }
325
+ rotateX(rad, out = this) {
326
+ const s = Math.sin(rad), c = Math.cos(rad);
327
+ const a10 = this[4], a11 = this[5], a12 = this[6], a13 = this[7];
328
+ const a20 = this[8], a21 = this[9], a22 = this[10], a23 = this[11];
329
+ if (this !== out) {
330
+ out[0] = this[0];
331
+ out[1] = this[1];
332
+ out[2] = this[2];
333
+ out[3] = this[3];
334
+ out[12] = this[12];
335
+ out[13] = this[13];
336
+ out[14] = this[14];
337
+ out[15] = this[15];
338
+ }
339
+ out[4] = a10 * c + a20 * s;
340
+ out[5] = a11 * c + a21 * s;
341
+ out[6] = a12 * c + a22 * s;
342
+ out[7] = a13 * c + a23 * s;
343
+ out[8] = a20 * c - a10 * s;
344
+ out[9] = a21 * c - a11 * s;
345
+ out[10] = a22 * c - a12 * s;
346
+ out[11] = a23 * c - a13 * s;
347
+ return out;
348
+ }
349
+ rotateY(rad, out = this) {
350
+ const s = Math.sin(rad), c = Math.cos(rad);
351
+ const a00 = this[0], a01 = this[1], a02 = this[2], a03 = this[3];
352
+ const a20 = this[8], a21 = this[9], a22 = this[10], a23 = this[11];
353
+ if (this !== out) {
354
+ out[4] = this[4];
355
+ out[5] = this[5];
356
+ out[6] = this[6];
357
+ out[7] = this[7];
358
+ out[12] = this[12];
359
+ out[13] = this[13];
360
+ out[14] = this[14];
361
+ out[15] = this[15];
362
+ }
363
+ out[0] = a00 * c - a20 * s;
364
+ out[1] = a01 * c - a21 * s;
365
+ out[2] = a02 * c - a22 * s;
366
+ out[3] = a03 * c - a23 * s;
367
+ out[8] = a00 * s + a20 * c;
368
+ out[9] = a01 * s + a21 * c;
369
+ out[10] = a02 * s + a22 * c;
370
+ out[11] = a03 * s + a23 * c;
371
+ return out;
372
+ }
373
+ rotateZ(rad, out = this) {
374
+ const s = Math.sin(rad), c = Math.cos(rad);
375
+ const a00 = this[0], a01 = this[1], a02 = this[2], a03 = this[3];
376
+ const a10 = this[4], a11 = this[5], a12 = this[6], a13 = this[7];
377
+ if (this !== out) {
378
+ out[8] = this[8];
379
+ out[9] = this[9];
380
+ out[10] = this[10];
381
+ out[11] = this[11];
382
+ out[12] = this[12];
383
+ out[13] = this[13];
384
+ out[14] = this[14];
385
+ out[15] = this[15];
386
+ }
387
+ out[0] = a00 * c + a10 * s;
388
+ out[1] = a01 * c + a11 * s;
389
+ out[2] = a02 * c + a12 * s;
390
+ out[3] = a03 * c + a13 * s;
391
+ out[4] = a10 * c - a00 * s;
392
+ out[5] = a11 * c - a01 * s;
393
+ out[6] = a12 * c - a02 * s;
394
+ out[7] = a13 * c - a03 * s;
395
+ return out;
396
+ }
397
+ rotate(axis, rad, out = this) {
398
+ let x = axis[0], y = axis[1], z = axis[2];
399
+ let len = Math.hypot(x, y, z);
400
+ if (len < EPSILON) {
401
+ if (out !== this)
402
+ out.set(this);
403
+ return out;
404
+ }
405
+ len = 1.0 / len;
406
+ x *= len;
407
+ y *= len;
408
+ z *= len;
409
+ const s = Math.sin(rad), c = Math.cos(rad), t = 1.0 - c;
410
+ const b00 = x * x * t + c, b01 = y * x * t + z * s, b02 = z * x * t - y * s;
411
+ const b10 = x * y * t - z * s, b11 = y * y * t + c, b12 = z * y * t + x * s;
412
+ const b20 = x * z * t + y * s, b21 = y * z * t - x * s, b22 = z * z * t + c;
413
+ const a00 = this[0], a01 = this[1], a02 = this[2], a03 = this[3];
414
+ const a10 = this[4], a11 = this[5], a12 = this[6], a13 = this[7];
415
+ const a20 = this[8], a21 = this[9], a22 = this[10], a23 = this[11];
416
+ out[0] = a00 * b00 + a10 * b01 + a20 * b02;
417
+ out[1] = a01 * b00 + a11 * b01 + a21 * b02;
418
+ out[2] = a02 * b00 + a12 * b01 + a22 * b02;
419
+ out[3] = a03 * b00 + a13 * b01 + a23 * b02;
420
+ out[4] = a00 * b10 + a10 * b11 + a20 * b12;
421
+ out[5] = a01 * b10 + a11 * b11 + a21 * b12;
422
+ out[6] = a02 * b10 + a12 * b11 + a22 * b12;
423
+ out[7] = a03 * b10 + a13 * b11 + a23 * b12;
424
+ out[8] = a00 * b20 + a10 * b21 + a20 * b22;
425
+ out[9] = a01 * b20 + a11 * b21 + a21 * b22;
426
+ out[10] = a02 * b20 + a12 * b21 + a22 * b22;
427
+ out[11] = a03 * b20 + a13 * b21 + a23 * b22;
428
+ if (this !== out) {
429
+ out[12] = this[12];
430
+ out[13] = this[13];
431
+ out[14] = this[14];
432
+ out[15] = this[15];
433
+ }
434
+ return out;
435
+ }
436
+ rotateQ(q, out = this) {
437
+ const rot = _Mat4.fromQuat(q);
438
+ return this.mul(rot, out);
439
+ }
440
+ scale(v, out = this) {
441
+ const x = v[0], y = v[1], z = v[2];
442
+ out[0] = this[0] * x;
443
+ out[1] = this[1] * x;
444
+ out[2] = this[2] * x;
445
+ out[3] = this[3] * x;
446
+ out[4] = this[4] * y;
447
+ out[5] = this[5] * y;
448
+ out[6] = this[6] * y;
449
+ out[7] = this[7] * y;
450
+ out[8] = this[8] * z;
451
+ out[9] = this[9] * z;
452
+ out[10] = this[10] * z;
453
+ out[11] = this[11] * z;
454
+ if (this !== out) {
455
+ out[12] = this[12];
456
+ out[13] = this[13];
457
+ out[14] = this[14];
458
+ out[15] = this[15];
459
+ }
460
+ return out;
461
+ }
462
+ getTranslation(out) {
463
+ out ??= new Vec3();
464
+ out[0] = this[12];
465
+ out[1] = this[13];
466
+ out[2] = this[14];
467
+ return out;
468
+ }
469
+ getScaling(out) {
470
+ out ??= new Vec3();
471
+ out[0] = Math.hypot(this[0], this[1], this[2]);
472
+ out[1] = Math.hypot(this[4], this[5], this[6]);
473
+ out[2] = Math.hypot(this[8], this[9], this[10]);
474
+ return out;
475
+ }
476
+ getRotation(out) {
477
+ out ??= new Quat();
478
+ const scaling = this.getScaling();
479
+ const isx = scaling[0] > EPSILON ? 1.0 / scaling[0] : 0;
480
+ const isy = scaling[1] > EPSILON ? 1.0 / scaling[1] : 0;
481
+ const isz = scaling[2] > EPSILON ? 1.0 / scaling[2] : 0;
482
+ const m00 = this[0] * isx, m01 = this[1] * isx, m02 = this[2] * isx;
483
+ const m10 = this[4] * isy, m11 = this[5] * isy, m12 = this[6] * isy;
484
+ const m20 = this[8] * isz, m21 = this[9] * isz, m22 = this[10] * isz;
485
+ const trace = m00 + m11 + m22;
486
+ if (trace > 0) {
487
+ const s = 0.5 / Math.sqrt(trace + 1.0);
488
+ out[3] = 0.25 / s;
489
+ out[0] = (m21 - m12) * s;
490
+ out[1] = (m02 - m20) * s;
491
+ out[2] = (m10 - m01) * s;
492
+ }
493
+ else if (m00 > m11 && m00 > m22) {
494
+ const s = 2.0 * Math.sqrt(1.0 + m00 - m11 - m22);
495
+ out[3] = (m21 - m12) / s;
496
+ out[0] = 0.25 * s;
497
+ out[1] = (m01 + m10) / s;
498
+ out[2] = (m02 + m20) / s;
499
+ }
500
+ else if (m11 > m22) {
501
+ const s = 2.0 * Math.sqrt(1.0 + m11 - m00 - m22);
502
+ out[3] = (m02 - m20) / s;
503
+ out[0] = (m01 + m10) / s;
504
+ out[1] = 0.25 * s;
505
+ out[2] = (m12 + m21) / s;
506
+ }
507
+ else {
508
+ const s = 2.0 * Math.sqrt(1.0 + m22 - m00 - m11);
509
+ out[3] = (m10 - m01) / s;
510
+ out[0] = (m02 + m20) / s;
511
+ out[1] = (m12 + m21) / s;
512
+ out[2] = 0.25 * s;
513
+ }
514
+ return out;
515
+ }
516
+ transformVec2(v, out) {
517
+ out ??= new Vec2();
518
+ const x = v[0], y = v[1];
519
+ out[0] = this[0] * x + this[4] * y + this[12];
520
+ out[1] = this[1] * x + this[5] * y + this[13];
521
+ return out;
522
+ }
523
+ transformVec3(v, out) {
524
+ out ??= new Vec3();
525
+ const x = v[0], y = v[1], z = v[2];
526
+ let w = this[3] * x + this[7] * y + this[11] * z + this[15];
527
+ w = w !== 0 ? 1.0 / w : 1.0;
528
+ out[0] = (this[0] * x + this[4] * y + this[8] * z + this[12]) * w;
529
+ out[1] = (this[1] * x + this[5] * y + this[9] * z + this[13]) * w;
530
+ out[2] = (this[2] * x + this[6] * y + this[10] * z + this[14]) * w;
531
+ return out;
532
+ }
533
+ transformDirection(v, out) {
534
+ out ??= new Vec3();
535
+ const x = v[0], y = v[1], z = v[2];
536
+ out[0] = this[0] * x + this[4] * y + this[8] * z;
537
+ out[1] = this[1] * x + this[5] * y + this[9] * z;
538
+ out[2] = this[2] * x + this[6] * y + this[10] * z;
539
+ return out;
540
+ }
541
+ transformVec4(v, out) {
542
+ out ??= new Vec4();
543
+ const x = v[0], y = v[1], z = v[2], w = v[3];
544
+ out[0] = this[0] * x + this[4] * y + this[8] * z + this[12] * w;
545
+ out[1] = this[1] * x + this[5] * y + this[9] * z + this[13] * w;
546
+ out[2] = this[2] * x + this[6] * y + this[10] * z + this[14] * w;
547
+ out[3] = this[3] * x + this[7] * y + this[11] * z + this[15] * w;
548
+ return out;
549
+ }
550
+ equals(b, epsilon = EPSILON) {
551
+ for (let i = 0; i < 16; i++) {
552
+ if (Math.abs(this[i] - b[i]) > epsilon)
553
+ return false;
554
+ }
555
+ return true;
556
+ }
557
+ exactEquals(b) {
558
+ for (let i = 0; i < 16; i++) {
559
+ if (this[i] !== b[i])
560
+ return false;
561
+ }
562
+ return true;
563
+ }
564
+ // Static factories and constructors
565
+ static fromTranslation(v, out) {
566
+ out ??= new _Mat4();
567
+ out[0] = 1;
568
+ out[1] = 0;
569
+ out[2] = 0;
570
+ out[3] = 0;
571
+ out[4] = 0;
572
+ out[5] = 1;
573
+ out[6] = 0;
574
+ out[7] = 0;
575
+ out[8] = 0;
576
+ out[9] = 0;
577
+ out[10] = 1;
578
+ out[11] = 0;
579
+ out[12] = v[0];
580
+ out[13] = v[1];
581
+ out[14] = v[2];
582
+ out[15] = 1;
583
+ return out;
584
+ }
585
+ static fromScaling(v, out) {
586
+ out ??= new _Mat4();
587
+ out[0] = v[0];
588
+ out[1] = 0;
589
+ out[2] = 0;
590
+ out[3] = 0;
591
+ out[4] = 0;
592
+ out[5] = v[1];
593
+ out[6] = 0;
594
+ out[7] = 0;
595
+ out[8] = 0;
596
+ out[9] = 0;
597
+ out[10] = v[2];
598
+ out[11] = 0;
599
+ out[12] = 0;
600
+ out[13] = 0;
601
+ out[14] = 0;
602
+ out[15] = 1;
603
+ return out;
604
+ }
605
+ static fromRotationX(rad, out) {
606
+ out ??= new _Mat4();
607
+ out[0] = 1;
608
+ out[1] = 0;
609
+ out[2] = 0;
610
+ out[3] = 0;
611
+ out[4] = 0;
612
+ out[7] = 0;
613
+ out[8] = 0;
614
+ out[11] = 0;
615
+ out[12] = 0;
616
+ out[13] = 0;
617
+ out[14] = 0;
618
+ out[15] = 1;
619
+ const c = Math.cos(rad), s = Math.sin(rad);
620
+ out[5] = c;
621
+ out[6] = s;
622
+ out[9] = -s;
623
+ out[10] = c;
624
+ return out;
625
+ }
626
+ static fromRotationY(rad, out) {
627
+ out ??= new _Mat4();
628
+ out[1] = 0;
629
+ out[3] = 0;
630
+ out[4] = 0;
631
+ out[5] = 1;
632
+ out[6] = 0;
633
+ out[7] = 0;
634
+ out[9] = 0;
635
+ out[11] = 0;
636
+ out[12] = 0;
637
+ out[13] = 0;
638
+ out[14] = 0;
639
+ out[15] = 1;
640
+ const c = Math.cos(rad), s = Math.sin(rad);
641
+ out[0] = c;
642
+ out[2] = -s;
643
+ out[8] = s;
644
+ out[10] = c;
645
+ return out;
646
+ }
647
+ static fromRotationZ(rad, out) {
648
+ out ??= new _Mat4();
649
+ out[2] = 0;
650
+ out[3] = 0;
651
+ out[6] = 0;
652
+ out[7] = 0;
653
+ out[8] = 0;
654
+ out[9] = 0;
655
+ out[10] = 1;
656
+ out[11] = 0;
657
+ out[12] = 0;
658
+ out[13] = 0;
659
+ out[14] = 0;
660
+ out[15] = 1;
661
+ const c = Math.cos(rad), s = Math.sin(rad);
662
+ out[0] = c;
663
+ out[1] = s;
664
+ out[4] = -s;
665
+ out[5] = c;
666
+ return out;
667
+ }
668
+ static fromQuat(q, out) {
669
+ out ??= new _Mat4();
670
+ const x = q[0], y = q[1], z = q[2], w = q[3];
671
+ const x2 = x + x, y2 = y + y, z2 = z + z;
672
+ const xx = x * x2, yx = y * x2, yy = y * y2;
673
+ const zx = z * x2, zy = z * y2, zz = z * z2;
674
+ const wx = w * x2, wy = w * y2, wz = w * z2;
675
+ out[0] = 1 - yy - zz;
676
+ out[1] = yx + wz;
677
+ out[2] = zx - wy;
678
+ out[3] = 0;
679
+ out[4] = yx - wz;
680
+ out[5] = 1 - xx - zz;
681
+ out[6] = zy + wx;
682
+ out[7] = 0;
683
+ out[8] = zx + wy;
684
+ out[9] = zy - wx;
685
+ out[10] = 1 - xx - yy;
686
+ out[11] = 0;
687
+ out[12] = 0;
688
+ out[13] = 0;
689
+ out[14] = 0;
690
+ out[15] = 1;
691
+ return out;
692
+ }
693
+ static fromTRS(pos, rotQ, scale, out) {
694
+ out ??= new _Mat4();
695
+ const x = rotQ[0], y = rotQ[1], z = rotQ[2], w = rotQ[3];
696
+ const x2 = x + x, y2 = y + y, z2 = z + z;
697
+ const xx = x * x2, xy = x * y2, xz = x * z2;
698
+ const yy = y * y2, yz = y * z2, zz = z * z2;
699
+ const wx = w * x2, wy = w * y2, wz = w * z2;
700
+ const sx = scale[0], sy = scale[1], sz = scale[2];
701
+ out[0] = (1 - (yy + zz)) * sx;
702
+ out[1] = (xy + wz) * sx;
703
+ out[2] = (xz - wy) * sx;
704
+ out[3] = 0;
705
+ out[4] = (xy - wz) * sy;
706
+ out[5] = (1 - (xx + zz)) * sy;
707
+ out[6] = (yz + wx) * sy;
708
+ out[7] = 0;
709
+ out[8] = (xz + wy) * sz;
710
+ out[9] = (yz - wx) * sz;
711
+ out[10] = (1 - (xx + yy)) * sz;
712
+ out[11] = 0;
713
+ out[12] = pos[0];
714
+ out[13] = pos[1];
715
+ out[14] = pos[2];
716
+ out[15] = 1;
717
+ return out;
718
+ }
719
+ /**
720
+ * Computes determinant of a 4x4 matrix.
721
+ */
722
+ static determinant(a) {
723
+ const a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3];
724
+ const a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7];
725
+ const a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11];
726
+ const a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15];
727
+ const b00 = a00 * a11 - a01 * a10;
728
+ const b01 = a00 * a12 - a02 * a10;
729
+ const b02 = a00 * a13 - a03 * a10;
730
+ const b03 = a01 * a12 - a02 * a11;
731
+ const b04 = a01 * a13 - a03 * a11;
732
+ const b05 = a02 * a13 - a03 * a12;
733
+ const b06 = a20 * a31 - a21 * a30;
734
+ const b07 = a20 * a32 - a22 * a30;
735
+ const b08 = a20 * a33 - a23 * a30;
736
+ const b09 = a21 * a32 - a22 * a31;
737
+ const b10 = a21 * a33 - a23 * a31;
738
+ const b11 = a22 * a33 - a23 * a32;
739
+ return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
740
+ }
741
+ /**
742
+ * Decomposes a 4x4 affine transformation matrix into translation, rotation quaternion, and scale.
743
+ */
744
+ static decompose(m, outTranslation, outRotation, outScale) {
745
+ const trans = outTranslation ?? new Vec3();
746
+ trans[0] = m[12];
747
+ trans[1] = m[13];
748
+ trans[2] = m[14];
749
+ let sx = Math.hypot(m[0], m[1], m[2]);
750
+ let sy = Math.hypot(m[4], m[5], m[6]);
751
+ let sz = Math.hypot(m[8], m[9], m[10]);
752
+ // Check for reflection via 3x3 determinant
753
+ const det3 = m[0] * (m[5] * m[10] - m[6] * m[9]) -
754
+ m[4] * (m[1] * m[10] - m[2] * m[9]) +
755
+ m[8] * (m[1] * m[6] - m[2] * m[5]);
756
+ if (det3 < 0) {
757
+ sx = -sx;
758
+ }
759
+ const scale = outScale ?? new Vec3();
760
+ scale[0] = sx;
761
+ scale[1] = sy;
762
+ scale[2] = sz;
763
+ const rot = outRotation ?? new Quat();
764
+ const invSx = Math.abs(sx) > EPSILON ? 1.0 / sx : 0;
765
+ const invSy = Math.abs(sy) > EPSILON ? 1.0 / sy : 0;
766
+ const invSz = Math.abs(sz) > EPSILON ? 1.0 / sz : 0;
767
+ const m00 = m[0] * invSx, m10 = m[1] * invSx, m20 = m[2] * invSx;
768
+ const m01 = m[4] * invSy, m11 = m[5] * invSy, m21 = m[6] * invSy;
769
+ const m02 = m[8] * invSz, m12 = m[9] * invSz, m22 = m[10] * invSz;
770
+ const trace = m00 + m11 + m22;
771
+ if (trace > 0) {
772
+ const s = 0.5 / Math.sqrt(trace + 1.0);
773
+ rot[3] = 0.25 / s;
774
+ rot[0] = (m21 - m12) * s;
775
+ rot[1] = (m02 - m20) * s;
776
+ rot[2] = (m10 - m01) * s;
777
+ }
778
+ else if (m00 > m11 && m00 > m22) {
779
+ const s = 2.0 * Math.sqrt(1.0 + m00 - m11 - m22);
780
+ rot[3] = (m21 - m12) / s;
781
+ rot[0] = 0.25 * s;
782
+ rot[1] = (m01 + m10) / s;
783
+ rot[2] = (m02 + m20) / s;
784
+ }
785
+ else if (m11 > m22) {
786
+ const s = 2.0 * Math.sqrt(1.0 + m11 - m00 - m22);
787
+ rot[3] = (m02 - m20) / s;
788
+ rot[0] = (m01 + m10) / s;
789
+ rot[1] = 0.25 * s;
790
+ rot[2] = (m12 + m21) / s;
791
+ }
792
+ else {
793
+ const s = 2.0 * Math.sqrt(1.0 + m22 - m00 - m11);
794
+ rot[3] = (m10 - m01) / s;
795
+ rot[0] = (m02 + m20) / s;
796
+ rot[1] = (m12 + m21) / s;
797
+ rot[2] = 0.25 * s;
798
+ }
799
+ return { translation: trans, rotation: rot, scale };
800
+ }
801
+ /**
802
+ * WebGPU / Direct3D / Metal zero-to-one [0, 1] clip space perspective projection.
803
+ * Essential for WebGPU pipelines to avoid near-plane clipping and maximize depth precision.
804
+ */
805
+ static perspectiveZO(fovy, aspect, near, far, out) {
806
+ out ??= new _Mat4();
807
+ const f = 1.0 / Math.tan(fovy * 0.5);
808
+ out[0] = f / aspect;
809
+ out[1] = 0;
810
+ out[2] = 0;
811
+ out[3] = 0;
812
+ out[4] = 0;
813
+ out[5] = f;
814
+ out[6] = 0;
815
+ out[7] = 0;
816
+ out[8] = 0;
817
+ out[9] = 0;
818
+ if (Number.isFinite(far)) {
819
+ const nf = 1.0 / (near - far);
820
+ out[10] = far * nf;
821
+ out[14] = far * near * nf;
822
+ }
823
+ else {
824
+ out[10] = -1.0;
825
+ out[14] = -near;
826
+ }
827
+ out[11] = -1.0;
828
+ out[12] = 0;
829
+ out[13] = 0;
830
+ out[15] = 0;
831
+ return out;
832
+ }
833
+ /**
834
+ * OpenGL / WebGL [-1, 1] clip space perspective projection.
835
+ */
836
+ static perspectiveNO(fovy, aspect, near, far, out) {
837
+ out ??= new _Mat4();
838
+ const f = 1.0 / Math.tan(fovy * 0.5), nf = 1.0 / (near - far);
839
+ out[0] = f / aspect;
840
+ out[1] = 0;
841
+ out[2] = 0;
842
+ out[3] = 0;
843
+ out[4] = 0;
844
+ out[5] = f;
845
+ out[6] = 0;
846
+ out[7] = 0;
847
+ out[8] = 0;
848
+ out[9] = 0;
849
+ out[10] = (far + near) * nf;
850
+ out[11] = -1.0;
851
+ out[12] = 0;
852
+ out[13] = 0;
853
+ out[14] = 2.0 * far * near * nf;
854
+ out[15] = 0;
855
+ return out;
856
+ }
857
+ /** Standard perspective (alias for WebGPU/Metal [0, 1] projection). */
858
+ static perspective = _Mat4.perspectiveZO;
859
+ /**
860
+ * WebGPU / Metal / DX [0, 1] depth orthographic projection.
861
+ */
862
+ static orthoZO(left, right, bottom, top, near, far, out) {
863
+ out ??= new _Mat4();
864
+ const lr = 1.0 / (left - right);
865
+ const bt = 1.0 / (bottom - top);
866
+ const nf = 1.0 / (near - far);
867
+ out[0] = -2.0 * lr;
868
+ out[1] = 0;
869
+ out[2] = 0;
870
+ out[3] = 0;
871
+ out[4] = 0;
872
+ out[5] = -2.0 * bt;
873
+ out[6] = 0;
874
+ out[7] = 0;
875
+ out[8] = 0;
876
+ out[9] = 0;
877
+ out[10] = nf;
878
+ out[11] = 0;
879
+ out[12] = (left + right) * lr;
880
+ out[13] = (top + bottom) * bt;
881
+ out[14] = near * nf;
882
+ out[15] = 1.0;
883
+ return out;
884
+ }
885
+ /**
886
+ * WebGL [-1, 1] depth orthographic projection.
887
+ */
888
+ static orthoNO(left, right, bottom, top, near, far, out) {
889
+ out ??= new _Mat4();
890
+ const lr = 1.0 / (left - right), bt = 1.0 / (bottom - top), nf = 1.0 / (near - far);
891
+ out[0] = -2.0 * lr;
892
+ out[1] = 0;
893
+ out[2] = 0;
894
+ out[3] = 0;
895
+ out[4] = 0;
896
+ out[5] = -2.0 * bt;
897
+ out[6] = 0;
898
+ out[7] = 0;
899
+ out[8] = 0;
900
+ out[9] = 0;
901
+ out[10] = 2.0 * nf;
902
+ out[11] = 0;
903
+ out[12] = (left + right) * lr;
904
+ out[13] = (top + bottom) * bt;
905
+ out[14] = (far + near) * nf;
906
+ out[15] = 1.0;
907
+ return out;
908
+ }
909
+ static ortho = _Mat4.orthoZO;
910
+ /**
911
+ * Generates a lookAt view matrix looking from eye towards target.
912
+ */
913
+ static lookAt(eye, target, up, out) {
914
+ out ??= new _Mat4();
915
+ const eyex = eye[0], eyey = eye[1], eyez = eye[2];
916
+ const upx = up[0], upy = up[1], upz = up[2];
917
+ const targetx = target[0], targety = target[1], targetz = target[2];
918
+ let z0 = eyex - targetx;
919
+ let z1 = eyey - targety;
920
+ let z2 = eyez - targetz;
921
+ let len = Math.hypot(z0, z1, z2);
922
+ if (len < EPSILON) {
923
+ out.identity();
924
+ return out;
925
+ }
926
+ len = 1.0 / len;
927
+ z0 *= len;
928
+ z1 *= len;
929
+ z2 *= len;
930
+ let x0 = upy * z2 - upz * z1;
931
+ let x1 = upz * z0 - upx * z2;
932
+ let x2 = upx * z1 - upy * z0;
933
+ len = Math.hypot(x0, x1, x2);
934
+ if (len < EPSILON) {
935
+ x0 = 0;
936
+ x1 = 0;
937
+ x2 = 0;
938
+ }
939
+ else {
940
+ len = 1.0 / len;
941
+ x0 *= len;
942
+ x1 *= len;
943
+ x2 *= len;
944
+ }
945
+ let y0 = z1 * x2 - z2 * x1;
946
+ let y1 = z2 * x0 - z0 * x2;
947
+ let y2 = z0 * x1 - z1 * x0;
948
+ len = Math.hypot(y0, y1, y2);
949
+ if (len < EPSILON) {
950
+ y0 = 0;
951
+ y1 = 0;
952
+ y2 = 0;
953
+ }
954
+ else {
955
+ len = 1.0 / len;
956
+ y0 *= len;
957
+ y1 *= len;
958
+ y2 *= len;
959
+ }
960
+ out[0] = x0;
961
+ out[1] = y0;
962
+ out[2] = z0;
963
+ out[3] = 0;
964
+ out[4] = x1;
965
+ out[5] = y1;
966
+ out[6] = z1;
967
+ out[7] = 0;
968
+ out[8] = x2;
969
+ out[9] = y2;
970
+ out[10] = z2;
971
+ out[11] = 0;
972
+ out[12] = -(x0 * eyex + x1 * eyey + x2 * eyez);
973
+ out[13] = -(y0 * eyex + y1 * eyey + y2 * eyez);
974
+ out[14] = -(z0 * eyex + z1 * eyey + z2 * eyez);
975
+ out[15] = 1.0;
976
+ return out;
977
+ }
978
+ /**
979
+ * Generates a matrix that points eye towards target.
980
+ */
981
+ static targetTo(eye, target, up, out) {
982
+ out ??= new _Mat4();
983
+ const eyex = eye[0], eyey = eye[1], eyez = eye[2];
984
+ const upx = up[0], upy = up[1], upz = up[2];
985
+ const targetx = target[0], targety = target[1], targetz = target[2];
986
+ let z0 = targetx - eyex;
987
+ let z1 = targety - eyey;
988
+ let z2 = targetz - eyez;
989
+ let len = Math.hypot(z0, z1, z2);
990
+ if (len < EPSILON) {
991
+ out.identity();
992
+ return out;
993
+ }
994
+ len = 1.0 / len;
995
+ z0 *= len;
996
+ z1 *= len;
997
+ z2 *= len;
998
+ let x0 = upy * z2 - upz * z1;
999
+ let x1 = upz * z0 - upx * z2;
1000
+ let x2 = upx * z1 - upy * z0;
1001
+ len = Math.hypot(x0, x1, x2);
1002
+ if (len < EPSILON) {
1003
+ x0 = 0;
1004
+ x1 = 0;
1005
+ x2 = 0;
1006
+ }
1007
+ else {
1008
+ len = 1.0 / len;
1009
+ x0 *= len;
1010
+ x1 *= len;
1011
+ x2 *= len;
1012
+ }
1013
+ let y0 = z1 * x2 - z2 * x1;
1014
+ let y1 = z2 * x0 - z0 * x2;
1015
+ let y2 = z0 * x1 - z1 * x0;
1016
+ len = Math.hypot(y0, y1, y2);
1017
+ if (len < EPSILON) {
1018
+ y0 = 0;
1019
+ y1 = 0;
1020
+ y2 = 0;
1021
+ }
1022
+ else {
1023
+ len = 1.0 / len;
1024
+ y0 *= len;
1025
+ y1 *= len;
1026
+ y2 *= len;
1027
+ }
1028
+ out[0] = x0;
1029
+ out[1] = x1;
1030
+ out[2] = x2;
1031
+ out[3] = 0;
1032
+ out[4] = y0;
1033
+ out[5] = y1;
1034
+ out[6] = y2;
1035
+ out[7] = 0;
1036
+ out[8] = z0;
1037
+ out[9] = z1;
1038
+ out[10] = z2;
1039
+ out[11] = 0;
1040
+ out[12] = eyex;
1041
+ out[13] = eyey;
1042
+ out[14] = eyez;
1043
+ out[15] = 1.0;
1044
+ return out;
1045
+ }
1046
+ // Static procedural operations
1047
+ static copy(a, out) {
1048
+ out ??= new _Mat4();
1049
+ for (let i = 0; i < 16; i++) {
1050
+ out[i] = a[i];
1051
+ }
1052
+ return out;
1053
+ }
1054
+ static clone(a) {
1055
+ return new _Mat4(a);
1056
+ }
1057
+ static mul(a, b, out) {
1058
+ out ??= new _Mat4();
1059
+ const a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3];
1060
+ const a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7];
1061
+ const a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11];
1062
+ const a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15];
1063
+ let b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3];
1064
+ out[0] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
1065
+ out[1] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
1066
+ out[2] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
1067
+ out[3] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
1068
+ b0 = b[4];
1069
+ b1 = b[5];
1070
+ b2 = b[6];
1071
+ b3 = b[7];
1072
+ out[4] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
1073
+ out[5] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
1074
+ out[6] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
1075
+ out[7] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
1076
+ b0 = b[8];
1077
+ b1 = b[9];
1078
+ b2 = b[10];
1079
+ b3 = b[11];
1080
+ out[8] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
1081
+ out[9] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
1082
+ out[10] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
1083
+ out[11] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
1084
+ b0 = b[12];
1085
+ b1 = b[13];
1086
+ b2 = b[14];
1087
+ b3 = b[15];
1088
+ out[12] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
1089
+ out[13] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
1090
+ out[14] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
1091
+ out[15] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
1092
+ return out;
1093
+ }
1094
+ static transpose(a, out) {
1095
+ out ??= new _Mat4();
1096
+ if (out === a) {
1097
+ let t;
1098
+ t = a[1];
1099
+ out[1] = a[4];
1100
+ out[4] = t;
1101
+ t = a[2];
1102
+ out[2] = a[8];
1103
+ out[8] = t;
1104
+ t = a[3];
1105
+ out[3] = a[12];
1106
+ out[12] = t;
1107
+ t = a[6];
1108
+ out[6] = a[9];
1109
+ out[9] = t;
1110
+ t = a[7];
1111
+ out[7] = a[13];
1112
+ out[13] = t;
1113
+ t = a[11];
1114
+ out[11] = a[14];
1115
+ out[14] = t;
1116
+ return out;
1117
+ }
1118
+ out[0] = a[0];
1119
+ out[1] = a[4];
1120
+ out[2] = a[8];
1121
+ out[3] = a[12];
1122
+ out[4] = a[1];
1123
+ out[5] = a[5];
1124
+ out[6] = a[9];
1125
+ out[7] = a[13];
1126
+ out[8] = a[2];
1127
+ out[9] = a[6];
1128
+ out[10] = a[10];
1129
+ out[11] = a[14];
1130
+ out[12] = a[3];
1131
+ out[13] = a[7];
1132
+ out[14] = a[11];
1133
+ out[15] = a[15];
1134
+ return out;
1135
+ }
1136
+ static invert(a, out) {
1137
+ out ??= new _Mat4();
1138
+ const a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3];
1139
+ const a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7];
1140
+ const a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11];
1141
+ const a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15];
1142
+ const b00 = a00 * a11 - a01 * a10;
1143
+ const b01 = a00 * a12 - a02 * a10;
1144
+ const b02 = a00 * a13 - a03 * a10;
1145
+ const b03 = a01 * a12 - a02 * a11;
1146
+ const b04 = a01 * a13 - a03 * a11;
1147
+ const b05 = a02 * a13 - a03 * a12;
1148
+ const b06 = a20 * a31 - a21 * a30;
1149
+ const b07 = a20 * a32 - a22 * a30;
1150
+ const b08 = a20 * a33 - a23 * a30;
1151
+ const b09 = a21 * a32 - a22 * a31;
1152
+ const b10 = a21 * a33 - a23 * a31;
1153
+ const b11 = a22 * a33 - a23 * a32;
1154
+ let det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
1155
+ if (Math.abs(det) <= EPSILON)
1156
+ return null;
1157
+ det = 1.0 / det;
1158
+ out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
1159
+ out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
1160
+ out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
1161
+ out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det;
1162
+ out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
1163
+ out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
1164
+ out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
1165
+ out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det;
1166
+ out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
1167
+ out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
1168
+ out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
1169
+ out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det;
1170
+ out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det;
1171
+ out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det;
1172
+ out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det;
1173
+ out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det;
1174
+ return out;
1175
+ }
1176
+ static invertRigid(a, out) {
1177
+ out ??= new _Mat4();
1178
+ const r00 = a[0], r01 = a[4], r02 = a[8];
1179
+ const r10 = a[1], r11 = a[5], r12 = a[9];
1180
+ const r20 = a[2], r21 = a[6], r22 = a[10];
1181
+ const tx = a[12], ty = a[13], tz = a[14];
1182
+ out[0] = r00;
1183
+ out[1] = r01;
1184
+ out[2] = r02;
1185
+ out[3] = 0;
1186
+ out[4] = r10;
1187
+ out[5] = r11;
1188
+ out[6] = r12;
1189
+ out[7] = 0;
1190
+ out[8] = r20;
1191
+ out[9] = r21;
1192
+ out[10] = r22;
1193
+ out[11] = 0;
1194
+ out[12] = -(r00 * tx + r10 * ty + r20 * tz);
1195
+ out[13] = -(r01 * tx + r11 * ty + r21 * tz);
1196
+ out[14] = -(r02 * tx + r12 * ty + r22 * tz);
1197
+ out[15] = 1;
1198
+ return out;
1199
+ }
1200
+ static normalMatrix(m, out) {
1201
+ out ??= new _Mat4();
1202
+ const a00 = m[0], a01 = m[1], a02 = m[2];
1203
+ const a10 = m[4], a11 = m[5], a12 = m[6];
1204
+ const a20 = m[8], a21 = m[9], a22 = m[10];
1205
+ const b01 = a22 * a11 - a12 * a21;
1206
+ const b11 = -a22 * a10 + a12 * a20;
1207
+ const b21 = a21 * a10 - a11 * a20;
1208
+ let det = a00 * b01 + a01 * b11 + a02 * b21;
1209
+ if (Math.abs(det) <= EPSILON)
1210
+ return null;
1211
+ det = 1.0 / det;
1212
+ out[0] = b01 * det;
1213
+ out[1] = (-a22 * a01 + a02 * a21) * det;
1214
+ out[2] = (a12 * a01 - a02 * a11) * det;
1215
+ out[3] = 0;
1216
+ out[4] = b11 * det;
1217
+ out[5] = (a22 * a00 - a02 * a20) * det;
1218
+ out[6] = (-a12 * a00 + a02 * a10) * det;
1219
+ out[7] = 0;
1220
+ out[8] = b21 * det;
1221
+ out[9] = (-a21 * a00 + a01 * a20) * det;
1222
+ out[10] = (a11 * a00 - a01 * a10) * det;
1223
+ out[11] = 0;
1224
+ out[12] = 0;
1225
+ out[13] = 0;
1226
+ out[14] = 0;
1227
+ out[15] = 1;
1228
+ return out;
1229
+ }
1230
+ static translate(m, v, out) {
1231
+ out ??= new _Mat4();
1232
+ const x = v[0], y = v[1], z = v[2];
1233
+ if (out !== m) {
1234
+ out[0] = m[0];
1235
+ out[1] = m[1];
1236
+ out[2] = m[2];
1237
+ out[3] = m[3];
1238
+ out[4] = m[4];
1239
+ out[5] = m[5];
1240
+ out[6] = m[6];
1241
+ out[7] = m[7];
1242
+ out[8] = m[8];
1243
+ out[9] = m[9];
1244
+ out[10] = m[10];
1245
+ out[11] = m[11];
1246
+ }
1247
+ out[12] = m[0] * x + m[4] * y + m[8] * z + m[12];
1248
+ out[13] = m[1] * x + m[5] * y + m[9] * z + m[13];
1249
+ out[14] = m[2] * x + m[6] * y + m[10] * z + m[14];
1250
+ out[15] = m[3] * x + m[7] * y + m[11] * z + m[15];
1251
+ return out;
1252
+ }
1253
+ static rotateX(m, rad, out) {
1254
+ out ??= new _Mat4();
1255
+ const s = Math.sin(rad), c = Math.cos(rad);
1256
+ const a10 = m[4], a11 = m[5], a12 = m[6], a13 = m[7];
1257
+ const a20 = m[8], a21 = m[9], a22 = m[10], a23 = m[11];
1258
+ if (m !== out) {
1259
+ out[0] = m[0];
1260
+ out[1] = m[1];
1261
+ out[2] = m[2];
1262
+ out[3] = m[3];
1263
+ out[12] = m[12];
1264
+ out[13] = m[13];
1265
+ out[14] = m[14];
1266
+ out[15] = m[15];
1267
+ }
1268
+ out[4] = a10 * c + a20 * s;
1269
+ out[5] = a11 * c + a21 * s;
1270
+ out[6] = a12 * c + a22 * s;
1271
+ out[7] = a13 * c + a23 * s;
1272
+ out[8] = a20 * c - a10 * s;
1273
+ out[9] = a21 * c - a11 * s;
1274
+ out[10] = a22 * c - a12 * s;
1275
+ out[11] = a23 * c - a13 * s;
1276
+ return out;
1277
+ }
1278
+ static rotateY(m, rad, out) {
1279
+ out ??= new _Mat4();
1280
+ const s = Math.sin(rad), c = Math.cos(rad);
1281
+ const a00 = m[0], a01 = m[1], a02 = m[2], a03 = m[3];
1282
+ const a20 = m[8], a21 = m[9], a22 = m[10], a23 = m[11];
1283
+ if (m !== out) {
1284
+ out[4] = m[4];
1285
+ out[5] = m[5];
1286
+ out[6] = m[6];
1287
+ out[7] = m[7];
1288
+ out[12] = m[12];
1289
+ out[13] = m[13];
1290
+ out[14] = m[14];
1291
+ out[15] = m[15];
1292
+ }
1293
+ out[0] = a00 * c - a20 * s;
1294
+ out[1] = a01 * c - a21 * s;
1295
+ out[2] = a02 * c - a22 * s;
1296
+ out[3] = a03 * c - a23 * s;
1297
+ out[8] = a00 * s + a20 * c;
1298
+ out[9] = a01 * s + a21 * c;
1299
+ out[10] = a02 * s + a22 * c;
1300
+ out[11] = a03 * s + a23 * c;
1301
+ return out;
1302
+ }
1303
+ static rotateZ(m, rad, out) {
1304
+ out ??= new _Mat4();
1305
+ const s = Math.sin(rad), c = Math.cos(rad);
1306
+ const a00 = m[0], a01 = m[1], a02 = m[2], a03 = m[3];
1307
+ const a10 = m[4], a11 = m[5], a12 = m[6], a13 = m[7];
1308
+ if (m !== out) {
1309
+ out[8] = m[8];
1310
+ out[9] = m[9];
1311
+ out[10] = m[10];
1312
+ out[11] = m[11];
1313
+ out[12] = m[12];
1314
+ out[13] = m[13];
1315
+ out[14] = m[14];
1316
+ out[15] = m[15];
1317
+ }
1318
+ out[0] = a00 * c + a10 * s;
1319
+ out[1] = a01 * c + a11 * s;
1320
+ out[2] = a02 * c + a12 * s;
1321
+ out[3] = a03 * c + a13 * s;
1322
+ out[4] = a10 * c - a00 * s;
1323
+ out[5] = a11 * c - a01 * s;
1324
+ out[6] = a12 * c - a02 * s;
1325
+ out[7] = a13 * c - a03 * s;
1326
+ return out;
1327
+ }
1328
+ static rotate(m, axis, rad, out) {
1329
+ let x = axis[0], y = axis[1], z = axis[2];
1330
+ let len = Math.hypot(x, y, z);
1331
+ if (len < EPSILON) {
1332
+ if (out && out !== m) {
1333
+ for (let i = 0; i < 16; i++)
1334
+ out[i] = m[i];
1335
+ }
1336
+ return out ?? new _Mat4(m);
1337
+ }
1338
+ len = 1.0 / len;
1339
+ x *= len;
1340
+ y *= len;
1341
+ z *= len;
1342
+ const s = Math.sin(rad), c = Math.cos(rad), t = 1.0 - c;
1343
+ const b00 = x * x * t + c, b01 = y * x * t + z * s, b02 = z * x * t - y * s;
1344
+ const b10 = x * y * t - z * s, b11 = y * y * t + c, b12 = z * y * t + x * s;
1345
+ const b20 = x * z * t + y * s, b21 = y * z * t - x * s, b22 = z * z * t + c;
1346
+ out ??= new _Mat4();
1347
+ const a00 = m[0], a01 = m[1], a02 = m[2], a03 = m[3];
1348
+ const a10 = m[4], a11 = m[5], a12 = m[6], a13 = m[7];
1349
+ const a20 = m[8], a21 = m[9], a22 = m[10], a23 = m[11];
1350
+ out[0] = a00 * b00 + a10 * b01 + a20 * b02;
1351
+ out[1] = a01 * b00 + a11 * b01 + a21 * b02;
1352
+ out[2] = a02 * b00 + a12 * b01 + a22 * b02;
1353
+ out[3] = a03 * b00 + a13 * b01 + a23 * b02;
1354
+ out[4] = a00 * b10 + a10 * b11 + a20 * b12;
1355
+ out[5] = a01 * b10 + a11 * b11 + a21 * b12;
1356
+ out[6] = a02 * b10 + a12 * b11 + a22 * b12;
1357
+ out[7] = a03 * b10 + a13 * b11 + a23 * b12;
1358
+ out[8] = a00 * b20 + a10 * b21 + a20 * b22;
1359
+ out[9] = a01 * b20 + a11 * b21 + a21 * b22;
1360
+ out[10] = a02 * b20 + a12 * b21 + a22 * b22;
1361
+ out[11] = a03 * b20 + a13 * b21 + a23 * b22;
1362
+ if (m !== out) {
1363
+ out[12] = m[12];
1364
+ out[13] = m[13];
1365
+ out[14] = m[14];
1366
+ out[15] = m[15];
1367
+ }
1368
+ return out;
1369
+ }
1370
+ static rotateQ(m, q, out) {
1371
+ return _Mat4.mul(m, _Mat4.fromQuat(q), out);
1372
+ }
1373
+ static scale(m, v, out) {
1374
+ out ??= new _Mat4();
1375
+ const x = v[0], y = v[1], z = v[2];
1376
+ out[0] = m[0] * x;
1377
+ out[1] = m[1] * x;
1378
+ out[2] = m[2] * x;
1379
+ out[3] = m[3] * x;
1380
+ out[4] = m[4] * y;
1381
+ out[5] = m[5] * y;
1382
+ out[6] = m[6] * y;
1383
+ out[7] = m[7] * y;
1384
+ out[8] = m[8] * z;
1385
+ out[9] = m[9] * z;
1386
+ out[10] = m[10] * z;
1387
+ out[11] = m[11] * z;
1388
+ if (m !== out) {
1389
+ out[12] = m[12];
1390
+ out[13] = m[13];
1391
+ out[14] = m[14];
1392
+ out[15] = m[15];
1393
+ }
1394
+ return out;
1395
+ }
1396
+ static transformV2(m, v, out) {
1397
+ out ??= new Vec2();
1398
+ const x = v[0], y = v[1];
1399
+ out[0] = m[0] * x + m[4] * y + m[12];
1400
+ out[1] = m[1] * x + m[5] * y + m[13];
1401
+ return out;
1402
+ }
1403
+ static transformV3(m, v, out) {
1404
+ return Vec3.transformMat4(v, m, out);
1405
+ }
1406
+ static transformV4(m, v, out) {
1407
+ out ??= new Vec4();
1408
+ const x = v[0], y = v[1], z = v[2], w = v[3];
1409
+ out[0] = m[0] * x + m[4] * y + m[8] * z + m[12] * w;
1410
+ out[1] = m[1] * x + m[5] * y + m[9] * z + m[13] * w;
1411
+ out[2] = m[2] * x + m[6] * y + m[10] * z + m[14] * w;
1412
+ out[3] = m[3] * x + m[7] * y + m[11] * z + m[15] * w;
1413
+ return out;
1414
+ }
1415
+ static equals(a, b, epsilon = EPSILON) {
1416
+ for (let i = 0; i < 16; i++) {
1417
+ if (Math.abs(a[i] - b[i]) > epsilon)
1418
+ return false;
1419
+ }
1420
+ return true;
1421
+ }
1422
+ static exactEquals(a, b) {
1423
+ for (let i = 0; i < 16; i++) {
1424
+ if (a[i] !== b[i])
1425
+ return false;
1426
+ }
1427
+ return true;
1428
+ }
1429
+ /**
1430
+ * Batch transform an array of 3D positions with perspective divide.
1431
+ */
1432
+ static transformPositions(m, src, dst, count, srcStride = 3, dstStride = 3) {
1433
+ Vec3.transformPositions(m, src, dst, count, srcStride, dstStride);
1434
+ }
1435
+ /**
1436
+ * Batch transform an array of 3D direction vectors (ignoring translation).
1437
+ */
1438
+ static transformVectors(m, src, dst, count, srcStride = 3, dstStride = 3) {
1439
+ Vec3.transformDirections(m, src, dst, count, srcStride, dstStride);
1440
+ }
1441
+ }
1442
+ const _Mat4Wrapper = function (a, b) {
1443
+ return new _Mat4(a, b);
1444
+ };
1445
+ Object.setPrototypeOf(_Mat4Wrapper, _Mat4);
1446
+ _Mat4Wrapper.prototype = _Mat4.prototype;
1447
+ export const Mat4 = _Mat4Wrapper;
1448
+ //# sourceMappingURL=mat4.js.map