matrix-engine-wgpu 1.4.4 → 1.4.6

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.
@@ -473,6 +473,10 @@ var loadVideoTexture = function () {
473
473
  // if you dont wanna light just use intesity = 0
474
474
  // videoTexture is app main instance
475
475
  videoTexture.addLight();
476
+ (0, _raycast.addRaycastsAABBListener)();
477
+ videoTexture.canvas.addEventListener("ray.hit.event", e => {
478
+ console.log('test ray after shadows merge');
479
+ });
476
480
  addEventListener('AmmoReady', () => {
477
481
  (0, _loaderObj.downloadMeshes)({
478
482
  welcomeText: "./res/meshes/blender/piramyd.obj",
@@ -510,8 +514,11 @@ var loadVideoTexture = function () {
510
514
  physics: {
511
515
  enabled: true,
512
516
  geometry: "Cube"
517
+ },
518
+ raycast: {
519
+ enabled: true,
520
+ radius: 12
513
521
  }
514
- // raycast: { enabled: true , radius: 2 }
515
522
  });
516
523
  var TEST = videoTexture.getSceneObjectByName('MyVideoTex');
517
524
  console.log(`%c Test video-texture...`, _utils.LOG_MATRIX);
@@ -8049,7 +8056,7 @@ class Materials {
8049
8056
  return;
8050
8057
  } else {}
8051
8058
  if (this.isVideo == true) {
8052
- console.info("✅ video sceneBindGroupForRender ");
8059
+ // console.info("✅ video sceneBindGroupForRender ");
8053
8060
  this.sceneBindGroupForRender = this.device.createBindGroup({
8054
8061
  layout: this.bglForRender,
8055
8062
  entries: [{
@@ -8912,34 +8919,64 @@ function getRayFromMouse(event, canvas, camera) {
8912
8919
  const rect = canvas.getBoundingClientRect();
8913
8920
  let x = (event.clientX - rect.left) / rect.width * 2 - 1;
8914
8921
  let y = (event.clientY - rect.top) / rect.height * 2 - 1;
8915
- // simple invert
8916
- x = -x;
8917
- y = -y;
8918
- const fov = Math.PI / 4;
8922
+ y = -y; // flip Y only (WebGPU NDC)
8923
+
8919
8924
  const aspect = canvas.width / canvas.height;
8920
- const near = 0.1;
8921
- const far = 1000;
8922
- camera.projectionMatrix = _wgpuMatrix.mat4.perspective(2 * Math.PI / 5, aspect, 1, 1000.0);
8925
+ camera.projectionMatrix = _wgpuMatrix.mat4.perspective(2 * Math.PI / 5, aspect, 0.1, 1000);
8923
8926
  const invProjection = _wgpuMatrix.mat4.inverse(camera.projectionMatrix);
8924
8927
  const invView = _wgpuMatrix.mat4.inverse(camera.view);
8925
- const ndc = [x, y, 1, 1];
8926
- let worldPos = multiplyMatrixVector(invProjection, ndc);
8927
- worldPos = multiplyMatrixVector(invView, worldPos);
8928
- let world;
8929
- if (worldPos[3] !== 0) {
8930
- world = [worldPos[0] / worldPos[3], worldPos[2] / worldPos[3], worldPos[1] / worldPos[3]];
8931
- } else {
8932
- console.log("[raycaster]special case 0.");
8933
- world = [worldPos[0], worldPos[1], worldPos[2]];
8934
- }
8935
- const rayOrigin = [camera.position[0], camera.position[1], camera.position[2]];
8936
- const rayDirection = _wgpuMatrix.vec3.normalize(_wgpuMatrix.vec3.subtract(world, rayOrigin));
8937
- rayDirection[2] = -rayDirection[2];
8928
+
8929
+ // NDC -> clip -> eye -> world
8930
+ let clip = [x, y, 1, 1];
8931
+ let eye = _wgpuMatrix.vec4.transformMat4(clip, invProjection);
8932
+ eye = [eye[0], eye[1], -1, 0]; // direction in view space
8933
+
8934
+ let worldDir = _wgpuMatrix.vec4.transformMat4(eye, invView);
8935
+ const rayDirection = _wgpuMatrix.vec3.normalize([worldDir[0], worldDir[1], worldDir[2]]);
8936
+ const rayOrigin = [...camera.position];
8938
8937
  return {
8939
8938
  rayOrigin,
8940
8939
  rayDirection
8941
8940
  };
8942
8941
  }
8942
+ // export function getRayFromMouse(event, canvas, camera) {
8943
+ // const rect = canvas.getBoundingClientRect();
8944
+ // let x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
8945
+ // let y = ((event.clientY - rect.top) / rect.height) * 2 - 1;
8946
+ // // simple invert
8947
+ // x = -x;
8948
+ // y = -y;
8949
+ // const fov = Math.PI / 4;
8950
+ // const aspect = canvas.width / canvas.height;
8951
+ // const near = 0.1;
8952
+ // const far = 1000;
8953
+ // camera.projectionMatrix = mat4.perspective((2 * Math.PI) / 5, aspect, 1, 1000.0);
8954
+ // const invProjection = mat4.inverse(camera.projectionMatrix);
8955
+ // const invView = mat4.inverse(camera.view);
8956
+ // const ndc = [x, y, 1, 1];
8957
+ // let worldPos = multiplyMatrixVector(invProjection, ndc);
8958
+ // worldPos = multiplyMatrixVector(invView, worldPos);
8959
+ // let world;
8960
+ // if(worldPos[3] !== 0) {
8961
+ // world = [
8962
+ // worldPos[0] / worldPos[3],
8963
+ // worldPos[2] / worldPos[3],
8964
+ // worldPos[1] / worldPos[3]
8965
+ // ];
8966
+ // } else {
8967
+ // console.log("[raycaster]special case 0.");
8968
+ // world = [
8969
+ // worldPos[0],
8970
+ // worldPos[1],
8971
+ // worldPos[2]
8972
+ // ];
8973
+ // }
8974
+ // const rayOrigin = [camera.position[0], camera.position[1], camera.position[2]];
8975
+ // const rayDirection = vec3.normalize(vec3.subtract(world, rayOrigin));
8976
+ // rayDirection[2] = -rayDirection[2];
8977
+ // return {rayOrigin, rayDirection};
8978
+ // }
8979
+
8943
8980
  function getRayFromMouse2(event, canvas, camera) {
8944
8981
  const rect = canvas.getBoundingClientRect();
8945
8982
  let x = (event.clientX - rect.left) / rect.width * 2 - 1;
@@ -8988,17 +9025,17 @@ function addRaycastListener(canvasId = "canvas1") {
8988
9025
  rayDirection
8989
9026
  } = getRayFromMouse(event, canvasDom, camera);
8990
9027
  for (const object of app.mainRenderBundle) {
8991
- if (object.raycast.enabled == false) return;
8992
- if (rayIntersectsSphere(rayOrigin, rayDirection, object.position, object.raycast.radius)) {
8993
- console.log('Object clicked:', object.name);
8994
- // Just like in matrix-engine webGL version "ray.hit.event"
8995
- dispatchEvent(new CustomEvent('ray.hit.event', {
8996
- detail: {
8997
- hitObject: object,
8998
- rayOrigin: rayOrigin,
8999
- rayDirection: rayDirection
9000
- }
9001
- }));
9028
+ if (object.raycast.enabled == true) {
9029
+ if (rayIntersectsSphere(rayOrigin, rayDirection, object.position, object.raycast.radius)) {
9030
+ // Just like in matrix-engine webGL version "ray.hit.event"
9031
+ canvasDom.dispatchEvent(new CustomEvent('ray.hit.event', {
9032
+ detail: {
9033
+ hitObject: object,
9034
+ rayOrigin: rayOrigin,
9035
+ rayDirection: rayDirection
9036
+ }
9037
+ }));
9038
+ }
9002
9039
  }
9003
9040
  }
9004
9041
  });
@@ -9059,6 +9096,7 @@ function addRaycastsAABBListener(canvasId = "canvas1") {
9059
9096
  return;
9060
9097
  }
9061
9098
  canvasDom.addEventListener('click', event => {
9099
+ console.warn(`Canvas click ${event} `);
9062
9100
  const camera = app.cameras.WASD;
9063
9101
  const {
9064
9102
  rayOrigin,
@@ -1,40 +1,40 @@
1
- # Blender 4.1.1
2
- # www.blender.org
3
- mtllib dice.mtl
4
- o Cube
5
- v 1.000000 1.000000 -1.000000
6
- v 1.000000 -1.000000 -1.000000
7
- v 1.000000 1.000000 1.000000
8
- v 1.000000 -1.000000 1.000000
9
- v -1.000000 1.000000 -1.000000
10
- v -1.000000 -1.000000 -1.000000
11
- v -1.000000 1.000000 1.000000
12
- v -1.000000 -1.000000 1.000000
13
- vn -0.0000 1.0000 -0.0000
14
- vn -0.0000 -0.0000 1.0000
15
- vn -1.0000 -0.0000 -0.0000
16
- vn -0.0000 -1.0000 -0.0000
17
- vn 1.0000 -0.0000 -0.0000
18
- vn -0.0000 -0.0000 -1.0000
19
- vt 0.625000 0.500000
20
- vt 0.875000 0.500000
21
- vt 0.875000 0.750000
22
- vt 0.625000 0.750000
23
- vt 0.375000 0.750000
24
- vt 0.625000 1.000000
25
- vt 0.375000 1.000000
26
- vt 0.375000 0.000000
27
- vt 0.625000 0.000000
28
- vt 0.625000 0.250000
29
- vt 0.375000 0.250000
30
- vt 0.125000 0.500000
31
- vt 0.375000 0.500000
32
- vt 0.125000 0.750000
33
- s 0
34
- usemtl Material
35
- f 1/1/1 5/2/1 7/3/1 3/4/1
36
- f 4/5/2 3/4/2 7/6/2 8/7/2
37
- f 8/8/3 7/9/3 5/10/3 6/11/3
38
- f 6/12/4 2/13/4 4/5/4 8/14/4
39
- f 2/13/5 1/1/5 3/4/5 4/5/5
40
- f 6/11/6 5/10/6 1/1/6 2/13/6
1
+ # Blender 4.1.1
2
+ # www.blender.org
3
+ mtllib dice.mtl
4
+ o Cube
5
+ v 1.000000 1.000000 -1.000000
6
+ v 1.000000 -1.000000 -1.000000
7
+ v 1.000000 1.000000 1.000000
8
+ v 1.000000 -1.000000 1.000000
9
+ v -1.000000 1.000000 -1.000000
10
+ v -1.000000 -1.000000 -1.000000
11
+ v -1.000000 1.000000 1.000000
12
+ v -1.000000 -1.000000 1.000000
13
+ vn -0.0000 1.0000 -0.0000
14
+ vn -0.0000 -0.0000 1.0000
15
+ vn -1.0000 -0.0000 -0.0000
16
+ vn -0.0000 -1.0000 -0.0000
17
+ vn 1.0000 -0.0000 -0.0000
18
+ vn -0.0000 -0.0000 -1.0000
19
+ vt 0.625000 0.500000
20
+ vt 0.875000 0.500000
21
+ vt 0.875000 0.750000
22
+ vt 0.625000 0.750000
23
+ vt 0.375000 0.750000
24
+ vt 0.625000 1.000000
25
+ vt 0.375000 1.000000
26
+ vt 0.375000 0.000000
27
+ vt 0.625000 0.000000
28
+ vt 0.625000 0.250000
29
+ vt 0.375000 0.250000
30
+ vt 0.125000 0.500000
31
+ vt 0.375000 0.500000
32
+ vt 0.125000 0.750000
33
+ s 0
34
+ usemtl Material
35
+ f 1/1/1 5/2/1 7/3/1 3/4/1
36
+ f 4/5/2 3/4/2 7/6/2 8/7/2
37
+ f 8/8/3 7/9/3 5/10/3 6/11/3
38
+ f 6/12/4 2/13/4 4/5/4 8/14/4
39
+ f 2/13/5 1/1/5 3/4/5 4/5/5
40
+ f 6/11/6 5/10/6 1/1/6 2/13/6
package/readme.md CHANGED
@@ -227,7 +227,8 @@ Automatic raycast listener:
227
227
  ```js
228
228
  addRaycastListener();
229
229
 
230
- window.addEventListener("ray.hit.event", event => {
230
+ // Must be app.canvas or [Program name].canvas
231
+ app.canvas.addEventListener("ray.hit.event", event => {
231
232
  console.log("Ray hit:", event.detail.hitObject);
232
233
  });
233
234
  ```
@@ -438,9 +439,14 @@ urlQuery.lang;
438
439
  `main.js` is the main instance for the Ultimate Yahtzee game template.
439
440
  It contains the game context, e.g., `dices`.
440
441
 
442
+ What ever you find here onder main.js is open source part.
443
+ Next level of upgrade is commercial part.
444
+
441
445
  For a clean startup without extra logic, use `empty.js`.
442
446
  This minimal build is ideal for online editors like CodePen or StackOverflow snippets.
443
447
 
448
+ <img width="860" height="640" src="https://github.com/zlatnaspirala/matrix-engine-wgpu/blob/main/non-project-files/3d-jamb.png?raw=true" />
449
+
444
450
  ---
445
451
 
446
452
  ## NPM Scripts
@@ -481,6 +487,11 @@ This is static file storage.
481
487
 
482
488
  ---
483
489
 
490
+ Performance for Jamb game:
491
+
492
+ <img width="860" height="640" src="https://github.com/zlatnaspirala/matrix-engine-wgpu/blob/main/non-project-files/performance.png?raw=true" />
493
+
494
+
484
495
  ## License
485
496
 
486
497
  ### Usage Note
@@ -356,7 +356,9 @@ export function createInputHandler(window, canvas) {
356
356
  case 'ControlLeft':
357
357
  case 'KeyC': digital.down = value; break;
358
358
  }
359
- e.preventDefault();
359
+ // if you wanna dosavle all keyboard input for some reason...
360
+ // add later like new option feature...
361
+ // e.preventDefault();
360
362
  e.stopPropagation();
361
363
  };
362
364
 
@@ -200,7 +200,7 @@ export default class Materials {
200
200
 
201
201
  }
202
202
  if(this.isVideo == true) {
203
- console.info("✅ video sceneBindGroupForRender ");
203
+ // console.info("✅ video sceneBindGroupForRender ");
204
204
  this.sceneBindGroupForRender = this.device.createBindGroup({
205
205
  layout: this.bglForRender,
206
206
  entries: [
@@ -28,39 +28,62 @@ export function getRayFromMouse(event, canvas, camera) {
28
28
  const rect = canvas.getBoundingClientRect();
29
29
  let x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
30
30
  let y = ((event.clientY - rect.top) / rect.height) * 2 - 1;
31
- // simple invert
32
- x = -x;
33
- y = -y;
34
- const fov = Math.PI / 4;
31
+ y = -y; // flip Y only (WebGPU NDC)
32
+
35
33
  const aspect = canvas.width / canvas.height;
36
- const near = 0.1;
37
- const far = 1000;
38
- camera.projectionMatrix = mat4.perspective((2 * Math.PI) / 5, aspect, 1, 1000.0);
34
+ camera.projectionMatrix = mat4.perspective((2 * Math.PI) / 5, aspect, 0.1, 1000);
35
+
39
36
  const invProjection = mat4.inverse(camera.projectionMatrix);
40
37
  const invView = mat4.inverse(camera.view);
41
- const ndc = [x, y, 1, 1];
42
- let worldPos = multiplyMatrixVector(invProjection, ndc);
43
- worldPos = multiplyMatrixVector(invView, worldPos);
44
- let world;
45
- if(worldPos[3] !== 0) {
46
- world = [
47
- worldPos[0] / worldPos[3],
48
- worldPos[2] / worldPos[3],
49
- worldPos[1] / worldPos[3]
50
- ];
51
- } else {
52
- console.log("[raycaster]special case 0.");
53
- world = [
54
- worldPos[0],
55
- worldPos[1],
56
- worldPos[2]
57
- ];
58
- }
59
- const rayOrigin = [camera.position[0], camera.position[1], camera.position[2]];
60
- const rayDirection = vec3.normalize(vec3.subtract(world, rayOrigin));
61
- rayDirection[2] = -rayDirection[2];
38
+
39
+ // NDC -> clip -> eye -> world
40
+ let clip = [x, y, 1, 1];
41
+ let eye = vec4.transformMat4(clip, invProjection);
42
+ eye = [eye[0], eye[1], -1, 0]; // direction in view space
43
+
44
+ let worldDir = vec4.transformMat4(eye, invView);
45
+ const rayDirection = vec3.normalize([worldDir[0], worldDir[1], worldDir[2]]);
46
+
47
+ const rayOrigin = [...camera.position];
62
48
  return {rayOrigin, rayDirection};
63
49
  }
50
+ // export function getRayFromMouse(event, canvas, camera) {
51
+ // const rect = canvas.getBoundingClientRect();
52
+ // let x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
53
+ // let y = ((event.clientY - rect.top) / rect.height) * 2 - 1;
54
+ // // simple invert
55
+ // x = -x;
56
+ // y = -y;
57
+ // const fov = Math.PI / 4;
58
+ // const aspect = canvas.width / canvas.height;
59
+ // const near = 0.1;
60
+ // const far = 1000;
61
+ // camera.projectionMatrix = mat4.perspective((2 * Math.PI) / 5, aspect, 1, 1000.0);
62
+ // const invProjection = mat4.inverse(camera.projectionMatrix);
63
+ // const invView = mat4.inverse(camera.view);
64
+ // const ndc = [x, y, 1, 1];
65
+ // let worldPos = multiplyMatrixVector(invProjection, ndc);
66
+ // worldPos = multiplyMatrixVector(invView, worldPos);
67
+ // let world;
68
+ // if(worldPos[3] !== 0) {
69
+ // world = [
70
+ // worldPos[0] / worldPos[3],
71
+ // worldPos[2] / worldPos[3],
72
+ // worldPos[1] / worldPos[3]
73
+ // ];
74
+ // } else {
75
+ // console.log("[raycaster]special case 0.");
76
+ // world = [
77
+ // worldPos[0],
78
+ // worldPos[1],
79
+ // worldPos[2]
80
+ // ];
81
+ // }
82
+ // const rayOrigin = [camera.position[0], camera.position[1], camera.position[2]];
83
+ // const rayDirection = vec3.normalize(vec3.subtract(world, rayOrigin));
84
+ // rayDirection[2] = -rayDirection[2];
85
+ // return {rayOrigin, rayDirection};
86
+ // }
64
87
 
65
88
  export function getRayFromMouse2(event, canvas, camera) {
66
89
  const rect = canvas.getBoundingClientRect();
@@ -114,17 +137,20 @@ export function addRaycastListener(canvasId = "canvas1") {
114
137
  let camera = app.cameras.WASD;
115
138
  const {rayOrigin, rayDirection} = getRayFromMouse(event, canvasDom, camera);
116
139
  for(const object of app.mainRenderBundle) {
117
- if(object.raycast.enabled == false) return;
118
- if(rayIntersectsSphere(rayOrigin, rayDirection, object.position, object.raycast.radius)) {
119
- console.log('Object clicked:', object.name);
120
- // Just like in matrix-engine webGL version "ray.hit.event"
121
- dispatchEvent(new CustomEvent('ray.hit.event', {
122
- detail: {
123
- hitObject: object,
124
- rayOrigin: rayOrigin,
125
- rayDirection: rayDirection
140
+ if(object.raycast.enabled == true) {
141
+ if(rayIntersectsSphere(rayOrigin, rayDirection, object.position, object.raycast.radius)) {
142
+ // Just like in matrix-engine webGL version "ray.hit.event"
143
+ canvasDom.dispatchEvent(new CustomEvent('ray.hit.event', {
144
+ detail: {
145
+ hitObject: object,
146
+ rayOrigin: rayOrigin,
147
+ rayDirection: rayDirection
148
+ }
149
+ }))
150
+ if(touchCoordinate.stopOnFirstDetectedHit == true) {
151
+ break;
126
152
  }
127
- }))
153
+ }
128
154
  }
129
155
  }
130
156
  });
@@ -200,6 +226,7 @@ export function addRaycastsAABBListener(canvasId = "canvas1") {
200
226
  }
201
227
 
202
228
  canvasDom.addEventListener('click', (event) => {
229
+ console.warn(`Canvas click ${event} `);
203
230
  const camera = app.cameras.WASD;
204
231
  const {rayOrigin, rayDirection} = getRayFromMouse2(event, canvasDom, camera);
205
232
  for(const object of app.mainRenderBundle) {
@@ -212,6 +239,9 @@ export function addRaycastsAABBListener(canvasId = "canvas1") {
212
239
  rayOrigin: rayOrigin,
213
240
  rayDirection: rayDirection
214
241
  }));
242
+ if(touchCoordinate.stopOnFirstDetectedHit == true) {
243
+ break;
244
+ }
215
245
  }
216
246
  }
217
247
  });