matrix-engine-wgpu 1.0.5 → 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 (88) 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 +5 -2
  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 +67 -37
  9. package/examples/unlit-textures.js +26 -23
  10. package/examples.js +35 -3
  11. package/main.js +441 -43
  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 +50 -41
  16. package/public/app.js +11409 -8893
  17. package/public/css/style.css +376 -112
  18. package/public/empty.html +1 -1
  19. package/public/empty.js +9890 -8885
  20. package/public/examples.html +10 -8
  21. package/public/examples.js +945 -203
  22. package/public/index.html +5 -7
  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/fonts/Accuratist.ttf +0 -0
  29. package/public/res/fonts/Closeness.ttf +0 -0
  30. package/public/res/fonts/WARGAMES.TTF +0 -0
  31. package/public/res/fonts/readme.txt +5 -0
  32. package/public/res/fonts/stormfaze.ttf +0 -0
  33. package/public/res/meshes/blender/cube.blend +0 -0
  34. package/public/res/meshes/blender/cube.blend1 +0 -0
  35. package/public/res/meshes/blender/cube.mtl +12 -0
  36. package/public/res/meshes/blender/cube.obj +46 -0
  37. package/public/res/meshes/blender/cube.png +0 -0
  38. package/public/res/meshes/blender/cubeSmartUV.blend +0 -0
  39. package/public/res/meshes/blender/cubeSmartUV.mtl +12 -0
  40. package/public/res/meshes/blender/cubeSmartUV.obj +46 -0
  41. package/public/res/meshes/blender/sphepe.blend +0 -0
  42. package/public/res/meshes/blender/sphepe.blend1 +0 -0
  43. package/public/res/meshes/blender/sphere.mtl +10 -0
  44. package/public/res/meshes/blender/sphere.obj +3402 -0
  45. package/public/res/meshes/jamb/bg.blend +0 -0
  46. package/public/res/meshes/jamb/bg.blend1 +0 -0
  47. package/public/res/meshes/jamb/bg.mtl +12 -0
  48. package/public/res/meshes/jamb/bg.obj +17 -0
  49. package/public/res/meshes/jamb/bg.png +0 -0
  50. package/public/res/meshes/jamb/dice-default.png +0 -0
  51. package/public/res/meshes/jamb/dice-mark.png +0 -0
  52. package/public/res/meshes/jamb/dice.mtl +12 -0
  53. package/public/res/meshes/jamb/dice.obj +40 -0
  54. package/public/res/meshes/jamb/dice.png +0 -0
  55. package/public/res/meshes/jamb/jamb-title.mtl +12 -0
  56. package/public/res/meshes/jamb/jamb-title.obj +26008 -0
  57. package/public/res/meshes/jamb/jamb.blend +0 -0
  58. package/public/res/meshes/jamb/jamb.blend1 +0 -0
  59. package/public/res/meshes/jamb/logo.png +0 -0
  60. package/public/res/meshes/jamb/nidzaDice.blend +0 -0
  61. package/public/res/meshes/jamb/nidzaDice.blend1 +0 -0
  62. package/public/res/meshes/jamb/pile.blend +0 -0
  63. package/public/res/meshes/jamb/simpleCube.blend +0 -0
  64. package/public/res/meshes/jamb/simpleCube.blend1 +0 -0
  65. package/public/res/meshes/jamb/sounds/roll1.wav +0 -0
  66. package/public/res/meshes/jamb/text.png +0 -0
  67. package/public/res/multilang/en.json +27 -0
  68. package/public/res/multilang/sr.json +27 -0
  69. package/public/test.html +636 -0
  70. package/public/three-test.js +165 -0
  71. package/public/worker.html +1 -1
  72. package/readme.md +232 -116
  73. package/src/engine/cube.js +10 -5
  74. package/src/engine/engine.js +3 -9
  75. package/src/engine/loader-obj.js +9 -6
  76. package/src/engine/matrix-class.js +240 -202
  77. package/src/engine/mesh-obj.js +605 -525
  78. package/src/engine/mesh.js +476 -0
  79. package/src/engine/raycast-test.js +93 -0
  80. package/src/engine/utils.js +129 -15
  81. package/src/multilang/lang.js +35 -0
  82. package/src/physics/matrix-ammo.js +204 -30
  83. package/src/shaders/fragment.wgsl.js +4 -2
  84. package/src/shaders/shaders.js +1 -1
  85. package/src/shaders/vertexShadow.wgsl.js +1 -1
  86. package/src/sounds/sounds.js +47 -0
  87. package/src/world.js +312 -236
  88. package/src/engine/matrix-mesh.js +0 -49
package/src/world.js CHANGED
@@ -2,245 +2,321 @@ import {vec3} from "wgpu-matrix";
2
2
  import MEBall from "./engine/ball.js";
3
3
  import MECube from './engine/cube.js';
4
4
  import {ArcballCamera, WASDCamera} from "./engine/engine.js";
5
+ import MEMesh from "./engine/mesh.js";
5
6
  import MEMeshObj from "./engine/mesh-obj.js";
6
7
  import MatrixAmmo from "./physics/matrix-ammo.js";
7
- // import {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";
8
11
 
9
12
  export default class MatrixEngineWGPU {
10
13
 
11
- mainRenderBundle = [];
12
- rbContainer = [];
13
- frame = () => {};
14
-
15
- entityHolder = [];
16
-
17
- entityArgPass = {
18
- loadOp: 'clear',
19
- storeOp: 'store',
20
- depthLoadOp: 'clear',
21
- depthStoreOp: 'store'
22
- }
23
-
24
- matrixAmmo = new MatrixAmmo();
25
-
26
- constructor(options, callback) {
27
- // console.log('typeof options ', typeof options )
28
- if(typeof options == 'undefined' || typeof options == "function") {
29
- this.options = {
30
- useSingleRenderPass: true,
31
- canvasSize: 'fullscreen',
32
- mainCameraParams: {
33
- type: 'WASD',
34
- responseCoef: 2000
35
- }
36
- }
37
- callback = options;
38
- }
39
- if(typeof options.mainCameraParams === 'undefined') {
40
- options.mainCameraParams = {
41
- type: 'WASD',
42
- responseCoef: 2000
43
- }
44
- }
45
- this.options = options;
46
-
47
- this.mainCameraParams = options.mainCameraParams;
48
-
49
- var canvas = document.createElement('canvas')
50
- if(this.options.canvasSize == 'fullscreen') {
51
- canvas.width = window.innerWidth;
52
- canvas.height = window.innerHeight;
53
- } else {
54
- canvas.width = this.options.canvasSize.w;
55
- canvas.height = this.options.canvasSize.h;
56
- }
57
- document.body.append(canvas)
58
-
59
- const initialCameraPosition = vec3.create(0, 0, 0);
60
- this.mainCameraParams = {
61
- type: this.options.mainCameraParams.type,
62
- responseCoef: this.options.mainCameraParams.responseCoef
63
- }
64
-
65
- this.cameras = {
66
- arcball: new ArcballCamera({position: initialCameraPosition}),
67
- WASD: new WASDCamera({position: initialCameraPosition}),
68
- };
69
-
70
- this.init({canvas, callback})
71
- }
72
-
73
- init = async ({canvas, callback}) => {
74
- this.canvas = canvas;
75
- this.adapter = await navigator.gpu.requestAdapter();
76
- this.device = await this.adapter.requestDevice();
77
- this.context = canvas.getContext('webgpu');
78
-
79
- const devicePixelRatio = window.devicePixelRatio;
80
- canvas.width = canvas.clientWidth * devicePixelRatio;
81
- canvas.height = canvas.clientHeight * devicePixelRatio;
82
- const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
83
-
84
- this.context.configure({
85
- device: this.device,
86
- format: presentationFormat,
87
- alphaMode: 'premultiplied',
88
- });
89
-
90
- if(this.options.useSingleRenderPass == true) {
91
- this.makeDefaultRenderPassDescriptor()
92
- this.frame = this.frameSinglePass;
93
- } else {
94
- // must be
95
- this.frame = this.frameSinglePass;
96
- }
97
-
98
- this.run(callback)
99
- };
100
-
101
- makeDefaultRenderPassDescriptor = () => {
102
-
103
- this.depthTexture = this.device.createTexture({
104
- size: [this.canvas.width, this.canvas.height],
105
- format: 'depth24plus',
106
- usage: GPUTextureUsage.RENDER_ATTACHMENT,
107
- });
108
-
109
- this.renderPassDescriptor = {
110
- colorAttachments: [
111
- {
112
- view: undefined,
113
- clearValue: {r: 0.0, g: 0.0, b: 0.0, a: 1.0},
114
- loadOp: 'clear',
115
- storeOp: 'store',
116
- },
117
- ],
118
- depthStencilAttachment: {
119
- view: this.depthTexture.createView(),
120
- depthClearValue: 1.0,
121
- depthLoadOp: 'clear',
122
- depthStoreOp: 'store',
123
- },
124
- };
125
- }
126
-
127
- addCube = (o) => {
128
- if(typeof o === 'undefined') {
129
- var o = {
130
- scale: 1,
131
- position: {x: 0, y: 0, z: -4},
132
- texturesPaths: ['./res/textures/default.png'],
133
- rotation: {x: 0, y: 0, z: 0},
134
- rotationSpeed: {x: 0, y: 0, z: 0},
135
- entityArgPass: this.entityArgPass,
136
- cameras: this.cameras,
137
- mainCameraParams: this.mainCameraParams
138
- }
139
- } else {
140
- if(typeof o.position === 'undefined') {o.position = {x: 0, y: 0, z: -4}}
141
- if(typeof o.rotation === 'undefined') {o.rotation = {x: 0, y: 0, z: 0}}
142
- if(typeof o.rotationSpeed === 'undefined') {o.rotationSpeed = {x: 0, y: 0, z: 0}}
143
- if(typeof o.texturesPaths === 'undefined') {o.texturesPaths = ['./res/textures/default.png']}
144
- if(typeof o.scale === 'undefined') {o.scale = 1;}
145
- if(typeof o.mainCameraParams === 'undefined') {o.mainCameraParams = this.mainCameraParams}
146
- o.entityArgPass = this.entityArgPass;
147
- o.cameras = this.cameras;
148
- }
149
- let myCube1 = new MECube(this.canvas, this.device, this.context, o)
150
- this.mainRenderBundle.push(myCube1);
151
- }
152
-
153
- addBall = (o) => {
154
- if(typeof o === 'undefined') {
155
- var o = {
156
- scale: 1,
157
- position: {x: 0, y: 0, z: -4},
158
- texturesPaths: ['./res/textures/default.png'],
159
- rotation: {x: 0, y: 0, z: 0},
160
- rotationSpeed: {x: 0, y: 0, z: 0},
161
- entityArgPass: this.entityArgPass,
162
- cameras: this.cameras,
163
- mainCameraParams: this.mainCameraParams
164
- }
165
- } else {
166
- if(typeof o.position === 'undefined') {o.position = {x: 0, y: 0, z: -4}}
167
- if(typeof o.rotation === 'undefined') {o.rotation = {x: 0, y: 0, z: 0}}
168
- if(typeof o.rotationSpeed === 'undefined') {o.rotationSpeed = {x: 0, y: 0, z: 0}}
169
- if(typeof o.texturesPaths === 'undefined') {o.texturesPaths = ['./res/textures/default.png']}
170
- if(typeof o.mainCameraParams === 'undefined') {o.mainCameraParams = this.mainCameraParams}
171
- if(typeof o.scale === 'undefined') {o.scale = 1;}
172
- o.entityArgPass = this.entityArgPass;
173
- o.cameras = this.cameras;
174
- }
175
- let myBall1 = new MEBall(this.canvas, this.device, this.context, o)
176
- this.mainRenderBundle.push(myBall1);
177
- }
178
-
179
- addMeshObj = (o) => {
180
- if(typeof o.position === 'undefined') {o.position = {x: 0, y: 0, z: -4}}
181
- if(typeof o.rotation === 'undefined') {o.rotation = {x: 0, y: 0, z: 0}}
182
- if(typeof o.rotationSpeed === 'undefined') {o.rotationSpeed = {x: 0, y: 0, z: 0}}
183
- if(typeof o.texturesPaths === 'undefined') {o.texturesPaths = ['./res/textures/default.png']}
184
- if(typeof o.mainCameraParams === 'undefined') {o.mainCameraParams = this.mainCameraParams}
185
- if(typeof o.scale === 'undefined') {o.scale = 1;}
186
- o.entityArgPass = this.entityArgPass;
187
- o.cameras = this.cameras;
188
- if(typeof o.name === 'undefined') {o.name = 'random' + Math.random();}
189
- if(typeof o.mesh === 'undefined') {
190
- throw console.error('arg mesh is empty...');
191
- return;
192
- }
193
- if(typeof o.physics === 'undefined') {
194
- o.physics = {
195
- enabled: false,
196
- geometry: "Sphere"
197
- }
198
- }
199
- if(typeof o.physics.enabled === 'undefined') {o.physics.enabled = false}
200
- if(typeof o.physics.geometry === 'undefined') {o.physics.geometry = "Sphere"}
201
- // console.log('Mesh procedure', o)
202
- let myMesh1 = new MEMeshObj(this.canvas, this.device, this.context, o)
203
- if(o.physics.enabled == true) {
204
- this.matrixAmmo.addPhysics(myMesh1, o.physics)
205
- }
206
- this.mainRenderBundle.push(myMesh1);
207
- }
208
-
209
- run(callback) {
210
- setTimeout(() => {requestAnimationFrame(this.frame)}, 30)
211
- setTimeout(() => {callback()}, 20)
212
- }
213
-
214
- frameSinglePass = () => {
215
- if(typeof this.mainRenderBundle == 'undefined') return;
216
- let shadowPass = null;
217
- let renderPass;
218
- let commandEncoder = this.device.createCommandEncoder();
219
-
220
- this.mainRenderBundle.forEach((meItem, index) => {
221
- meItem.position.update();
222
- })
223
-
224
- this.mainRenderBundle.forEach((meItem, index) => {
225
- meItem.draw(commandEncoder);
226
- shadowPass = commandEncoder.beginRenderPass(meItem.shadowPassDescriptor);
227
- shadowPass.setPipeline(meItem.shadowPipeline);
228
- meItem.drawShadows(shadowPass);
229
- shadowPass.end();
230
- })
231
-
232
- this.mainRenderBundle.forEach((meItem, index) => {
233
- if(index == 0) renderPass = commandEncoder.beginRenderPass(meItem.renderPassDescriptor);
234
- if(index == 1) renderPass.setPipeline(meItem.pipeline);
235
- })
236
-
237
- this.mainRenderBundle.forEach((meItem, index) => {
238
- meItem.drawElements(renderPass);
239
- })
240
-
241
- renderPass.end();
242
-
243
- this.device.queue.submit([commandEncoder.finish()]);
244
- requestAnimationFrame(this.frame);
245
- }
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
+
246
322
  }
@@ -1,49 +0,0 @@
1
- import {computeSurfaceNormals, computeProjectedPlaneUVs} from './utils2.js';
2
-
3
- export function makeMeshData1(m) {
4
-
5
- let mesh = {
6
- positions: m.positions,
7
- triangles: m.cells,
8
- normals: [],
9
- uvs: [],
10
- };
11
-
12
- // Compute surface normals
13
- mesh.normals = computeSurfaceNormals(mesh.positions, mesh.triangles);
14
-
15
- // Compute some easy uvs for testing
16
- mesh.uvs = computeProjectedPlaneUVs(mesh.positions, 'xy');
17
-
18
- // // Push indices for an additional ground plane
19
- mesh.triangles.push(
20
- [mesh.positions.length, mesh.positions.length + 2, mesh.positions.length + 1],
21
- // [mesh.positions.length, mesh.positions.length + 1, mesh.positions.length + 3]
22
- );
23
-
24
- // // Push vertex attributes for an additional ground plane
25
- // // prettier-ignore
26
- // mesh.positions.push(
27
- // [-100, 20, -100], //
28
- // [100, 20, 100], //
29
- // [-100, 20, 100], //
30
- // [100, 20, -100]
31
- // );
32
- // mesh.normals.push(
33
- // [0, 1, 0], //
34
- // [0, 1, 0], //
35
- // [0, 1, 0], //
36
- // [0, 1, 0]
37
- // );
38
- // mesh.uvs.push(
39
- // [0, 0], //
40
- // [1, 1], //
41
- // [0, 1], //
42
- // [1, 0]
43
- // );
44
-
45
- // mesh.positions = new Float32Array(m.positions)
46
- // mesh.triangles = new Float32Array(m.cells)
47
-
48
- return mesh;
49
- }