matrix-engine-wgpu 1.0.2 → 1.0.5

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 (51) hide show
  1. package/REFERENCE.md +3 -0
  2. package/app-worker.js +45 -0
  3. package/empty.js +12 -0
  4. package/examples/load-obj-file.js +48 -0
  5. package/examples/unlit-textures.js +27 -0
  6. package/examples.js +7 -0
  7. package/index.js +18 -5
  8. package/main.js +49 -23
  9. package/package.json +11 -2
  10. package/public/ammojs/ammo.js +957 -0
  11. package/public/ammojs/ammo.wasm.js +921 -0
  12. package/public/ammojs/ammo.wasm.wasm +0 -0
  13. package/public/app-worker.js +47 -0
  14. package/public/app.js +2640 -305
  15. package/public/css/style.css +1 -2
  16. package/public/empty.html +25 -0
  17. package/public/empty.js +9107 -0
  18. package/public/examples.html +25 -0
  19. package/public/examples.js +9180 -0
  20. package/public/index.html +1 -1
  21. package/public/res/meshes/blender/lopta.mtl +10 -0
  22. package/public/res/meshes/blender/lopta.obj +3402 -0
  23. package/public/res/meshes/blender/piramyd.blend +0 -0
  24. package/public/res/meshes/blender/piramyd.blend1 +0 -0
  25. package/public/res/meshes/blender/piramyd.js +42 -0
  26. package/public/res/meshes/blender/piramyd.mtl +10 -0
  27. package/public/res/meshes/blender/piramyd.obj +18696 -0
  28. package/public/res/meshes/blender/piramyd1.js +42 -0
  29. package/public/res/meshes/blender/welcomeTextblend.blend +0 -0
  30. package/public/res/meshes/dragon/stanfordDragonData.js +5 -0
  31. package/public/res/meshes/obj/armor.obj +319 -0
  32. package/public/res/meshes/obj/armor.png +0 -0
  33. package/public/worker.html +25 -0
  34. package/readme.md +130 -48
  35. package/src/engine/ball.js +26 -10
  36. package/src/engine/cube.js +100 -81
  37. package/src/engine/engine.js +466 -4
  38. package/src/engine/final/adaptJSON1.js +53 -0
  39. package/src/engine/final/utils2.js +63 -0
  40. package/src/engine/loader-obj.js +469 -0
  41. package/src/engine/matrix-class.js +5 -4
  42. package/src/engine/matrix-mesh.js +49 -0
  43. package/src/engine/mesh-obj.js +534 -0
  44. package/src/engine/utils.js +300 -45
  45. package/src/physics/matrix-ammo.js +119 -0
  46. package/src/shaders/fragment.wgsl.js +48 -0
  47. package/src/shaders/shaders.js +4 -124
  48. package/src/shaders/vertex.wgsl.js +49 -0
  49. package/src/shaders/vertexShadow.wgsl.js +20 -0
  50. package/src/world.js +246 -0
  51. package/src/meWGPU.js +0 -173
@@ -1,6 +1,7 @@
1
- import {BALL_SHADER} from "../shaders/shaders";
1
+ import {UNLIT_SHADER} from "../shaders/shaders";
2
2
  import {mat4, vec3} from 'wgpu-matrix';
3
3
  import {Position, Rotation} from "./matrix-class";
4
+ import {createInputHandler} from "./engine";
4
5
 
5
6
  var SphereLayout = {
6
7
  vertexStride: 8 * 4,
@@ -14,12 +15,18 @@ export default class MECube {
14
15
  constructor(canvas, device, context, o) {
15
16
  this.device = device;
16
17
  this.context = context;
17
-
18
18
  this.entityArgPass = o.entityArgPass;
19
-
20
- console.log('passed args', o.entityArgPass)
19
+ this.inputHandler = createInputHandler(window, canvas);
20
+ this.cameras = o.cameras;
21
+ console.log('passed : o.mainCameraParams.responseCoef ', o.mainCameraParams.responseCoef)
22
+ this.mainCameraParams = {
23
+ type: o.mainCameraParams.type,
24
+ responseCoef: o.mainCameraParams.responseCoef
25
+ } // | WASD 'arcball' };
26
+
27
+ this.lastFrameMS = 0;
21
28
  this.shaderModule = device.createShaderModule({
22
- code: BALL_SHADER,
29
+ code: UNLIT_SHADER,
23
30
  });
24
31
 
25
32
  this.texturesPaths = [];
@@ -30,10 +37,13 @@ export default class MECube {
30
37
 
31
38
  this.presentationFormat = navigator.gpu.getPreferredCanvasFormat();
32
39
  this.position = new Position(o.position.x, o.position.y, o.position.z);
40
+ console.log('cube added on pos : ', this.position)
33
41
  this.rotation = new Rotation(o.rotation.x, o.rotation.y, o.rotation.z);
34
42
  this.rotation.rotationSpeed.x = o.rotationSpeed.x;
35
43
  this.rotation.rotationSpeed.y = o.rotationSpeed.y;
36
44
  this.rotation.rotationSpeed.z = o.rotationSpeed.z;
45
+
46
+ this.scale = o.scale;
37
47
  this.pipeline = device.createRenderPipeline({
38
48
  layout: 'auto',
39
49
  vertex: {
@@ -107,12 +117,15 @@ export default class MECube {
107
117
  mat4.identity(this.transform);
108
118
 
109
119
  // Create one large central planet surrounded by a large ring of asteroids
110
- this.planet = this.createSphereRenderable(1.0);
120
+ this.planet = this.createGeometry({
121
+ scale: this.scale,
122
+ useUVShema4x2: false
123
+ });
111
124
  this.planet.bindGroup = this.createSphereBindGroup(this.texture0, this.transform);
112
125
 
126
+ // can be used like instance draws
113
127
  var asteroids = [
114
- this.createSphereRenderable(0.2, 8, 6, 0.15),
115
- this.createSphereRenderable(0.13, 8, 6, 0.15)
128
+ // this.createGeometry(0.2, 8, 6, 0.15),
116
129
  ];
117
130
 
118
131
  this.renderables = [this.planet];
@@ -122,7 +135,7 @@ export default class MECube {
122
135
  {
123
136
  view: undefined,
124
137
  clearValue: {r: 0.0, g: 0.0, b: 0.0, a: 1.0},
125
- loadOp: this.entityArgPass.loadOp,
138
+ loadOp: this.entityArgPass.loadOp,
126
139
  storeOp: this.entityArgPass.storeOp,
127
140
  },
128
141
  ],
@@ -135,7 +148,7 @@ export default class MECube {
135
148
  };
136
149
 
137
150
  const aspect = canvas.width / canvas.height;
138
- this.projectionMatrix = mat4.perspective((2 * Math.PI) / 5, aspect, 1, 100.0);
151
+ this.projectionMatrix = mat4.perspective((2 * Math.PI) / 5, aspect, 1, 1000.0);
139
152
  this.modelViewProjectionMatrix = mat4.create();
140
153
 
141
154
  this.frameBindGroup = device.createBindGroup({
@@ -195,30 +208,29 @@ export default class MECube {
195
208
  this.renderBundle = renderBundleEncoder.finish();
196
209
  }
197
210
 
198
- createSphereRenderable(radius, widthSegments = 32, heightSegments = 16, randomness = 0) {
199
-
200
- const sphereMesh = this.createCubeVertices();
211
+ createGeometry(options) {
212
+ const mesh = this.createCubeVertices(options);
201
213
  // Create a vertex buffer from the sphere data.
202
214
  const vertices = this.device.createBuffer({
203
- size: sphereMesh.vertices.byteLength,
215
+ size: mesh.vertices.byteLength,
204
216
  usage: GPUBufferUsage.VERTEX,
205
217
  mappedAtCreation: true,
206
218
  });
207
- new Float32Array(vertices.getMappedRange()).set(sphereMesh.vertices);
219
+ new Float32Array(vertices.getMappedRange()).set(mesh.vertices);
208
220
  vertices.unmap();
209
221
 
210
222
  const indices = this.device.createBuffer({
211
- size: sphereMesh.indices.byteLength,
223
+ size: mesh.indices.byteLength,
212
224
  usage: GPUBufferUsage.INDEX,
213
225
  mappedAtCreation: true,
214
226
  });
215
- new Uint16Array(indices.getMappedRange()).set(sphereMesh.indices);
227
+ new Uint16Array(indices.getMappedRange()).set(mesh.indices);
216
228
  indices.unmap();
217
229
 
218
230
  return {
219
231
  vertices,
220
232
  indices,
221
- indexCount: sphereMesh.indices.length,
233
+ indexCount: mesh.indices.length,
222
234
  };
223
235
  }
224
236
 
@@ -257,19 +269,21 @@ export default class MECube {
257
269
  }
258
270
 
259
271
  getTransformationMatrix(pos) {
260
- // const now = Date.now() / 1000;
272
+ const now = Date.now();
273
+ const deltaTime = (now - this.lastFrameMS) / this.mainCameraParams.responseCoef;
274
+ this.lastFrameMS = now;
261
275
 
262
- const viewMatrix = mat4.identity();
276
+ // const viewMatrix = mat4.identity(); ORI
277
+ const camera = this.cameras[this.mainCameraParams.type];
278
+ const viewMatrix = camera.update(deltaTime, this.inputHandler());
263
279
 
264
280
  mat4.translate(viewMatrix, vec3.fromValues(pos.x, pos.y, pos.z), viewMatrix);
265
-
266
281
  mat4.rotateX(viewMatrix, Math.PI * this.rotation.getRotX(), viewMatrix);
267
282
  mat4.rotateY(viewMatrix, Math.PI * this.rotation.getRotY(), viewMatrix);
268
283
  mat4.rotateZ(viewMatrix, Math.PI * this.rotation.getRotZ(), viewMatrix);
269
-
270
284
  mat4.multiply(this.projectionMatrix, viewMatrix, this.modelViewProjectionMatrix);
271
- return this.modelViewProjectionMatrix;
272
285
 
286
+ return this.modelViewProjectionMatrix;
273
287
  }
274
288
 
275
289
  async loadTex1(device) {
@@ -319,6 +333,10 @@ export default class MECube {
319
333
  }
320
334
 
321
335
 
336
+ draw = () => {}
337
+ drawElements = () => {}
338
+ drawShadows = () => {}
339
+
322
340
  // Render bundles function as partial, limited render passes, so we can use the
323
341
  // same code both to render the scene normally and to build the render bundle.
324
342
  renderScene(passEncoder) {
@@ -353,80 +371,81 @@ export default class MECube {
353
371
  useUVShema4x2: false
354
372
  }
355
373
  }
374
+ if(typeof options.scale === 'undefined') options.scale = 1;
356
375
 
357
376
  let vertices;
358
377
  if(options.useUVShema4x2 == true) {
359
- vertices = new Float32Array([
360
- // position | texture coordinate
361
- //-------------+----------------------
362
- // front face select the top left image 1, 0.5,
363
- -1, 1, 1, 1, 0, 0, 0, 0,
364
- -1, -1, 1, 1, 0, 0, 0, 0.5,
365
- 1, 1, 1, 1, 0, 0, 0.25, 0,
366
- 1, -1, 1, 1, 0, 0, 0.25, 0.5,
367
- // right face select the top middle image
368
- 1, 1, -1, 1, 0, 0, 0.25, 0,
369
- 1, 1, 1, 1, 0, 0, 0.5, 0,
370
- 1, -1, -1, 1, 0, 0, 0.25, 0.5,
371
- 1, -1, 1, 1, 0, 0, 0.5, 0.5,
372
- // back face select to top right image
373
- 1, 1, -1, 1, 0, 0, 0.5, 0,
374
- 1, -1, -1, 1, 0, 0, 0.5, 0.5,
375
- -1, 1, -1, 1, 0, 0, 0.75, 0,
376
- -1, -1, -1, 1, 0, 0, 0.75, 0.5,
377
- // left face select the bottom left image
378
- -1, 1, 1, 1, 0, 0, 0, 0.5,
379
- -1, 1, -1, 1, 0, 0, 0.25, 0.5,
380
- -1, -1, 1, 1, 0, 0, 0, 1,
381
- -1, -1, -1, 1, 0, 0, 0.25, 1,
382
- // bottom face select the bottom middle image
383
- 1, -1, 1, 1, 0, 0, 0.25, 0.5,
384
- -1, -1, 1, 1, 0, 0, 0.5, 0.5,
385
- 1, -1, -1, 1, 0, 0, 0.25, 1,
386
- -1, -1, -1, 1, 0, 0, 0.5, 1,
387
- // top face select the bottom right image
388
- -1, 1, 1, 1, 0, 0, 0.5, 0.5,
389
- 1, 1, 1, 1, 0, 0, 0.75, 0.5,
390
- -1, 1, -1, 1, 0, 0, 0.5, 1,
391
- 1, 1, -1, 1, 0, 0, 0.75, 1,
392
- ]);
393
- } else {
394
378
  vertices = new Float32Array([
395
379
  // position | texture coordinate
396
380
  //-------------+----------------------
397
381
  // front face select the top left image 1, 0.5,
398
382
  -1, 1, 1, 1, 0, 0, 0, 0,
383
+ -1, -1, 1, 1, 0, 0, 0, 0.5,
384
+ 1, 1, 1, 1, 0, 0, 0.25, 0,
385
+ 1, -1, 1, 1, 0, 0, 0.25, 0.5,
386
+ // right face select the top middle image
387
+ 1, 1, -1, 1, 0, 0, 0.25, 0,
388
+ 1, 1, 1, 1, 0, 0, 0.5, 0,
389
+ 1, -1, -1, 1, 0, 0, 0.25, 0.5,
390
+ 1, -1, 1, 1, 0, 0, 0.5, 0.5,
391
+ // back face select to top right image
392
+ 1, 1, -1, 1, 0, 0, 0.5, 0,
393
+ 1, -1, -1, 1, 0, 0, 0.5, 0.5,
394
+ -1, 1, -1, 1, 0, 0, 0.75, 0,
395
+ -1, -1, -1, 1, 0, 0, 0.75, 0.5,
396
+ // left face select the bottom left image
397
+ -1, 1, 1, 1, 0, 0, 0, 0.5,
398
+ -1, 1, -1, 1, 0, 0, 0.25, 0.5,
399
399
  -1, -1, 1, 1, 0, 0, 0, 1,
400
- 1, 1, 1, 1, 0, 0, 1, 0,
401
- 1, -1, 1, 1, 0, 0, 1, 1,
400
+ -1, -1, -1, 1, 0, 0, 0.25, 1,
401
+ // bottom face select the bottom middle image
402
+ 1, -1, 1, 1, 0, 0, 0.25, 0.5,
403
+ -1, -1, 1, 1, 0, 0, 0.5, 0.5,
404
+ 1, -1, -1, 1, 0, 0, 0.25, 1,
405
+ -1, -1, -1, 1, 0, 0, 0.5, 1,
406
+ // top face select the bottom right image
407
+ -1, 1, 1, 1, 0, 0, 0.5, 0.5,
408
+ 1, 1, 1, 1, 0, 0, 0.75, 0.5,
409
+ -1, 1, -1, 1, 0, 0, 0.5, 1,
410
+ 1, 1, -1, 1, 0, 0, 0.75, 1,
411
+ ]);
412
+ } else {
413
+ vertices = new Float32Array([
414
+ // position | texture coordinate
415
+ //------------- +----------------------
416
+ // front face select the top left image 1, 0.5,
417
+ -1 * options.scale, 1 * options.scale, 1 * options.scale, 1, 0, 0, 0, 0,
418
+ -1 * options.scale, -1 * options.scale, 1 * options.scale, 1, 0, 0, 0, 1,
419
+ 1 * options.scale, 1 * options.scale, 1 * options.scale, 1, 0, 0, 1, 0,
420
+ 1 * options.scale, -1 * options.scale, 1 * options.scale, 1, 0, 0, 1, 1,
402
421
  // right face select the top middle image
403
- 1, 1, -1, 1, 0, 0, 0, 0,
404
- 1, 1, 1, 1, 0, 0, 0, 1,
405
- 1, -1, -1, 1, 0, 0, 1, 0,
406
- 1, -1, 1, 1, 0, 0, 1, 1,
422
+ 1 * options.scale, 1 * options.scale, -1 * options.scale, 1, 0, 0, 0, 0,
423
+ 1 * options.scale, 1 * options.scale, 1 * options.scale, 1, 0, 0, 0, 1,
424
+ 1 * options.scale, -1 * options.scale, -1 * options.scale, 1, 0, 0, 1, 0,
425
+ 1 * options.scale, -1 * options.scale, 1 * options.scale, 1, 0, 0, 1, 1,
407
426
  // back face select to top right image
408
- 1, 1, -1, 1, 0, 0, 0, 0,
409
- 1, -1, -1, 1, 0, 0, 0, 1,
410
- -1, 1, -1, 1, 0, 0, 1, 0,
411
- -1, -1, -1, 1, 0, 0, 1, 1,
427
+ 1 * options.scale, 1 * options.scale, -1 * options.scale, 1, 0, 0, 0, 0,
428
+ 1 * options.scale, -1 * options.scale, -1 * options.scale, 1, 0, 0, 0, 1,
429
+ -1 * options.scale, 1 * options.scale, -1 * options.scale, 1, 0, 0, 1, 0,
430
+ -1 * options.scale, -1 * options.scale, -1 * options.scale, 1, 0, 0, 1, 1,
412
431
  // left face select the bottom left image
413
- -1, 1, 1, 1, 0, 0, 0, 0,
414
- -1, 1, -1, 1, 0, 0, 0, 1,
415
- -1, -1, 1, 1, 0, 0, 1, 0,
416
- -1, -1, -1, 1, 0, 0, 1, 1,
432
+ -1 * options.scale, 1 * options.scale, 1 * options.scale, 1, 0, 0, 0, 0,
433
+ -1 * options.scale, 1 * options.scale, -1 * options.scale, 1, 0, 0, 0, 1,
434
+ -1 * options.scale, -1 * options.scale, 1 * options.scale, 1, 0, 0, 1, 0,
435
+ -1 * options.scale, -1 * options.scale, -1 * options.scale, 1, 0, 0, 1, 1,
417
436
  // bottom face select the bottom middle image
418
- 1, -1, 1, 1, 0, 0, 0, 0,
419
- -1, -1, 1, 1, 0, 0, 0, 1,
420
- 1, -1, -1, 1, 0, 0, 1, 0,
421
- -1, -1, -1, 1, 0, 0, 1, 1,
437
+ 1 * options.scale, -1 * options.scale, 1 * options.scale, 1, 0, 0, 0, 0,
438
+ -1 * options.scale, -1 * options.scale, 1 * options.scale, 1, 0, 0, 0, 1,
439
+ 1 * options.scale, -1 * options.scale, -1 * options.scale, 1, 0, 0, 1, 0,
440
+ -1 * options.scale, -1 * options.scale, -1 * options.scale, 1, 0, 0, 1, 1,
422
441
  // top face select the bottom right image
423
- -1, 1, 1, 1, 0, 0, 0, 0,
424
- 1, 1, 1, 1, 0, 0, 0, 1,
425
- -1, 1, -1, 1, 0, 0, 1, 0,
426
- 1, 1, -1, 1, 0, 0, 1, 1,
442
+ -1 * options.scale, 1 * options.scale, 1 * options.scale, 1, 0, 0, 0, 0,
443
+ 1 * options.scale, 1 * options.scale, 1 * options.scale, 1, 0, 0, 0, 1,
444
+ -1 * options.scale, 1 * options.scale, -1 * options.scale, 1, 0, 0, 1, 0,
445
+ 1 * options.scale, 1 * options.scale, -1 * options.scale, 1, 0, 0, 1, 1,
427
446
  ])
428
447
  }
429
-
448
+
430
449
 
431
450
  const indices = new Uint16Array([
432
451
  0, 1, 2, 2, 1, 3, // front