matrix-engine-wgpu 1.3.16 → 1.3.18

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 (273) hide show
  1. package/.codesandbox/tasks.json +46 -0
  2. package/.devcontainer/devcontainer.json +22 -0
  3. package/.github/dependabot.yml +12 -0
  4. package/REFERENCE.md +59 -0
  5. package/app-worker.js +45 -0
  6. package/dev.md +541 -0
  7. package/empty.js +17 -0
  8. package/examples/camera-texture.js +60 -0
  9. package/examples/games/jamb/html-content.js +128 -0
  10. package/examples/games/jamb/jamb.js +1341 -0
  11. package/examples/games/jamb/readme.md +3 -0
  12. package/examples/load-jamb.js +6 -0
  13. package/examples/load-obj-file.js +95 -0
  14. package/examples/load-objs-sequence.js +68 -0
  15. package/examples/unlit-textures.js +31 -0
  16. package/examples/video-texture.js +61 -0
  17. package/examples.js +73 -0
  18. package/main.js +635 -0
  19. package/non-project-files/cubebuffer-example.js +51 -0
  20. package/non-project-files/dev.txt +21 -0
  21. package/non-project-files/image1.png +0 -0
  22. package/non-project-files/image6.png +0 -0
  23. package/package.json +2 -5
  24. package/public/ammojs/ammo.js +957 -0
  25. package/public/ammojs/ammo.wasm.js +921 -0
  26. package/public/ammojs/ammo.wasm.wasm +0 -0
  27. package/public/app-worker.js +47 -0
  28. package/public/app.js +12744 -0
  29. package/public/css/style.css +711 -0
  30. package/public/empty.html +25 -0
  31. package/public/empty.js +10453 -0
  32. package/public/examples.html +30 -0
  33. package/public/examples.js +11217 -0
  34. package/public/index.html +20 -0
  35. package/public/manifest copy.web +35 -0
  36. package/public/manifest.web +25 -0
  37. package/public/res/audios/block.mp3 +0 -0
  38. package/public/res/audios/dice-roll.mp3 +0 -0
  39. package/public/res/audios/dice1.mp3 +0 -0
  40. package/public/res/audios/dice2.mp3 +0 -0
  41. package/public/res/audios/kenney/Kenney.url +2 -0
  42. package/public/res/audios/kenney/License.txt +22 -0
  43. package/public/res/audios/kenney/Patreon.url +2 -0
  44. package/public/res/audios/kenney/audios/back_001.ogg +0 -0
  45. package/public/res/audios/kenney/audios/back_002.ogg +0 -0
  46. package/public/res/audios/kenney/audios/back_003.ogg +0 -0
  47. package/public/res/audios/kenney/audios/back_004.ogg +0 -0
  48. package/public/res/audios/kenney/audios/bong_001.ogg +0 -0
  49. package/public/res/audios/kenney/audios/click_001.ogg +0 -0
  50. package/public/res/audios/kenney/audios/click_002.ogg +0 -0
  51. package/public/res/audios/kenney/audios/click_003.ogg +0 -0
  52. package/public/res/audios/kenney/audios/click_004.ogg +0 -0
  53. package/public/res/audios/kenney/audios/click_005.ogg +0 -0
  54. package/public/res/audios/kenney/audios/close_001.ogg +0 -0
  55. package/public/res/audios/kenney/audios/close_002.ogg +0 -0
  56. package/public/res/audios/kenney/audios/close_003.ogg +0 -0
  57. package/public/res/audios/kenney/audios/close_004.ogg +0 -0
  58. package/public/res/audios/kenney/audios/confirmation_001.ogg +0 -0
  59. package/public/res/audios/kenney/audios/confirmation_002.ogg +0 -0
  60. package/public/res/audios/kenney/audios/confirmation_003.ogg +0 -0
  61. package/public/res/audios/kenney/audios/confirmation_004.ogg +0 -0
  62. package/public/res/audios/kenney/audios/drop_001.ogg +0 -0
  63. package/public/res/audios/kenney/audios/drop_002.ogg +0 -0
  64. package/public/res/audios/kenney/audios/drop_003.ogg +0 -0
  65. package/public/res/audios/kenney/audios/drop_004.ogg +0 -0
  66. package/public/res/audios/kenney/audios/error_001.ogg +0 -0
  67. package/public/res/audios/kenney/audios/error_002.ogg +0 -0
  68. package/public/res/audios/kenney/audios/error_003.ogg +0 -0
  69. package/public/res/audios/kenney/audios/error_004.ogg +0 -0
  70. package/public/res/audios/kenney/audios/error_005.ogg +0 -0
  71. package/public/res/audios/kenney/audios/error_006.ogg +0 -0
  72. package/public/res/audios/kenney/audios/error_007.ogg +0 -0
  73. package/public/res/audios/kenney/audios/error_008.ogg +0 -0
  74. package/public/res/audios/kenney/audios/glass_001.ogg +0 -0
  75. package/public/res/audios/kenney/audios/glass_002.ogg +0 -0
  76. package/public/res/audios/kenney/audios/glass_003.ogg +0 -0
  77. package/public/res/audios/kenney/audios/glass_004.ogg +0 -0
  78. package/public/res/audios/kenney/audios/glass_005.ogg +0 -0
  79. package/public/res/audios/kenney/audios/glass_006.ogg +0 -0
  80. package/public/res/audios/kenney/audios/glitch_001.ogg +0 -0
  81. package/public/res/audios/kenney/audios/glitch_002.ogg +0 -0
  82. package/public/res/audios/kenney/audios/glitch_003.ogg +0 -0
  83. package/public/res/audios/kenney/audios/glitch_004.ogg +0 -0
  84. package/public/res/audios/kenney/audios/maximize_001.ogg +0 -0
  85. package/public/res/audios/kenney/audios/maximize_002.ogg +0 -0
  86. package/public/res/audios/kenney/audios/maximize_003.ogg +0 -0
  87. package/public/res/audios/kenney/audios/maximize_004.ogg +0 -0
  88. package/public/res/audios/kenney/audios/maximize_005.ogg +0 -0
  89. package/public/res/audios/kenney/audios/maximize_006.ogg +0 -0
  90. package/public/res/audios/kenney/audios/maximize_007.ogg +0 -0
  91. package/public/res/audios/kenney/audios/maximize_008.ogg +0 -0
  92. package/public/res/audios/kenney/audios/maximize_009.ogg +0 -0
  93. package/public/res/audios/kenney/audios/minimize_001.ogg +0 -0
  94. package/public/res/audios/kenney/audios/minimize_002.ogg +0 -0
  95. package/public/res/audios/kenney/audios/minimize_003.ogg +0 -0
  96. package/public/res/audios/kenney/audios/minimize_004.ogg +0 -0
  97. package/public/res/audios/kenney/audios/minimize_005.ogg +0 -0
  98. package/public/res/audios/kenney/audios/minimize_006.ogg +0 -0
  99. package/public/res/audios/kenney/audios/minimize_007.ogg +0 -0
  100. package/public/res/audios/kenney/audios/minimize_008.ogg +0 -0
  101. package/public/res/audios/kenney/audios/minimize_009.ogg +0 -0
  102. package/public/res/audios/kenney/audios/open_001.ogg +0 -0
  103. package/public/res/audios/kenney/audios/open_002.ogg +0 -0
  104. package/public/res/audios/kenney/audios/open_003.ogg +0 -0
  105. package/public/res/audios/kenney/audios/open_004.ogg +0 -0
  106. package/public/res/audios/kenney/audios/pluck_001.ogg +0 -0
  107. package/public/res/audios/kenney/audios/pluck_002.ogg +0 -0
  108. package/public/res/audios/kenney/audios/question_001.ogg +0 -0
  109. package/public/res/audios/kenney/audios/question_002.ogg +0 -0
  110. package/public/res/audios/kenney/audios/question_003.ogg +0 -0
  111. package/public/res/audios/kenney/audios/question_004.ogg +0 -0
  112. package/public/res/audios/kenney/audios/scratch_001.ogg +0 -0
  113. package/public/res/audios/kenney/audios/scratch_002.ogg +0 -0
  114. package/public/res/audios/kenney/audios/scratch_003.ogg +0 -0
  115. package/public/res/audios/kenney/audios/scratch_004.ogg +0 -0
  116. package/public/res/audios/kenney/audios/scratch_005.ogg +0 -0
  117. package/public/res/audios/kenney/audios/scroll_001.ogg +0 -0
  118. package/public/res/audios/kenney/audios/scroll_002.ogg +0 -0
  119. package/public/res/audios/kenney/audios/scroll_003.ogg +0 -0
  120. package/public/res/audios/kenney/audios/scroll_004.ogg +0 -0
  121. package/public/res/audios/kenney/audios/scroll_005.ogg +0 -0
  122. package/public/res/audios/kenney/audios/select_001.ogg +0 -0
  123. package/public/res/audios/kenney/audios/select_002.ogg +0 -0
  124. package/public/res/audios/kenney/audios/select_003.ogg +0 -0
  125. package/public/res/audios/kenney/audios/select_004.ogg +0 -0
  126. package/public/res/audios/kenney/audios/select_005.ogg +0 -0
  127. package/public/res/audios/kenney/audios/select_006.ogg +0 -0
  128. package/public/res/audios/kenney/audios/select_007.ogg +0 -0
  129. package/public/res/audios/kenney/audios/select_008.ogg +0 -0
  130. package/public/res/audios/kenney/audios/switch_001.ogg +0 -0
  131. package/public/res/audios/kenney/audios/switch_002.ogg +0 -0
  132. package/public/res/audios/kenney/audios/switch_003.ogg +0 -0
  133. package/public/res/audios/kenney/audios/switch_004.ogg +0 -0
  134. package/public/res/audios/kenney/audios/switch_005.ogg +0 -0
  135. package/public/res/audios/kenney/audios/switch_006.ogg +0 -0
  136. package/public/res/audios/kenney/audios/switch_007.ogg +0 -0
  137. package/public/res/audios/kenney/audios/tick_001.ogg +0 -0
  138. package/public/res/audios/kenney/audios/tick_002.ogg +0 -0
  139. package/public/res/audios/kenney/audios/tick_004.ogg +0 -0
  140. package/public/res/audios/kenney/audios/toggle_001.ogg +0 -0
  141. package/public/res/audios/kenney/audios/toggle_002.ogg +0 -0
  142. package/public/res/audios/kenney/audios/toggle_003.ogg +0 -0
  143. package/public/res/audios/kenney/audios/toggle_004.ogg +0 -0
  144. package/public/res/audios/start.mp3 +0 -0
  145. package/public/res/audios/toggle_002.mp3 +0 -0
  146. package/public/res/fonts/Accuratist.ttf +0 -0
  147. package/public/res/fonts/Closeness.ttf +0 -0
  148. package/public/res/fonts/WARGAMES.TTF +0 -0
  149. package/public/res/fonts/readme.txt +5 -0
  150. package/public/res/fonts/stormfaze.ttf +0 -0
  151. package/public/res/icons/512.png +0 -0
  152. package/public/res/icons/webgpu-horizontal.svg +45 -0
  153. package/public/res/meshes/blender/cube.blend +0 -0
  154. package/public/res/meshes/blender/cube.blend1 +0 -0
  155. package/public/res/meshes/blender/cube.mtl +12 -0
  156. package/public/res/meshes/blender/cube.obj +46 -0
  157. package/public/res/meshes/blender/cube.png +0 -0
  158. package/public/res/meshes/blender/cubeSmartUV.blend +0 -0
  159. package/public/res/meshes/blender/cubeSmartUV.mtl +12 -0
  160. package/public/res/meshes/blender/cubeSmartUV.obj +46 -0
  161. package/public/res/meshes/blender/lopta.mtl +10 -0
  162. package/public/res/meshes/blender/lopta.obj +3402 -0
  163. package/public/res/meshes/blender/piramyd.blend +0 -0
  164. package/public/res/meshes/blender/piramyd.blend1 +0 -0
  165. package/public/res/meshes/blender/piramyd.js +42 -0
  166. package/public/res/meshes/blender/piramyd.mtl +10 -0
  167. package/public/res/meshes/blender/piramyd.obj +18696 -0
  168. package/public/res/meshes/blender/piramyd1.js +42 -0
  169. package/public/res/meshes/blender/sphepe.blend +0 -0
  170. package/public/res/meshes/blender/sphepe.blend1 +0 -0
  171. package/public/res/meshes/blender/sphere.mtl +10 -0
  172. package/public/res/meshes/blender/sphere.obj +3402 -0
  173. package/public/res/meshes/blender/welcomeTextblend.blend +0 -0
  174. package/public/res/meshes/dragon/stanfordDragonData.js +5 -0
  175. package/public/res/meshes/jamb/bg.blend +0 -0
  176. package/public/res/meshes/jamb/bg.blend1 +0 -0
  177. package/public/res/meshes/jamb/bg.mtl +12 -0
  178. package/public/res/meshes/jamb/bg.obj +17 -0
  179. package/public/res/meshes/jamb/bg.png +0 -0
  180. package/public/res/meshes/jamb/dice-default.png +0 -0
  181. package/public/res/meshes/jamb/dice-mark.png +0 -0
  182. package/public/res/meshes/jamb/dice.mtl +12 -0
  183. package/public/res/meshes/jamb/dice.obj +40 -0
  184. package/public/res/meshes/jamb/dice.png +0 -0
  185. package/public/res/meshes/jamb/jamb-title.mtl +12 -0
  186. package/public/res/meshes/jamb/jamb-title.obj +26008 -0
  187. package/public/res/meshes/jamb/jamb.blend +0 -0
  188. package/public/res/meshes/jamb/jamb.blend1 +0 -0
  189. package/public/res/meshes/jamb/logo.png +0 -0
  190. package/public/res/meshes/jamb/nidzaDice.blend +0 -0
  191. package/public/res/meshes/jamb/nidzaDice.blend1 +0 -0
  192. package/public/res/meshes/jamb/pile.blend +0 -0
  193. package/public/res/meshes/jamb/simpleCube.blend +0 -0
  194. package/public/res/meshes/jamb/simpleCube.blend1 +0 -0
  195. package/public/res/meshes/jamb/sounds/roll1.wav +0 -0
  196. package/public/res/meshes/jamb/text.png +0 -0
  197. package/public/res/meshes/obj/armor.obj +319 -0
  198. package/public/res/meshes/obj/armor.png +0 -0
  199. package/public/res/meshes/objs-sequence/swat-walk-pistol_000001.mtl +22 -0
  200. package/public/res/meshes/objs-sequence/swat-walk-pistol_000001.obj +23264 -0
  201. package/public/res/meshes/objs-sequence/swat-walk-pistol_000002.mtl +22 -0
  202. package/public/res/meshes/objs-sequence/swat-walk-pistol_000002.obj +23261 -0
  203. package/public/res/meshes/objs-sequence/swat-walk-pistol_000003.mtl +22 -0
  204. package/public/res/meshes/objs-sequence/swat-walk-pistol_000003.obj +23264 -0
  205. package/public/res/meshes/objs-sequence/swat-walk-pistol_000004.mtl +22 -0
  206. package/public/res/meshes/objs-sequence/swat-walk-pistol_000004.obj +23261 -0
  207. package/public/res/meshes/objs-sequence/swat-walk-pistol_000005.mtl +22 -0
  208. package/public/res/meshes/objs-sequence/swat-walk-pistol_000005.obj +23261 -0
  209. package/public/res/meshes/objs-sequence/swat-walk-pistol_000006.mtl +22 -0
  210. package/public/res/meshes/objs-sequence/swat-walk-pistol_000006.obj +23261 -0
  211. package/public/res/meshes/objs-sequence/swat-walk-pistol_000007.mtl +22 -0
  212. package/public/res/meshes/objs-sequence/swat-walk-pistol_000007.obj +23264 -0
  213. package/public/res/meshes/objs-sequence/swat-walk-pistol_000008.mtl +22 -0
  214. package/public/res/meshes/objs-sequence/swat-walk-pistol_000008.obj +23263 -0
  215. package/public/res/meshes/objs-sequence/swat-walk-pistol_000009.mtl +22 -0
  216. package/public/res/meshes/objs-sequence/swat-walk-pistol_000009.obj +23264 -0
  217. package/public/res/meshes/objs-sequence/swat-walk-pistol_000010.mtl +22 -0
  218. package/public/res/meshes/objs-sequence/swat-walk-pistol_000010.obj +23260 -0
  219. package/public/res/meshes/objs-sequence/swat-walk-pistol_000011.mtl +22 -0
  220. package/public/res/meshes/objs-sequence/swat-walk-pistol_000011.obj +23262 -0
  221. package/public/res/meshes/objs-sequence/swat-walk-pistol_000012.mtl +22 -0
  222. package/public/res/meshes/objs-sequence/swat-walk-pistol_000012.obj +23262 -0
  223. package/public/res/meshes/objs-sequence/swat-walk-pistol_000013.mtl +22 -0
  224. package/public/res/meshes/objs-sequence/swat-walk-pistol_000013.obj +23263 -0
  225. package/public/res/meshes/objs-sequence/swat-walk-pistol_000014.mtl +22 -0
  226. package/public/res/meshes/objs-sequence/swat-walk-pistol_000014.obj +23262 -0
  227. package/public/res/meshes/objs-sequence/swat-walk-pistol_000015.mtl +22 -0
  228. package/public/res/meshes/objs-sequence/swat-walk-pistol_000015.obj +23263 -0
  229. package/public/res/meshes/objs-sequence/swat-walk-pistol_000016.mtl +22 -0
  230. package/public/res/meshes/objs-sequence/swat-walk-pistol_000016.obj +23264 -0
  231. package/public/res/meshes/objs-sequence/swat-walk-pistol_000017.mtl +22 -0
  232. package/public/res/meshes/objs-sequence/swat-walk-pistol_000017.obj +23263 -0
  233. package/public/res/meshes/objs-sequence/swat-walk-pistol_000018.mtl +22 -0
  234. package/public/res/meshes/objs-sequence/swat-walk-pistol_000018.obj +23261 -0
  235. package/public/res/meshes/objs-sequence/swat-walk-pistol_000019.mtl +22 -0
  236. package/public/res/meshes/objs-sequence/swat-walk-pistol_000019.obj +23263 -0
  237. package/public/res/meshes/objs-sequence/swat-walk-pistol_000020.mtl +22 -0
  238. package/public/res/meshes/objs-sequence/swat-walk-pistol_000020.obj +23261 -0
  239. package/public/res/meshes/shapes/star1.obj +60 -0
  240. package/public/res/multilang/en.json +39 -0
  241. package/public/res/multilang/sr.json +39 -0
  242. package/public/res/textures/default.png +0 -0
  243. package/public/res/textures/rust.jpg +0 -0
  244. package/public/res/textures/tex1.jpg +0 -0
  245. package/public/res/videos/readme.txt +2 -0
  246. package/public/res/videos/tunel.mp4 +0 -0
  247. package/public/test.html +636 -0
  248. package/public/three-test.js +165 -0
  249. package/public/worker.html +25 -0
  250. package/src/engine/ball.js +482 -0
  251. package/src/engine/cube.js +496 -0
  252. package/src/engine/engine.js +404 -0
  253. package/src/engine/final/adaptJSON1.js +53 -0
  254. package/src/engine/final/utils2.js +63 -0
  255. package/src/engine/lights.js +153 -0
  256. package/src/engine/loader-obj.js +473 -0
  257. package/src/engine/materials.js +295 -0
  258. package/src/engine/matrix-class.js +252 -0
  259. package/src/engine/mesh-obj.js +574 -0
  260. package/src/engine/raycast.js +218 -0
  261. package/src/engine/utils.js +881 -0
  262. package/src/libs/mat.js +0 -0
  263. package/src/multilang/lang.js +35 -0
  264. package/src/physics/matrix-ammo.js +363 -0
  265. package/src/shaders/fragment.video.wgsl.js +83 -0
  266. package/src/shaders/fragment.wgsl.js +75 -0
  267. package/src/shaders/shaders.js +51 -0
  268. package/src/shaders/standard-matrix-engine-shaders/standard-matrix-engine-fs.glsl +56 -0
  269. package/src/shaders/standard-matrix-engine-shaders/standard-matrix-engine-vs.glsl +75 -0
  270. package/src/shaders/vertex.wgsl.js +54 -0
  271. package/src/shaders/vertexShadow.wgsl.js +20 -0
  272. package/src/sounds/sounds.js +69 -0
  273. package/src/world.js +474 -0
@@ -0,0 +1,295 @@
1
+ /**
2
+ * @description
3
+ * Created for matrix-engine-wgpu project.
4
+ * MeshObj class estends Materials.
5
+ * @author Nikola Lukic
6
+ * @email zlatnaspirala@gmail.com
7
+ */
8
+
9
+ export default class Materials {
10
+ constructor(device) {
11
+ this.device = device;
12
+ this.isVideo = false;
13
+ this.compareSampler = this.device.createSampler({compare: 'less'});
14
+ // For image textures (standard sampler)
15
+ this.imageSampler = this.device.createSampler({
16
+ magFilter: 'linear',
17
+ minFilter: 'linear',
18
+ });
19
+ // For external video textures (needs to be filtering sampler too!)
20
+ this.videoSampler = this.device.createSampler({
21
+ magFilter: 'linear',
22
+ minFilter: 'linear',
23
+ });
24
+
25
+ // FX effect
26
+ this.postFXModeBuffer = this.device.createBuffer({
27
+ size: 4, // u32 = 4 bytes
28
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
29
+ });
30
+
31
+ // Dymmy buffer
32
+ this.dummySpotlightUniformBuffer = this.device.createBuffer({
33
+ size: 64, // Must match size in shader
34
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
35
+ });
36
+ this.device.queue.writeBuffer(this.dummySpotlightUniformBuffer, 0, new Float32Array(16));
37
+ }
38
+
39
+ updatePostFXMode(mode) {
40
+ const arrayBuffer = new Uint32Array([mode]);
41
+ this.device.queue.writeBuffer(this.postFXModeBuffer, 0, arrayBuffer);
42
+ }
43
+
44
+ async loadTex0(texturesPaths) {
45
+ this.sampler = this.device.createSampler({
46
+ magFilter: 'linear',
47
+ minFilter: 'linear',
48
+ });
49
+ return new Promise(async (resolve) => {
50
+ const response = await fetch(texturesPaths[0]);
51
+ const imageBitmap = await createImageBitmap(await response.blob());
52
+ this.texture0 = this.device.createTexture({
53
+ size: [imageBitmap.width, imageBitmap.height, 1], // REMOVED 1
54
+ format: 'rgba8unorm',
55
+ usage:
56
+ GPUTextureUsage.TEXTURE_BINDING |
57
+ GPUTextureUsage.COPY_DST |
58
+ GPUTextureUsage.RENDER_ATTACHMENT,
59
+ });
60
+ this.device.queue.copyExternalImageToTexture(
61
+ {source: imageBitmap},
62
+ {texture: this.texture0},
63
+ [imageBitmap.width, imageBitmap.height]
64
+ );
65
+ resolve()
66
+ })
67
+ }
68
+
69
+ async loadVideoTexture(arg) {
70
+ this.isVideo = true;
71
+ if(arg.type === 'video') {
72
+ this.video = document.createElement('video');
73
+ this.video.src = arg.src || 'res/videos/tunel.mp4';
74
+ this.video.crossOrigin = 'anonymous';
75
+ this.video.autoplay = true;
76
+ this.video.loop = true;
77
+ document.body.append(this.video);
78
+ this.video.style.display = 'none';
79
+ await this.video.play();
80
+ } else if(arg.type === 'videoElement') {
81
+ this.video = arg.el;
82
+ await this.video.play();
83
+ } else if(arg.type === 'camera') {
84
+ this.video = document.createElement('video');
85
+ this.video.autoplay = true;
86
+ this.video.muted = true;
87
+ this.video.playsInline = true;
88
+ this.video.style.display = 'none';
89
+ document.body.append(this.video);
90
+ try {
91
+ const stream = await (navigator.mediaDevices?.getUserMedia?.({
92
+ video: {
93
+ width: {ideal: 1280},
94
+ height: {ideal: 720},
95
+ },
96
+ audio: false
97
+ }));
98
+
99
+ this.video.srcObject = stream;
100
+ await this.video.play();
101
+ } catch(err) {
102
+ console.error("❌ Failed to access camera:", err);
103
+ return;
104
+ }
105
+ } else if(arg.type === 'canvas2d') {
106
+ // Existing canvas (arg.el) — assume it's actively drawing
107
+ this.video = document.createElement('video');
108
+ this.video.autoplay = true;
109
+ this.video.muted = true;
110
+ this.video.playsInline = true;
111
+ this.video.style.display = 'none';
112
+ document.body.append(this.video);
113
+ const stream = arg.el.captureStream?.() || arg.el.mozCaptureStream?.();
114
+ if(!stream) {
115
+ console.error('❌ Cannot capture stream from canvas2d');
116
+ return;
117
+ }
118
+ this.video.srcObject = stream;
119
+ await this.video.play();
120
+ } else if(arg.type === 'canvas2d-inline') {
121
+ // Miniature inline-drawn canvas created dynamically
122
+ const canvas = document.createElement('canvas');
123
+ canvas.width = arg.width || 256;
124
+ canvas.height = arg.height || 256;
125
+ const ctx = canvas.getContext('2d');
126
+ if(typeof arg.canvaInlineProgram === 'function') {
127
+ // Start drawing loop
128
+ const drawLoop = () => {
129
+ arg.canvaInlineProgram(ctx, canvas);
130
+ requestAnimationFrame(drawLoop);
131
+ };
132
+ drawLoop();
133
+ }
134
+
135
+ this.video = document.createElement('video');
136
+ this.video.autoplay = true;
137
+ this.video.muted = true;
138
+ this.video.playsInline = true;
139
+ this.video.style.display = 'none';
140
+ document.body.append(this.video);
141
+ const stream = canvas.captureStream?.() || canvas.mozCaptureStream?.();
142
+ if(!stream) {
143
+ console.error('❌ Cannot capture stream from inline canvas');
144
+ return;
145
+ }
146
+ this.video.srcObject = stream;
147
+ await this.video.play();
148
+ }
149
+
150
+ this.sampler = this.device.createSampler({
151
+ magFilter: 'linear',
152
+ minFilter: 'linear',
153
+ });
154
+
155
+ // ✅ Now
156
+ this.createLayoutForRender();
157
+ this.setupPipeline();
158
+ setTimeout(() => this.createBindGroupForRender(), 1500);
159
+ }
160
+
161
+ updateVideoTexture() {
162
+ if(!this.video || this.video.readyState < 2) return;
163
+ this.externalTexture = this.device.importExternalTexture({source: this.video});
164
+ this.createBindGroupForRender();
165
+ }
166
+
167
+ createBindGroupForRender() {
168
+ const textureResource = this.isVideo
169
+ ? this.externalTexture // must be set via updateVideoTexture
170
+ : this.texture0.createView();
171
+ if(!textureResource || !this.sceneUniformBuffer || !this.shadowDepthTextureView || !this.sampler) {
172
+ console.warn("❗Missing res skipping...");
173
+ return;
174
+ }
175
+ // console.log('what is this.lightContainer.length ', this.lightContainer.length)
176
+ if(this.isVideo == true) {
177
+ this.sceneBindGroupForRender = this.device.createBindGroup({
178
+ layout: this.bglForRender,
179
+ entries: [
180
+ {
181
+ binding: 0,
182
+ resource: {buffer: this.sceneUniformBuffer},
183
+ },
184
+ {
185
+ binding: 1,
186
+ resource: this.shadowDepthTextureView,
187
+ },
188
+ {
189
+ binding: 2,
190
+ resource: this.compareSampler,
191
+ },
192
+ {
193
+ binding: 3,
194
+ resource: textureResource,
195
+ },
196
+ {
197
+ binding: 4,
198
+ resource: this.videoSampler,
199
+ },
200
+ {binding: 5, resource: {buffer: this.postFXModeBuffer}}
201
+ ],
202
+ });
203
+ } else {
204
+ this.sceneBindGroupForRender = this.device.createBindGroup({
205
+ layout: this.bglForRender,
206
+ entries: [
207
+ {
208
+ binding: 0,
209
+ resource: {buffer: this.sceneUniformBuffer},
210
+ },
211
+ {
212
+ binding: 1,
213
+ resource: this.shadowDepthTextureView,
214
+ },
215
+ {
216
+ binding: 2,
217
+ resource: this.compareSampler,
218
+ },
219
+ {
220
+ binding: 3,
221
+ resource: textureResource,
222
+ },
223
+ {
224
+ binding: 4,
225
+ resource: this.imageSampler,
226
+ },
227
+ {
228
+ binding: 5,
229
+ resource: {buffer: this.lightContainer.length == 0 ? this.dummySpotlightUniformBuffer : this.lightContainer[0].spotlightUniformBuffer},
230
+ },
231
+ ],
232
+ });
233
+ }
234
+ }
235
+
236
+ createLayoutForRender() {
237
+ this.bglForRender = this.device.createBindGroupLayout({
238
+ entries: [
239
+ {
240
+ binding: 0,
241
+ visibility: GPUShaderStage.VERTEX | GPUShaderStage.FRAGMENT,
242
+ buffer: {type: 'uniform'},
243
+ },
244
+ {
245
+ binding: 1,
246
+ visibility: GPUShaderStage.FRAGMENT,
247
+ texture: {sampleType: 'depth'},
248
+ },
249
+ {
250
+ binding: 2,
251
+ visibility: GPUShaderStage.FRAGMENT,
252
+ sampler: {type: 'comparison'},
253
+ },
254
+ ...(this.isVideo
255
+ ? [ // VIDEO
256
+ {
257
+ binding: 3,
258
+ visibility: GPUShaderStage.FRAGMENT,
259
+ externalTexture: {},
260
+ },
261
+ {
262
+ binding: 4,
263
+ visibility: GPUShaderStage.FRAGMENT,
264
+ sampler: {type: 'filtering'}, // for video sampling
265
+ },
266
+ {
267
+ binding: 5,
268
+ visibility: GPUShaderStage.FRAGMENT,
269
+ buffer: {type: 'uniform'},
270
+ }
271
+ ]
272
+ : [ // IMAGE
273
+ {
274
+ binding: 3,
275
+ visibility: GPUShaderStage.FRAGMENT,
276
+ texture: {
277
+ sampleType: 'float',
278
+ viewDimension: '2d',
279
+ },
280
+ },
281
+ {
282
+ binding: 4,
283
+ visibility: GPUShaderStage.FRAGMENT,
284
+ sampler: {type: 'filtering'},
285
+ },
286
+ {
287
+ binding: 5,
288
+ visibility: GPUShaderStage.FRAGMENT,
289
+ buffer: {type: 'uniform'},
290
+ }
291
+ ])
292
+ ],
293
+ });
294
+ }
295
+ }
@@ -0,0 +1,252 @@
1
+ import {degToRad, radToDeg} from "./utils";
2
+
3
+ /**
4
+ * @description
5
+ * Sub classes for matrix-wgpu
6
+ * Base class
7
+ * Position { x, y, z }
8
+ */
9
+
10
+ export class Position {
11
+ constructor(x, y, z) {
12
+ // console.log('TEST TYTPOF ', x)
13
+ // Not in use for nwo this is from matrix-engine project [nameUniq]
14
+ this.nameUniq = null;
15
+
16
+ if(typeof x == 'undefined') x = 0;
17
+ if(typeof y == 'undefined') y = 0;
18
+ if(typeof z == 'undefined') z = 0;
19
+
20
+ this.x = parseFloat(x);
21
+ this.y = parseFloat(y);
22
+ this.z = parseFloat(z);
23
+
24
+ this.velY = 0;
25
+ this.velX = 0;
26
+ this.velZ = 0;
27
+ this.inMove = false;
28
+ this.targetX = parseFloat(x);
29
+ this.targetY = parseFloat(y);
30
+ this.targetZ = parseFloat(z);
31
+ this.thrust = 0.01;
32
+
33
+ return this;
34
+ }
35
+
36
+ setSpeed(n) {
37
+ if(typeof n === 'number') {
38
+ this.thrust = n;
39
+ } else {
40
+ console.log('Description: arguments (w, h) must be type of number.');
41
+ }
42
+ }
43
+
44
+ translateByX(x) {
45
+ this.inMove = true;
46
+ this.targetX = parseFloat(x);
47
+ };
48
+
49
+ translateByY(y) {
50
+ this.inMove = true;
51
+ this.targetY = parseFloat(y);
52
+ }
53
+
54
+ translateByZ(z) {
55
+ this.inMove = true;
56
+ this.targetZ = parseFloat(z);
57
+ }
58
+
59
+ translateByXY(x, y) {
60
+ this.inMove = true;
61
+ this.targetX = parseFloat(x);
62
+ this.targetY = parseFloat(y);
63
+ }
64
+
65
+ translateByXZ(x, z) {
66
+ this.inMove = true;
67
+ this.targetX = parseFloat(x);
68
+ this.targetZ = parseFloat(z);
69
+ }
70
+
71
+ translateByYZ(y, z) {
72
+ this.inMove = true;
73
+ this.targetY = parseFloat(y);
74
+ this.targetZ = parseFloat(z);
75
+ }
76
+
77
+ onTargetPositionReach() {}
78
+
79
+ update() {
80
+ var tx = parseFloat(this.targetX) - parseFloat(this.x),
81
+ ty = parseFloat(this.targetY) - parseFloat(this.y),
82
+ tz = parseFloat(this.targetZ) - parseFloat(this.z),
83
+ dist = Math.sqrt(tx * tx + ty * ty + tz * tz);
84
+ this.velX = (tx / dist) * this.thrust;
85
+ this.velY = (ty / dist) * this.thrust;
86
+ this.velZ = (tz / dist) * this.thrust;
87
+ if(this.inMove == true) {
88
+ if(dist > this.thrust) {
89
+ this.x += this.velX;
90
+ this.y += this.velY;
91
+ this.z += this.velZ;
92
+
93
+ // // from me
94
+ // if(net && net.connection && typeof em === 'undefined' && App.scene[this.nameUniq].net.enable == true) net.connection.send({
95
+ // netPos: {x: this.x, y: this.y, z: this.z},
96
+ // netObjId: this.nameUniq,
97
+ // });
98
+
99
+ } else {
100
+ this.x = this.targetX;
101
+ this.y = this.targetY;
102
+ this.z = this.targetZ;
103
+ this.inMove = false;
104
+ this.onTargetPositionReach();
105
+
106
+ // // from me
107
+ // if(net && net.connection && typeof em === 'undefined' && App.scene[this.nameUniq].net.enable == true) net.connection.send({
108
+ // netPos: {x: this.x, y: this.y, z: this.z},
109
+ // netObjId: this.nameUniq,
110
+ // });
111
+ }
112
+ }
113
+ }
114
+
115
+ get worldLocation() {
116
+ return [parseFloat(this.x), parseFloat(this.y), parseFloat(this.z)];
117
+ }
118
+
119
+ SetX(newx, em) {
120
+ this.x = newx;
121
+ this.targetX = newx;
122
+ this.inMove = false;
123
+
124
+ // if(net && net.connection && typeof em === 'undefined' &&
125
+ // App.scene[this.nameUniq].net && App.scene[this.nameUniq].net.enable == true) {
126
+ // net.connection.send({
127
+ // netPos: {x: this.x, y: this.y, z: this.z},
128
+ // netObjId: this.nameUniq,
129
+ // });
130
+ // }
131
+ }
132
+
133
+ SetY(newy, em) {
134
+ this.y = newy;
135
+ this.targetY = newy;
136
+ this.inMove = false;
137
+ // if(net && net.connection && typeof em === 'undefined' &&
138
+ // App.scene[this.nameUniq].net && App.scene[this.nameUniq].net.enable == true) net.connection.send({
139
+ // netPos: {x: this.x, y: this.y, z: this.z},
140
+ // netObjId: this.nameUniq,
141
+ // });
142
+ }
143
+
144
+ SetZ(newz, em) {
145
+ this.z = newz;
146
+ this.targetZ = newz;
147
+ this.inMove = false;
148
+ // if(net && net.connection && typeof em === 'undefined' &&
149
+ // App.scene[this.nameUniq].net && App.scene[this.nameUniq].net.enable == true) net.connection.send({
150
+ // netPos: {x: this.x, y: this.y, z: this.z},
151
+ // netObjId: this.nameUniq,
152
+ // });
153
+ }
154
+
155
+ get X() {
156
+ return parseFloat(this.x)
157
+ }
158
+
159
+ get Y() {
160
+ return parseFloat(this.y)
161
+ }
162
+
163
+ get Z() {
164
+ return parseFloat(this.z)
165
+ }
166
+
167
+ setPosition(newx, newy, newz) {
168
+ this.x = newx;
169
+ this.y = newy;
170
+ this.z = newz;
171
+ this.targetX = newx;
172
+ this.targetY = newy;
173
+ this.targetZ = newz;
174
+ this.inMove = false;
175
+
176
+ // from me
177
+ // if(App.scene[this.nameUniq] && net && net.connection && typeof em === 'undefined' &&
178
+ // App.scene[this.nameUniq].net && App.scene[this.nameUniq].net.enable == true) net.connection.send({
179
+ // netPos: {x: this.x, y: this.y, z: this.z},
180
+ // netObjId: this.nameUniq,
181
+ // });
182
+ }
183
+ }
184
+
185
+ export class Rotation {
186
+
187
+ constructor(x, y, z) {
188
+ // Not in use for nwo this is from matrix-engine project [nameUniq]
189
+ this.nameUniq = null;
190
+ if(typeof x == 'undefined') x = 0;
191
+ if(typeof y == 'undefined') y = 0;
192
+ if(typeof z == 'undefined') z = 0;
193
+ this.x = x;
194
+ this.y = y;
195
+ this.z = z;
196
+ this.rotationSpeed = {x: 0, y: 0, z: 0};
197
+ this.angle = 0;
198
+ this.axis = {x: 0, y: 0, z: 0};
199
+ // not in use good for exstend logic
200
+ this.matrixRotation = null;
201
+ }
202
+
203
+ toDegree() {
204
+
205
+ /*
206
+ heading = atan2(y * sin(angle)- x * z * (1 - cos(angle)) , 1 - (y2 + z2 ) * (1 - cos(angle)))
207
+ attitude = asin(x * y * (1 - cos(angle)) + z * sin(angle))
208
+ bank = atan2(x * sin(angle)-y * z * (1 - cos(angle)) , 1 - (x2 + z2) * (1 - cos(angle)))
209
+ */
210
+ return [radToDeg(this.axis.x), radToDeg(this.axis.y), radToDeg(this.axis.z)];
211
+ }
212
+
213
+ toDegreeX() {
214
+ return Math.cos(radToDeg(this.axis.x) / 2)
215
+ }
216
+
217
+ toDegreeY() {
218
+ return Math.cos(radToDeg(this.axis.z) / 2)
219
+ }
220
+
221
+ toDegreeZ() {
222
+ return Math.cos(radToDeg(this.axis.y) / 2)
223
+ }
224
+
225
+ getRotX() {
226
+ if(this.rotationSpeed.x == 0) {
227
+ return degToRad(this.x);
228
+ } else {
229
+ this.x = this.x + this.rotationSpeed.x * 0.001;
230
+ return degToRad(this.x);
231
+ }
232
+ }
233
+
234
+ getRotY() {
235
+ if(this.rotationSpeed.y == 0) {
236
+ return degToRad(this.y);
237
+ } else {
238
+ this.y = this.y + this.rotationSpeed.y * 0.001;
239
+ return degToRad(this.y);
240
+ }
241
+ }
242
+
243
+ getRotZ() {
244
+ if(this.rotationSpeed.z == 0) {
245
+ return degToRad(this.z);
246
+ } else {
247
+ this.z = this.z + this.rotationSpeed.z * 0.001;
248
+ return degToRad(this.z);
249
+ }
250
+ }
251
+
252
+ }