matrix-engine-wgpu 1.3.12 → 1.3.16

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.
@@ -1,49 +0,0 @@
1
- export let vertexWGSL = `struct Scene {
2
- lightViewProjMatrix: mat4x4f,
3
- cameraViewProjMatrix: mat4x4f,
4
- lightPos: vec3f,
5
- }
6
-
7
- struct Model {
8
- modelMatrix: mat4x4f,
9
- }
10
-
11
- @group(0) @binding(0) var<uniform> scene : Scene;
12
- @group(1) @binding(0) var<uniform> model : Model;
13
-
14
- struct VertexOutput {
15
- @location(0) shadowPos: vec3f,
16
- @location(1) fragPos: vec3f,
17
- @location(2) fragNorm: vec3f,
18
- @location(3) uv : vec2f,
19
-
20
- @builtin(position) Position: vec4f,
21
- }
22
-
23
- @vertex
24
- fn main(
25
- @location(0) position: vec3f,
26
- @location(1) normal: vec3f,
27
- @location(2) uv : vec2f
28
- ) -> VertexOutput {
29
- var output : VertexOutput;
30
-
31
- // XY is in (-1, 1) space, Z is in (0, 1) space
32
- let posFromLight = scene.lightViewProjMatrix * model.modelMatrix * vec4(position, 1.0);
33
-
34
- // Convert XY to (0, 1)
35
- // Y is flipped because texture coords are Y-down.
36
- output.shadowPos = vec3(
37
- posFromLight.xy * vec2(0.5, -0.5) + vec2(0.5),
38
- posFromLight.z
39
- );
40
-
41
- output.Position = scene.cameraViewProjMatrix * model.modelMatrix * vec4(position, 1.0);
42
- output.fragPos = output.Position.xyz;
43
- output.fragNorm = normal;
44
- // nidza
45
- output.uv = uv;
46
-
47
- return output;
48
- }
49
- `;
@@ -1,20 +0,0 @@
1
- export let vertexShadowWGSL = `struct Scene {
2
- lightViewProjMatrix: mat4x4f,
3
- cameraViewProjMatrix: mat4x4f,
4
- lightPos: vec3f,
5
- }
6
-
7
- struct Model {
8
- modelMatrix: mat4x4f,
9
- }
10
-
11
- @group(0) @binding(0) var<uniform> scene : Scene;
12
- @group(1) @binding(0) var<uniform> model : Model;
13
-
14
- @vertex
15
- fn main(
16
- @location(0) position: vec3f
17
- ) -> @builtin(position) vec4f {
18
- return scene.lightViewProjMatrix * model.modelMatrix * vec4(position, 1);
19
- }
20
- `
@@ -1,69 +0,0 @@
1
- export class MatrixSounds {
2
-
3
- constructor() {
4
- this.volume = 0.5;
5
- this.audios = {};
6
- this.enabled = true; // 🔇 global flag to mute/allow audio
7
- }
8
-
9
- muteAll() {
10
- this.enabled = false;
11
- Object.values(this.audios).forEach(audio => audio.pause());
12
- }
13
-
14
- unmuteAll() {
15
- this.enabled = true;
16
- }
17
-
18
- createClones(c, name, path) {
19
- for(let x = 1;x < c;x++) {
20
- const a = new Audio(path);
21
- a.id = name + x;
22
- a.volume = this.volume;
23
- this.audios[name + x] = a;
24
- document.body.append(a);
25
- }
26
- }
27
-
28
- createAudio(name, path, useClones) {
29
- const a = new Audio(path);
30
- a.id = name;
31
- a.volume = this.volume;
32
- this.audios[name] = a;
33
- document.body.append(a);
34
- if(typeof useClones !== 'undefined') {
35
- this.createClones(useClones, name, path);
36
- }
37
- }
38
-
39
- play(name) {
40
- if(!this.enabled) return; // 🔇 prevent playing if muted
41
-
42
- const audio = this.audios[name];
43
- if(!audio) return;
44
-
45
- if(audio.paused) {
46
- audio.play().catch((e) => {
47
- if(e.name !== 'NotAllowedError') console.warn("sounds error:", e);
48
- });
49
- } else {
50
- this.tryClone(name);
51
- }
52
- }
53
-
54
- tryClone(name) {
55
- if(!this.enabled) return; // 🔇 prevent playing clones
56
-
57
- let cc = 1;
58
- try {
59
- while(this.audios[name + cc] && this.audios[name + cc].paused === false) {
60
- cc++;
61
- }
62
- if(this.audios[name + cc]) {
63
- this.audios[name + cc].play();
64
- }
65
- } catch(err) {
66
- console.warn("Clone play failed:", err);
67
- }
68
- }
69
- }
package/src/world.js DELETED
@@ -1,399 +0,0 @@
1
- import {vec3} from "wgpu-matrix";
2
- import MEBall from "./engine/ball.js";
3
- import MECube from './engine/cube.js';
4
- import {ArcballCamera, WASDCamera} from "./engine/engine.js";
5
- import MEMesh from "./engine/mesh.js";
6
- import MEMeshObj from "./engine/mesh-obj.js";
7
- import MatrixAmmo from "./physics/matrix-ammo.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";
11
- import {play} from "./engine/loader-obj.js";
12
-
13
- export default class MatrixEngineWGPU {
14
-
15
- mainRenderBundle = [];
16
- rbContainer = [];
17
- frame = () => {};
18
-
19
- entityHolder = [];
20
-
21
- entityArgPass = {
22
- loadOp: 'clear',
23
- storeOp: 'store',
24
- depthLoadOp: 'clear',
25
- depthStoreOp: 'store'
26
- }
27
-
28
- matrixAmmo = new MatrixAmmo();
29
- matrixSounds = new MatrixSounds();
30
-
31
- // The input handler
32
- constructor(options, callback) {
33
- // console.log('typeof options ', typeof options )
34
- if(typeof options == 'undefined' || typeof options == "function") {
35
- this.options = {
36
- useSingleRenderPass: true,
37
- canvasSize: 'fullscreen',
38
- canvasId: 'canvas1',
39
- mainCameraParams: {
40
- type: 'WASD',
41
- responseCoef: 2000
42
- },
43
- clearColor: {r: 0.584, g: 0, b: 0.239, a: 1.0}
44
- }
45
- callback = options;
46
- }
47
- if(typeof options.clearColor === 'undefined') {
48
- options.clearColor = {r: 0.584, g: 0, b: 0.239, a: 1.0};
49
- }
50
- if(typeof options.canvasId === 'undefined') {
51
- options.canvasId = 'canvas1';
52
- }
53
- if(typeof options.mainCameraParams === 'undefined') {
54
- options.mainCameraParams = {
55
- type: 'WASD',
56
- responseCoef: 2000
57
- }
58
- }
59
- this.options = options;
60
- this.mainCameraParams = options.mainCameraParams;
61
-
62
- const target = this.options.appendTo || document.body;
63
- var canvas = document.createElement('canvas')
64
- canvas.id = this.options.canvasId;
65
- if(this.options.canvasSize == 'fullscreen') {
66
- canvas.width = window.innerWidth;
67
- canvas.height = window.innerHeight;
68
- } else {
69
- canvas.width = this.options.canvasSize.w;
70
- canvas.height = this.options.canvasSize.h;
71
- }
72
- target.append(canvas);
73
-
74
- // The camera types
75
- const initialCameraPosition = vec3.create(0, 0, 0);
76
- // console.log('passed : o.mainCameraParams.responseCoef ', o.mainCameraParams.responseCoef)
77
- this.mainCameraParams = {
78
- type: this.options.mainCameraParams.type,
79
- responseCoef: this.options.mainCameraParams.responseCoef
80
- }
81
-
82
- this.cameras = {
83
- arcball: new ArcballCamera({position: initialCameraPosition}),
84
- WASD: new WASDCamera({position: initialCameraPosition}),
85
- };
86
-
87
- this.label = new MultiLang()
88
- if(urlQuery.lang != null) {
89
- this.label.loadMultilang(urlQuery.lang).then((r) => {
90
- this.label.get = r;
91
- });
92
- } else {
93
- this.label.loadMultilang().then((r) => {
94
- this.label.get = r;
95
- });
96
- }
97
-
98
- this.init({canvas, callback})
99
- }
100
-
101
- init = async ({canvas, callback}) => {
102
- this.canvas = canvas;
103
- this.adapter = await navigator.gpu.requestAdapter();
104
- this.device = await this.adapter.requestDevice({
105
- extensions: ["ray_tracing"]
106
- });
107
-
108
- // Maybe works in ssl with webworkers...
109
- // const adapterInfo = await this.adapter.requestAdapterInfo();
110
- // var test = this.adapter.features()
111
- // console.log(adapterInfo.vendor);
112
- // console.log('test' + test);
113
- // console.log("FEATURES : " + this.adapter.features)
114
-
115
- this.context = canvas.getContext('webgpu');
116
-
117
- const devicePixelRatio = window.devicePixelRatio;
118
- canvas.width = canvas.clientWidth * devicePixelRatio;
119
- canvas.height = canvas.clientHeight * devicePixelRatio;
120
- const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
121
-
122
- this.context.configure({
123
- device: this.device,
124
- format: presentationFormat,
125
- alphaMode: 'premultiplied',
126
- });
127
-
128
- if(this.options.useSingleRenderPass == true) {
129
- this.frame = this.frameSinglePass;
130
- } else {
131
- this.frame = this.framePassPerObject;
132
- }
133
-
134
- this.run(callback)
135
- };
136
-
137
- getSceneObjectByName(name) {
138
- return this.mainRenderBundle.find((sceneObject) => sceneObject.name === name )
139
- }
140
-
141
- // Not in use for now
142
- addCube = (o) => {
143
- if(typeof o === 'undefined') {
144
- var o = {
145
- scale: 1,
146
- position: {x: 0, y: 0, z: -4},
147
- texturesPaths: ['./res/textures/default.png'],
148
- rotation: {x: 0, y: 0, z: 0},
149
- rotationSpeed: {x: 0, y: 0, z: 0},
150
- entityArgPass: this.entityArgPass,
151
- cameras: this.cameras,
152
- mainCameraParams: this.mainCameraParams
153
- }
154
- } else {
155
- if(typeof o.position === 'undefined') {o.position = {x: 0, y: 0, z: -4}}
156
- if(typeof o.rotation === 'undefined') {o.rotation = {x: 0, y: 0, z: 0}}
157
- if(typeof o.rotationSpeed === 'undefined') {o.rotationSpeed = {x: 0, y: 0, z: 0}}
158
- if(typeof o.texturesPaths === 'undefined') {o.texturesPaths = ['./res/textures/default.png']}
159
- if(typeof o.scale === 'undefined') {o.scale = 1;}
160
- if(typeof o.mainCameraParams === 'undefined') {o.mainCameraParams = this.mainCameraParams}
161
- o.entityArgPass = this.entityArgPass;
162
- o.cameras = this.cameras;
163
- }
164
-
165
- if(typeof o.physics === 'undefined') {
166
- o.physics = {
167
- scale: [1, 1, 1],
168
- enabled: true,
169
- geometry: "Sphere",
170
- radius: o.scale,
171
- name: o.name,
172
- rotation: o.rotation
173
- }
174
- }
175
- if(typeof o.position !== 'undefined') {o.physics.position = o.position;}
176
- if(typeof o.physics.enabled === 'undefined') {o.physics.enabled = true}
177
- if(typeof o.physics.geometry === 'undefined') {o.physics.geometry = "Sphere"}
178
- if(typeof o.physics.radius === 'undefined') {o.physics.radius = o.scale}
179
- if(typeof o.physics.mass === 'undefined') {o.physics.mass = 1;}
180
- if(typeof o.physics.name === 'undefined') {o.physics.name = o.name;}
181
- if(typeof o.physics.scale === 'undefined') {o.physics.scale = o.scale;}
182
- if(typeof o.physics.rotation === 'undefined') {o.physics.rotation = o.rotation;}
183
-
184
- let myCube1 = new MECube(this.canvas, this.device, this.context, o)
185
- if(o.physics.enabled == true) {
186
- this.matrixAmmo.addPhysics(myCube1, o.physics);
187
- }
188
- this.mainRenderBundle.push(myCube1);
189
- }
190
-
191
- // Not in use for now
192
- addBall = (o) => {
193
- if(typeof o === 'undefined') {
194
- var o = {
195
- scale: 1,
196
- position: {x: 0, y: 0, z: -4},
197
- texturesPaths: ['./res/textures/default.png'],
198
- rotation: {x: 0, y: 0, z: 0},
199
- rotationSpeed: {x: 0, y: 0, z: 0},
200
- entityArgPass: this.entityArgPass,
201
- cameras: this.cameras,
202
- mainCameraParams: this.mainCameraParams
203
- }
204
- } else {
205
- if(typeof o.position === 'undefined') {o.position = {x: 0, y: 0, z: -4}}
206
- if(typeof o.rotation === 'undefined') {o.rotation = {x: 0, y: 0, z: 0}}
207
- if(typeof o.rotationSpeed === 'undefined') {o.rotationSpeed = {x: 0, y: 0, z: 0}}
208
- if(typeof o.texturesPaths === 'undefined') {o.texturesPaths = ['./res/textures/default.png']}
209
- if(typeof o.mainCameraParams === 'undefined') {o.mainCameraParams = this.mainCameraParams}
210
- if(typeof o.scale === 'undefined') {o.scale = 1;}
211
- o.entityArgPass = this.entityArgPass;
212
- o.cameras = this.cameras;
213
- }
214
-
215
- if(typeof o.physics === 'undefined') {
216
- o.physics = {
217
- scale: [1, 1, 1],
218
- enabled: true,
219
- geometry: "Sphere",
220
- radius: o.scale,
221
- name: o.name,
222
- rotation: o.rotation
223
- }
224
- }
225
- if(typeof o.position !== 'undefined') {o.physics.position = o.position;}
226
- if(typeof o.physics.enabled === 'undefined') {o.physics.enabled = true}
227
- if(typeof o.physics.geometry === 'undefined') {o.physics.geometry = "Sphere"}
228
- if(typeof o.physics.radius === 'undefined') {o.physics.radius = o.scale}
229
- if(typeof o.physics.mass === 'undefined') {o.physics.mass = 1;}
230
- if(typeof o.physics.name === 'undefined') {o.physics.name = o.name;}
231
- if(typeof o.physics.scale === 'undefined') {o.physics.scale = o.scale;}
232
- if(typeof o.physics.rotation === 'undefined') {o.physics.rotation = o.rotation;}
233
-
234
- let myBall1 = new MEBall(this.canvas, this.device, this.context, o);
235
- if(o.physics.enabled == true) {
236
- this.matrixAmmo.addPhysics(myBall1, o.physics)
237
- }
238
- this.mainRenderBundle.push(myBall1);
239
- }
240
-
241
- // Not in use for now
242
- addMesh = (o) => {
243
- if(typeof o.position === 'undefined') {o.position = {x: 0, y: 0, z: -4}}
244
- if(typeof o.rotation === 'undefined') {o.rotation = {x: 0, y: 0, z: 0}}
245
- if(typeof o.rotationSpeed === 'undefined') {o.rotationSpeed = {x: 0, y: 0, z: 0}}
246
- if(typeof o.texturesPaths === 'undefined') {o.texturesPaths = ['./res/textures/default.png']}
247
- if(typeof o.mainCameraParams === 'undefined') {o.mainCameraParams = this.mainCameraParams}
248
- if(typeof o.scale === 'undefined') {o.scale = 1;}
249
- o.entityArgPass = this.entityArgPass;
250
- o.cameras = this.cameras;
251
- if(typeof o.name === 'undefined') {o.name = 'random' + Math.random();}
252
- if(typeof o.mesh === 'undefined') {
253
- throw console.error('arg mesh is empty...');
254
- return;
255
- }
256
- console.log('Mesh procedure', o)
257
- let myMesh1 = new MEMesh(this.canvas, this.device, this.context, o)
258
- this.mainRenderBundle.push(myMesh1);
259
- }
260
-
261
- addMeshObj = (o , clearColor=this.options.clearColor) => {
262
- if(typeof o.name === 'undefined') {o.name = genName(9)}
263
- if(typeof o.position === 'undefined') {o.position = {x: 0, y: 0, z: -4}}
264
- if(typeof o.rotation === 'undefined') {o.rotation = {x: 0, y: 0, z: 0}}
265
- if(typeof o.rotationSpeed === 'undefined') {o.rotationSpeed = {x: 0, y: 0, z: 0}}
266
- if(typeof o.texturesPaths === 'undefined') {o.texturesPaths = ['./res/textures/default.png']}
267
- if(typeof o.mainCameraParams === 'undefined') {o.mainCameraParams = this.mainCameraParams}
268
- if(typeof o.scale === 'undefined') {o.scale = [1, 1, 1];}
269
- if(typeof o.raycast === 'undefined') {o.raycast = {enabled: false, radius: 2}}
270
- o.entityArgPass = this.entityArgPass;
271
- o.cameras = this.cameras;
272
- if(typeof o.physics === 'undefined') {
273
- o.physics = {
274
- scale: [1, 1, 1],
275
- enabled: true,
276
- geometry: "Sphere",// must be fixed<<
277
- radius: (typeof o.scale == Number ? o.scale : o.scale[0]),
278
- name: o.name,
279
- rotation: o.rotation
280
- }
281
- }
282
- if(typeof o.physics.enabled === 'undefined') {o.physics.enabled = true}
283
- if(typeof o.physics.geometry === 'undefined') {o.physics.geometry = "Cube"}
284
- if(typeof o.physics.radius === 'undefined') {o.physics.radius = o.scale}
285
- if(typeof o.physics.mass === 'undefined') {o.physics.mass = 1;}
286
- if(typeof o.physics.name === 'undefined') {o.physics.name = o.name;}
287
- if(typeof o.physics.scale === 'undefined') {o.physics.scale = o.scale;}
288
- if(typeof o.physics.rotation === 'undefined') {o.physics.rotation = o.rotation;}
289
- o.physics.position = o.position;
290
- // console.log('Mesh procedure', o)
291
- // TEST OBJS SEQ ANIMS
292
- if(typeof o.objAnim == 'undefined' || typeof o.objAnim == null) {
293
- o.objAnim = null;
294
- } else {
295
- // console.log('o.anim', o.objAnim)
296
- if(typeof o.objAnim.animations !== 'undefined') {
297
- o.objAnim.play = play;
298
- }
299
- // no need for single test it in future
300
- o.objAnim.meshList = o.objAnim.meshList;
301
-
302
- if(typeof o.mesh === 'undefined') {
303
- o.mesh = o.objAnim.meshList[0];
304
- console.info('objSeq animation is active.');
305
- }
306
- // scale for all second option!
307
- o.objAnim.scaleAll = function(s) {
308
- for(var k in this.meshList) {
309
- console.log('SCALE');
310
- this.meshList[k].setScale(s);
311
- }
312
- }
313
- }
314
- let myMesh1 = new MEMeshObj(this.canvas, this.device, this.context, o);
315
- myMesh1.clearColor = clearColor;
316
- if(o.physics.enabled == true) {
317
- this.matrixAmmo.addPhysics(myMesh1, o.physics)
318
- }
319
- this.mainRenderBundle.push(myMesh1);
320
- }
321
-
322
- run(callback) {
323
- setTimeout(() => {requestAnimationFrame(this.frame)}, 500)
324
- setTimeout(() => {callback(this)}, 20)
325
- }
326
-
327
- destroyProgram = () => {
328
- this.mainRenderBundle = [];
329
- this.canvas.remove();
330
- }
331
-
332
- frameSinglePass = () => {
333
- if(typeof this.mainRenderBundle == 'undefined') return;
334
- try {
335
- let shadowPass = null;
336
- let renderPass;
337
- let commandEncoder = this.device.createCommandEncoder();
338
-
339
- this.mainRenderBundle.forEach((meItem, index) => {
340
- meItem.position.update();
341
- })
342
-
343
- if (this.matrixAmmo) this.matrixAmmo.updatePhysics();
344
-
345
- this.mainRenderBundle.forEach((meItem, index) => {
346
- meItem.draw(commandEncoder);
347
-
348
- shadowPass = commandEncoder.beginRenderPass(meItem.shadowPassDescriptor);
349
- shadowPass.setPipeline(meItem.shadowPipeline);
350
- meItem.drawShadows(shadowPass);
351
- shadowPass.end();
352
- })
353
-
354
- this.mainRenderBundle.forEach((meItem, index) => {
355
- if(index == 0) {
356
- renderPass = commandEncoder.beginRenderPass(meItem.renderPassDescriptor);
357
- renderPass.setPipeline(meItem.pipeline);
358
- }
359
- })
360
-
361
- this.mainRenderBundle.forEach((meItem, index) => {
362
- meItem.drawElements(renderPass);
363
- })
364
- if(renderPass) renderPass.end();
365
-
366
- this.device.queue.submit([commandEncoder.finish()]);
367
- requestAnimationFrame(this.frame);
368
- } catch(err) {
369
- console.log('%cDraw func (err):' + err, LOG_WARN)
370
- requestAnimationFrame(this.frame);
371
- }
372
- }
373
-
374
- framePassPerObject = () => {
375
- let commandEncoder = this.device.createCommandEncoder();
376
- this.matrixAmmo.updatePhysics();
377
- this.mainRenderBundle.forEach((meItem, index) => {
378
- if(index === 0) {
379
- if(meItem.renderPassDescriptor) meItem.renderPassDescriptor.colorAttachments[0].loadOp = 'clear';
380
- } else {
381
- if(meItem.renderPassDescriptor) meItem.renderPassDescriptor.colorAttachments[0].loadOp = 'load';
382
- }
383
- // Update transforms, physics, etc. (optional)
384
- meItem.draw(commandEncoder); // optional: if this does per-frame updates
385
- if(meItem.renderBundle) {
386
- // Set up view per object
387
- meItem.renderPassDescriptor.colorAttachments[0].view =
388
- this.context.getCurrentTexture().createView();
389
- const passEncoder = commandEncoder.beginRenderPass(meItem.renderPassDescriptor);
390
- passEncoder.executeBundles([meItem.renderBundle]); // ✅ Use only this bundle
391
- passEncoder.end();
392
- } else {
393
- meItem.draw(commandEncoder); // fallback if no renderBundle
394
- }
395
- });
396
- this.device.queue.submit([commandEncoder.finish()]);
397
- requestAnimationFrame(this.frame);
398
- }
399
- }