matrix-engine-wgpu 1.0.6 → 1.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 (69) 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 +3 -5
  5. package/dev.md +460 -0
  6. package/empty.js +7 -6
  7. package/examples/games/jamb/jamb.js +1127 -0
  8. package/examples/load-obj-file.js +65 -28
  9. package/examples/unlit-textures.js +26 -23
  10. package/examples.js +35 -3
  11. package/main.js +442 -48
  12. package/non-project-files/dev.txt +21 -0
  13. package/non-project-files/image1.png +0 -0
  14. package/non-project-files/image6.png +0 -0
  15. package/package.json +28 -13
  16. package/public/app.js +11405 -9375
  17. package/public/css/style.css +371 -110
  18. package/public/empty.html +1 -1
  19. package/public/empty.js +9887 -9264
  20. package/public/examples.html +10 -8
  21. package/public/examples.js +553 -193
  22. package/public/index.html +3 -5
  23. package/public/manifest copy.web +35 -0
  24. package/public/res/audios/block.mp3 +0 -0
  25. package/public/res/audios/dice1.mp3 +0 -0
  26. package/public/res/audios/dice2.mp3 +0 -0
  27. package/public/res/audios/start.mp3 +0 -0
  28. package/public/res/meshes/jamb/bg.blend +0 -0
  29. package/public/res/meshes/jamb/bg.blend1 +0 -0
  30. package/public/res/meshes/jamb/bg.mtl +12 -0
  31. package/public/res/meshes/jamb/bg.obj +17 -0
  32. package/public/res/meshes/jamb/bg.png +0 -0
  33. package/public/res/meshes/jamb/dice-default.png +0 -0
  34. package/public/res/meshes/jamb/dice-mark.png +0 -0
  35. package/public/res/meshes/jamb/dice.mtl +12 -0
  36. package/public/res/meshes/jamb/dice.obj +40 -0
  37. package/public/res/meshes/jamb/dice.png +0 -0
  38. package/public/res/meshes/jamb/jamb-title.mtl +12 -0
  39. package/public/res/meshes/jamb/jamb-title.obj +26008 -0
  40. package/public/res/meshes/jamb/jamb.blend +0 -0
  41. package/public/res/meshes/jamb/jamb.blend1 +0 -0
  42. package/public/res/meshes/jamb/logo.png +0 -0
  43. package/public/res/meshes/jamb/nidzaDice.blend +0 -0
  44. package/public/res/meshes/jamb/nidzaDice.blend1 +0 -0
  45. package/public/res/meshes/jamb/pile.blend +0 -0
  46. package/public/res/meshes/jamb/simpleCube.blend +0 -0
  47. package/public/res/meshes/jamb/simpleCube.blend1 +0 -0
  48. package/public/res/meshes/jamb/sounds/roll1.wav +0 -0
  49. package/public/res/meshes/jamb/text.png +0 -0
  50. package/public/res/multilang/en.json +27 -0
  51. package/public/res/multilang/sr.json +27 -0
  52. package/public/test.html +636 -0
  53. package/public/three-test.js +165 -0
  54. package/public/worker.html +1 -1
  55. package/readme.md +189 -115
  56. package/src/engine/cube.js +10 -1
  57. package/src/engine/engine.js +1 -5
  58. package/src/engine/loader-obj.js +9 -6
  59. package/src/engine/matrix-class.js +237 -204
  60. package/src/engine/mesh-obj.js +605 -515
  61. package/src/engine/raycast-test.js +93 -0
  62. package/src/engine/utils.js +69 -3
  63. package/src/multilang/lang.js +35 -0
  64. package/src/physics/matrix-ammo.js +168 -15
  65. package/src/shaders/fragment.wgsl.js +4 -2
  66. package/src/shaders/shaders.js +1 -1
  67. package/src/shaders/vertexShadow.wgsl.js +1 -1
  68. package/src/sounds/sounds.js +47 -0
  69. package/src/world.js +311 -248
package/src/world.js CHANGED
@@ -5,255 +5,318 @@ import {ArcballCamera, WASDCamera} from "./engine/engine.js";
5
5
  import MEMesh from "./engine/mesh.js";
6
6
  import MEMeshObj from "./engine/mesh-obj.js";
7
7
  import MatrixAmmo from "./physics/matrix-ammo.js";
8
- import {LOG_WARN, scriptManager} from "./engine/utils.js";
8
+ import {LOG_WARN, genName, mb, scriptManager, urlQuery} from "./engine/utils.js";
9
+ import {MultiLang} from "./multilang/lang.js";
10
+ import {MatrixSounds} from "./sounds/sounds.js";
9
11
 
10
12
  export default class MatrixEngineWGPU {
11
13
 
12
- mainRenderBundle = [];
13
- rbContainer = [];
14
- frame = () => {};
15
-
16
- entityHolder = [];
17
-
18
- entityArgPass = {
19
- loadOp: 'clear',
20
- storeOp: 'store',
21
- depthLoadOp: 'clear',
22
- depthStoreOp: 'store'
23
- }
24
-
25
- matrixAmmo = new MatrixAmmo();
26
-
27
- // The input handler
28
- constructor(options, callback) {
29
- // console.log('typeof options ', typeof options )
30
- if(typeof options == 'undefined' || typeof options == "function") {
31
- this.options = {
32
- useSingleRenderPass: true,
33
- canvasSize: 'fullscreen',
34
- mainCameraParams: {
35
- type: 'WASD',
36
- responseCoef: 2000
37
- }
38
- }
39
- callback = options;
40
- }
41
- if(typeof options.mainCameraParams === 'undefined') {
42
- options.mainCameraParams = {
43
- type: 'WASD',
44
- responseCoef: 2000
45
- }
46
- }
47
- this.options = options;
48
-
49
- this.mainCameraParams = options.mainCameraParams;
50
-
51
- var canvas = document.createElement('canvas')
52
- if(this.options.canvasSize == 'fullscreen') {
53
- canvas.width = window.innerWidth;
54
- canvas.height = window.innerHeight;
55
- } else {
56
- canvas.width = this.options.canvasSize.w;
57
- canvas.height = this.options.canvasSize.h;
58
- }
59
- document.body.append(canvas);
60
-
61
- // The camera types
62
- const initialCameraPosition = vec3.create(0, 0, 0);
63
- // console.log('passed : o.mainCameraParams.responseCoef ', o.mainCameraParams.responseCoef)
64
- this.mainCameraParams = {
65
- type: this.options.mainCameraParams.type,
66
- responseCoef: this.options.mainCameraParams.responseCoef
67
- }
68
-
69
- this.cameras = {
70
- arcball: new ArcballCamera({position: initialCameraPosition}),
71
- WASD: new WASDCamera({position: initialCameraPosition}),
72
- };
73
-
74
- this.init({canvas, callback})
75
- }
76
-
77
- init = async ({canvas, callback}) => {
78
- this.canvas = canvas;
79
- this.adapter = await navigator.gpu.requestAdapter();
80
- this.device = await this.adapter.requestDevice();
81
- this.context = canvas.getContext('webgpu');
82
-
83
- const devicePixelRatio = window.devicePixelRatio;
84
- canvas.width = canvas.clientWidth * devicePixelRatio;
85
- canvas.height = canvas.clientHeight * devicePixelRatio;
86
- const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
87
-
88
- this.context.configure({
89
- device: this.device,
90
- format: presentationFormat,
91
- alphaMode: 'premultiplied',
92
- });
93
-
94
- if(this.options.useSingleRenderPass == true) {
95
- this.frame = this.frameSinglePass;
96
- } else {
97
- this.frame = this.framePassPerObject;
98
- }
99
-
100
- this.run(callback)
101
- };
102
-
103
- // Not in use for now
104
- addCube = (o) => {
105
- if(typeof o === 'undefined') {
106
- var o = {
107
- scale: 1,
108
- position: {x: 0, y: 0, z: -4},
109
- texturesPaths: ['./res/textures/default.png'],
110
- rotation: {x: 0, y: 0, z: 0},
111
- rotationSpeed: {x: 0, y: 0, z: 0},
112
- entityArgPass: this.entityArgPass,
113
- cameras: this.cameras,
114
- mainCameraParams: this.mainCameraParams
115
- }
116
- } else {
117
- if(typeof o.position === 'undefined') {o.position = {x: 0, y: 0, z: -4}}
118
- if(typeof o.rotation === 'undefined') {o.rotation = {x: 0, y: 0, z: 0}}
119
- if(typeof o.rotationSpeed === 'undefined') {o.rotationSpeed = {x: 0, y: 0, z: 0}}
120
- if(typeof o.texturesPaths === 'undefined') {o.texturesPaths = ['./res/textures/default.png']}
121
- if(typeof o.scale === 'undefined') {o.scale = 1;}
122
- if(typeof o.mainCameraParams === 'undefined') {o.mainCameraParams = this.mainCameraParams}
123
- o.entityArgPass = this.entityArgPass;
124
- o.cameras = this.cameras;
125
- }
126
- let myCube1 = new MECube(this.canvas, this.device, this.context, o)
127
- this.mainRenderBundle.push(myCube1);
128
- }
129
-
130
- // Not in use for now
131
- addBall = (o) => {
132
- if(typeof o === 'undefined') {
133
- var o = {
134
- scale: 1,
135
- position: {x: 0, y: 0, z: -4},
136
- texturesPaths: ['./res/textures/default.png'],
137
- rotation: {x: 0, y: 0, z: 0},
138
- rotationSpeed: {x: 0, y: 0, z: 0},
139
- entityArgPass: this.entityArgPass,
140
- cameras: this.cameras,
141
- mainCameraParams: this.mainCameraParams
142
- }
143
- } else {
144
- if(typeof o.position === 'undefined') {o.position = {x: 0, y: 0, z: -4}}
145
- if(typeof o.rotation === 'undefined') {o.rotation = {x: 0, y: 0, z: 0}}
146
- if(typeof o.rotationSpeed === 'undefined') {o.rotationSpeed = {x: 0, y: 0, z: 0}}
147
- if(typeof o.texturesPaths === 'undefined') {o.texturesPaths = ['./res/textures/default.png']}
148
- if(typeof o.mainCameraParams === 'undefined') {o.mainCameraParams = this.mainCameraParams}
149
- if(typeof o.scale === 'undefined') {o.scale = 1;}
150
- o.entityArgPass = this.entityArgPass;
151
- o.cameras = this.cameras;
152
- }
153
- let myBall1 = new MEBall(this.canvas, this.device, this.context, o)
154
- this.mainRenderBundle.push(myBall1);
155
- }
156
-
157
- // Not in use for now
158
- addMesh = (o) => {
159
- if(typeof o.position === 'undefined') {o.position = {x: 0, y: 0, z: -4}}
160
- if(typeof o.rotation === 'undefined') {o.rotation = {x: 0, y: 0, z: 0}}
161
- if(typeof o.rotationSpeed === 'undefined') {o.rotationSpeed = {x: 0, y: 0, z: 0}}
162
- if(typeof o.texturesPaths === 'undefined') {o.texturesPaths = ['./res/textures/default.png']}
163
- if(typeof o.mainCameraParams === 'undefined') {o.mainCameraParams = this.mainCameraParams}
164
- if(typeof o.scale === 'undefined') {o.scale = 1;}
165
- o.entityArgPass = this.entityArgPass;
166
- o.cameras = this.cameras;
167
- if(typeof o.name === 'undefined') {o.name = 'random' + Math.random();}
168
- if(typeof o.mesh === 'undefined') {
169
- throw console.error('arg mesh is empty...');
170
- return;
171
- }
172
- console.log('Mesh procedure', o)
173
- let myMesh1 = new MEMesh(this.canvas, this.device, this.context, o)
174
- this.mainRenderBundle.push(myMesh1);
175
- }
176
-
177
- addMeshObj = (o) => {
178
- if(typeof o.position === 'undefined') {o.position = {x: 0, y: 0, z: -4}}
179
- if(typeof o.rotation === 'undefined') {o.rotation = {x: 0, y: 0, z: 0}}
180
- if(typeof o.rotationSpeed === 'undefined') {o.rotationSpeed = {x: 0, y: 0, z: 0}}
181
- if(typeof o.texturesPaths === 'undefined') {o.texturesPaths = ['./res/textures/default.png']}
182
- if(typeof o.mainCameraParams === 'undefined') {o.mainCameraParams = this.mainCameraParams}
183
- if(typeof o.scale === 'undefined') {o.scale = 1;}
184
- o.entityArgPass = this.entityArgPass;
185
- o.cameras = this.cameras;
186
- if(typeof o.name === 'undefined') {o.name = 'random' + Math.random();}
187
- if(typeof o.mesh === 'undefined') {
188
- throw console.error('arg mesh is empty...');
189
- return;
190
- }
191
- if(typeof o.physics === 'undefined') {
192
- o.physics = {
193
- enabled: false,
194
- geometry: "Sphere",
195
- radius: o.scale
196
- }
197
- }
198
- if(typeof o.physics.enabled === 'undefined') {o.physics.enabled = false}
199
- if(typeof o.physics.geometry === 'undefined') {o.physics.geometry = "Sphere"}
200
- if(typeof o.physics.radius === 'undefined') {o.physics.radius = o.scale}
201
-
202
- // send same pos
203
- o.physics.position = o.position;
204
-
205
- // console.log('Mesh procedure', o)
206
- let myMesh1 = new MEMeshObj(this.canvas, this.device, this.context, o)
207
- if(o.physics.enabled == true) {
208
- this.matrixAmmo.addPhysics(myMesh1, o.physics)
209
- }
210
- this.mainRenderBundle.push(myMesh1);
211
- }
212
-
213
- run(callback) {
214
- setTimeout(() => {requestAnimationFrame(this.frame)}, 500)
215
- setTimeout(() => {callback()}, 20)
216
- }
217
-
218
- frameSinglePass = () => {
219
- if(typeof this.mainRenderBundle == 'undefined') return;
220
- try {
221
- let shadowPass = null;
222
- let renderPass;
223
- let commandEncoder = this.device.createCommandEncoder();
224
-
225
- this.mainRenderBundle.forEach((meItem, index) => {
226
- meItem.position.update();
227
- })
228
-
229
- this.matrixAmmo.updatePhysics()
230
-
231
- this.mainRenderBundle.forEach((meItem, index) => {
232
- meItem.draw(commandEncoder);
233
-
234
- shadowPass = commandEncoder.beginRenderPass(meItem.shadowPassDescriptor);
235
- shadowPass.setPipeline(meItem.shadowPipeline);
236
- meItem.drawShadows(shadowPass);
237
- shadowPass.end();
238
- })
239
-
240
- this.mainRenderBundle.forEach((meItem, index) => {
241
- if(index == 0) {
242
- renderPass = commandEncoder.beginRenderPass(meItem.renderPassDescriptor);
243
- renderPass.setPipeline(meItem.pipeline);
244
- }
245
- })
246
-
247
- this.mainRenderBundle.forEach((meItem, index) => {
248
- meItem.drawElements(renderPass);
249
- })
250
- renderPass.end();
251
-
252
- this.device.queue.submit([commandEncoder.finish()]);
253
- requestAnimationFrame(this.frame);
254
- } catch(err) {
255
- console.log('%cDraw func (err):' + err , LOG_WARN)
256
- requestAnimationFrame(this.frame);
257
- }
258
- }
14
+ mainRenderBundle = [];
15
+ rbContainer = [];
16
+ frame = () => {};
17
+
18
+ entityHolder = [];
19
+
20
+ entityArgPass = {
21
+ loadOp: 'clear',
22
+ storeOp: 'store',
23
+ depthLoadOp: 'clear',
24
+ depthStoreOp: 'store'
25
+ }
26
+
27
+ matrixAmmo = new MatrixAmmo();
28
+ matrixSounds = new MatrixSounds();
29
+
30
+ // The input handler
31
+ constructor(options, callback) {
32
+ // console.log('typeof options ', typeof options )
33
+ if(typeof options == 'undefined' || typeof options == "function") {
34
+ this.options = {
35
+ useSingleRenderPass: true,
36
+ canvasSize: 'fullscreen',
37
+ mainCameraParams: {
38
+ type: 'WASD',
39
+ responseCoef: 2000
40
+ }
41
+ }
42
+ callback = options;
43
+ }
44
+ if(typeof options.mainCameraParams === 'undefined') {
45
+ options.mainCameraParams = {
46
+ type: 'WASD',
47
+ responseCoef: 2000
48
+ }
49
+ }
50
+ this.options = options;
51
+
52
+ this.mainCameraParams = options.mainCameraParams;
53
+
54
+ var canvas = document.createElement('canvas')
55
+ if(this.options.canvasSize == 'fullscreen') {
56
+ canvas.width = window.innerWidth;
57
+ canvas.height = window.innerHeight;
58
+ } else {
59
+ canvas.width = this.options.canvasSize.w;
60
+ canvas.height = this.options.canvasSize.h;
61
+ }
62
+ document.body.append(canvas);
63
+
64
+ // The camera types
65
+ const initialCameraPosition = vec3.create(0, 0, 0);
66
+ // console.log('passed : o.mainCameraParams.responseCoef ', o.mainCameraParams.responseCoef)
67
+ this.mainCameraParams = {
68
+ type: this.options.mainCameraParams.type,
69
+ responseCoef: this.options.mainCameraParams.responseCoef
70
+ }
71
+
72
+ this.cameras = {
73
+ arcball: new ArcballCamera({position: initialCameraPosition}),
74
+ WASD: new WASDCamera({position: initialCameraPosition}),
75
+ };
76
+
77
+ //
78
+ this.label = new MultiLang()
79
+ if(urlQuery.lang != null) {
80
+ this.label.loadMultilang(urlQuery.lang).then((r) => {
81
+ this.label.get = r;
82
+ });
83
+ } else {
84
+ this.label.loadMultilang().then((r) => {
85
+ this.label.get = r;
86
+ });
87
+ }
88
+
89
+ this.init({canvas, callback})
90
+ }
91
+
92
+ init = async ({canvas, callback}) => {
93
+ this.canvas = canvas;
94
+ this.adapter = await navigator.gpu.requestAdapter();
95
+ this.device = await this.adapter.requestDevice({
96
+ extensions: ["ray_tracing"]
97
+ });
98
+
99
+ // Maybe works in ssl with webworkers...
100
+ // const adapterInfo = await this.adapter.requestAdapterInfo();
101
+ // var test = this.adapter.features()
102
+ // console.log(adapterInfo.vendor);
103
+ // console.log('test' + test);
104
+ // console.log("FEATURES : " + this.adapter.features)
105
+
106
+ this.context = canvas.getContext('webgpu');
107
+
108
+ const devicePixelRatio = window.devicePixelRatio;
109
+ canvas.width = canvas.clientWidth * devicePixelRatio;
110
+ canvas.height = canvas.clientHeight * devicePixelRatio;
111
+ const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
112
+
113
+ this.context.configure({
114
+ device: this.device,
115
+ format: presentationFormat,
116
+ alphaMode: 'premultiplied',
117
+ });
118
+
119
+ if(this.options.useSingleRenderPass == true) {
120
+ this.frame = this.frameSinglePass;
121
+ } else {
122
+ this.frame = this.framePassPerObject;
123
+ }
124
+
125
+ this.run(callback)
126
+ };
127
+
128
+ // Not in use for now
129
+ addCube = (o) => {
130
+ if(typeof o === 'undefined') {
131
+ var o = {
132
+ scale: 1,
133
+ position: {x: 0, y: 0, z: -4},
134
+ texturesPaths: ['./res/textures/default.png'],
135
+ rotation: {x: 0, y: 0, z: 0},
136
+ rotationSpeed: {x: 0, y: 0, z: 0},
137
+ entityArgPass: this.entityArgPass,
138
+ cameras: this.cameras,
139
+ mainCameraParams: this.mainCameraParams
140
+ }
141
+ } else {
142
+ if(typeof o.position === 'undefined') {o.position = {x: 0, y: 0, z: -4}}
143
+ if(typeof o.rotation === 'undefined') {o.rotation = {x: 0, y: 0, z: 0}}
144
+ if(typeof o.rotationSpeed === 'undefined') {o.rotationSpeed = {x: 0, y: 0, z: 0}}
145
+ if(typeof o.texturesPaths === 'undefined') {o.texturesPaths = ['./res/textures/default.png']}
146
+ if(typeof o.scale === 'undefined') {o.scale = 1;}
147
+ if(typeof o.mainCameraParams === 'undefined') {o.mainCameraParams = this.mainCameraParams}
148
+ o.entityArgPass = this.entityArgPass;
149
+ o.cameras = this.cameras;
150
+ }
151
+ let myCube1 = new MECube(this.canvas, this.device, this.context, o)
152
+ this.mainRenderBundle.push(myCube1);
153
+ }
154
+
155
+ // Not in use for now
156
+ addBall = (o) => {
157
+ if(typeof o === 'undefined') {
158
+ var o = {
159
+ scale: 1,
160
+ position: {x: 0, y: 0, z: -4},
161
+ texturesPaths: ['./res/textures/default.png'],
162
+ rotation: {x: 0, y: 0, z: 0},
163
+ rotationSpeed: {x: 0, y: 0, z: 0},
164
+ entityArgPass: this.entityArgPass,
165
+ cameras: this.cameras,
166
+ mainCameraParams: this.mainCameraParams
167
+ }
168
+ } else {
169
+ if(typeof o.position === 'undefined') {o.position = {x: 0, y: 0, z: -4}}
170
+ if(typeof o.rotation === 'undefined') {o.rotation = {x: 0, y: 0, z: 0}}
171
+ if(typeof o.rotationSpeed === 'undefined') {o.rotationSpeed = {x: 0, y: 0, z: 0}}
172
+ if(typeof o.texturesPaths === 'undefined') {o.texturesPaths = ['./res/textures/default.png']}
173
+ if(typeof o.mainCameraParams === 'undefined') {o.mainCameraParams = this.mainCameraParams}
174
+ if(typeof o.scale === 'undefined') {o.scale = 1;}
175
+ o.entityArgPass = this.entityArgPass;
176
+ o.cameras = this.cameras;
177
+ }
178
+ let myBall1 = new MEBall(this.canvas, this.device, this.context, o)
179
+ this.mainRenderBundle.push(myBall1);
180
+ }
181
+
182
+ // Not in use for now
183
+ addMesh = (o) => {
184
+ if(typeof o.position === 'undefined') {o.position = {x: 0, y: 0, z: -4}}
185
+ if(typeof o.rotation === 'undefined') {o.rotation = {x: 0, y: 0, z: 0}}
186
+ if(typeof o.rotationSpeed === 'undefined') {o.rotationSpeed = {x: 0, y: 0, z: 0}}
187
+ if(typeof o.texturesPaths === 'undefined') {o.texturesPaths = ['./res/textures/default.png']}
188
+ if(typeof o.mainCameraParams === 'undefined') {o.mainCameraParams = this.mainCameraParams}
189
+ if(typeof o.scale === 'undefined') {o.scale = 1;}
190
+ o.entityArgPass = this.entityArgPass;
191
+ o.cameras = this.cameras;
192
+ if(typeof o.name === 'undefined') {o.name = 'random' + Math.random();}
193
+ if(typeof o.mesh === 'undefined') {
194
+ throw console.error('arg mesh is empty...');
195
+ return;
196
+ }
197
+ console.log('Mesh procedure', o)
198
+ let myMesh1 = new MEMesh(this.canvas, this.device, this.context, o)
199
+ this.mainRenderBundle.push(myMesh1);
200
+ }
201
+
202
+ addMeshObj = (o) => {
203
+ if(typeof o.name === 'undefined') {o.name = genName(9)}
204
+ if(typeof o.position === 'undefined') {o.position = {x: 0, y: 0, z: -4}}
205
+ if(typeof o.rotation === 'undefined') {o.rotation = {x: 0, y: 0, z: 0}}
206
+ if(typeof o.rotationSpeed === 'undefined') {o.rotationSpeed = {x: 0, y: 0, z: 0}}
207
+ if(typeof o.texturesPaths === 'undefined') {o.texturesPaths = ['./res/textures/default.png']}
208
+ if(typeof o.mainCameraParams === 'undefined') {o.mainCameraParams = this.mainCameraParams}
209
+ if(typeof o.scale === 'undefined') {o.scale = [1, 1, 1];}
210
+ if(typeof o.raycast === 'undefined') {o.raycast = { enabled: false }}
211
+
212
+ o.entityArgPass = this.entityArgPass;
213
+ o.cameras = this.cameras;
214
+ // if(typeof o.name === 'undefined') {o.name = 'random' + Math.random();}
215
+ if(typeof o.mesh === 'undefined') {
216
+ mb.error('arg mesh is empty for ', o.name);
217
+ throw console.error('arg mesh is empty...');
218
+ return;
219
+ }
220
+ if(typeof o.physics === 'undefined') {
221
+ o.physics = {
222
+ scale: [1, 1, 1],
223
+ enabled: true,
224
+ geometry: "Sphere",
225
+ radius: o.scale,
226
+ name: o.name,
227
+ rotation: o.rotation
228
+ }
229
+ }
230
+ if(typeof o.physics.enabled === 'undefined') {o.physics.enabled = true}
231
+ if(typeof o.physics.geometry === 'undefined') {o.physics.geometry = "Sphere"}
232
+ if(typeof o.physics.radius === 'undefined') {o.physics.radius = o.scale}
233
+ if(typeof o.physics.mass === 'undefined') {o.physics.mass = 1;}
234
+ if(typeof o.physics.name === 'undefined') {o.physics.name = o.name;}
235
+ if(typeof o.physics.scale === 'undefined') {o.physics.scale = o.scale;}
236
+ if(typeof o.physics.rotation === 'undefined') {o.physics.rotation = o.rotation;}
237
+
238
+ // send same pos
239
+ o.physics.position = o.position;
240
+ // console.log('Mesh procedure', o)
241
+ let myMesh1 = new MEMeshObj(this.canvas, this.device, this.context, o)
242
+ if(o.physics.enabled == true) {
243
+ this.matrixAmmo.addPhysics(myMesh1, o.physics)
244
+ }
245
+ this.mainRenderBundle.push(myMesh1);
246
+ }
247
+
248
+ run(callback) {
249
+ setTimeout(() => {requestAnimationFrame(this.frame)}, 500)
250
+ setTimeout(() => {callback(this)}, 20)
251
+ }
252
+
253
+ destroyProgram = () => {
254
+ this.mainRenderBundle = undefined;
255
+ this.canvas.remove();
256
+ }
257
+
258
+ frameSinglePass = () => {
259
+ if(typeof this.mainRenderBundle == 'undefined') return;
260
+ try {
261
+ let shadowPass = null;
262
+ let renderPass;
263
+ let commandEncoder = this.device.createCommandEncoder();
264
+
265
+ this.mainRenderBundle.forEach((meItem, index) => {
266
+ meItem.position.update();
267
+ })
268
+
269
+ this.matrixAmmo.updatePhysics()
270
+
271
+ this.mainRenderBundle.forEach((meItem, index) => {
272
+ meItem.draw(commandEncoder);
273
+
274
+ shadowPass = commandEncoder.beginRenderPass(meItem.shadowPassDescriptor);
275
+ shadowPass.setPipeline(meItem.shadowPipeline);
276
+ meItem.drawShadows(shadowPass);
277
+ shadowPass.end();
278
+ })
279
+
280
+ this.mainRenderBundle.forEach((meItem, index) => {
281
+ if(index == 0) {
282
+ renderPass = commandEncoder.beginRenderPass(meItem.renderPassDescriptor);
283
+ renderPass.setPipeline(meItem.pipeline);
284
+ }
285
+ })
286
+
287
+ this.mainRenderBundle.forEach((meItem, index) => {
288
+ meItem.drawElements(renderPass);
289
+ })
290
+ if(renderPass) renderPass.end();
291
+
292
+ this.device.queue.submit([commandEncoder.finish()]);
293
+ requestAnimationFrame(this.frame);
294
+ } catch(err) {
295
+ console.log('%cDraw func (err):' + err, LOG_WARN)
296
+ requestAnimationFrame(this.frame);
297
+ }
298
+ }
299
+
300
+ framePassPerObject = () => {
301
+ // console.log('framePassPerObject')
302
+ let commandEncoder = this.device.createCommandEncoder();
303
+ this.rbContainer = [];
304
+ let passEncoder;
305
+ this.mainRenderBundle.forEach((meItem, index) => {
306
+ meItem.draw(commandEncoder);
307
+
308
+ if(meItem.renderBundle) {
309
+ this.rbContainer.push(meItem.renderBundle)
310
+ passEncoder = commandEncoder.beginRenderPass(meItem.renderPassDescriptor);
311
+ passEncoder.executeBundles(this.rbContainer);
312
+ passEncoder.end();
313
+ } else {
314
+ meItem.draw(commandEncoder)
315
+ }
316
+
317
+ })
318
+ this.device.queue.submit([commandEncoder.finish()]);
319
+ requestAnimationFrame(this.frame);
320
+ }
321
+
259
322
  }