matrix-engine-wgpu 1.3.17 → 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 +1 -4
  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,574 @@
1
+ import {mat4, vec3} from 'wgpu-matrix';
2
+ import {Position, Rotation} from "./matrix-class";
3
+ import {vertexShadowWGSL} from '../shaders/vertexShadow.wgsl';
4
+ import {fragmentWGSL} from '../shaders/fragment.wgsl';
5
+ import {vertexWGSL} from '../shaders/vertex.wgsl';
6
+ import {degToRad, genName, LOG_FUNNY_SMALL} from './utils';
7
+ import Materials from './materials';
8
+ import {fragmentVideoWGSL} from '../shaders/fragment.video.wgsl';
9
+
10
+ export default class MEMeshObj extends Materials {
11
+ constructor(canvas, device, context, o, sceneUniformBuffer) {
12
+ super(device);
13
+ if(typeof o.name === 'undefined') o.name = genName(9);
14
+ if(typeof o.raycast === 'undefined') {
15
+ this.raycast = {
16
+ enabled: false,
17
+ radius: 2
18
+ };
19
+ } else {
20
+ this.raycast = o.raycast;
21
+ }
22
+
23
+ this.name = o.name;
24
+ this.done = false;
25
+ this.device = device;
26
+ this.context = context;
27
+ this.entityArgPass = o.entityArgPass;
28
+ this.clearColor = "red";
29
+ this.video = null;
30
+
31
+ // Mesh stuff - for single mesh or t-posed (fiktive-first in loading order)
32
+ this.mesh = o.mesh;
33
+ this.mesh.uvs = this.mesh.textures;
34
+ console.log(`%c Mesh loaded: ${o.name}`, LOG_FUNNY_SMALL);
35
+
36
+ // ObjSequence animation
37
+ if(typeof o.objAnim !== 'undefined' && o.objAnim != null) {
38
+ this.objAnim = o.objAnim;
39
+ for(var key in this.objAnim.animations) {
40
+ if(key != 'active') this.objAnim.animations[key].speedCounter = 0;
41
+ }
42
+ console.log(`%c Mesh objAnim exist: ${o.objAnim}`, LOG_FUNNY_SMALL);
43
+ this.drawElements = this.drawElementsAnim;
44
+ }
45
+
46
+ this.inputHandler = null;
47
+ this.cameras = o.cameras;
48
+
49
+ this.mainCameraParams = {
50
+ type: o.mainCameraParams.type,
51
+ responseCoef: o.mainCameraParams.responseCoef
52
+ }
53
+
54
+ this.lastFrameMS = 0;
55
+ this.texturesPaths = [];
56
+ o.texturesPaths.forEach((t) => {this.texturesPaths.push(t)})
57
+ this.presentationFormat = navigator.gpu.getPreferredCanvasFormat();
58
+ this.position = new Position(o.position.x, o.position.y, o.position.z);
59
+ this.rotation = new Rotation(o.rotation.x, o.rotation.y, o.rotation.z);
60
+ this.rotation.rotationSpeed.x = o.rotationSpeed.x;
61
+ this.rotation.rotationSpeed.y = o.rotationSpeed.y;
62
+ this.rotation.rotationSpeed.z = o.rotationSpeed.z;
63
+ this.scale = o.scale;
64
+
65
+ this.runProgram = () => {
66
+ return new Promise(async (resolve) => {
67
+ this.shadowDepthTextureSize = 1024;
68
+ // const aspect = canvas.width / canvas.height;
69
+ this.modelViewProjectionMatrix = mat4.create();
70
+ this.loadTex0(this.texturesPaths).then(() => {
71
+ resolve()
72
+ })
73
+ })
74
+ }
75
+
76
+ this.runProgram().then(() => {
77
+ // const aspect = canvas.width / canvas.height;
78
+ this.context.configure({
79
+ device: this.device,
80
+ format: this.presentationFormat,
81
+ alphaMode: 'premultiplied',
82
+ });
83
+
84
+ // Create the model vertex buffer.
85
+ this.vertexBuffer = this.device.createBuffer({
86
+ size: this.mesh.vertices.length * Float32Array.BYTES_PER_ELEMENT,
87
+ usage: GPUBufferUsage.VERTEX,
88
+ mappedAtCreation: true,
89
+ });
90
+ {
91
+ // const mapping = new Float32Array(this.vertexBuffer.getMappedRange());
92
+ // // for(let i = 0;i < this.mesh.vertices.length;++i) {
93
+ // // mapping.set(this.mesh.vertices[i], 6 * i);
94
+ // // mapping.set(this.mesh.normals[i], 6 * i + 3);
95
+ // // }
96
+ // this.vertexBuffer.unmap();
97
+ new Float32Array(this.vertexBuffer.getMappedRange()).set(this.mesh.vertices);
98
+ this.vertexBuffer.unmap();
99
+ }
100
+
101
+ // Create the model vertex buffer.
102
+ this.vertexNormalsBuffer = this.device.createBuffer({
103
+ size: this.mesh.vertexNormals.length * Float32Array.BYTES_PER_ELEMENT,
104
+ usage: GPUBufferUsage.VERTEX,
105
+ mappedAtCreation: true,
106
+ });
107
+ {
108
+ new Float32Array(this.vertexNormalsBuffer.getMappedRange()).set(this.mesh.vertexNormals);
109
+ this.vertexNormalsBuffer.unmap();
110
+ }
111
+
112
+ this.vertexTexCoordsBuffer = this.device.createBuffer({
113
+ size: this.mesh.textures.length * Float32Array.BYTES_PER_ELEMENT,
114
+ usage: GPUBufferUsage.VERTEX,
115
+ mappedAtCreation: true,
116
+ });
117
+ {
118
+ new Float32Array(this.vertexTexCoordsBuffer.getMappedRange()).set(this.mesh.textures);
119
+ this.vertexTexCoordsBuffer.unmap();
120
+ }
121
+
122
+ // Create the model index buffer.
123
+ this.indexCount = this.mesh.indices.length;
124
+ const indexCount = this.mesh.indices.length;
125
+ const size = Math.ceil(indexCount * Uint16Array.BYTES_PER_ELEMENT / 4) * 4;
126
+
127
+ this.indexBuffer = this.device.createBuffer({
128
+ size,
129
+ usage: GPUBufferUsage.INDEX | GPUBufferUsage.COPY_DST,
130
+ mappedAtCreation: true
131
+ });
132
+
133
+ new Uint16Array(this.indexBuffer.getMappedRange()).set(this.mesh.indices);
134
+ this.indexBuffer.unmap();
135
+ this.indexCount = indexCount;
136
+
137
+ // Create the depth texture for rendering/sampling the shadow map.
138
+ this.shadowDepthTexture = this.device.createTexture({
139
+ size: [this.shadowDepthTextureSize, this.shadowDepthTextureSize, 1],
140
+ usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING,
141
+ format: 'depth32float',
142
+ });
143
+ this.shadowDepthTextureView = this.shadowDepthTexture.createView();
144
+
145
+ // Create some common descriptors used for both the shadow pipeline
146
+ // and the color rendering pipeline.
147
+ this.vertexBuffers = [
148
+ {
149
+ arrayStride: Float32Array.BYTES_PER_ELEMENT * 3,
150
+ attributes: [
151
+ {
152
+ // position
153
+ shaderLocation: 0,
154
+ offset: 0,
155
+ format: "float32x3",
156
+ }
157
+ ],
158
+ },
159
+ {
160
+ arrayStride: Float32Array.BYTES_PER_ELEMENT * 3,
161
+ attributes: [
162
+ {
163
+ // normal
164
+ shaderLocation: 1,
165
+ offset: 0,
166
+ format: "float32x3",
167
+ },
168
+ ],
169
+ },
170
+ {
171
+ arrayStride: Float32Array.BYTES_PER_ELEMENT * 2,
172
+ attributes: [
173
+ {
174
+ // uvs
175
+ shaderLocation: 2,
176
+ offset: 0,
177
+ format: "float32x2",
178
+ },
179
+ ],
180
+ },
181
+ ];
182
+
183
+ this.primitive = {
184
+ topology: 'triangle-list',
185
+ // cullMode: 'back',
186
+ cullMode: 'none',
187
+ };
188
+
189
+ this.uniformBufferBindGroupLayout = this.device.createBindGroupLayout({
190
+ entries: [
191
+ {
192
+ binding: 0,
193
+ visibility: GPUShaderStage.VERTEX,
194
+ buffer: {
195
+ type: 'uniform',
196
+ },
197
+ },
198
+ ],
199
+ });
200
+
201
+ this.shadowPipeline = this.device.createRenderPipeline({
202
+ layout: this.device.createPipelineLayout({
203
+ bindGroupLayouts: [
204
+ this.uniformBufferBindGroupLayout,
205
+ this.uniformBufferBindGroupLayout,
206
+ ],
207
+ }),
208
+ vertex: {
209
+ module: this.device.createShaderModule({
210
+ code: vertexShadowWGSL,
211
+ }),
212
+ buffers: this.vertexBuffers,
213
+ },
214
+ depthStencil: {
215
+ depthWriteEnabled: true,
216
+ depthCompare: 'less',
217
+ format: 'depth32float',
218
+ },
219
+ primitive: this.primitive,
220
+ });
221
+
222
+ // Create a bind group layout which holds the scene uniforms and
223
+ // the texture+sampler for depth. We create it manually because the WebPU
224
+ // implementation doesn't infer this from the shader (yet).
225
+ this.createLayoutForRender();
226
+ this.setupPipeline();
227
+
228
+ const depthTexture = this.device.createTexture({
229
+ size: [canvas.width, canvas.height],
230
+ // format: 'depth24plus-stencil8',
231
+ format: 'depth24plus',
232
+ usage: GPUTextureUsage.RENDER_ATTACHMENT,
233
+ });
234
+
235
+ this.renderPassDescriptor = {
236
+ colorAttachments: [
237
+ {
238
+ // view is acquired and set in render loop.
239
+ view: undefined,
240
+ clearValue: this.clearColor,
241
+ loadOp: 'clear', // load -> clear = fix for FF
242
+ storeOp: 'store',
243
+ },
244
+ ],
245
+ depthStencilAttachment: {
246
+ view: depthTexture.createView(),
247
+ depthClearValue: 1.0,
248
+ depthLoadOp: 'clear',
249
+ depthStoreOp: 'store',
250
+ // stencilClearValue: 0,
251
+ // stencilLoadOp: 'clear',
252
+ // stencilStoreOp: 'store',
253
+ },
254
+ };
255
+
256
+ this.modelUniformBuffer = this.device.createBuffer({
257
+ size: 4 * 16, // 4x4 matrix
258
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
259
+ });
260
+
261
+ this.sceneUniformBuffer = this.device.createBuffer({
262
+ // Two 4x4 viewProj matrices,
263
+ // one for the camera and one for the light.
264
+ // Then a vec3 for the light position.
265
+ // Rounded to the nearest multiple of 16.
266
+ size: 2 * 4 * 16 + 4 * 4,
267
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
268
+ });
269
+
270
+ console.log('test buffer sceneUniformBuffer ', this.sceneUniformBuffer);
271
+
272
+ this.sceneBindGroupForShadow = this.device.createBindGroup({
273
+ layout: this.uniformBufferBindGroupLayout,
274
+ entries: [
275
+ {
276
+ binding: 0,
277
+ resource: {
278
+ buffer: this.sceneUniformBuffer,
279
+ },
280
+ },
281
+ ],
282
+ });
283
+
284
+ this.createBindGroupForRender();
285
+
286
+ this.modelBindGroup = this.device.createBindGroup({
287
+ layout: this.uniformBufferBindGroupLayout,
288
+ entries: [
289
+ {
290
+ binding: 0,
291
+ resource: {
292
+ buffer: this.modelUniformBuffer,
293
+ },
294
+ },
295
+ ],
296
+ });
297
+
298
+ // Rotates the camera around the origin based on time.
299
+ this.getTransformationMatrix = (pos) => {
300
+ const now = Date.now();
301
+ const deltaTime = (now - this.lastFrameMS) / this.mainCameraParams.responseCoef;
302
+ this.lastFrameMS = now;
303
+ // const this.viewMatrix = mat4.identity()
304
+ const camera = this.cameras[this.mainCameraParams.type];
305
+
306
+ // engine frame
307
+ camera.update(dt, inputHandler());
308
+ const camVP = mat4.multiply(camera.projectionMatrix, camera.view);
309
+
310
+ for(const mesh of mainRenderBundle) {
311
+ // Light’s viewProj should come from your SpotLight
312
+ // If you have multiple lights, you’ll need an array UBO or multiple passes.
313
+ const sceneData = new Float32Array(16 + 16 + 4); // lightVP, camVP, lightPos(+pad)
314
+ sceneData.set(spotLight.viewProjMatrix, 0);
315
+ sceneData.set(camVP, 16);
316
+ sceneData.set(spotLight.position, 32);
317
+
318
+ device.queue.writeBuffer(
319
+ mesh.sceneUniformBuffer, // or a shared one if/when you centralize it
320
+ 0,
321
+ sceneData.buffer,
322
+ sceneData.byteOffset,
323
+ sceneData.byteLength
324
+ );
325
+ }
326
+ // this.viewMatrix = camera.update(deltaTime, this.inputHandler());
327
+ // const scaleVec = [1, 1, 1]; // your desired scale OPTION 1
328
+ // const scaleMatrix = mat4.scaling(scaleVec);
329
+ // // Apply scaling
330
+ // mat4.multiply(scaleMatrix, this.viewMatrix, this.viewMatrix);
331
+ // mat4.translate(this.viewMatrix, vec3.fromValues(pos.x, pos.y, pos.z), this.viewMatrix);
332
+
333
+ // if(this.itIsPhysicsBody == true) {
334
+ // mat4.rotate(
335
+ // this.viewMatrix,
336
+ // vec3.fromValues(this.rotation.axis.x, this.rotation.axis.y, this.rotation.axis.z),
337
+ // degToRad(this.rotation.angle), this.viewMatrix)
338
+ // } else {
339
+ // mat4.rotateX(this.viewMatrix, Math.PI * this.rotation.getRotX(), this.viewMatrix);
340
+ // mat4.rotateY(this.viewMatrix, Math.PI * this.rotation.getRotY(), this.viewMatrix);
341
+ // mat4.rotateZ(this.viewMatrix, Math.PI * this.rotation.getRotZ(), this.viewMatrix);
342
+ // // console.info('NOT PHYSICS angle: ', this.rotation.angle, ' axis ', this.rotation.axis.x, ' , ', this.rotation.axis.y, ' , ', this.rotation.axis.z)
343
+ // }
344
+ // mat4.multiply(camera.projectionMatrix, this.viewMatrix, this.modelViewProjectionMatrix);
345
+ // return this.modelViewProjectionMatrix;
346
+ }
347
+
348
+ this.getModelMatrix = (pos) => {
349
+ let modelMatrix = mat4.identity();
350
+ mat4.translate(modelMatrix, [pos.x, pos.y, pos.z], modelMatrix);
351
+ if(this.itIsPhysicsBody) {
352
+ mat4.rotate(modelMatrix,
353
+ [this.rotation.axis.x, this.rotation.axis.y, this.rotation.axis.z],
354
+ degToRad(this.rotation.angle),
355
+ modelMatrix
356
+ );
357
+ } else {
358
+ mat4.rotateX(modelMatrix, this.rotation.getRotX(), modelMatrix);
359
+ mat4.rotateY(modelMatrix, this.rotation.getRotY(), modelMatrix);
360
+ mat4.rotateZ(modelMatrix, this.rotation.getRotZ(), modelMatrix);
361
+ }
362
+ // Apply scale if you have it, e.g.:
363
+ // mat4.scale(modelMatrix, modelMatrix, [this.scale.x, this.scale.y, this.scale.z]);
364
+ return modelMatrix;
365
+ };
366
+
367
+ // looks like affect on transformations for now const 0
368
+ const modelMatrix = mat4.translation([0, 0, 0]);
369
+ const modelData = modelMatrix;
370
+ this.device.queue.writeBuffer(
371
+ this.modelUniformBuffer,
372
+ 0,
373
+ modelData.buffer,
374
+ modelData.byteOffset,
375
+ modelData.byteLength
376
+ );
377
+
378
+ this.shadowPassDescriptor = {
379
+ colorAttachments: [],
380
+ depthStencilAttachment: {
381
+ view: this.shadowDepthTextureView,
382
+ depthClearValue: 1.0,
383
+ depthLoadOp: 'clear',
384
+ depthStoreOp: 'store',
385
+ },
386
+ };
387
+
388
+ this.done = true;
389
+ }).then(() => {
390
+ if(typeof this.objAnim !== 'undefined' && this.objAnim !== null) {
391
+ console.log('after all load configutr mesh list buffers')
392
+ this.updateMeshListBuffers()
393
+ }
394
+ })
395
+ }
396
+
397
+ setupPipeline = () => {
398
+ console.log('test >>>>>>>>>>>>>>>>>>>FORMAT>✅' + this.presentationFormat);
399
+ this.pipeline = this.device.createRenderPipeline({
400
+ layout: this.device.createPipelineLayout({
401
+ bindGroupLayouts: [this.bglForRender, this.uniformBufferBindGroupLayout],
402
+ }),
403
+ vertex: {
404
+ entryPoint: 'main', // ✅ Add this
405
+ module: this.device.createShaderModule({
406
+ code: vertexWGSL,
407
+ }),
408
+ buffers: this.vertexBuffers,
409
+ },
410
+ fragment: {
411
+ entryPoint: 'main', // ✅ Add this
412
+ module: this.device.createShaderModule({
413
+ code: (this.isVideo == true ? fragmentVideoWGSL : fragmentWGSL),
414
+ }),
415
+ targets: [
416
+ {
417
+ format: this.presentationFormat,
418
+ },
419
+ ],
420
+ constants: {
421
+ shadowDepthTextureSize: this.shadowDepthTextureSize,
422
+ },
423
+ },
424
+ depthStencil: {
425
+ depthWriteEnabled: true,
426
+ depthCompare: 'less',
427
+ // format: 'depth24plus-stencil8',
428
+ format: 'depth24plus',
429
+ },
430
+ primitive: this.primitive,
431
+ });
432
+ }
433
+
434
+ draw = () => {
435
+ // This code -> light follow camera. can be used like options later!
436
+ // if(this.done == false) return;
437
+ // const transformationMatrix = this.getTransformationMatrix(this.position);
438
+ // this.device.queue.writeBuffer(this.sceneUniformBuffer, 64, transformationMatrix.buffer, transformationMatrix.byteOffset, transformationMatrix.byteLength);
439
+ // this.renderPassDescriptor.colorAttachments[0].view = this.context
440
+ // .getCurrentTexture()
441
+ // .createView();
442
+
443
+ // test
444
+ if(this.done == false) return;
445
+
446
+ // Per-object model matrix only
447
+ const modelMatrix = this.getModelMatrix(this.position);
448
+ this.device.queue.writeBuffer(
449
+ this.modelUniformBuffer,
450
+ 0,
451
+ modelMatrix.buffer,
452
+ modelMatrix.byteOffset,
453
+ modelMatrix.byteLength
454
+ );
455
+
456
+ // Acquire swapchain view for the pass
457
+ this.renderPassDescriptor.colorAttachments[0].view =
458
+ this.context.getCurrentTexture().createView();
459
+ }
460
+
461
+ drawElements = (renderPass) => {
462
+ if(this.isVideo) {
463
+ this.updateVideoTexture();
464
+ }
465
+ renderPass.setBindGroup(0, this.sceneBindGroupForRender);
466
+ renderPass.setBindGroup(1, this.modelBindGroup);
467
+ renderPass.setVertexBuffer(0, this.vertexBuffer);
468
+ renderPass.setVertexBuffer(1, this.vertexNormalsBuffer);
469
+ renderPass.setVertexBuffer(2, this.vertexTexCoordsBuffer);
470
+ renderPass.setIndexBuffer(this.indexBuffer, 'uint16');
471
+ renderPass.drawIndexed(this.indexCount);
472
+ }
473
+
474
+ createGPUBuffer(dataArray, usage) {
475
+ if(!dataArray || typeof dataArray.length !== 'number') {
476
+ throw new Error('Invalid data array passed to createGPUBuffer');
477
+ }
478
+
479
+ const size = dataArray.length * dataArray.BYTES_PER_ELEMENT;
480
+ if(!Number.isFinite(size) || size <= 0) {
481
+ throw new Error(`Invalid buffer size: ${size}`);
482
+ }
483
+
484
+ const buffer = this.device.createBuffer({
485
+ size,
486
+ usage,
487
+ mappedAtCreation: true,
488
+ });
489
+
490
+ const writeArray = dataArray.constructor === Float32Array
491
+ ? new Float32Array(buffer.getMappedRange())
492
+ : new Uint16Array(buffer.getMappedRange());
493
+
494
+ writeArray.set(dataArray);
495
+ buffer.unmap();
496
+
497
+ return buffer;
498
+ }
499
+
500
+ updateMeshListBuffers() {
501
+ for(const key in this.objAnim.meshList) {
502
+ const mesh = this.objAnim.meshList[key];
503
+
504
+ mesh.vertexBuffer = this.device.createBuffer({
505
+ size: mesh.vertices.length * Float32Array.BYTES_PER_ELEMENT,
506
+ usage: GPUBufferUsage.VERTEX,
507
+ mappedAtCreation: true,
508
+ });
509
+ new Float32Array(mesh.vertexBuffer.getMappedRange()).set(mesh.vertices);
510
+ mesh.vertexBuffer.unmap();
511
+ // Normals
512
+ mesh.vertexNormalsBuffer = this.device.createBuffer({
513
+ size: mesh.vertexNormals.length * Float32Array.BYTES_PER_ELEMENT,
514
+ usage: GPUBufferUsage.VERTEX,
515
+ mappedAtCreation: true,
516
+ });
517
+ new Float32Array(mesh.vertexNormalsBuffer.getMappedRange()).set(mesh.vertexNormals);
518
+ mesh.vertexNormalsBuffer.unmap();
519
+
520
+ // UVs
521
+ mesh.vertexTexCoordsBuffer = this.device.createBuffer({
522
+ size: mesh.textures.length * Float32Array.BYTES_PER_ELEMENT,
523
+ usage: GPUBufferUsage.VERTEX,
524
+ mappedAtCreation: true,
525
+ });
526
+ new Float32Array(mesh.vertexTexCoordsBuffer.getMappedRange()).set(mesh.textures);
527
+ mesh.vertexTexCoordsBuffer.unmap();
528
+
529
+ // Indices
530
+ const indexCount = mesh.indices.length;
531
+ const indexSize = Math.ceil(indexCount * Uint16Array.BYTES_PER_ELEMENT / 4) * 4;
532
+ mesh.indexBuffer = this.device.createBuffer({
533
+ size: indexSize,
534
+ usage: GPUBufferUsage.INDEX | GPUBufferUsage.COPY_DST,
535
+ mappedAtCreation: true,
536
+ });
537
+ new Uint16Array(mesh.indexBuffer.getMappedRange()).set(mesh.indices);
538
+ mesh.indexBuffer.unmap();
539
+ mesh.indexCount = indexCount;
540
+ }
541
+ }
542
+
543
+ drawElementsAnim = (renderPass) => {
544
+ renderPass.setBindGroup(0, this.sceneBindGroupForRender);
545
+ renderPass.setBindGroup(1, this.modelBindGroup);
546
+ const mesh = this.objAnim.meshList[this.objAnim.id + this.objAnim.currentAni];
547
+ renderPass.setVertexBuffer(0, mesh.vertexBuffer);
548
+ renderPass.setVertexBuffer(1, mesh.vertexNormalsBuffer);
549
+ renderPass.setVertexBuffer(2, mesh.vertexTexCoordsBuffer);
550
+ renderPass.setIndexBuffer(mesh.indexBuffer, 'uint16');
551
+ renderPass.drawIndexed(mesh.indexCount);
552
+ if(this.objAnim.playing == true) {
553
+ if(this.objAnim.animations[this.objAnim.animations.active].speedCounter >= this.objAnim.animations[this.objAnim.animations.active].speed) {
554
+ this.objAnim.currentAni++;
555
+ this.objAnim.animations[this.objAnim.animations.active].speedCounter = 0;
556
+ } else {
557
+ this.objAnim.animations[this.objAnim.animations.active].speedCounter++;
558
+ }
559
+ if(this.objAnim.currentAni >= this.objAnim.animations[this.objAnim.animations.active].to) {
560
+ this.objAnim.currentAni = this.objAnim.animations[this.objAnim.animations.active].from;
561
+ }
562
+ }
563
+ }
564
+
565
+ drawShadows = (shadowPass) => {
566
+ shadowPass.setBindGroup(0, this.sceneBindGroupForShadow);
567
+ shadowPass.setBindGroup(1, this.modelBindGroup);
568
+ shadowPass.setVertexBuffer(0, this.vertexBuffer);
569
+ shadowPass.setVertexBuffer(1, this.vertexNormalsBuffer);
570
+ shadowPass.setVertexBuffer(2, this.vertexTexCoordsBuffer);
571
+ shadowPass.setIndexBuffer(this.indexBuffer, 'uint16');
572
+ shadowPass.drawIndexed(this.indexCount);
573
+ }
574
+ }