matrix-engine-wgpu 1.3.2 → 1.3.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.
package/index.js CHANGED
@@ -6,7 +6,7 @@
6
6
  // import {degToRad, radToDeg} from "./utils";
7
7
  import {downloadMeshes} from "./src/engine/loader-obj.js";
8
8
  import MatrixEngineWGPU from "./src/world.js";
9
- import {addRaycastListener, getRayFromMouse, rayIntersectsSphere} from "./src/engine/raycast.js";
9
+ import {addRaycastsAABBListener, addRaycastListener, getRayFromMouse, rayIntersectsSphere} from "./src/engine/raycast.js";
10
10
 
11
11
  var about = () => {
12
12
  console.log("Hi npm. matrix-engine for webgpu is ready...")
@@ -25,5 +25,6 @@ export {
25
25
  rayIntersectsSphere,
26
26
  getRayFromMouse,
27
27
  addRaycastListener,
28
+ addRaycastsAABBListener,
28
29
  about
29
30
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "matrix-engine-wgpu",
3
- "version": "1.3.2",
3
+ "version": "1.3.5",
4
4
  "description": "obj sequence anim +HOTFIX raycast, webGPU powered pwa application. Crazy fast rendering with AmmoJS physics support. Simple raycaster hit object added.",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -25,6 +25,9 @@ export default class MEMeshObj {
25
25
  this.context = context;
26
26
  this.entityArgPass = o.entityArgPass;
27
27
 
28
+ // comes from engine not from args
29
+ this.clearColor = "red";
30
+
28
31
  // Mesh stuff - for single mesh or t-posed (fiktive-first in loading order)
29
32
  this.mesh = o.mesh;
30
33
  this.mesh.uvs = this.mesh.textures;
@@ -310,7 +313,7 @@ export default class MEMeshObj {
310
313
  {
311
314
  // view is acquired and set in render loop.
312
315
  view: undefined,
313
- clearValue: {r: 0.5, g: 0.5, b: 0.5, a: 1.0},
316
+ clearValue: this.clearColor,
314
317
  loadOp: 'clear', // load old fix for FF
315
318
  storeOp: 'store',
316
319
  },
@@ -72,8 +72,8 @@ export function rayIntersectsSphere(rayOrigin, rayDirection, sphereCenter, spher
72
72
  return discriminant > 0;
73
73
  }
74
74
 
75
- export function addRaycastListener() {
76
- let canvasDom = document.getElementById("canvas1");
75
+ export function addRaycastListener(canvasId = "canvas1") {
76
+ let canvasDom = document.getElementById(canvasId);
77
77
  canvasDom.addEventListener('click', (event) => {
78
78
  let camera = app.cameras.WASD;
79
79
  const {rayOrigin, rayDirection} = getRayFromMouse(event, canvasDom, camera);
@@ -89,4 +89,108 @@ export function addRaycastListener() {
89
89
  }
90
90
  }
91
91
  });
92
- }
92
+ }
93
+
94
+ export function rayIntersectsAABB(
95
+ rayOrigin,
96
+ rayDirection,
97
+ boxMin,
98
+ boxMax
99
+ ) {
100
+ let tmin = (boxMin[0] - rayOrigin[0]) / rayDirection[0];
101
+ let tmax = (boxMax[0] - rayOrigin[0]) / rayDirection[0];
102
+ if(tmin > tmax) [tmin, tmax] = [tmax, tmin];
103
+
104
+ let tymin = (boxMin[1] - rayOrigin[1]) / rayDirection[1];
105
+ let tymax = (boxMax[1] - rayOrigin[1]) / rayDirection[1];
106
+ if(tymin > tymax) [tymin, tymax] = [tymax, tymin];
107
+
108
+ if(tmin > tymax || tymin > tmax) return false;
109
+ if(tymin > tmin) tmin = tymin;
110
+ if(tymax < tmax) tmax = tymax;
111
+
112
+ let tzmin = (boxMin[2] - rayOrigin[2]) / rayDirection[2];
113
+ let tzmax = (boxMax[2] - rayOrigin[2]) / rayDirection[2];
114
+ if(tzmin > tzmax) [tzmin, tzmax] = [tzmax, tzmin];
115
+
116
+ if(tmin > tzmax || tzmin > tmax) return false;
117
+
118
+ return true;
119
+ }
120
+
121
+ export function computeAABBFromVertices(vertices) {
122
+ const min = [Infinity, Infinity, Infinity];
123
+ const max = [-Infinity, -Infinity, -Infinity];
124
+
125
+ for(let i = 0;i < vertices.length;i += 3) {
126
+ const x = vertices[i];
127
+ const y = vertices[i + 1];
128
+ const z = vertices[i + 2];
129
+
130
+ min[0] = Math.min(min[0], x);
131
+ min[1] = Math.min(min[1], y);
132
+ min[2] = Math.min(min[2], z);
133
+
134
+ max[0] = Math.max(max[0], x);
135
+ max[1] = Math.max(max[1], y);
136
+ max[2] = Math.max(max[2], z);
137
+ }
138
+
139
+ return [min, max];
140
+ }
141
+
142
+ export function addRaycastsAABBListener(canvasId = "canvas1") {
143
+ const canvasDom = document.getElementById(canvasId);
144
+ if(!canvasDom) {
145
+ console.warn(`Canvas with id ${canvasId} not found`);
146
+ return;
147
+ }
148
+
149
+ canvasDom.addEventListener('click', (event) => {
150
+ const camera = app.cameras.WASD;
151
+ const {rayOrigin, rayDirection} = getRayFromMouse(event, canvasDom, camera);
152
+
153
+ for(const object of app.mainRenderBundle) {
154
+ // Compute AABB min/max from object position and size
155
+ const pos = [object.position.x,object.position.y,object.position.z]; // [x,y,z]
156
+
157
+ ////////////
158
+ const [boxMinLocal, boxMaxLocal] = computeAABBFromVertices(object.mesh.vertices);
159
+ // Optionally transform to world space using object.position
160
+ // const pos = object.position;
161
+
162
+ const boxMin = [
163
+ boxMinLocal[0] + pos[0],
164
+ boxMinLocal[1] + pos[1],
165
+ boxMinLocal[2] + pos[2]
166
+ ];
167
+
168
+ const boxMax = [
169
+ boxMaxLocal[0] + pos[0],
170
+ boxMaxLocal[1] + pos[1],
171
+ boxMaxLocal[2] + pos[2]
172
+ ];
173
+ //////////////
174
+ // const size = object.size || [1, 1, 1]; // Replace with actual object size or default 1x1x1
175
+
176
+ // const boxMin = [
177
+ // pos[0] - size[0] / 2,
178
+ // pos[1] - size[1] / 2,
179
+ // pos[2] - size[2] / 2
180
+ // ];
181
+ // const boxMax = [
182
+ // pos[0] + size[0] / 2,
183
+ // pos[1] + size[1] / 2,
184
+ // pos[2] + size[2] / 2
185
+ // ];
186
+
187
+ if(rayIntersectsAABB(rayOrigin, rayDirection, boxMin, boxMax)) {
188
+ console.log('AABB hit:', object.name);
189
+
190
+ canvasDom.dispatchEvent(new CustomEvent('ray.hit.event', {
191
+ detail: {hitObject: object}
192
+ }));
193
+ }
194
+ }
195
+ });
196
+ }
package/src/world.js CHANGED
@@ -39,10 +39,14 @@ export default class MatrixEngineWGPU {
39
39
  mainCameraParams: {
40
40
  type: 'WASD',
41
41
  responseCoef: 2000
42
- }
42
+ },
43
+ clearColor: {r: 0.584, g: 0, b: 0.239, a: 1.0}
43
44
  }
44
45
  callback = options;
45
46
  }
47
+ if(typeof options.clearColor === 'undefined') {
48
+ options.clearColor = {r: 0.584, g: 0, b: 0.239, a: 1.0};
49
+ }
46
50
  if(typeof options.canvasId === 'undefined') {
47
51
  options.canvasId = 'canvas1';
48
52
  }
@@ -251,7 +255,7 @@ export default class MatrixEngineWGPU {
251
255
  this.mainRenderBundle.push(myMesh1);
252
256
  }
253
257
 
254
- addMeshObj = (o) => {
258
+ addMeshObj = (o , clearColor=this.options.clearColor) => {
255
259
  if(typeof o.name === 'undefined') {o.name = genName(9)}
256
260
  if(typeof o.position === 'undefined') {o.position = {x: 0, y: 0, z: -4}}
257
261
  if(typeof o.rotation === 'undefined') {o.rotation = {x: 0, y: 0, z: 0}}
@@ -285,18 +289,8 @@ export default class MatrixEngineWGPU {
285
289
  if(typeof o.objAnim == 'undefined' || typeof o.objAnim == null) {
286
290
  o.objAnim = null;
287
291
  } else {
288
- console.log('o.anim', o.objAnim)
289
- // o.objAnim = {
290
- // id: o.objAnim.id,
291
- // sumOfAniFrames: o.objAnim.sumOfAniFrames,
292
- // currentAni: o.objAnim.currentAni,
293
- // speed: o.objAnim.speed,
294
- // currentDraws: 0
295
- // };
296
-
292
+ // console.log('o.anim', o.objAnim)
297
293
  if(typeof o.objAnim.animations !== 'undefined') {
298
- // o.objAnim.animation.anims = o.objAnim.animations;
299
- console.log('o.o.objAnim.animations ', o.objAnim.animations )
300
294
  o.objAnim.play = play;
301
295
  }
302
296
  // no need for single test it in future
@@ -314,7 +308,8 @@ export default class MatrixEngineWGPU {
314
308
  }
315
309
  }
316
310
  }
317
- let myMesh1 = new MEMeshObj(this.canvas, this.device, this.context, o)
311
+ let myMesh1 = new MEMeshObj(this.canvas, this.device, this.context, o);
312
+ myMesh1.clearColor = clearColor;
318
313
  if(o.physics.enabled == true) {
319
314
  this.matrixAmmo.addPhysics(myMesh1, o.physics)
320
315
  }