@needle-tools/engine 3.4.0-alpha → 3.5.1-alpha

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 (79) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/needle-engine.js +59388 -59097
  3. package/dist/needle-engine.min.js +416 -391
  4. package/dist/needle-engine.umd.cjs +388 -363
  5. package/lib/engine/api.d.ts +1 -0
  6. package/lib/engine/api.js +1 -0
  7. package/lib/engine/api.js.map +1 -1
  8. package/lib/engine/engine_context.d.ts +9 -4
  9. package/lib/engine/engine_context.js +57 -32
  10. package/lib/engine/engine_context.js.map +1 -1
  11. package/lib/engine/engine_context_registry.d.ts +5 -3
  12. package/lib/engine/engine_context_registry.js +10 -2
  13. package/lib/engine/engine_context_registry.js.map +1 -1
  14. package/lib/engine/engine_element.js.map +1 -1
  15. package/lib/engine/engine_element_loading.js +2 -3
  16. package/lib/engine/engine_element_loading.js.map +1 -1
  17. package/lib/engine/engine_input.d.ts +2 -2
  18. package/lib/engine/engine_physics.d.ts +20 -93
  19. package/lib/engine/engine_physics.js +20 -892
  20. package/lib/engine/engine_physics.js.map +1 -1
  21. package/lib/engine/engine_physics.types.js.map +1 -1
  22. package/lib/engine/engine_physics_rapier.d.ts +103 -0
  23. package/lib/engine/engine_physics_rapier.js +1003 -0
  24. package/lib/engine/engine_physics_rapier.js.map +1 -0
  25. package/lib/engine/engine_types.d.ts +50 -1
  26. package/lib/engine/engine_types.js +8 -0
  27. package/lib/engine/engine_types.js.map +1 -1
  28. package/lib/engine-components/Collider.js +6 -6
  29. package/lib/engine-components/Collider.js.map +1 -1
  30. package/lib/engine-components/Joints.js +2 -2
  31. package/lib/engine-components/Joints.js.map +1 -1
  32. package/lib/engine-components/ReflectionProbe.js +16 -7
  33. package/lib/engine-components/ReflectionProbe.js.map +1 -1
  34. package/lib/engine-components/Renderer.js +3 -4
  35. package/lib/engine-components/Renderer.js.map +1 -1
  36. package/lib/engine-components/RigidBody.d.ts +0 -1
  37. package/lib/engine-components/RigidBody.js +24 -30
  38. package/lib/engine-components/RigidBody.js.map +1 -1
  39. package/lib/engine-components/export/usdz/ThreeUSDZExporter.js +52 -26
  40. package/lib/engine-components/export/usdz/ThreeUSDZExporter.js.map +1 -1
  41. package/lib/engine-components/export/usdz/extensions/behavior/BehaviourComponents.d.ts +8 -2
  42. package/lib/engine-components/export/usdz/extensions/behavior/BehaviourComponents.js +44 -7
  43. package/lib/engine-components/export/usdz/extensions/behavior/BehaviourComponents.js.map +1 -1
  44. package/lib/engine-components/ui/Canvas.js +29 -16
  45. package/lib/engine-components/ui/Canvas.js.map +1 -1
  46. package/lib/engine-components/ui/Layout.js +10 -5
  47. package/lib/engine-components/ui/Layout.js.map +1 -1
  48. package/lib/engine-components/ui/RectTransform.js +8 -3
  49. package/lib/engine-components/ui/RectTransform.js.map +1 -1
  50. package/lib/tsconfig.tsbuildinfo +1 -1
  51. package/package.json +1 -1
  52. package/plugins/vite/config.js +2 -1
  53. package/plugins/vite/defines.js +30 -0
  54. package/plugins/vite/dependency-watcher.js +173 -0
  55. package/plugins/vite/editor-connection.js +37 -39
  56. package/plugins/vite/index.js +5 -1
  57. package/plugins/vite/reload.js +3 -1
  58. package/src/engine/api.ts +1 -0
  59. package/src/engine/codegen/register_types.js +2 -2
  60. package/src/engine/engine_context.ts +75 -42
  61. package/src/engine/engine_context_registry.ts +13 -6
  62. package/src/engine/engine_element.ts +2 -1
  63. package/src/engine/engine_element_loading.ts +2 -3
  64. package/src/engine/engine_input.ts +2 -2
  65. package/src/engine/engine_physics.ts +25 -1020
  66. package/src/engine/engine_physics.types.ts +1 -3
  67. package/src/engine/engine_physics_rapier.ts +1127 -0
  68. package/src/engine/engine_types.ts +66 -4
  69. package/src/engine-components/Collider.ts +6 -6
  70. package/src/engine-components/Joints.ts +2 -2
  71. package/src/engine-components/ReflectionProbe.ts +17 -7
  72. package/src/engine-components/Renderer.ts +5 -5
  73. package/src/engine-components/RendererLightmap.ts +1 -1
  74. package/src/engine-components/RigidBody.ts +24 -31
  75. package/src/engine-components/export/usdz/ThreeUSDZExporter.ts +58 -29
  76. package/src/engine-components/export/usdz/extensions/behavior/BehaviourComponents.ts +51 -9
  77. package/src/engine-components/ui/Canvas.ts +29 -16
  78. package/src/engine-components/ui/Layout.ts +10 -5
  79. package/src/engine-components/ui/RectTransform.ts +9 -4
@@ -1,23 +1,7 @@
1
- import { Box3, BufferAttribute, BufferGeometry, Layers, LineBasicMaterial, LineSegments, Matrix4, Quaternion, Raycaster, Sphere, Vector2, Vector3 } from 'three';
2
- import { CircularBuffer, getParam } from "./engine_utils";
3
- import { getWorldPosition, getWorldQuaternion, getWorldScale, setWorldPositionXYZ, setWorldQuaternionXYZW } from "./engine_three_utils";
4
- import { Collision, ContactPoint, } from './engine_types';
5
- import { foreachComponent } from './engine_gameobject';
6
- import RAPIER, { ActiveCollisionTypes, ActiveEvents, CoefficientCombineRule, Collider, ColliderDesc, EventQueue, JointData, QueryFilterFlags, RigidBody, RigidBodyType, World } from '@dimforge/rapier3d-compat';
7
- import { CollisionDetectionMode, PhysicsMaterialCombine } from '../engine/engine_physics.types';
8
- import { Gizmos } from './engine_gizmos';
9
- import { Mathf } from './engine_math';
1
+ import { Box3, Layers, Raycaster, Sphere, Vector2 } from 'three';
2
+ import { getParam } from "./engine_utils";
3
+ import { getWorldPosition } from "./engine_three_utils";
10
4
  const debugPhysics = getParam("debugphysics");
11
- const debugColliderPlacement = getParam("debugphysicscolliders");
12
- const debugCollisions = getParam("debugcollisions");
13
- const showColliders = getParam("showcolliders");
14
- const noPhysics = getParam("nophysics");
15
- /** on physics body and references the needle component */
16
- const $componentKey = Symbol("needle component");
17
- /** on needle component and references physics body */
18
- const $bodyKey = Symbol("physics body");
19
- const $colliderRigidbody = Symbol("rigidbody");
20
- // const $removed = Symbol("removed");
21
5
  const layerMaskHelper = new Layers();
22
6
  export class RaycastOptions {
23
7
  ray = undefined;
@@ -66,15 +50,24 @@ export class SphereIntersection {
66
50
  this.point = point;
67
51
  }
68
52
  }
69
- export class SphereOverlapResult {
70
- object;
71
- collider;
72
- constructor(object, collider) {
73
- this.object = object;
74
- this.collider = collider;
75
- }
76
- }
77
53
  export class Physics {
54
+ /**@deprecated use this.context.physics.engine.raycast */
55
+ raycastPhysicsFast(origin, direction = undefined, maxDistance = Infinity, solid = true) {
56
+ return this.context.physics.engine?.raycast(origin, direction, maxDistance, solid) ?? null;
57
+ }
58
+ /**@deprecated use this.context.physics.engine.raycastAndGetNormal */
59
+ raycastPhysicsFastAndGetNormal(origin, direction = undefined, maxDistance = Infinity, solid = true) {
60
+ return this.context.physics.engine?.raycastAndGetNormal(origin, direction, maxDistance, solid) ?? null;
61
+ }
62
+ /**@deprecated use this.context.physics.engine.sphereOverlap */
63
+ sphereOverlapPhysics(point, radius) {
64
+ return this.context.physics.engine?.sphereOverlap(point, radius) ?? null;
65
+ }
66
+ context;
67
+ engine;
68
+ constructor(context) {
69
+ this.context = context;
70
+ }
78
71
  // raycasting
79
72
  raycaster = new Raycaster();
80
73
  defaultRaycastOptions = new RaycastOptions();
@@ -209,870 +202,5 @@ export class Physics {
209
202
  }
210
203
  return results;
211
204
  }
212
- rapierRay = new RAPIER.Ray({ x: 0, y: 0, z: 0 }, { x: 0, y: 0, z: 1 });
213
- raycastVectorsBuffer = new CircularBuffer(() => new Vector3(), 10);
214
- /** Fast raycast against physics colliders
215
- * @param origin ray origin in screen or worldspace
216
- * @param direction ray direction in worldspace
217
- * @param maxDistance max distance to raycast
218
- * @param solid if true it will also hit the collider if origin is already inside it
219
- */
220
- raycastPhysicsFast(origin, direction = undefined, maxDistance = Infinity, solid = true) {
221
- const ray = this.getPhysicsRay(this.rapierRay, origin, direction);
222
- if (!ray)
223
- return null;
224
- const hit = this.world?.castRay(ray, maxDistance, solid, undefined, undefined, undefined, undefined, (c) => {
225
- // ignore objects in the IgnoreRaycast=2 layer
226
- return !c[$componentKey]?.gameObject.layers.isEnabled(2);
227
- });
228
- if (hit) {
229
- const point = ray.pointAt(hit.toi);
230
- const vec = this.raycastVectorsBuffer.get();
231
- vec.set(point.x, point.y, point.z);
232
- return { point: vec, collider: hit.collider[$componentKey] };
233
- }
234
- return null;
235
- }
236
- raycastPhysicsFastAndGetNormal(origin, direction = undefined, maxDistance = Infinity, solid = true) {
237
- const ray = this.getPhysicsRay(this.rapierRay, origin, direction);
238
- if (!ray)
239
- return null;
240
- const hit = this.world?.castRayAndGetNormal(ray, maxDistance, solid, undefined, undefined, undefined, undefined, (c) => {
241
- // ignore objects in the IgnoreRaycast=2 layer
242
- return !c[$componentKey]?.gameObject.layers.isEnabled(2);
243
- });
244
- if (hit) {
245
- const point = ray.pointAt(hit.toi);
246
- const normal = hit.normal;
247
- const vec = this.raycastVectorsBuffer.get();
248
- const nor = this.raycastVectorsBuffer.get();
249
- vec.set(point.x, point.y, point.z);
250
- nor.set(normal.x, normal.y, normal.z);
251
- return { point: vec, normal: nor, collider: hit.collider[$componentKey] };
252
- }
253
- return null;
254
- }
255
- getPhysicsRay(ray, origin, direction = undefined) {
256
- const cam = this.context.mainCamera;
257
- // if we get origin in 2d space we need to project it to 3d space
258
- if (origin["z"] === undefined) {
259
- if (!cam) {
260
- console.error("Can not perform raycast from 2d point - no main camera found");
261
- return null;
262
- }
263
- const vec3 = this.raycastVectorsBuffer.get();
264
- vec3.x = origin.x;
265
- vec3.y = origin.y;
266
- vec3.z = 0;
267
- // if the origin is in screen space we need to convert it to raycaster space
268
- if (vec3.x > 1 || vec3.y > 1 || vec3.y < -1 || vec3.x < -1) {
269
- this.context.input.convertScreenspaceToRaycastSpace(vec3);
270
- }
271
- vec3.unproject(cam);
272
- origin = vec3;
273
- }
274
- const o = origin;
275
- ray.origin.x = o.x;
276
- ray.origin.y = o.y;
277
- ray.origin.z = o.z;
278
- const vec = this.raycastVectorsBuffer.get();
279
- if (direction)
280
- vec.set(direction.x, direction.y, direction.z);
281
- else {
282
- if (!cam) {
283
- console.error("Can not perform raycast - no camera found");
284
- return null;
285
- }
286
- vec.set(ray.origin.x, ray.origin.y, ray.origin.z);
287
- const camPosition = getWorldPosition(cam);
288
- vec.sub(camPosition);
289
- }
290
- // we need to normalize the ray because our input is a max travel length and the direction may be not normalized
291
- vec.normalize();
292
- ray.dir.x = vec.x;
293
- ray.dir.y = vec.y;
294
- ray.dir.z = vec.z;
295
- // Gizmos.DrawRay(ray.origin, ray.dir, 0xff0000, Infinity);
296
- return ray;
297
- }
298
- rapierSphere = null;
299
- rapierColliderArray = [];
300
- rapierIdentityRotation = { x: 0, y: 0, z: 0, w: 1 };
301
- rapierForwardVector = { x: 0, y: 0, z: 1 };
302
- /** Precice sphere overlap detection using rapier against colliders
303
- * @param point center of the sphere in worldspace
304
- * @param radius radius of the sphere
305
- * @returns array of colliders that overlap with the sphere. Note: they currently only contain the collider and the gameobject
306
- */
307
- sphereOverlapPhysics(point, radius) {
308
- this.rapierColliderArray.length = 0;
309
- if (!this.world)
310
- return this.rapierColliderArray;
311
- if (!this.rapierSphere)
312
- this.rapierSphere = new RAPIER.Ball(radius);
313
- this.rapierSphere.radius = radius;
314
- this.world.intersectionsWithShape(point, this.rapierIdentityRotation, this.rapierSphere, col => {
315
- const collider = col[$componentKey];
316
- // if (collider.gameObject.layers.isEnabled(2)) return true;
317
- const intersection = new SphereOverlapResult(collider.gameObject, collider);
318
- this.rapierColliderArray.push(intersection);
319
- return true; // Return `false` instead if we want to stop searching for other colliders that contain this point.
320
- }, QueryFilterFlags.EXCLUDE_SENSORS, undefined, undefined, undefined, col => {
321
- const collider = col[$componentKey];
322
- return collider.gameObject.layers.isEnabled(2) == false;
323
- });
324
- return this.rapierColliderArray;
325
- // TODO: this only returns one hit
326
- // let filterGroups = 0xffffffff;
327
- // filterGroups &= ~(1 << 2);
328
- // const hit: ShapeColliderTOI | null = this.world.castShape(point,
329
- // this.rapierIdentityRotation,
330
- // this.rapierForwardVector,
331
- // this.rapierSphere,
332
- // 0,
333
- // QueryFilterFlags.EXCLUDE_SENSORS,
334
- // // filterGroups,
335
- // );
336
- // // console.log(hit);
337
- // if (hit) {
338
- // const collider = hit.collider[$componentKey] as ICollider
339
- // const intersection = new SphereOverlapResult(collider.gameObject);
340
- // this.rapierColliderArray.push(intersection);
341
- // // const localpt = hit.witness2;
342
- // // // const normal = hit.normal2;
343
- // // const hitPoint = new Vector3(localpt.x, localpt.y, localpt.z);
344
- // // // collider.gameObject.localToWorld(hitPoint);
345
- // // // const normalPt = new Vector3(normal.x, normal.y, normal.z);
346
- // // // const mat = new Matrix4().setPosition(point).scale(new Vector3(radius, radius, radius));
347
- // // // hitPoint.applyMatrix4(mat);
348
- // // console.log(hit.witness2)
349
- // // // hitPoint.add(point);
350
- // // const dist = hitPoint.distanceTo(point);
351
- // }
352
- // return this.rapierColliderArray;
353
- }
354
- // physics simulation
355
- enabled = true;
356
- _tempPosition = new Vector3();
357
- _tempQuaternion = new Quaternion();
358
- _tempScale = new Vector3();
359
- _tempMatrix = new Matrix4();
360
- static _didLoadPhysicsEngine = false;
361
- _isUpdatingPhysicsWorld = false;
362
- get isUpdating() { return this._isUpdatingPhysicsWorld; }
363
- context;
364
- world;
365
- _hasCreatedWorld = false;
366
- eventQueue;
367
- collisionHandler;
368
- objects = [];
369
- bodies = [];
370
- _meshCache = new Map();
371
- constructor(context) {
372
- this.context = context;
373
- }
374
- async createWorld() {
375
- if (this._hasCreatedWorld) {
376
- console.error("Invalid call to create physics world: world is already created");
377
- return;
378
- }
379
- this._hasCreatedWorld = true;
380
- if (!Physics._didLoadPhysicsEngine) {
381
- await RAPIER.init().then(() => RAPIER);
382
- Physics._didLoadPhysicsEngine = true;
383
- }
384
- this.world = new World(this._gravity);
385
- if (noPhysics)
386
- this.enabled = false;
387
- }
388
- _gravity = { x: 0.0, y: -9.81, z: 0.0 };
389
- get gravity() {
390
- return this.world?.gravity ?? this._gravity;
391
- }
392
- set gravity(value) {
393
- if (this.world) {
394
- this.world.gravity = value;
395
- }
396
- else {
397
- this._gravity = value;
398
- }
399
- }
400
- clearCaches() {
401
- this._meshCache.clear();
402
- }
403
- addBoxCollider(collider, center, size) {
404
- if (!this.enabled) {
405
- if (debugPhysics)
406
- console.warn("Physics are disabled");
407
- return;
408
- }
409
- const obj = collider.gameObject;
410
- const scale = getWorldScale(obj, this._tempPosition).multiply(size);
411
- scale.multiplyScalar(0.5);
412
- // prevent negative scale
413
- if (scale.x < 0)
414
- scale.x = Math.abs(scale.x);
415
- if (scale.y < 0)
416
- scale.y = Math.abs(scale.y);
417
- if (scale.z < 0)
418
- scale.z = Math.abs(scale.z);
419
- // prevent zero scale - seems normals are flipped otherwise
420
- if (scale.x == 0)
421
- scale.x = 0.0000001;
422
- if (scale.y == 0)
423
- scale.y = 0.0000001;
424
- if (scale.z == 0)
425
- scale.z = 0.0000001;
426
- const desc = ColliderDesc.cuboid(scale.x, scale.y, scale.z);
427
- // const objectLayerMask = collider.gameObject.layers.mask;
428
- // const mask = objectLayerMask & ~2;
429
- // TODO: https://rapier.rs/docs/user_guides/javascript/colliders/#collision-groups-and-solver-groups
430
- // desc.setCollisionGroups(objectLayerMask);
431
- this.createCollider(collider, desc, center);
432
- }
433
- addSphereCollider(collider, center, radius) {
434
- if (!this.enabled) {
435
- if (debugPhysics)
436
- console.warn("Physics are disabled");
437
- return;
438
- }
439
- const obj = collider.gameObject;
440
- const scale = getWorldScale(obj, this._tempPosition).multiplyScalar(radius);
441
- // Prevent negative scales
442
- scale.x = Math.abs(scale.x);
443
- const desc = ColliderDesc.ball(scale.x);
444
- this.createCollider(collider, desc, center);
445
- }
446
- addCapsuleCollider(collider, center, height, radius) {
447
- if (!this.enabled) {
448
- if (debugPhysics)
449
- console.warn("Physics are disabled");
450
- return;
451
- }
452
- const obj = collider.gameObject;
453
- const scale = getWorldScale(obj, this._tempPosition);
454
- // Prevent negative scales
455
- scale.x = Math.abs(scale.x);
456
- scale.y = Math.abs(scale.y);
457
- const desc = ColliderDesc.capsule(height * .5 * scale.y - radius, radius * scale.x);
458
- this.createCollider(collider, desc, center);
459
- }
460
- addMeshCollider(collider, mesh, convex, scale) {
461
- if (!this.enabled) {
462
- if (debugPhysics)
463
- console.warn("Physics are disabled");
464
- return;
465
- }
466
- const geo = mesh.geometry;
467
- if (!geo) {
468
- if (debugPhysics)
469
- console.warn("Missing mesh geometry", mesh.name);
470
- return;
471
- }
472
- let positions = geo.getAttribute("position").array;
473
- const indices = geo.index?.array;
474
- // console.log(geo.center())
475
- // scaling seems not supported yet https://github.com/dimforge/rapier/issues/243
476
- if (Math.abs(scale.x - 1) > 0.0001 || Math.abs(scale.y - 1) > 0.0001 || Math.abs(scale.z - 1) > 0.0001) {
477
- const key = geo.uuid + "_" + scale.x + "_" + scale.y + "_" + scale.z + "_" + convex;
478
- if (this._meshCache.has(key)) {
479
- positions = this._meshCache.get(key);
480
- }
481
- else {
482
- console.warn("Your model is using scaled mesh colliders which is not optimal for performance", mesh.name, Object.assign({}, scale), mesh);
483
- // showBalloonWarning("Your model is using scaled mesh colliders which is not optimal for performance: " + mesh.name + ", consider using unscaled objects");
484
- const scaledPositions = new Float32Array(positions.length);
485
- for (let i = 0; i < positions.length; i += 3) {
486
- scaledPositions[i] = positions[i] * scale.x;
487
- scaledPositions[i + 1] = positions[i + 1] * scale.y;
488
- scaledPositions[i + 2] = positions[i + 2] * scale.z;
489
- }
490
- positions = scaledPositions;
491
- this._meshCache.set(key, scaledPositions);
492
- }
493
- }
494
- const desc = convex ? ColliderDesc.convexMesh(positions) : ColliderDesc.trimesh(positions, indices);
495
- if (desc) {
496
- const col = this.createCollider(collider, desc);
497
- col.setMassProperties(1, { x: 0, y: 0, z: 0 }, { x: 0, y: 0, z: 0 }, { x: 0, y: 0, z: 0, w: 1 });
498
- // rb?.setTranslation({ x: 0, y: 2, z: 0 });
499
- // col.setTranslationWrtParent(new Vector3(0,2,0));
500
- }
501
- }
502
- createCollider(collider, desc, center) {
503
- if (!this.world)
504
- throw new Error("Physics world not initialized");
505
- const matrix = this._tempMatrix;
506
- const { rigidBody, useExplicitMassProperties } = this.getRigidbody(collider, this._tempMatrix);
507
- matrix.decompose(this._tempPosition, this._tempQuaternion, this._tempScale);
508
- getWorldScale(collider.gameObject, this._tempScale);
509
- if (center) {
510
- center.multiply(this._tempScale);
511
- this._tempPosition.x -= center.x;
512
- this._tempPosition.y += center.y;
513
- this._tempPosition.z += center.z;
514
- }
515
- desc.setTranslation(this._tempPosition.x, this._tempPosition.y, this._tempPosition.z);
516
- desc.setRotation(this._tempQuaternion);
517
- desc.setSensor(collider.isTrigger);
518
- // TODO: we might want to update this if the material changes
519
- const physicsMaterial = collider.sharedMaterial;
520
- if (physicsMaterial) {
521
- CoefficientCombineRule;
522
- desc.setRestitution(physicsMaterial.bounciness);
523
- switch (physicsMaterial.bounceCombine) {
524
- case PhysicsMaterialCombine.Average:
525
- desc.setRestitutionCombineRule(CoefficientCombineRule.Average);
526
- break;
527
- case PhysicsMaterialCombine.Maximum:
528
- desc.setRestitutionCombineRule(CoefficientCombineRule.Max);
529
- break;
530
- case PhysicsMaterialCombine.Minimum:
531
- desc.setRestitutionCombineRule(CoefficientCombineRule.Min);
532
- break;
533
- case PhysicsMaterialCombine.Multiply:
534
- desc.setRestitutionCombineRule(CoefficientCombineRule.Multiply);
535
- break;
536
- }
537
- desc.setFriction(physicsMaterial.dynamicFriction);
538
- switch (physicsMaterial.frictionCombine) {
539
- case PhysicsMaterialCombine.Average:
540
- desc.setFrictionCombineRule(CoefficientCombineRule.Average);
541
- break;
542
- case PhysicsMaterialCombine.Maximum:
543
- desc.setFrictionCombineRule(CoefficientCombineRule.Max);
544
- break;
545
- case PhysicsMaterialCombine.Minimum:
546
- desc.setFrictionCombineRule(CoefficientCombineRule.Min);
547
- break;
548
- case PhysicsMaterialCombine.Multiply:
549
- desc.setFrictionCombineRule(CoefficientCombineRule.Multiply);
550
- break;
551
- }
552
- }
553
- // if we want to use explicit mass properties, we need to set the collider density to 0
554
- // otherwise rapier will compute the mass properties based on the collider shape and density
555
- // https://rapier.rs/docs/user_guides/javascript/rigid_bodies#mass-properties
556
- if (useExplicitMassProperties) {
557
- // desc.setDensity(0);
558
- }
559
- const col = this.world.createCollider(desc, rigidBody);
560
- col[$componentKey] = collider;
561
- collider[$bodyKey] = col;
562
- col.setActiveEvents(ActiveEvents.COLLISION_EVENTS);
563
- // We want to receive collisitons between two triggers too
564
- col.setActiveCollisionTypes(ActiveCollisionTypes.ALL);
565
- // const objectLayerMask = collider.gameObject.layers.mask;
566
- // const mask = objectLayerMask & ~2;
567
- // col.setCollisionGroups(objectLayerMask);
568
- this.objects.push(collider);
569
- this.bodies.push(col);
570
- return col;
571
- }
572
- getRigidbody(collider, _matrix) {
573
- if (!this.world)
574
- throw new Error("Physics world not initialized");
575
- let rigidBody = null;
576
- let useExplicitMassProperties = false;
577
- if (collider.attachedRigidbody) {
578
- const rb = collider.attachedRigidbody;
579
- rigidBody = rb[$bodyKey];
580
- useExplicitMassProperties = true;
581
- if (!rigidBody) {
582
- const kinematic = rb.isKinematic && !debugColliderPlacement;
583
- if (debugPhysics)
584
- console.log("Create rigidbody", kinematic);
585
- const rigidBodyDesc = kinematic ? RAPIER.RigidBodyDesc.kinematicPositionBased() : RAPIER.RigidBodyDesc.dynamic();
586
- const pos = getWorldPosition(collider.attachedRigidbody.gameObject);
587
- rigidBodyDesc.setTranslation(pos.x, pos.y, pos.z);
588
- rigidBodyDesc.setRotation(getWorldQuaternion(collider.attachedRigidbody.gameObject));
589
- rigidBody = this.world.createRigidBody(rigidBodyDesc);
590
- this.bodies.push(rigidBody);
591
- this.objects.push(rb);
592
- }
593
- rigidBody[$componentKey] = rb;
594
- rb[$bodyKey] = rigidBody;
595
- this.internalUpdateProperties(rb, rigidBody);
596
- this.getRigidbodyRelativeMatrix(collider.gameObject, rb.gameObject, _matrix);
597
- }
598
- else {
599
- const rigidBodyDesc = RAPIER.RigidBodyDesc.kinematicPositionBased();
600
- const pos = getWorldPosition(collider.gameObject);
601
- rigidBodyDesc.setTranslation(pos.x, pos.y, pos.z);
602
- rigidBodyDesc.setRotation(getWorldQuaternion(collider.gameObject));
603
- rigidBody = this.world.createRigidBody(rigidBodyDesc);
604
- _matrix.identity();
605
- rigidBody[$componentKey] = null;
606
- }
607
- collider[$colliderRigidbody] = rigidBody;
608
- return { rigidBody: rigidBody, useExplicitMassProperties: useExplicitMassProperties };
609
- }
610
- removeBody(obj) {
611
- const body = obj[$bodyKey];
612
- obj[$bodyKey] = null;
613
- if (body && this.world) {
614
- const index = this.objects.findIndex(o => o === obj);
615
- if (index >= 0) {
616
- const body = this.bodies[index];
617
- this.bodies.splice(index, 1);
618
- this.objects.splice(index, 1);
619
- if (body instanceof Collider) {
620
- const collider = body;
621
- this.world?.removeCollider(collider, true);
622
- // remove the rigidbody if it doesnt have colliders anymore
623
- const rb = collider.parent();
624
- if (rb && rb.numColliders() <= 0) {
625
- this.world?.removeRigidBody(rb);
626
- }
627
- }
628
- else if (body instanceof RigidBody) {
629
- // TODO: running this code below causes a crash in rapier
630
- // const rb = body as RigidBody;
631
- // console.log("colliders", rb.numColliders())
632
- // for (let i = 0; i < rb.numColliders(); i++) {
633
- // const col = rb.collider(i);
634
- // this.world?.removeCollider(col, true);
635
- // }
636
- // console.log("colliders", rb.numColliders(), rb)
637
- // console.log(rb.handle, rb.userData);
638
- // if (rb.userData === undefined)
639
- // this.world?.removeRigidBody(rb);
640
- }
641
- // check if we need to remove the rigidbody too
642
- // const col = obj as ICollider;
643
- // if (col.isCollider && col.attachedRigidbody) {
644
- // const rb = col.attachedRigidbody[$bodyKey] as RigidBody;
645
- // if (rb && rb.numColliders() <= 0) {
646
- // // this.world?.removeRigidBody(rb);
647
- // }
648
- // }
649
- }
650
- }
651
- }
652
- updateBody(comp, translation, rotation) {
653
- if (!this.enabled)
654
- return;
655
- if (comp.destroyed || !comp.gameObject)
656
- return;
657
- if (!translation && !rotation)
658
- return;
659
- if (comp.isCollider === true) {
660
- // const collider = comp as ICollider;
661
- console.warn("TODO: implement updating collider position");
662
- }
663
- else {
664
- const rigidbody = comp;
665
- const body = rigidbody[$bodyKey];
666
- if (body) {
667
- this.syncPhysicsBody(rigidbody.gameObject, body, translation, rotation);
668
- }
669
- }
670
- }
671
- updateProperties(rigidbody) {
672
- const physicsBody = rigidbody[$bodyKey];
673
- if (physicsBody) {
674
- this.internalUpdateProperties(rigidbody, physicsBody);
675
- }
676
- }
677
- internal_getRigidbody(rb) {
678
- return rb[$bodyKey];
679
- }
680
- internalUpdateProperties(rb, rigidbody) {
681
- // continuous collision detection
682
- // https://rapier.rs/docs/user_guides/javascript/rigid_bodies#continuous-collision-detection
683
- rigidbody.enableCcd(rb.collisionDetectionMode !== CollisionDetectionMode.Discrete);
684
- rigidbody.setLinearDamping(rb.drag);
685
- rigidbody.setAngularDamping(rb.angularDrag);
686
- rigidbody.setGravityScale(rb.useGravity ? rb.gravityScale : 0, true);
687
- // https://rapier.rs/docs/user_guides/javascript/rigid_bodies#mass-properties
688
- // rigidbody.setAdditionalMass(rb.mass, true);
689
- // for (let i = 0; i < rigidbody.numColliders(); i++) {
690
- // const collider = rigidbody.collider(i);
691
- // if (collider) {
692
- // collider.setMass(rb.mass);
693
- // // const density = rb.mass / collider.shape.computeMassProperties().mass;
694
- // }
695
- // }
696
- // lock rotations
697
- rigidbody.setEnabledRotations(!rb.lockRotationX, !rb.lockRotationY, !rb.lockRotationZ, true);
698
- rigidbody.setEnabledTranslations(!rb.lockPositionX, !rb.lockPositionY, !rb.lockPositionZ, true);
699
- if (rb.isKinematic) {
700
- rigidbody.setBodyType(RAPIER.RigidBodyType.KinematicPositionBased);
701
- }
702
- else {
703
- rigidbody.setBodyType(RAPIER.RigidBodyType.Dynamic);
704
- }
705
- }
706
- // private _lastStepTime: number | undefined = 0;
707
- lines;
708
- step(dt) {
709
- if (!this.world)
710
- return;
711
- if (!this.enabled)
712
- return;
713
- this._isUpdatingPhysicsWorld = true;
714
- if (!this.eventQueue) {
715
- this.eventQueue = new EventQueue(false);
716
- }
717
- if (dt) {
718
- // if we make to sudden changes to the timestep the physics can get unstable
719
- // https://rapier.rs/docs/user_guides/javascript/integration_parameters/#dt
720
- this.world.timestep = Mathf.lerp(this.world.timestep, dt, 0.8);
721
- }
722
- this.world.step(this.eventQueue);
723
- this._isUpdatingPhysicsWorld = false;
724
- this.updateDebugRendering(this.world);
725
- }
726
- updateDebugRendering(world) {
727
- if (debugPhysics || debugColliderPlacement || showColliders) {
728
- if (!this.lines) {
729
- const material = new LineBasicMaterial({
730
- color: 0x227700,
731
- // vertexColors: THREE.VertexColors
732
- });
733
- const geometry = new BufferGeometry();
734
- this.lines = new LineSegments(geometry, material);
735
- this.context.scene.add(this.lines);
736
- }
737
- const buffers = world.debugRender();
738
- this.lines.geometry.setAttribute('position', new BufferAttribute(buffers.vertices, 3));
739
- this.lines.geometry.setAttribute('color', new BufferAttribute(buffers.colors, 4));
740
- }
741
- }
742
- postStep() {
743
- if (!this.world)
744
- return;
745
- if (!this.enabled)
746
- return;
747
- this._isUpdatingPhysicsWorld = true;
748
- this.syncObjects();
749
- this._isUpdatingPhysicsWorld = false;
750
- if (this.eventQueue && !this.collisionHandler) {
751
- this.collisionHandler = new PhysicsCollisionHandler(this.world, this.eventQueue);
752
- }
753
- if (this.collisionHandler) {
754
- this.collisionHandler.handleCollisionEvents();
755
- this.collisionHandler.update();
756
- }
757
- }
758
- /** sync rendered objects with physics world (except for colliders without rigidbody) */
759
- syncObjects() {
760
- if (debugColliderPlacement)
761
- return;
762
- for (let i = 0; i < this.bodies.length; i++) {
763
- const obj = this.objects[i];
764
- const body = this.bodies[i];
765
- // if the collider is not attached to a rigidbody
766
- // it means that its kinematic so we need to update its position
767
- const col = obj;
768
- if (col?.isCollider === true && !col.attachedRigidbody) {
769
- const rigidbody = body.parent();
770
- if (rigidbody)
771
- this.syncPhysicsBody(obj.gameObject, rigidbody, true, true);
772
- continue;
773
- }
774
- // sync
775
- const pos = body.translation();
776
- const rot = body.rotation();
777
- // make sure to keep the collider offset
778
- const center = obj["center"];
779
- if (center && center.isVector3) {
780
- this._tempQuaternion.set(rot.x, rot.y, rot.z, rot.w);
781
- const offset = this._tempPosition.copy(center).applyQuaternion(this._tempQuaternion);
782
- const scale = getWorldScale(obj.gameObject);
783
- offset.multiply(scale);
784
- pos.x -= offset.x;
785
- pos.y -= offset.y;
786
- pos.z -= offset.z;
787
- }
788
- setWorldPositionXYZ(obj.gameObject, pos.x, pos.y, pos.z);
789
- setWorldQuaternionXYZW(obj.gameObject, rot.x, rot.y, rot.z, rot.w);
790
- }
791
- }
792
- syncPhysicsBody(obj, body, translation, rotation) {
793
- // const bodyType = body.bodyType();
794
- // const previous = physicsBody.translation();
795
- // const vel = physicsBody.linvel();
796
- const worldPosition = getWorldPosition(obj, this._tempPosition);
797
- const worldQuaternion = getWorldQuaternion(obj, this._tempQuaternion);
798
- const type = body.bodyType();
799
- switch (type) {
800
- case RigidBodyType.Fixed:
801
- case RigidBodyType.KinematicPositionBased:
802
- case RigidBodyType.KinematicVelocityBased:
803
- if (translation)
804
- body.setNextKinematicTranslation(worldPosition);
805
- if (rotation)
806
- body.setNextKinematicRotation(worldQuaternion);
807
- break;
808
- default:
809
- if (translation)
810
- body.setTranslation(worldPosition, false);
811
- if (rotation)
812
- body.setRotation(worldQuaternion, false);
813
- break;
814
- }
815
- body.wakeUp();
816
- // physicsBody.setBodyType(RAPIER.RigidBodyType.Fixed);
817
- // physicsBody.setLinvel(vel, false);
818
- // update velocity
819
- // const pos = physicsBody.translation();
820
- // pos.x -= previous.x;
821
- // pos.y -= previous.y;
822
- // pos.z -= previous.z;
823
- // // threhold
824
- // const t = 1;
825
- // const canUpdateVelocity = Math.abs(pos.x) < t && Math.abs(pos.y) < t && Math.abs(pos.z) < t;
826
- // if (canUpdateVelocity) {
827
- // const damping = 1 + this.context.time.deltaTime;
828
- // vel.x *= damping;
829
- // vel.y *= damping;
830
- // vel.z *= damping;
831
- // vel.x += pos.x;
832
- // vel.y += pos.y;
833
- // vel.z += pos.z;
834
- // console.log(vel);
835
- // physicsBody.setLinvel(vel, true);
836
- // }
837
- // else if(debugPhysics) console.warn("Movement exceeded threshold, not updating velocity", pos);
838
- // body.setBodyType(bodyType);
839
- }
840
- static _matricesBuffer = [];
841
- getRigidbodyRelativeMatrix(comp, rigidbody, mat, matrices) {
842
- // collect all matrices to the rigidbody and then build the rigidbody relative matrix
843
- if (matrices === undefined) {
844
- matrices = Physics._matricesBuffer;
845
- matrices.length = 0;
846
- }
847
- if (comp === rigidbody) {
848
- const scale = getWorldScale(comp, this._tempPosition);
849
- mat.makeScale(scale.x, scale.y, scale.z);
850
- for (let i = matrices.length - 1; i >= 0; i--) {
851
- mat.multiply(matrices[i]);
852
- }
853
- return mat;
854
- }
855
- matrices.push(comp.matrix);
856
- if (comp.parent) {
857
- this.getRigidbodyRelativeMatrix(comp.parent, rigidbody, mat, matrices);
858
- }
859
- return mat;
860
- }
861
- static centerConnectionPos = { x: 0, y: 0, z: 0 };
862
- static centerConnectionRot = { x: 0, y: 0, z: 0, w: 1 };
863
- addFixedJoint(body1, body2) {
864
- if (!this.world) {
865
- console.error("Physics world not initialized");
866
- return;
867
- }
868
- const b1 = body1[$bodyKey];
869
- const b2 = body2[$bodyKey];
870
- this.calculateJointRelativeMatrices(body1.gameObject, body2.gameObject, this._tempMatrix);
871
- this._tempMatrix.decompose(this._tempPosition, this._tempQuaternion, this._tempScale);
872
- const params = JointData.fixed(Physics.centerConnectionPos, Physics.centerConnectionRot, this._tempPosition, this._tempQuaternion);
873
- const joint = this.world.createImpulseJoint(params, b1, b2, true);
874
- if (debugPhysics)
875
- console.log("ADD FIXED JOINT", joint);
876
- }
877
- /** The joint prevents any relative movement between two rigid-bodies, except for relative rotations along one axis. This is typically used to simulate wheels, fans, etc. They are characterized by one local anchor as well as one local axis on each rigid-body. */
878
- addHingeJoint(body1, body2, anchor, axis) {
879
- if (!this.world) {
880
- console.error("Physics world not initialized");
881
- return;
882
- }
883
- const b1 = body1[$bodyKey];
884
- const b2 = body2[$bodyKey];
885
- this.calculateJointRelativeMatrices(body1.gameObject, body2.gameObject, this._tempMatrix);
886
- this._tempMatrix.decompose(this._tempPosition, this._tempQuaternion, this._tempScale);
887
- let params = RAPIER.JointData.revolute(anchor, this._tempPosition, axis);
888
- let joint = this.world.createImpulseJoint(params, b1, b2, true);
889
- if (debugPhysics)
890
- console.log("ADD HINGE JOINT", joint);
891
- }
892
- calculateJointRelativeMatrices(body1, body2, mat) {
893
- body1.updateWorldMatrix(true, false);
894
- body2.updateWorldMatrix(true, false);
895
- const world1 = body1.matrixWorld;
896
- const world2 = body2.matrixWorld;
897
- // set scale to 1
898
- world1.elements[0] = 1;
899
- world1.elements[5] = 1;
900
- world1.elements[10] = 1;
901
- world2.elements[0] = 1;
902
- world2.elements[5] = 1;
903
- world2.elements[10] = 1;
904
- mat.copy(world2).premultiply(world1.invert()).invert();
905
- }
906
- }
907
- /** responsible of processing collision events for the component system */
908
- class PhysicsCollisionHandler {
909
- world;
910
- eventQueue;
911
- constructor(world, eventQueue) {
912
- this.world = world;
913
- this.eventQueue = eventQueue;
914
- }
915
- activeCollisions = [];
916
- activeCollisionsStay = [];
917
- activeTriggers = [];
918
- handleCollisionEvents() {
919
- if (!this.eventQueue)
920
- return;
921
- if (!this.world)
922
- return;
923
- this.eventQueue.drainCollisionEvents((handle1, handle2, started) => {
924
- const col1 = this.world.getCollider(handle1);
925
- const col2 = this.world.getCollider(handle2);
926
- const colliderComponent1 = col1[$componentKey];
927
- const colliderComponent2 = col2[$componentKey];
928
- if (debugCollisions)
929
- console.log("EVT", colliderComponent1.name, colliderComponent2.name, started, col1, col2);
930
- if (colliderComponent1 && colliderComponent2) {
931
- if (started) {
932
- this.onCollisionStarted(colliderComponent1, col1, colliderComponent2, col2);
933
- this.onCollisionStarted(colliderComponent2, col2, colliderComponent1, col1);
934
- }
935
- else {
936
- this.onCollisionEnded(colliderComponent1, colliderComponent2);
937
- this.onCollisionEnded(colliderComponent2, colliderComponent1);
938
- }
939
- }
940
- });
941
- }
942
- update() {
943
- this.onHandleCollisionStay();
944
- }
945
- onCollisionStarted(self, selfBody, other, otherBody) {
946
- let collision = null;
947
- // if one is a trigger we dont get collisions but want to raise the trigger events
948
- if (self.isTrigger || other.isTrigger) {
949
- foreachComponent(self.gameObject, (c) => {
950
- if (c.onTriggerEnter && !c.destroyed) {
951
- c.onTriggerEnter(other);
952
- }
953
- this.activeTriggers.push({ collider: self, component: c, otherCollider: other });
954
- });
955
- }
956
- else {
957
- const object = self.gameObject;
958
- // TODO: we dont respect the flip value here!
959
- this.world.contactPair(selfBody, otherBody, (manifold, _flipped) => {
960
- foreachComponent(object, (c) => {
961
- if (c.destroyed)
962
- return;
963
- const hasDeclaredEventMethod = c.onCollisionEnter || c.onCollisionStay || c.onCollisionExit;
964
- if (hasDeclaredEventMethod || debugCollisions) {
965
- if (!collision) {
966
- const contacts = [];
967
- const normal = manifold.normal();
968
- for (let i = 0; i < manifold.numSolverContacts(); i++) {
969
- // solver points are in world space
970
- // https://rapier.rs/docs/user_guides/javascript/advanced_collision_detection_js#the-contact-graph
971
- const pt = manifold.solverContactPoint(i);
972
- const impulse = manifold.contactImpulse(i);
973
- if (pt) {
974
- const dist = manifold.contactDist(i);
975
- const friction = manifold.solverContactFriction(i);
976
- const contact = new ContactPoint(pt, dist, normal, impulse, friction);
977
- contacts.push(contact);
978
- if (debugCollisions) {
979
- Gizmos.DrawDirection(pt, normal, 0xff0000, 3, true);
980
- }
981
- }
982
- }
983
- collision = new Collision(object, other, contacts);
984
- }
985
- // we only need to keep track if any event exists
986
- if (hasDeclaredEventMethod) {
987
- const info = { collider: self, component: c, collision };
988
- this.activeCollisions.push(info);
989
- if (c.onCollisionStay) {
990
- this.activeCollisionsStay.push(info);
991
- }
992
- c.onCollisionEnter?.call(c, collision);
993
- }
994
- }
995
- });
996
- });
997
- }
998
- }
999
- onHandleCollisionStay() {
1000
- for (const active of this.activeCollisionsStay) {
1001
- const c = active.component;
1002
- if (c.destroyed)
1003
- continue;
1004
- if (c.activeAndEnabled && c.onCollisionStay) {
1005
- const arg = active.collision;
1006
- c.onCollisionStay(arg);
1007
- }
1008
- }
1009
- for (const active of this.activeTriggers) {
1010
- const c = active.component;
1011
- if (c.destroyed)
1012
- continue;
1013
- if (c.activeAndEnabled && c.onTriggerStay) {
1014
- const arg = active.otherCollider;
1015
- c.onTriggerStay(arg);
1016
- }
1017
- }
1018
- }
1019
- onCollisionEnded(self, other) {
1020
- if (self.destroyed || other.destroyed)
1021
- return;
1022
- for (let i = 0; i < this.activeCollisions.length; i++) {
1023
- const active = this.activeCollisions[i];
1024
- const collider = active.collider;
1025
- if (collider.destroyed) {
1026
- this.activeCollisions.splice(i, 1);
1027
- i--;
1028
- continue;
1029
- }
1030
- if (collider === self && active.collision.collider === other) {
1031
- const c = active.component;
1032
- this.activeCollisions.splice(i, 1);
1033
- i--;
1034
- if (c.activeAndEnabled && c.onCollisionExit) {
1035
- const collision = active.collision;
1036
- c.onCollisionExit(collision);
1037
- }
1038
- }
1039
- }
1040
- for (let i = 0; i < this.activeCollisionsStay.length; i++) {
1041
- const active = this.activeCollisionsStay[i];
1042
- const collider = active.collider;
1043
- if (collider.destroyed) {
1044
- this.activeCollisionsStay.splice(i, 1);
1045
- i--;
1046
- continue;
1047
- }
1048
- if (collider === self && active.collision.collider === other) {
1049
- const c = active.component;
1050
- this.activeCollisionsStay.splice(i, 1);
1051
- i--;
1052
- if (c.activeAndEnabled && c.onCollisionExit) {
1053
- const collision = active.collision;
1054
- c.onCollisionExit(collision);
1055
- }
1056
- }
1057
- }
1058
- for (let i = 0; i < this.activeTriggers.length; i++) {
1059
- const active = this.activeTriggers[i];
1060
- const collider = active.collider;
1061
- if (collider.destroyed) {
1062
- this.activeTriggers.splice(i, 1);
1063
- i--;
1064
- continue;
1065
- }
1066
- if (collider === self && active.otherCollider === other) {
1067
- const c = active.component;
1068
- this.activeTriggers.splice(i, 1);
1069
- i--;
1070
- if (c.activeAndEnabled && c.onTriggerExit) {
1071
- const collision = active.otherCollider;
1072
- c.onTriggerExit(collision);
1073
- }
1074
- }
1075
- }
1076
- }
1077
205
  }
1078
206
  //# sourceMappingURL=engine_physics.js.map