matrix-engine-wgpu 1.0.1 → 1.0.4

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 (46) hide show
  1. package/LICENSE +22 -17
  2. package/REFERENCE.md +48 -2
  3. package/app-worker.js +45 -0
  4. package/empty.js +12 -0
  5. package/examples/load-obj-file.js +48 -0
  6. package/examples/unlit-textures.js +27 -0
  7. package/examples.js +7 -0
  8. package/index.js +18 -5
  9. package/main.js +29 -12
  10. package/package.json +11 -2
  11. package/public/app-worker.js +47 -0
  12. package/public/app.js +2833 -279
  13. package/public/css/style.css +1 -2
  14. package/public/empty.html +25 -0
  15. package/public/empty.js +9107 -0
  16. package/public/examples.html +25 -0
  17. package/public/examples.js +9180 -0
  18. package/public/res/meshes/blender/piramyd.blend +0 -0
  19. package/public/res/meshes/blender/piramyd.blend1 +0 -0
  20. package/public/res/meshes/blender/piramyd.js +42 -0
  21. package/public/res/meshes/blender/piramyd.mtl +10 -0
  22. package/public/res/meshes/blender/piramyd.obj +18696 -0
  23. package/public/res/meshes/blender/piramyd1.js +42 -0
  24. package/public/res/meshes/blender/welcomeTextblend.blend +0 -0
  25. package/public/res/meshes/dragon/stanfordDragonData.js +5 -0
  26. package/public/res/meshes/obj/armor.obj +319 -0
  27. package/public/res/meshes/obj/armor.png +0 -0
  28. package/public/worker.html +25 -0
  29. package/readme.md +77 -36
  30. package/src/engine/ball.js +43 -23
  31. package/src/engine/cube.js +112 -87
  32. package/src/engine/engine.js +470 -0
  33. package/src/engine/final/adaptJSON1.js +53 -0
  34. package/src/engine/final/utils2.js +63 -0
  35. package/src/engine/loader-obj.js +469 -0
  36. package/src/engine/matrix-class.js +44 -1
  37. package/src/engine/matrix-mesh.js +49 -0
  38. package/src/engine/mesh-obj.js +526 -0
  39. package/src/engine/mesh.js +477 -0
  40. package/src/engine/utils.js +2 -0
  41. package/src/shaders/fragment.wgsl.js +48 -0
  42. package/src/shaders/shaders.js +4 -124
  43. package/src/shaders/vertex.wgsl.js +49 -0
  44. package/src/shaders/vertexShadow.wgsl.js +20 -0
  45. package/src/world.js +263 -0
  46. package/src/meWGPU.js +0 -92
@@ -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
- import {Position} from "./matrix-class";
3
+ import {Position, Rotation} from "./matrix-class";
4
+ import {createInputHandler} from "./engine";
4
5
 
5
6
  var SphereLayout = {
6
7
  vertexStride: 8 * 4,
@@ -14,9 +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
+ this.entityArgPass = 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.cameraParams = {
23
+ type: o.mainCameraParams.type,
24
+ responseCoef: o.mainCameraParams.responseCoef
25
+ } // | WASD 'arcball' };
26
+
27
+ this.lastFrameMS = 0;
18
28
  this.shaderModule = device.createShaderModule({
19
- code: BALL_SHADER,
29
+ code: UNLIT_SHADER,
20
30
  });
21
31
 
22
32
  this.texturesPaths = [];
@@ -26,7 +36,14 @@ export default class MECube {
26
36
  })
27
37
 
28
38
  this.presentationFormat = navigator.gpu.getPreferredCanvasFormat();
29
- this.position = new Position(o.position.x, o.position.y, o.position.z)
39
+ this.position = new Position(o.position.x, o.position.y, o.position.z);
40
+ console.log('cube added on pos : ', this.position)
41
+ this.rotation = new Rotation(o.rotation.x, o.rotation.y, o.rotation.z);
42
+ this.rotation.rotationSpeed.x = o.rotationSpeed.x;
43
+ this.rotation.rotationSpeed.y = o.rotationSpeed.y;
44
+ this.rotation.rotationSpeed.z = o.rotationSpeed.z;
45
+
46
+ this.scale = o.scale;
30
47
  this.pipeline = device.createRenderPipeline({
31
48
  layout: 'auto',
32
49
  vertex: {
@@ -100,37 +117,38 @@ export default class MECube {
100
117
  mat4.identity(this.transform);
101
118
 
102
119
  // Create one large central planet surrounded by a large ring of asteroids
103
- this.planet = this.createSphereRenderable(1.0);
120
+ this.planet = this.createGeometry({
121
+ scale: this.scale,
122
+ useUVShema4x2: false
123
+ });
104
124
  this.planet.bindGroup = this.createSphereBindGroup(this.texture0, this.transform);
105
125
 
126
+ // can be used like instance draws
106
127
  var asteroids = [
107
- this.createSphereRenderable(0.2, 8, 6, 0.15),
108
- this.createSphereRenderable(0.13, 8, 6, 0.15)
128
+ // this.createGeometry(0.2, 8, 6, 0.15),
109
129
  ];
110
130
 
111
131
  this.renderables = [this.planet];
112
-
113
132
  // this.ensureEnoughAsteroids(asteroids, this.transform);
114
-
115
133
  this.renderPassDescriptor = {
116
134
  colorAttachments: [
117
135
  {
118
136
  view: undefined,
119
137
  clearValue: {r: 0.0, g: 0.0, b: 0.0, a: 1.0},
120
- loadOp: 'clear',
121
- storeOp: 'store',
138
+ loadOp: this.entityArgPass.loadOp,
139
+ storeOp: this.entityArgPass.storeOp,
122
140
  },
123
141
  ],
124
142
  depthStencilAttachment: {
125
143
  view: this.depthTexture.createView(),
126
144
  depthClearValue: 1.0,
127
- depthLoadOp: 'clear',
128
- depthStoreOp: 'store',
145
+ depthLoadOp: this.entityArgPass.depthLoadOp,
146
+ depthStoreOp: this.entityArgPass.depthStoreOp,
129
147
  },
130
148
  };
131
149
 
132
150
  const aspect = canvas.width / canvas.height;
133
- 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);
134
152
  this.modelViewProjectionMatrix = mat4.create();
135
153
 
136
154
  this.frameBindGroup = device.createBindGroup({
@@ -181,7 +199,7 @@ export default class MECube {
181
199
  }
182
200
 
183
201
  updateRenderBundle() {
184
- console.log('sss')
202
+ console.log('[CUBE] updateRenderBundle')
185
203
  const renderBundleEncoder = this.device.createRenderBundleEncoder({
186
204
  colorFormats: [this.presentationFormat],
187
205
  depthStencilFormat: 'depth24plus',
@@ -190,30 +208,29 @@ export default class MECube {
190
208
  this.renderBundle = renderBundleEncoder.finish();
191
209
  }
192
210
 
193
- createSphereRenderable(radius, widthSegments = 32, heightSegments = 16, randomness = 0) {
194
-
195
- const sphereMesh = this.createCubeVertices();
211
+ createGeometry(options) {
212
+ const mesh = this.createCubeVertices(options);
196
213
  // Create a vertex buffer from the sphere data.
197
214
  const vertices = this.device.createBuffer({
198
- size: sphereMesh.vertices.byteLength,
215
+ size: mesh.vertices.byteLength,
199
216
  usage: GPUBufferUsage.VERTEX,
200
217
  mappedAtCreation: true,
201
218
  });
202
- new Float32Array(vertices.getMappedRange()).set(sphereMesh.vertices);
219
+ new Float32Array(vertices.getMappedRange()).set(mesh.vertices);
203
220
  vertices.unmap();
204
221
 
205
222
  const indices = this.device.createBuffer({
206
- size: sphereMesh.indices.byteLength,
223
+ size: mesh.indices.byteLength,
207
224
  usage: GPUBufferUsage.INDEX,
208
225
  mappedAtCreation: true,
209
226
  });
210
- new Uint16Array(indices.getMappedRange()).set(sphereMesh.indices);
227
+ new Uint16Array(indices.getMappedRange()).set(mesh.indices);
211
228
  indices.unmap();
212
229
 
213
230
  return {
214
231
  vertices,
215
232
  indices,
216
- indexCount: sphereMesh.indices.length,
233
+ indexCount: mesh.indices.length,
217
234
  };
218
235
  }
219
236
 
@@ -252,13 +269,20 @@ export default class MECube {
252
269
  }
253
270
 
254
271
  getTransformationMatrix(pos) {
255
- const viewMatrix = mat4.identity();
272
+ const now = Date.now();
273
+ const deltaTime = (now - this.lastFrameMS) / this.cameraParams.responseCoef;
274
+ this.lastFrameMS = now;
275
+
276
+ // const viewMatrix = mat4.identity(); ORI
277
+ const camera = this.cameras[this.cameraParams.type];
278
+ const viewMatrix = camera.update(deltaTime, this.inputHandler());
279
+
256
280
  mat4.translate(viewMatrix, vec3.fromValues(pos.x, pos.y, pos.z), viewMatrix);
257
- const now = Date.now() / 1000;
258
- mat4.rotateZ(viewMatrix, Math.PI * 0, viewMatrix);
259
- mat4.rotateX(viewMatrix, Math.PI * 0, viewMatrix);
260
- mat4.rotateY(viewMatrix, -45, viewMatrix);
281
+ mat4.rotateX(viewMatrix, Math.PI * this.rotation.getRotX(), viewMatrix);
282
+ mat4.rotateY(viewMatrix, Math.PI * this.rotation.getRotY(), viewMatrix);
283
+ mat4.rotateZ(viewMatrix, Math.PI * this.rotation.getRotZ(), viewMatrix);
261
284
  mat4.multiply(this.projectionMatrix, viewMatrix, this.modelViewProjectionMatrix);
285
+
262
286
  return this.modelViewProjectionMatrix;
263
287
  }
264
288
 
@@ -343,80 +367,81 @@ export default class MECube {
343
367
  useUVShema4x2: false
344
368
  }
345
369
  }
370
+ if(typeof options.scale === 'undefined') options.scale = 1;
346
371
 
347
372
  let vertices;
348
373
  if(options.useUVShema4x2 == true) {
349
- vertices = new Float32Array([
350
- // position | texture coordinate
351
- //-------------+----------------------
352
- // front face select the top left image 1, 0.5,
353
- -1, 1, 1, 1, 0, 0, 0, 0,
354
- -1, -1, 1, 1, 0, 0, 0, 0.5,
355
- 1, 1, 1, 1, 0, 0, 0.25, 0,
356
- 1, -1, 1, 1, 0, 0, 0.25, 0.5,
357
- // right face select the top middle image
358
- 1, 1, -1, 1, 0, 0, 0.25, 0,
359
- 1, 1, 1, 1, 0, 0, 0.5, 0,
360
- 1, -1, -1, 1, 0, 0, 0.25, 0.5,
361
- 1, -1, 1, 1, 0, 0, 0.5, 0.5,
362
- // back face select to top right image
363
- 1, 1, -1, 1, 0, 0, 0.5, 0,
364
- 1, -1, -1, 1, 0, 0, 0.5, 0.5,
365
- -1, 1, -1, 1, 0, 0, 0.75, 0,
366
- -1, -1, -1, 1, 0, 0, 0.75, 0.5,
367
- // left face select the bottom left image
368
- -1, 1, 1, 1, 0, 0, 0, 0.5,
369
- -1, 1, -1, 1, 0, 0, 0.25, 0.5,
370
- -1, -1, 1, 1, 0, 0, 0, 1,
371
- -1, -1, -1, 1, 0, 0, 0.25, 1,
372
- // bottom face select the bottom middle image
373
- 1, -1, 1, 1, 0, 0, 0.25, 0.5,
374
- -1, -1, 1, 1, 0, 0, 0.5, 0.5,
375
- 1, -1, -1, 1, 0, 0, 0.25, 1,
376
- -1, -1, -1, 1, 0, 0, 0.5, 1,
377
- // top face select the bottom right image
378
- -1, 1, 1, 1, 0, 0, 0.5, 0.5,
379
- 1, 1, 1, 1, 0, 0, 0.75, 0.5,
380
- -1, 1, -1, 1, 0, 0, 0.5, 1,
381
- 1, 1, -1, 1, 0, 0, 0.75, 1,
382
- ]);
383
- } else {
384
374
  vertices = new Float32Array([
385
375
  // position | texture coordinate
386
376
  //-------------+----------------------
387
377
  // front face select the top left image 1, 0.5,
388
378
  -1, 1, 1, 1, 0, 0, 0, 0,
379
+ -1, -1, 1, 1, 0, 0, 0, 0.5,
380
+ 1, 1, 1, 1, 0, 0, 0.25, 0,
381
+ 1, -1, 1, 1, 0, 0, 0.25, 0.5,
382
+ // right face select the top middle image
383
+ 1, 1, -1, 1, 0, 0, 0.25, 0,
384
+ 1, 1, 1, 1, 0, 0, 0.5, 0,
385
+ 1, -1, -1, 1, 0, 0, 0.25, 0.5,
386
+ 1, -1, 1, 1, 0, 0, 0.5, 0.5,
387
+ // back face select to top right image
388
+ 1, 1, -1, 1, 0, 0, 0.5, 0,
389
+ 1, -1, -1, 1, 0, 0, 0.5, 0.5,
390
+ -1, 1, -1, 1, 0, 0, 0.75, 0,
391
+ -1, -1, -1, 1, 0, 0, 0.75, 0.5,
392
+ // left face select the bottom left image
393
+ -1, 1, 1, 1, 0, 0, 0, 0.5,
394
+ -1, 1, -1, 1, 0, 0, 0.25, 0.5,
389
395
  -1, -1, 1, 1, 0, 0, 0, 1,
390
- 1, 1, 1, 1, 0, 0, 1, 0,
391
- 1, -1, 1, 1, 0, 0, 1, 1,
396
+ -1, -1, -1, 1, 0, 0, 0.25, 1,
397
+ // bottom face select the bottom middle image
398
+ 1, -1, 1, 1, 0, 0, 0.25, 0.5,
399
+ -1, -1, 1, 1, 0, 0, 0.5, 0.5,
400
+ 1, -1, -1, 1, 0, 0, 0.25, 1,
401
+ -1, -1, -1, 1, 0, 0, 0.5, 1,
402
+ // top face select the bottom right image
403
+ -1, 1, 1, 1, 0, 0, 0.5, 0.5,
404
+ 1, 1, 1, 1, 0, 0, 0.75, 0.5,
405
+ -1, 1, -1, 1, 0, 0, 0.5, 1,
406
+ 1, 1, -1, 1, 0, 0, 0.75, 1,
407
+ ]);
408
+ } else {
409
+ vertices = new Float32Array([
410
+ // position | texture coordinate
411
+ //------------- +----------------------
412
+ // front face select the top left image 1, 0.5,
413
+ -1 * options.scale, 1 * options.scale, 1 * options.scale, 1, 0, 0, 0, 0,
414
+ -1 * options.scale, -1 * options.scale, 1 * options.scale, 1, 0, 0, 0, 1,
415
+ 1 * options.scale, 1 * options.scale, 1 * options.scale, 1, 0, 0, 1, 0,
416
+ 1 * options.scale, -1 * options.scale, 1 * options.scale, 1, 0, 0, 1, 1,
392
417
  // right face select the top middle image
393
- 1, 1, -1, 1, 0, 0, 0, 0,
394
- 1, 1, 1, 1, 0, 0, 0, 1,
395
- 1, -1, -1, 1, 0, 0, 1, 0,
396
- 1, -1, 1, 1, 0, 0, 1, 1,
418
+ 1 * options.scale, 1 * options.scale, -1 * options.scale, 1, 0, 0, 0, 0,
419
+ 1 * options.scale, 1 * options.scale, 1 * options.scale, 1, 0, 0, 0, 1,
420
+ 1 * options.scale, -1 * options.scale, -1 * options.scale, 1, 0, 0, 1, 0,
421
+ 1 * options.scale, -1 * options.scale, 1 * options.scale, 1, 0, 0, 1, 1,
397
422
  // back face select to top right image
398
- 1, 1, -1, 1, 0, 0, 0, 0,
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,
423
+ 1 * options.scale, 1 * options.scale, -1 * options.scale, 1, 0, 0, 0, 0,
424
+ 1 * options.scale, -1 * options.scale, -1 * options.scale, 1, 0, 0, 0, 1,
425
+ -1 * options.scale, 1 * options.scale, -1 * options.scale, 1, 0, 0, 1, 0,
426
+ -1 * options.scale, -1 * options.scale, -1 * options.scale, 1, 0, 0, 1, 1,
402
427
  // left face select the bottom left 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,
428
+ -1 * options.scale, 1 * options.scale, 1 * options.scale, 1, 0, 0, 0, 0,
429
+ -1 * options.scale, 1 * options.scale, -1 * options.scale, 1, 0, 0, 0, 1,
430
+ -1 * options.scale, -1 * options.scale, 1 * options.scale, 1, 0, 0, 1, 0,
431
+ -1 * options.scale, -1 * options.scale, -1 * options.scale, 1, 0, 0, 1, 1,
407
432
  // bottom face select the bottom middle 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,
433
+ 1 * options.scale, -1 * options.scale, 1 * options.scale, 1, 0, 0, 0, 0,
434
+ -1 * options.scale, -1 * options.scale, 1 * options.scale, 1, 0, 0, 0, 1,
435
+ 1 * options.scale, -1 * options.scale, -1 * options.scale, 1, 0, 0, 1, 0,
436
+ -1 * options.scale, -1 * options.scale, -1 * options.scale, 1, 0, 0, 1, 1,
412
437
  // top face select the bottom right 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,
438
+ -1 * options.scale, 1 * options.scale, 1 * options.scale, 1, 0, 0, 0, 0,
439
+ 1 * options.scale, 1 * options.scale, 1 * options.scale, 1, 0, 0, 0, 1,
440
+ -1 * options.scale, 1 * options.scale, -1 * options.scale, 1, 0, 0, 1, 0,
441
+ 1 * options.scale, 1 * options.scale, -1 * options.scale, 1, 0, 0, 1, 1,
417
442
  ])
418
443
  }
419
-
444
+
420
445
 
421
446
  const indices = new Uint16Array([
422
447
  0, 1, 2, 2, 1, 3, // front