@galacean/engine-physics-physx 0.0.0-experimental-2.0-migrate.6 → 0.0.0-experimental-2.0-migrate.7

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.
@@ -0,0 +1,2095 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@galacean/engine')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', '@galacean/engine'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.Galacean = global.Galacean || {}, global.Galacean.PhysicsPhysX = {}), global.Galacean));
5
+ })(this, (function (exports, engine) { 'use strict';
6
+
7
+ function _type_of(obj) {
8
+ "@swc/helpers - typeof";
9
+
10
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
11
+ }
12
+
13
+ function _instanceof(left, right) {
14
+ if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
15
+ return !!right[Symbol.hasInstance](left);
16
+ } else return left instanceof right;
17
+ }
18
+
19
+ function _set_prototype_of(o, p) {
20
+ _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
21
+ o.__proto__ = p;
22
+
23
+ return o;
24
+ };
25
+
26
+ return _set_prototype_of(o, p);
27
+ }
28
+
29
+ function _inherits(subClass, superClass) {
30
+ if (typeof superClass !== "function" && superClass !== null) {
31
+ throw new TypeError("Super expression must either be null or a function");
32
+ }
33
+
34
+ subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } });
35
+
36
+ if (superClass) _set_prototype_of(subClass, superClass);
37
+ }
38
+
39
+ /**
40
+ * Flags which affect the behavior of Shapes.
41
+ */ var ShapeFlag = /*#__PURE__*/ function(ShapeFlag) {
42
+ /** The shape will partake in collision in the physical simulation. */ ShapeFlag[ShapeFlag["SIMULATION_SHAPE"] = 1] = "SIMULATION_SHAPE";
43
+ /** The shape will partake in scene queries (ray casts, overlap tests, sweeps, ...). */ ShapeFlag[ShapeFlag["SCENE_QUERY_SHAPE"] = 2] = "SCENE_QUERY_SHAPE";
44
+ /** The shape is a trigger which can send reports whenever other shapes enter/leave its volume. */ ShapeFlag[ShapeFlag["TRIGGER_SHAPE"] = 4] = "TRIGGER_SHAPE";
45
+ return ShapeFlag;
46
+ }({});
47
+ /**
48
+ * Abstract class for collider shapes.
49
+ */ var PhysXColliderShape = /*#__PURE__*/ function() {
50
+ function PhysXColliderShape(physXPhysics) {
51
+ /** @internal */ this._controllers = new engine.DisorderedArray();
52
+ /** @internal */ this._contactOffset = 0.02;
53
+ /** @internal */ this._worldScale = new engine.Vector3(1, 1, 1);
54
+ /** @internal */ this._position = new engine.Vector3();
55
+ /** @internal */ this._rotation = new engine.Vector3();
56
+ this._axis = null;
57
+ this._physXRotation = new engine.Quaternion();
58
+ this._shapeFlags = 2 | 1;
59
+ this._physXPhysics = physXPhysics;
60
+ this._contactOffset = physXPhysics.getDefaultContactOffset();
61
+ }
62
+ var _proto = PhysXColliderShape.prototype;
63
+ /**
64
+ * {@inheritDoc IColliderShape.setRotation }
65
+ */ _proto.setRotation = function setRotation(value) {
66
+ var rotation = this._rotation.set(engine.MathUtil.degreeToRadian(value.x), engine.MathUtil.degreeToRadian(value.y), engine.MathUtil.degreeToRadian(value.z));
67
+ engine.Quaternion.rotationYawPitchRoll(rotation.y, rotation.x, rotation.z, this._physXRotation);
68
+ this._axis && engine.Quaternion.multiply(this._physXRotation, this._axis, this._physXRotation);
69
+ this._physXRotation.normalize();
70
+ this._setLocalPose();
71
+ };
72
+ /**
73
+ * {@inheritDoc IColliderShape.setPosition }
74
+ */ _proto.setPosition = function setPosition(value) {
75
+ if (value !== this._position) {
76
+ this._position.copyFrom(value);
77
+ }
78
+ var controllers = this._controllers;
79
+ for(var i = 0, n = controllers.length; i < n; i++){
80
+ controllers.get(i)._updateShapePosition(this._position, this._worldScale);
81
+ }
82
+ this._setLocalPose();
83
+ };
84
+ /**
85
+ * {@inheritDoc IColliderShape.setWorldScale }
86
+ */ _proto.setWorldScale = function setWorldScale(scale) {
87
+ this._worldScale.set(Math.abs(scale.x), Math.abs(scale.y), Math.abs(scale.z));
88
+ this._setLocalPose();
89
+ var controllers = this._controllers;
90
+ for(var i = 0, n = controllers.length; i < n; i++){
91
+ controllers.get(i)._updateShapePosition(this._position, this._worldScale);
92
+ }
93
+ };
94
+ /**
95
+ * {@inheritDoc IColliderShape.setContactOffset }
96
+ * @default 0.02f * PxTolerancesScale::length
97
+ */ _proto.setContactOffset = function setContactOffset(offset) {
98
+ this._contactOffset = offset;
99
+ var controllers = this._controllers;
100
+ if (controllers.length) {
101
+ for(var i = 0, n = controllers.length; i < n; i++){
102
+ var _controllers_get__pxController;
103
+ (_controllers_get__pxController = controllers.get(i)._pxController) == null ? void 0 : _controllers_get__pxController.setContactOffset(offset);
104
+ }
105
+ } else {
106
+ this._pxShape.setContactOffset(offset);
107
+ }
108
+ };
109
+ /**
110
+ * {@inheritDoc IColliderShape.setMaterial }
111
+ */ _proto.setMaterial = function setMaterial(value) {
112
+ this._pxMaterial = value._pxMaterial;
113
+ this._pxShape.setMaterial(this._pxMaterial);
114
+ };
115
+ /**
116
+ * {@inheritDoc IColliderShape.setIsTrigger }
117
+ */ _proto.setIsTrigger = function setIsTrigger(value) {
118
+ this._modifyFlag(1, !value);
119
+ this._modifyFlag(4, value);
120
+ this._setShapeFlags(this._shapeFlags);
121
+ };
122
+ /**
123
+ * {@inheritDoc IColliderShape.pointDistance }
124
+ */ _proto.pointDistance = function pointDistance(point) {
125
+ var info = this._pxGeometry.pointDistance(this._pxShape.getGlobalPose(), point);
126
+ var closestPoint = info.closestPoint;
127
+ var res = PhysXColliderShape._tempVector4;
128
+ res.set(closestPoint.x, closestPoint.y, closestPoint.z, info.distance);
129
+ return res;
130
+ };
131
+ /**
132
+ * {@inheritDoc IColliderShape.destroy }
133
+ */ _proto.destroy = function destroy() {
134
+ var _this__pxGeometry;
135
+ this._pxShape.release();
136
+ (_this__pxGeometry = this._pxGeometry) == null ? void 0 : _this__pxGeometry.delete();
137
+ };
138
+ /**
139
+ * @internal
140
+ */ _proto._setShapeFlags = function _setShapeFlags(flags) {
141
+ this._shapeFlags = flags;
142
+ var shapeFlags = new this._physXPhysics._physX.PxShapeFlags(this._shapeFlags);
143
+ this._pxShape.setFlags(shapeFlags);
144
+ shapeFlags.delete();
145
+ };
146
+ _proto._setLocalPose = function _setLocalPose() {
147
+ var transform = PhysXColliderShape.transform;
148
+ engine.Vector3.multiply(this._position, this._worldScale, transform.translation);
149
+ transform.rotation = this._physXRotation;
150
+ this._pxShape.setLocalPose(transform);
151
+ };
152
+ _proto._initialize = function _initialize(material, id) {
153
+ this._id = id;
154
+ this._pxMaterial = material._pxMaterial;
155
+ var shapeFlags = new this._physXPhysics._physX.PxShapeFlags(this._shapeFlags);
156
+ this._pxShape = this._physXPhysics._pxPhysics.createShape(this._pxGeometry, material._pxMaterial, true, shapeFlags);
157
+ shapeFlags.delete();
158
+ this._pxShape.setUUID(id);
159
+ };
160
+ _proto._modifyFlag = function _modifyFlag(flag, value) {
161
+ this._shapeFlags = value ? this._shapeFlags | flag : this._shapeFlags & ~flag;
162
+ };
163
+ return PhysXColliderShape;
164
+ }();
165
+ PhysXColliderShape.halfSqrt = 0.70710678118655;
166
+ PhysXColliderShape.transform = {
167
+ translation: new engine.Vector3(),
168
+ rotation: null
169
+ };
170
+ PhysXColliderShape._tempVector4 = new engine.Vector4();
171
+
172
+ /**
173
+ * Box collider shape in PhysX.
174
+ */ var PhysXBoxColliderShape = /*#__PURE__*/ function(PhysXColliderShape) {
175
+ _inherits(PhysXBoxColliderShape, PhysXColliderShape);
176
+ function PhysXBoxColliderShape(physXPhysics, uniqueID, size, material) {
177
+ var _this;
178
+ _this = PhysXColliderShape.call(this, physXPhysics) || this, /** @internal */ _this._halfSize = new engine.Vector3();
179
+ var halfSize = _this._halfSize;
180
+ halfSize.set(size.x * 0.5, size.y * 0.5, size.z * 0.5);
181
+ _this._pxGeometry = new physXPhysics._physX.PxBoxGeometry(halfSize.x, halfSize.y, halfSize.z);
182
+ _this._initialize(material, uniqueID);
183
+ _this._setLocalPose();
184
+ return _this;
185
+ }
186
+ var _proto = PhysXBoxColliderShape.prototype;
187
+ /**
188
+ * {@inheritDoc IBoxColliderShape.setSize }
189
+ */ _proto.setSize = function setSize(value) {
190
+ var halfSize = this._halfSize;
191
+ var tempExtents = PhysXBoxColliderShape._tempHalfExtents;
192
+ halfSize.set(value.x * 0.5, value.y * 0.5, value.z * 0.5);
193
+ engine.Vector3.multiply(halfSize, this._worldScale, tempExtents);
194
+ this._pxGeometry.halfExtents = tempExtents;
195
+ this._pxShape.setGeometry(this._pxGeometry);
196
+ this._updateController(tempExtents);
197
+ };
198
+ /**
199
+ * {@inheritDoc IColliderShape.setRotation }
200
+ */ _proto.setRotation = function setRotation(value) {
201
+ PhysXColliderShape.prototype.setRotation.call(this, value);
202
+ if (this._controllers.length > 0) {
203
+ console.warn("Box character controller `rotation` is not supported in PhysX and will be ignored");
204
+ }
205
+ };
206
+ /**
207
+ * {@inheritDoc IColliderShape.setWorldScale }
208
+ */ _proto.setWorldScale = function setWorldScale(scale) {
209
+ PhysXColliderShape.prototype.setWorldScale.call(this, scale);
210
+ var tempExtents = PhysXBoxColliderShape._tempHalfExtents;
211
+ engine.Vector3.multiply(this._halfSize, this._worldScale, tempExtents);
212
+ this._pxGeometry.halfExtents = tempExtents;
213
+ this._pxShape.setGeometry(this._pxGeometry);
214
+ this._updateController(tempExtents);
215
+ };
216
+ _proto._updateController = function _updateController(extents) {
217
+ var controllers = this._controllers;
218
+ for(var i = 0, n = controllers.length; i < n; i++){
219
+ var pxController = controllers.get(i)._pxController;
220
+ if (pxController) {
221
+ pxController.setHalfHeight(extents.y);
222
+ pxController.setHalfSideExtent(extents.x);
223
+ pxController.setHalfForwardExtent(extents.z);
224
+ }
225
+ }
226
+ };
227
+ return PhysXBoxColliderShape;
228
+ }(PhysXColliderShape);
229
+ PhysXBoxColliderShape._tempHalfExtents = new engine.Vector3();
230
+
231
+ /**
232
+ * Capsule collider shape in PhysX.
233
+ */ var PhysXCapsuleColliderShape = /*#__PURE__*/ function(PhysXColliderShape1) {
234
+ _inherits(PhysXCapsuleColliderShape, PhysXColliderShape1);
235
+ function PhysXCapsuleColliderShape(physXPhysics, uniqueID, radius, height, material) {
236
+ var _this;
237
+ _this = PhysXColliderShape1.call(this, physXPhysics) || this, /** @internal */ _this._upAxis = 1;
238
+ _this._radius = radius;
239
+ _this._halfHeight = height * 0.5;
240
+ _this._axis = new engine.Quaternion(0, 0, PhysXColliderShape.halfSqrt, PhysXColliderShape.halfSqrt);
241
+ _this._physXRotation.copyFrom(_this._axis);
242
+ _this._pxGeometry = new physXPhysics._physX.PxCapsuleGeometry(radius, _this._halfHeight);
243
+ _this._initialize(material, uniqueID);
244
+ _this._setLocalPose();
245
+ return _this;
246
+ }
247
+ var _proto = PhysXCapsuleColliderShape.prototype;
248
+ /**
249
+ * {@inheritDoc ICapsuleColliderShape.setRadius }
250
+ */ _proto.setRadius = function setRadius(value) {
251
+ this._radius = value;
252
+ var sizeScale = this._worldScale;
253
+ switch(this._upAxis){
254
+ case 0:
255
+ this._pxGeometry.radius = this._radius * Math.max(sizeScale.y, sizeScale.z);
256
+ break;
257
+ case 1:
258
+ this._pxGeometry.radius = this._radius * Math.max(sizeScale.x, sizeScale.z);
259
+ break;
260
+ case 2:
261
+ this._pxGeometry.radius = this._radius * Math.max(sizeScale.x, sizeScale.y);
262
+ break;
263
+ }
264
+ this._pxShape.setGeometry(this._pxGeometry);
265
+ var radius = this._pxGeometry.radius;
266
+ var controllers = this._controllers;
267
+ for(var i = 0, n = controllers.length; i < n; i++){
268
+ var _controllers_get__pxController;
269
+ (_controllers_get__pxController = controllers.get(i)._pxController) == null ? void 0 : _controllers_get__pxController.setRadius(radius);
270
+ }
271
+ };
272
+ /**
273
+ * {@inheritDoc ICapsuleColliderShape.setHeight }
274
+ */ _proto.setHeight = function setHeight(value) {
275
+ this._halfHeight = value * 0.5;
276
+ var sizeScale = this._worldScale;
277
+ switch(this._upAxis){
278
+ case 0:
279
+ this._pxGeometry.halfHeight = this._halfHeight * sizeScale.x;
280
+ break;
281
+ case 1:
282
+ this._pxGeometry.halfHeight = this._halfHeight * sizeScale.y;
283
+ break;
284
+ case 2:
285
+ this._pxGeometry.halfHeight = this._halfHeight * sizeScale.z;
286
+ break;
287
+ }
288
+ this._pxShape.setGeometry(this._pxGeometry);
289
+ var height = this._pxGeometry.halfHeight * 2;
290
+ var controllers = this._controllers;
291
+ for(var i = 0, n = controllers.length; i < n; i++){
292
+ var _controllers_get__pxController;
293
+ (_controllers_get__pxController = controllers.get(i)._pxController) == null ? void 0 : _controllers_get__pxController.setHeight(height);
294
+ }
295
+ };
296
+ /**
297
+ * {@inheritDoc ICapsuleColliderShape.setRotation }
298
+ */ _proto.setRotation = function setRotation(value) {
299
+ PhysXColliderShape1.prototype.setRotation.call(this, value);
300
+ if (this._controllers.length > 0) {
301
+ console.warn("Capsule character controller `rotation` is not supported in PhysX and will be ignored");
302
+ }
303
+ };
304
+ /**
305
+ * {@inheritDoc ICapsuleColliderShape.setUpAxis }
306
+ */ _proto.setUpAxis = function setUpAxis(upAxis) {
307
+ var _this = this, rotation = _this._rotation, axis = _this._axis, physXRotation = _this._physXRotation;
308
+ this._upAxis = upAxis;
309
+ switch(this._upAxis){
310
+ case 0:
311
+ axis.set(0, 0, 0, 1);
312
+ break;
313
+ case 1:
314
+ axis.set(0, 0, PhysXColliderShape.halfSqrt, PhysXColliderShape.halfSqrt);
315
+ break;
316
+ case 2:
317
+ axis.set(0, PhysXColliderShape.halfSqrt, 0, PhysXColliderShape.halfSqrt);
318
+ break;
319
+ }
320
+ if (rotation) {
321
+ engine.Quaternion.rotationYawPitchRoll(rotation.y, rotation.x, rotation.z, physXRotation);
322
+ engine.Quaternion.multiply(physXRotation, axis, physXRotation);
323
+ } else {
324
+ physXRotation.copyFrom(axis);
325
+ }
326
+ this._setLocalPose();
327
+ if (this._controllers.length > 0) {
328
+ console.warn("Capsule character controller `upAxis` is not supported in PhysX and will be ignored");
329
+ }
330
+ };
331
+ /**
332
+ * {@inheritDoc IColliderShape.setWorldScale }
333
+ */ _proto.setWorldScale = function setWorldScale(scale) {
334
+ PhysXColliderShape1.prototype.setWorldScale.call(this, scale);
335
+ var sizeScale = this._worldScale;
336
+ var geometry = this._pxGeometry;
337
+ switch(this._upAxis){
338
+ case 0:
339
+ geometry.radius = this._radius * Math.max(sizeScale.y, sizeScale.z);
340
+ geometry.halfHeight = this._halfHeight * sizeScale.x;
341
+ break;
342
+ case 1:
343
+ geometry.radius = this._radius * Math.max(sizeScale.x, sizeScale.z);
344
+ geometry.halfHeight = this._halfHeight * sizeScale.y;
345
+ break;
346
+ case 2:
347
+ geometry.radius = this._radius * Math.max(sizeScale.x, sizeScale.y);
348
+ geometry.halfHeight = this._halfHeight * sizeScale.z;
349
+ break;
350
+ }
351
+ this._pxShape.setGeometry(geometry);
352
+ var radius = geometry.radius;
353
+ var height = geometry.halfHeight * 2;
354
+ var controllers = this._controllers;
355
+ for(var i = 0, n = controllers.length; i < n; i++){
356
+ var pxController = controllers.get(i)._pxController;
357
+ if (pxController) {
358
+ pxController.setRadius(radius);
359
+ pxController.setHeight(height);
360
+ }
361
+ }
362
+ };
363
+ return PhysXCapsuleColliderShape;
364
+ }(PhysXColliderShape);
365
+ /**
366
+ * The up axis of the collider shape.
367
+ */ var ColliderShapeUpAxis = /*#__PURE__*/ function(ColliderShapeUpAxis) {
368
+ /** Up axis is X. */ ColliderShapeUpAxis[ColliderShapeUpAxis["X"] = 0] = "X";
369
+ /** Up axis is Y. */ ColliderShapeUpAxis[ColliderShapeUpAxis["Y"] = 1] = "Y";
370
+ /** Up axis is Z. */ ColliderShapeUpAxis[ColliderShapeUpAxis["Z"] = 2] = "Z";
371
+ return ColliderShapeUpAxis;
372
+ }({});
373
+
374
+ /**
375
+ * Base class for character controllers.
376
+ */ var PhysXCharacterController = /*#__PURE__*/ function() {
377
+ function PhysXCharacterController(physXPhysics) {
378
+ /** @internal */ this._scene = null;
379
+ this._shapeScaledPosition = new engine.Vector3();
380
+ this._worldPosition = null;
381
+ this._physXPhysics = physXPhysics;
382
+ }
383
+ var _proto = PhysXCharacterController.prototype;
384
+ /**
385
+ * {@inheritDoc ICharacterController.move }
386
+ */ _proto.move = function move(disp, minDist, elapsedTime) {
387
+ var _this__pxController;
388
+ var _this__pxController_move;
389
+ return (_this__pxController_move = (_this__pxController = this._pxController) == null ? void 0 : _this__pxController.move(disp, minDist, elapsedTime)) != null ? _this__pxController_move : 0;
390
+ };
391
+ /**
392
+ * {@inheritDoc ICharacterController.setWorldPosition }
393
+ */ _proto.setWorldPosition = function setWorldPosition(position) {
394
+ this._worldPosition = position;
395
+ this._updateNativePosition();
396
+ };
397
+ /**
398
+ * {@inheritDoc ICharacterController.getWorldPosition }
399
+ */ _proto.getWorldPosition = function getWorldPosition(position) {
400
+ if (this._pxController) {
401
+ position.copyFrom(this._pxController.getPosition());
402
+ position.subtract(this._shapeScaledPosition);
403
+ }
404
+ };
405
+ /**
406
+ * {@inheritDoc ICharacterController.setStepOffset }
407
+ */ _proto.setStepOffset = function setStepOffset(offset) {
408
+ var _this__pxController;
409
+ (_this__pxController = this._pxController) == null ? void 0 : _this__pxController.setStepOffset(offset);
410
+ };
411
+ /**
412
+ * {@inheritDoc ICharacterController.setNonWalkableMode }
413
+ */ _proto.setNonWalkableMode = function setNonWalkableMode(flag) {
414
+ var _this__pxController;
415
+ (_this__pxController = this._pxController) == null ? void 0 : _this__pxController.setNonWalkableMode(flag);
416
+ };
417
+ /**
418
+ * {@inheritDoc ICharacterController.setUpDirection }
419
+ */ _proto.setUpDirection = function setUpDirection(up) {
420
+ var _this__pxController;
421
+ (_this__pxController = this._pxController) == null ? void 0 : _this__pxController.setUpDirection(up);
422
+ };
423
+ /**
424
+ * {@inheritDoc ICharacterController.setSlopeLimit }
425
+ */ _proto.setSlopeLimit = function setSlopeLimit(slopeLimit) {
426
+ var _this__pxController;
427
+ (_this__pxController = this._pxController) == null ? void 0 : _this__pxController.setSlopeLimit(Math.cos(slopeLimit * Math.PI / 180));
428
+ };
429
+ /**
430
+ * {@inheritDoc ICharacterController.addShape }
431
+ */ _proto.addShape = function addShape(shape) {
432
+ var _this__pxController, _this__scene;
433
+ // Add shape should sync latest position and world scale to pxController
434
+ this._updateShapePosition(shape._position, shape._worldScale);
435
+ // When CharacterController is disabled, set shape property need check pxController whether exist because of this._pxManager is null and won't create pxController
436
+ this._pxManager && this._createPXController(this._pxManager, shape);
437
+ this._shape = shape;
438
+ shape._controllers.add(this);
439
+ (_this__pxController = this._pxController) == null ? void 0 : _this__pxController.setContactOffset(shape._contactOffset);
440
+ (_this__scene = this._scene) == null ? void 0 : _this__scene._addColliderShape(shape._id);
441
+ };
442
+ /**
443
+ * {@inheritDoc ICharacterController.removeShape }
444
+ */ _proto.removeShape = function removeShape(shape) {
445
+ var _this__scene;
446
+ this._destroyPXController();
447
+ this._shape = null;
448
+ shape._controllers.delete(this);
449
+ (_this__scene = this._scene) == null ? void 0 : _this__scene._removeColliderShape(shape._id);
450
+ };
451
+ /**
452
+ * {@inheritDoc ICollider.setCollisionLayer }
453
+ */ _proto.setCollisionLayer = function setCollisionLayer(layer) {
454
+ var _this__pxController;
455
+ var actor = (_this__pxController = this._pxController) == null ? void 0 : _this__pxController.getActor();
456
+ if (actor) {
457
+ this._physXPhysics._physX.setGroup(actor, layer);
458
+ }
459
+ };
460
+ /**
461
+ * {@inheritDoc ICharacterController.destroy }
462
+ */ _proto.destroy = function destroy() {
463
+ this._destroyPXController();
464
+ };
465
+ /**
466
+ * @internal
467
+ */ _proto._createPXController = function _createPXController(pxManager, shape) {
468
+ var desc;
469
+ if (_instanceof(shape, PhysXBoxColliderShape)) {
470
+ desc = new this._physXPhysics._physX.PxBoxControllerDesc();
471
+ desc.halfHeight = shape._halfSize.y;
472
+ desc.halfSideExtent = shape._halfSize.x;
473
+ desc.halfForwardExtent = shape._halfSize.z;
474
+ if (shape._rotation.lengthSquared() > 0) {
475
+ console.warn("Box character controller `rotation` is not supported in PhysX and will be ignored");
476
+ }
477
+ } else if (_instanceof(shape, PhysXCapsuleColliderShape)) {
478
+ desc = new this._physXPhysics._physX.PxCapsuleControllerDesc();
479
+ desc.radius = shape._radius;
480
+ desc.height = shape._halfHeight * 2;
481
+ desc.climbingMode = 1; // constraint mode
482
+ if (shape._rotation.lengthSquared() > 0) {
483
+ console.warn("Capsule character controller `rotation` is not supported in PhysX and will be ignored");
484
+ }
485
+ if (shape._upAxis !== ColliderShapeUpAxis.Y) {
486
+ console.warn("Capsule character controller `upAxis` is not supported in PhysX and will be ignored");
487
+ }
488
+ } else {
489
+ throw "unsupported shape type";
490
+ }
491
+ desc.setMaterial(shape._pxMaterial);
492
+ this._pxController = pxManager._getControllerManager().createController(desc);
493
+ desc.delete();
494
+ this._pxController.setUUID(shape._id);
495
+ this._updateNativePosition();
496
+ };
497
+ /**
498
+ * @internal
499
+ */ _proto._destroyPXController = function _destroyPXController() {
500
+ if (this._pxController) {
501
+ this._pxController.release();
502
+ this._pxController = null;
503
+ }
504
+ };
505
+ /**
506
+ * @internal
507
+ */ _proto._updateShapePosition = function _updateShapePosition(shapePosition, worldScale) {
508
+ engine.Vector3.multiply(shapePosition, worldScale, this._shapeScaledPosition);
509
+ this._updateNativePosition();
510
+ };
511
+ _proto._updateNativePosition = function _updateNativePosition() {
512
+ var worldPosition = this._worldPosition;
513
+ if (this._pxController && worldPosition) {
514
+ engine.Vector3.add(worldPosition, this._shapeScaledPosition, PhysXCharacterController._tempVec);
515
+ this._pxController.setPosition(PhysXCharacterController._tempVec);
516
+ }
517
+ };
518
+ return PhysXCharacterController;
519
+ }();
520
+ PhysXCharacterController._tempVec = new engine.Vector3();
521
+
522
+ /**
523
+ * Abstract class of physical collider.
524
+ */ var PhysXCollider = /*#__PURE__*/ function() {
525
+ function PhysXCollider(physXPhysics) {
526
+ /** @internal */ this._scene = null;
527
+ /** @internal */ this._shapes = new Array();
528
+ this._physXPhysics = physXPhysics;
529
+ }
530
+ var _proto = PhysXCollider.prototype;
531
+ /**
532
+ * {@inheritDoc ICollider.addShape }
533
+ */ _proto.addShape = function addShape(shape) {
534
+ var _this__scene;
535
+ if (!this._pxActor.attachShape(shape._pxShape)) {
536
+ throw new Error("PhysXCollider: failed to attach shape to the native actor.");
537
+ }
538
+ this._shapes.push(shape);
539
+ (_this__scene = this._scene) == null ? void 0 : _this__scene._addColliderShape(shape._id);
540
+ };
541
+ /**
542
+ * {@inheritDoc ICollider.removeShape }
543
+ */ _proto.removeShape = function removeShape(shape) {
544
+ var _this__scene;
545
+ this._pxActor.detachShape(shape._pxShape, true);
546
+ var shapes = this._shapes;
547
+ shapes.splice(shapes.indexOf(shape), 1);
548
+ (_this__scene = this._scene) == null ? void 0 : _this__scene._removeColliderShape(shape._id);
549
+ };
550
+ /**
551
+ * {@inheritDoc ICollider.setWorldTransform }
552
+ */ _proto.setWorldTransform = function setWorldTransform(position, rotation) {
553
+ this._pxActor.setGlobalPose(this._transform(position, rotation), true);
554
+ };
555
+ /**
556
+ * {@inheritDoc ICollider.getWorldTransform }
557
+ */ _proto.getWorldTransform = function getWorldTransform(outPosition, outRotation) {
558
+ var transform = this._pxActor.getGlobalPose();
559
+ outPosition.set(transform.translation.x, transform.translation.y, transform.translation.z);
560
+ outRotation.set(transform.rotation.x, transform.rotation.y, transform.rotation.z, transform.rotation.w);
561
+ };
562
+ /**
563
+ * {@inheritDoc ICollider.setCollisionLayer }
564
+ */ _proto.setCollisionLayer = function setCollisionLayer(layer) {
565
+ this._physXPhysics._physX.setGroup(this._pxActor, layer);
566
+ };
567
+ /**
568
+ * {@inheritDoc ICollider.destroy }
569
+ */ _proto.destroy = function destroy() {
570
+ this._pxActor.release();
571
+ };
572
+ /**
573
+ * @internal
574
+ */ _proto._transform = function _transform(pos, rot) {
575
+ var transform = PhysXCollider._tempTransform;
576
+ transform.translation = pos;
577
+ transform.rotation = rot.normalize();
578
+ return transform;
579
+ };
580
+ return PhysXCollider;
581
+ }();
582
+ PhysXCollider._tempTransform = {
583
+ translation: null,
584
+ rotation: null
585
+ };
586
+
587
+ /**
588
+ * A dynamic collider can act with self-defined movement or physical force
589
+ */ var PhysXDynamicCollider = /*#__PURE__*/ function(PhysXCollider) {
590
+ _inherits(PhysXDynamicCollider, PhysXCollider);
591
+ function PhysXDynamicCollider(physXPhysics, position, rotation) {
592
+ var _this;
593
+ _this = PhysXCollider.call(this, physXPhysics) || this, /**
594
+ * Whether actor is currently kinematic.
595
+ * PhysX 拒绝在 kinematic actor 上启用 CCD(会打印警告并忽略),
596
+ * 所以 setCollisionDetectionMode 在 kinematic 状态下只缓存目标值,
597
+ * 等切回 dynamic 时再真正写到 PhysX。
598
+ */ _this._isKinematic = false, /**
599
+ * Cached collision detection mode. Always reflects user's intent.
600
+ * 实际 PhysX CCD flag 可能跟这个不一致(kinematic 时强制 Discrete)。
601
+ */ _this._collisionDetectionMode = 0;
602
+ var transform = _this._transform(position, rotation);
603
+ _this._pxActor = physXPhysics._pxPhysics.createRigidDynamic(transform);
604
+ return _this;
605
+ }
606
+ var _proto = PhysXDynamicCollider.prototype;
607
+ /**
608
+ * {@inheritDoc IDynamicCollider.setLinearDamping }
609
+ */ _proto.setLinearDamping = function setLinearDamping(value) {
610
+ this._pxActor.setLinearDamping(value);
611
+ };
612
+ /**
613
+ * {@inheritDoc IDynamicCollider.setAngularDamping }
614
+ */ _proto.setAngularDamping = function setAngularDamping(value) {
615
+ this._pxActor.setAngularDamping(value);
616
+ };
617
+ /**
618
+ * {@inheritDoc IDynamicCollider.getLinearVelocity }
619
+ */ _proto.getLinearVelocity = function getLinearVelocity(out) {
620
+ var velocity = this._pxActor.getLinearVelocity();
621
+ return out.set(velocity.x, velocity.y, velocity.z);
622
+ };
623
+ /**
624
+ * {@inheritDoc IDynamicCollider.setLinearVelocity }
625
+ */ _proto.setLinearVelocity = function setLinearVelocity(value) {
626
+ this._pxActor.setLinearVelocity(value, true);
627
+ };
628
+ /**
629
+ * {@inheritDoc IDynamicCollider.getAngularVelocity }
630
+ */ _proto.getAngularVelocity = function getAngularVelocity(out) {
631
+ var velocity = this._pxActor.getAngularVelocity();
632
+ return out.set(engine.MathUtil.radianToDegree(velocity.x), engine.MathUtil.radianToDegree(velocity.y), engine.MathUtil.radianToDegree(velocity.z));
633
+ };
634
+ /**
635
+ * {@inheritDoc IDynamicCollider.setAngularVelocity }
636
+ */ _proto.setAngularVelocity = function setAngularVelocity(value) {
637
+ PhysXDynamicCollider._tempTranslation.set(engine.MathUtil.degreeToRadian(value.x), engine.MathUtil.degreeToRadian(value.y), engine.MathUtil.degreeToRadian(value.z));
638
+ this._pxActor.setAngularVelocity(PhysXDynamicCollider._tempTranslation, true);
639
+ };
640
+ /**
641
+ * {@inheritDoc IDynamicCollider.setMass }
642
+ */ _proto.setMass = function setMass(value) {
643
+ this._pxActor.setMass(value);
644
+ };
645
+ /**
646
+ * {@inheritDoc IDynamicCollider.getCenterOfMass }
647
+ */ _proto.getCenterOfMass = function getCenterOfMass(out) {
648
+ var translation = this._pxActor.getCMassLocalPose().translation;
649
+ return out.set(translation.x, translation.y, translation.z);
650
+ };
651
+ /**
652
+ * {@inheritDoc IDynamicCollider.setCenterOfMass }
653
+ */ _proto.setCenterOfMass = function setCenterOfMass(position) {
654
+ this._pxActor.setCMassLocalPose(position);
655
+ };
656
+ /**
657
+ * {@inheritDoc IDynamicCollider.setInertiaTensor }
658
+ */ _proto.setInertiaTensor = function setInertiaTensor(value) {
659
+ this._pxActor.setMassSpaceInertiaTensor(value);
660
+ };
661
+ /**
662
+ * {@inheritDoc IDynamicCollider.getInertiaTensor }
663
+ */ _proto.getInertiaTensor = function getInertiaTensor(out) {
664
+ var inertia = this._pxActor.getMassSpaceInertiaTensor();
665
+ return out.set(inertia.x, inertia.y, inertia.z);
666
+ };
667
+ /**
668
+ * {@inheritDoc IDynamicCollider.setMassAndUpdateInertia }
669
+ */ _proto.setMassAndUpdateInertia = function setMassAndUpdateInertia(mass) {
670
+ this._pxActor.setMassAndUpdateInertia(mass);
671
+ };
672
+ /**
673
+ * {@inheritDoc IDynamicCollider.setMaxAngularVelocity }
674
+ */ _proto.setMaxAngularVelocity = function setMaxAngularVelocity(value) {
675
+ this._pxActor.setMaxAngularVelocity(engine.MathUtil.degreeToRadian(value));
676
+ };
677
+ /**
678
+ * {@inheritDoc IDynamicCollider.setMaxDepenetrationVelocity }
679
+ */ _proto.setMaxDepenetrationVelocity = function setMaxDepenetrationVelocity(value) {
680
+ this._pxActor.setMaxDepenetrationVelocity(value);
681
+ };
682
+ /**
683
+ * {@inheritDoc IDynamicCollider.setSleepThreshold }
684
+ * @default 5e-5f * PxTolerancesScale::speed * PxTolerancesScale::speed
685
+ */ _proto.setSleepThreshold = function setSleepThreshold(value) {
686
+ this._pxActor.setSleepThreshold(value);
687
+ };
688
+ /**
689
+ * {@inheritDoc IDynamicCollider.setSolverIterations }
690
+ */ _proto.setSolverIterations = function setSolverIterations(value) {
691
+ this._pxActor.setSolverIterationCounts(value, 1);
692
+ };
693
+ /**
694
+ * {@inheritDoc IDynamicCollider.setCollisionDetectionMode }
695
+ *
696
+ * PhysX 在 kinematic actor 上调用 setRigidBodyFlag(eENABLE_CCD, true) 会触发警告:
697
+ * "kinematic bodies with CCD enabled are not supported! CCD will be ignored"
698
+ * 虽然 PhysX 会忽略这次调用而非真的拒绝(切回 dynamic 时 flag 不会自动恢复),
699
+ * 但每次 setIsKinematic 切换都会让这个 warning 重复打印,污染日志,
700
+ * 同时让 actor 在 dynamic 状态下 CCD flag 状态不确定。
701
+ *
702
+ * 解决: 只在 dynamic 状态时立即 apply CCD flags。kinematic 时仅缓存到
703
+ * `_collisionDetectionMode`,等切回 dynamic 时由 setIsKinematic 重新 apply。
704
+ */ _proto.setCollisionDetectionMode = function setCollisionDetectionMode(value) {
705
+ this._collisionDetectionMode = value;
706
+ if (!this._isKinematic) {
707
+ this._applyCollisionDetectionFlags(value);
708
+ }
709
+ };
710
+ /**
711
+ * {@inheritDoc IDynamicCollider.setUseGravity }
712
+ */ _proto.setUseGravity = function setUseGravity(value) {
713
+ this._pxActor.setActorFlag(this._physXPhysics._physX.PxActorFlag.eDISABLE_GRAVITY, !value);
714
+ };
715
+ /**
716
+ * {@inheritDoc IDynamicCollider.setIsKinematic }
717
+ *
718
+ * 切换 kinematic 状态时同步处理 CCD flag:
719
+ * - 切到 kinematic 前先关 CCD(避免 PhysX 警告 + 让状态显式)
720
+ * - 切回 dynamic 后恢复用户期望的 CCD mode(来自 `_collisionDetectionMode` 缓存)
721
+ */ _proto.setIsKinematic = function setIsKinematic(value) {
722
+ if (this._isKinematic === value) return;
723
+ var physX = this._physXPhysics._physX;
724
+ if (value) {
725
+ this._applyCollisionDetectionFlags(0);
726
+ this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eKINEMATIC, true);
727
+ } else {
728
+ this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eKINEMATIC, false);
729
+ this._applyCollisionDetectionFlags(this._collisionDetectionMode);
730
+ }
731
+ this._isKinematic = value;
732
+ };
733
+ _proto._applyCollisionDetectionFlags = function _applyCollisionDetectionFlags(value) {
734
+ var physX = this._physXPhysics._physX;
735
+ switch(value){
736
+ case 1:
737
+ this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eENABLE_CCD, true);
738
+ this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eENABLE_CCD_FRICTION, false);
739
+ this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eENABLE_SPECULATIVE_CCD, false);
740
+ break;
741
+ case 2:
742
+ this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eENABLE_CCD, true);
743
+ this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eENABLE_CCD_FRICTION, true);
744
+ this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eENABLE_SPECULATIVE_CCD, false);
745
+ break;
746
+ case 3:
747
+ this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eENABLE_CCD, false);
748
+ this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eENABLE_CCD_FRICTION, false);
749
+ this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eENABLE_SPECULATIVE_CCD, true);
750
+ break;
751
+ case 0:
752
+ this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eENABLE_CCD, false);
753
+ this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eENABLE_CCD_FRICTION, false);
754
+ this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eENABLE_SPECULATIVE_CCD, false);
755
+ break;
756
+ }
757
+ };
758
+ /**
759
+ * {@inheritDoc IDynamicCollider.setConstraints }
760
+ */ _proto.setConstraints = function setConstraints(flags) {
761
+ this._pxActor.setRigidDynamicLockFlags(flags);
762
+ };
763
+ /**
764
+ * {@inheritDoc IDynamicCollider.addForce }
765
+ *
766
+ * PhysX 在 kinematic actor 上调 addForce 是 no-op(doc: "kinematic bodies don't
767
+ * respond to forces")。提前 return 避免无意义的 wasm boundary cross。
768
+ *
769
+ * Sleeping actor 不需要显式 wakeUp — wasm binding 调用 `addForce(force, eFORCE,
770
+ * autowake=true)`,PhysX 自动唤醒(已通过 `applyForce on sleeping actor` 测试验证)。
771
+ */ _proto.addForce = function addForce(force) {
772
+ if (this._isKinematic) return;
773
+ this._pxActor.addForce(force);
774
+ };
775
+ /**
776
+ * {@inheritDoc IDynamicCollider.addForceAtPosition }
777
+ */ _proto.addForceAtPosition = function addForceAtPosition(force, position) {
778
+ this._pxActor.addForceAtPos(force, position);
779
+ };
780
+ /**
781
+ * {@inheritDoc IDynamicCollider.addTorque }
782
+ *
783
+ * 同 addForce — kinematic 提前 return,sleeping 由 PhysX autowake 自动处理。
784
+ */ _proto.addTorque = function addTorque(torque) {
785
+ if (this._isKinematic) return;
786
+ this._pxActor.addTorque(torque);
787
+ };
788
+ /**
789
+ * {@inheritDoc IDynamicCollider.move }
790
+ *
791
+ * PhysX 要求 setKinematicTarget 的 rotation 是 normalized quaternion,否则会触发
792
+ * 内部 assertion / 警告,并把 actor 转到错误的姿态。所以在写入 wasm 边界前统一 normalize。
793
+ */ _proto.move = function move(positionOrRotation, rotation) {
794
+ var tempTranslation = PhysXDynamicCollider._tempTranslation;
795
+ var tempRotation = PhysXDynamicCollider._tempRotation;
796
+ if (rotation) {
797
+ tempRotation.copyFrom(rotation).normalize();
798
+ this._pxActor.setKinematicTarget(positionOrRotation, tempRotation);
799
+ return;
800
+ }
801
+ if (_instanceof(positionOrRotation, engine.Vector3)) {
802
+ this.getWorldTransform(tempTranslation, tempRotation);
803
+ // current rotation read from PhysX is already normalized; no extra work needed
804
+ this._pxActor.setKinematicTarget(positionOrRotation, tempRotation);
805
+ } else {
806
+ this.getWorldTransform(tempTranslation, tempRotation);
807
+ tempRotation.copyFrom(positionOrRotation).normalize();
808
+ this._pxActor.setKinematicTarget(tempTranslation, tempRotation);
809
+ }
810
+ };
811
+ /**
812
+ * {@inheritDoc IDynamicCollider.sleep }
813
+ */ _proto.sleep = function sleep() {
814
+ return this._pxActor.putToSleep();
815
+ };
816
+ /**
817
+ * {@inheritDoc IDynamicCollider.isSleeping }
818
+ */ _proto.isSleeping = function isSleeping() {
819
+ return this._pxActor.isSleeping();
820
+ };
821
+ /**
822
+ * {@inheritDoc IDynamicCollider.wakeUp }
823
+ */ _proto.wakeUp = function wakeUp() {
824
+ return this._pxActor.wakeUp();
825
+ };
826
+ return PhysXDynamicCollider;
827
+ }(PhysXCollider);
828
+ PhysXDynamicCollider._tempTranslation = new engine.Vector3();
829
+ PhysXDynamicCollider._tempRotation = new engine.Quaternion();
830
+
831
+ var PhysXPhysicsManager = function PhysXPhysicsManager() {
832
+ /** @internal */ this._eventMap = {};
833
+ };
834
+
835
+ /**
836
+ * Physics material describes how to handle colliding objects (friction, bounciness).
837
+ */ var PhysXPhysicsMaterial = /*#__PURE__*/ function() {
838
+ function PhysXPhysicsMaterial(physXPhysics, staticFriction, dynamicFriction, bounciness, frictionCombine, bounceCombine) {
839
+ this._physXPhysics = physXPhysics;
840
+ var pxMaterial = physXPhysics._pxPhysics.createMaterial(staticFriction, dynamicFriction, bounciness);
841
+ pxMaterial.setFrictionCombineMode(frictionCombine);
842
+ pxMaterial.setRestitutionCombineMode(bounceCombine);
843
+ this._pxMaterial = pxMaterial;
844
+ }
845
+ var _proto = PhysXPhysicsMaterial.prototype;
846
+ /**
847
+ * {@inheritDoc IPhysicsMaterial.setBounciness }
848
+ */ _proto.setBounciness = function setBounciness(value) {
849
+ this._pxMaterial.setRestitution(value);
850
+ };
851
+ /**
852
+ * {@inheritDoc IPhysicsMaterial.setDynamicFriction }
853
+ */ _proto.setDynamicFriction = function setDynamicFriction(value) {
854
+ this._pxMaterial.setDynamicFriction(value);
855
+ };
856
+ /**
857
+ * {@inheritDoc IPhysicsMaterial.setStaticFriction }
858
+ */ _proto.setStaticFriction = function setStaticFriction(value) {
859
+ this._pxMaterial.setStaticFriction(value);
860
+ };
861
+ /**
862
+ * {@inheritDoc IPhysicsMaterial.setBounceCombine }
863
+ */ _proto.setBounceCombine = function setBounceCombine(value) {
864
+ this._pxMaterial.setRestitutionCombineMode(value);
865
+ };
866
+ /**
867
+ * {@inheritDoc IPhysicsMaterial.setFrictionCombine }
868
+ */ _proto.setFrictionCombine = function setFrictionCombine(value) {
869
+ this._pxMaterial.setFrictionCombineMode(value);
870
+ };
871
+ /**
872
+ * {@inheritDoc IPhysicsMaterial.destroy }
873
+ */ _proto.destroy = function destroy() {
874
+ this._pxMaterial.release();
875
+ };
876
+ return PhysXPhysicsMaterial;
877
+ }();
878
+
879
+ function _defineProperties(target, props) {
880
+ for (var i = 0; i < props.length; i++) {
881
+ var descriptor = props[i];
882
+ descriptor.enumerable = descriptor.enumerable || false;
883
+ descriptor.configurable = true;
884
+
885
+ if ("value" in descriptor) descriptor.writable = true;
886
+
887
+ Object.defineProperty(target, descriptor.key, descriptor);
888
+ }
889
+ }
890
+ function _create_class(Constructor, protoProps, staticProps) {
891
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
892
+ if (staticProps) _defineProperties(Constructor, staticProps);
893
+
894
+ return Constructor;
895
+ }
896
+
897
+ /**
898
+ * A manager is a collection of colliders and constraints which can interact.
899
+ */ var PhysXPhysicsScene = /*#__PURE__*/ function() {
900
+ function PhysXPhysicsScene(physXPhysics, physicsManager) {
901
+ var _this = this;
902
+ /** @internal */ this._pxControllerManager = null;
903
+ // Cached geometry objects for reuse
904
+ this._boxGeometry = null;
905
+ this._sphereGeometry = null;
906
+ this._capsuleGeometry = null;
907
+ this._currentOnQuery = null;
908
+ this._activeTriggers = new engine.DisorderedArray();
909
+ this._contactEvents = [];
910
+ this._contactEventCount = 0;
911
+ this._contactEventEnabled = true;
912
+ this._triggerEvents = [];
913
+ this._physicsEvents = {
914
+ contactEvents: [],
915
+ contactEventCount: 0,
916
+ triggerEvents: []
917
+ };
918
+ this._triggerEventPool = [];
919
+ this._physXPhysics = physXPhysics;
920
+ this._physXManager = physicsManager;
921
+ var physX = physXPhysics._physX;
922
+ this._pxRaycastHit = new physX.PxRaycastHit();
923
+ this._pxSweepHit = new physX.PxSweepHit();
924
+ this._pxFilterData = new physX.PxQueryFilterData();
925
+ this._pxFilterData.flags = new physX.PxQueryFlags(1 | 2 | 4);
926
+ this._pxRaycastSweepFilterData = new physX.PxQueryFilterData();
927
+ this._pxRaycastSweepFilterData.flags = new physX.PxQueryFlags(1 | 2 | 4 | 8);
928
+ var triggerCallback = {
929
+ onContactBegin: function(collision) {
930
+ _this._bufferContactEvent(collision, 0);
931
+ },
932
+ onContactEnd: function(collision) {
933
+ _this._bufferContactEvent(collision, 2);
934
+ },
935
+ onContactPersist: function(collision) {
936
+ _this._bufferContactEvent(collision, 1);
937
+ },
938
+ onTriggerBegin: function(index1, index2) {
939
+ var event = index1 < index2 ? _this._getTrigger(index1, index2) : _this._getTrigger(index2, index1);
940
+ event.state = 0;
941
+ _this._activeTriggers.add(event);
942
+ },
943
+ onTriggerEnd: function(index1, index2) {
944
+ var event;
945
+ if (index1 < index2) {
946
+ var subMap = _this._physXManager._eventMap[index1];
947
+ event = subMap[index2];
948
+ subMap[index2] = undefined;
949
+ } else {
950
+ var subMap1 = _this._physXManager._eventMap[index2];
951
+ event = subMap1[index1];
952
+ subMap1[index1] = undefined;
953
+ }
954
+ event.state = 2;
955
+ }
956
+ };
957
+ var pxPhysics = physXPhysics._pxPhysics;
958
+ this._physXSimulationCallbackInstance = physX.PxSimulationEventCallback.implement(triggerCallback);
959
+ var sceneDesc = physX.getDefaultSceneDesc(pxPhysics.getTolerancesScale(), 0, this._physXSimulationCallbackInstance);
960
+ this._pxScene = pxPhysics.createScene(sceneDesc);
961
+ sceneDesc.delete();
962
+ this._pxQueryCallback = physX.PxQueryFilterCallback.implement({
963
+ preFilter: function(_filterData, index, _actor) {
964
+ return _this._currentOnQuery(index) ? 2 : 0;
965
+ },
966
+ // distance <= 0 means initial overlap — drop the hit so subsequent hits can be considered.
967
+ // Only invoked when the query's filter data includes POST_FILTER (raycast/sweep, not overlap).
968
+ postFilter: function(_filterData, distance) {
969
+ return distance <= 0 ? 0 : 2;
970
+ }
971
+ });
972
+ }
973
+ var _proto = PhysXPhysicsScene.prototype;
974
+ /**
975
+ * {@inheritDoc IPhysicsScene.setGravity }
976
+ */ _proto.setGravity = function setGravity(value) {
977
+ this._pxScene.setGravity(value);
978
+ };
979
+ /**
980
+ * {@inheritDoc IPhysicsScene.addCollider }
981
+ */ _proto.addCollider = function addCollider(collider) {
982
+ collider._scene = this;
983
+ this._pxScene.addActor(collider._pxActor, null);
984
+ var shapes = collider._shapes;
985
+ for(var i = 0, n = shapes.length; i < n; i++){
986
+ this._addColliderShape(shapes[i]._id);
987
+ }
988
+ };
989
+ /**
990
+ * {@inheritDoc IPhysicsScene.removeCollider }
991
+ */ _proto.removeCollider = function removeCollider(collider) {
992
+ collider._scene = null;
993
+ this._pxScene.removeActor(collider._pxActor, true);
994
+ var shapes = collider._shapes;
995
+ for(var i = 0, n = shapes.length; i < n; i++){
996
+ this._removeColliderShape(shapes[i]._id);
997
+ }
998
+ };
999
+ /**
1000
+ * {@inheritDoc IPhysicsScene.addCharacterController }
1001
+ */ _proto.addCharacterController = function addCharacterController(characterController) {
1002
+ characterController._scene = this;
1003
+ // Physx have no API to remove/readd cct into scene.
1004
+ if (!characterController._pxController) {
1005
+ var shape = characterController._shape;
1006
+ if (shape) {
1007
+ var lastPXManager = characterController._pxManager;
1008
+ if (lastPXManager !== this) {
1009
+ lastPXManager && characterController._destroyPXController();
1010
+ characterController._createPXController(this, shape);
1011
+ }
1012
+ this._addColliderShape(shape._id);
1013
+ }
1014
+ }
1015
+ characterController._pxManager = this;
1016
+ };
1017
+ /**
1018
+ * {@inheritDoc IPhysicsScene.removeCharacterController }
1019
+ */ _proto.removeCharacterController = function removeCharacterController(characterController) {
1020
+ characterController._scene = null;
1021
+ characterController._pxManager = null;
1022
+ characterController._destroyPXController();
1023
+ var shape = characterController._shape;
1024
+ shape && this._removeColliderShape(shape._id);
1025
+ };
1026
+ /**
1027
+ * {@inheritDoc IPhysicsScene.update }
1028
+ */ _proto.update = function update(elapsedTime) {
1029
+ this._contactEventCount = 0;
1030
+ this._simulate(elapsedTime);
1031
+ this._fetchResults();
1032
+ };
1033
+ /**
1034
+ * {@inheritDoc IPhysicsScene.setContactEventEnabled }
1035
+ */ _proto.setContactEventEnabled = function setContactEventEnabled(enabled) {
1036
+ if (this._contactEventEnabled === enabled) {
1037
+ return;
1038
+ }
1039
+ this._contactEventEnabled = enabled;
1040
+ if (!enabled) {
1041
+ this._contactEventCount = 0;
1042
+ }
1043
+ };
1044
+ /**
1045
+ * {@inheritDoc IPhysicsScene.updateEvents }
1046
+ */ _proto.updateEvents = function updateEvents() {
1047
+ var physicsEvents = this._physicsEvents;
1048
+ // Collect trigger events: snapshot state for dispatch, then advance
1049
+ var _this = this, triggerEventPool = _this._triggerEventPool, activeTriggers = _this._activeTriggers, triggerEvents = _this._triggerEvents;
1050
+ triggerEvents.length = 0;
1051
+ activeTriggers.forEach(function(event, i) {
1052
+ event.dispatchState = event.state;
1053
+ triggerEvents.push(event);
1054
+ if (event.state === 0) {
1055
+ event.state = 1;
1056
+ } else if (event.state === 2) {
1057
+ activeTriggers.deleteByIndex(i);
1058
+ triggerEventPool.push(event);
1059
+ }
1060
+ });
1061
+ physicsEvents.contactEvents = this._contactEvents;
1062
+ physicsEvents.contactEventCount = this._contactEventCount;
1063
+ physicsEvents.triggerEvents = triggerEvents;
1064
+ return physicsEvents;
1065
+ };
1066
+ /**
1067
+ * {@inheritDoc IPhysicsScene.raycast }
1068
+ */ _proto.raycast = function raycast(ray, distance, onRaycast, hit) {
1069
+ var _this = this, pxHitResult = _this._pxRaycastHit;
1070
+ distance = Math.min(distance, 3.4e38); // float32 max value limit in physX raycast.
1071
+ var prevOnQuery = this._currentOnQuery;
1072
+ this._currentOnQuery = onRaycast;
1073
+ var result;
1074
+ try {
1075
+ result = this._pxScene.raycastSingle(ray.origin, ray.direction, distance, pxHitResult, this._pxRaycastSweepFilterData, this._pxQueryCallback);
1076
+ } finally{
1077
+ this._currentOnQuery = prevOnQuery;
1078
+ }
1079
+ if (result && hit != undefined) {
1080
+ var position = PhysXPhysicsScene._tempPosition, normal = PhysXPhysicsScene._tempNormal;
1081
+ var pxPosition = pxHitResult.position, pxNormal = pxHitResult.normal;
1082
+ position.set(pxPosition.x, pxPosition.y, pxPosition.z);
1083
+ normal.set(pxNormal.x, pxNormal.y, pxNormal.z);
1084
+ hit(pxHitResult.getShape().getUUID(), pxHitResult.distance, position, normal);
1085
+ }
1086
+ return result;
1087
+ };
1088
+ /**
1089
+ * {@inheritDoc IPhysicsScene.boxCast }
1090
+ */ _proto.boxCast = function boxCast(center, orientation, halfExtents, direction, distance, onSweep, outHitResult) {
1091
+ if (!this._boxGeometry) {
1092
+ this._boxGeometry = new this._physXPhysics._physX.PxBoxGeometry(halfExtents.x, halfExtents.y, halfExtents.z);
1093
+ } else {
1094
+ this._boxGeometry.halfExtents = halfExtents;
1095
+ }
1096
+ var pose = PhysXPhysicsScene._tempPose;
1097
+ pose.translation.copyFrom(center);
1098
+ pose.rotation.copyFrom(orientation);
1099
+ return this._sweepSingle(this._boxGeometry, pose, direction, distance, onSweep, outHitResult);
1100
+ };
1101
+ /**
1102
+ * {@inheritDoc IPhysicsScene.sphereCast }
1103
+ */ _proto.sphereCast = function sphereCast(center, radius, direction, distance, onSweep, outHitResult) {
1104
+ if (!this._sphereGeometry) {
1105
+ this._sphereGeometry = new this._physXPhysics._physX.PxSphereGeometry(radius);
1106
+ } else {
1107
+ this._sphereGeometry.radius = radius;
1108
+ }
1109
+ var tempQuat = PhysXPhysicsScene._tempQuaternion;
1110
+ tempQuat.set(0, 0, 0, 1); // Identity quaternion
1111
+ var pose = {
1112
+ translation: center,
1113
+ rotation: tempQuat
1114
+ };
1115
+ return this._sweepSingle(this._sphereGeometry, pose, direction, distance, onSweep, outHitResult);
1116
+ };
1117
+ /**
1118
+ * {@inheritDoc IPhysicsScene.capsuleCast }
1119
+ */ _proto.capsuleCast = function capsuleCast(center, radius, height, orientation, direction, distance, onSweep, outHitResult) {
1120
+ if (!this._capsuleGeometry) {
1121
+ this._capsuleGeometry = new this._physXPhysics._physX.PxCapsuleGeometry(radius, height * 0.5);
1122
+ } else {
1123
+ this._capsuleGeometry.radius = radius;
1124
+ this._capsuleGeometry.halfHeight = height * 0.5;
1125
+ }
1126
+ var pose = PhysXPhysicsScene._tempPose;
1127
+ pose.translation.copyFrom(center);
1128
+ pose.rotation.copyFrom(orientation);
1129
+ return this._sweepSingle(this._capsuleGeometry, pose, direction, distance, onSweep, outHitResult);
1130
+ };
1131
+ /**
1132
+ * {@inheritDoc IPhysicsScene.overlapBoxAll }
1133
+ */ _proto.overlapBoxAll = function overlapBoxAll(center, orientation, halfExtents, onOverlap) {
1134
+ if (!this._boxGeometry) {
1135
+ this._boxGeometry = new this._physXPhysics._physX.PxBoxGeometry(halfExtents.x, halfExtents.y, halfExtents.z);
1136
+ } else {
1137
+ this._boxGeometry.halfExtents = halfExtents;
1138
+ }
1139
+ var pose = PhysXPhysicsScene._tempPose;
1140
+ pose.translation.copyFrom(center);
1141
+ pose.rotation.copyFrom(orientation);
1142
+ return this._overlapMultiple(this._boxGeometry, pose, onOverlap);
1143
+ };
1144
+ /**
1145
+ * {@inheritDoc IPhysicsScene.overlapSphereAll }
1146
+ */ _proto.overlapSphereAll = function overlapSphereAll(center, radius, onOverlap) {
1147
+ if (!this._sphereGeometry) {
1148
+ this._sphereGeometry = new this._physXPhysics._physX.PxSphereGeometry(radius);
1149
+ } else {
1150
+ this._sphereGeometry.radius = radius;
1151
+ }
1152
+ var tempQuat = PhysXPhysicsScene._tempQuaternion;
1153
+ tempQuat.set(0, 0, 0, 1);
1154
+ var pose = {
1155
+ translation: center,
1156
+ rotation: tempQuat
1157
+ };
1158
+ return this._overlapMultiple(this._sphereGeometry, pose, onOverlap);
1159
+ };
1160
+ /**
1161
+ * {@inheritDoc IPhysicsScene.overlapCapsuleAll }
1162
+ */ _proto.overlapCapsuleAll = function overlapCapsuleAll(center, radius, height, orientation, onOverlap) {
1163
+ if (!this._capsuleGeometry) {
1164
+ this._capsuleGeometry = new this._physXPhysics._physX.PxCapsuleGeometry(radius, height * 0.5);
1165
+ } else {
1166
+ this._capsuleGeometry.radius = radius;
1167
+ this._capsuleGeometry.halfHeight = height * 0.5;
1168
+ }
1169
+ var pose = PhysXPhysicsScene._tempPose;
1170
+ pose.translation.copyFrom(center);
1171
+ pose.rotation.copyFrom(orientation);
1172
+ return this._overlapMultiple(this._capsuleGeometry, pose, onOverlap);
1173
+ };
1174
+ /**
1175
+ * {@inheritDoc IPhysicsScene.gc }
1176
+ */ _proto.gc = function gc() {
1177
+ this._contactEvents.length = this._contactEventCount;
1178
+ };
1179
+ /**
1180
+ * {@inheritDoc IPhysicsScene.destroy }
1181
+ */ _proto.destroy = function destroy() {
1182
+ var _this__boxGeometry, _this__sphereGeometry, _this__capsuleGeometry, // Need to release the controller manager before release the scene.
1183
+ _this__pxControllerManager;
1184
+ (_this__boxGeometry = this._boxGeometry) == null ? void 0 : _this__boxGeometry.delete();
1185
+ (_this__sphereGeometry = this._sphereGeometry) == null ? void 0 : _this__sphereGeometry.delete();
1186
+ (_this__capsuleGeometry = this._capsuleGeometry) == null ? void 0 : _this__capsuleGeometry.delete();
1187
+ this._physXSimulationCallbackInstance.delete();
1188
+ this._pxRaycastHit.delete();
1189
+ this._pxSweepHit.delete();
1190
+ this._pxFilterData.flags.delete();
1191
+ this._pxFilterData.delete();
1192
+ this._pxRaycastSweepFilterData.flags.delete();
1193
+ this._pxRaycastSweepFilterData.delete();
1194
+ this._pxQueryCallback.delete();
1195
+ (_this__pxControllerManager = this._pxControllerManager) == null ? void 0 : _this__pxControllerManager.release();
1196
+ this._pxScene.release();
1197
+ };
1198
+ /**
1199
+ * @internal
1200
+ */ _proto._getControllerManager = function _getControllerManager() {
1201
+ var pxControllerManager = this._pxControllerManager;
1202
+ if (pxControllerManager === null) {
1203
+ this._pxControllerManager = pxControllerManager = this._pxScene.createControllerManager();
1204
+ }
1205
+ return pxControllerManager;
1206
+ };
1207
+ /**
1208
+ * @internal
1209
+ */ _proto._addColliderShape = function _addColliderShape(id) {
1210
+ this._physXManager._eventMap[id] = Object.create(null);
1211
+ };
1212
+ /**
1213
+ * @internal
1214
+ */ _proto._removeColliderShape = function _removeColliderShape(id) {
1215
+ var _this = this, triggerEventPool = _this._triggerEventPool, activeTriggers = _this._activeTriggers;
1216
+ var _this__physXManager = this._physXManager, eventMap = _this__physXManager._eventMap;
1217
+ activeTriggers.forEach(function(event, i) {
1218
+ if (event.index1 == id) {
1219
+ activeTriggers.deleteByIndex(i);
1220
+ triggerEventPool.push(event);
1221
+ } else if (event.index2 == id) {
1222
+ activeTriggers.deleteByIndex(i);
1223
+ triggerEventPool.push(event);
1224
+ // If the shape is big index, should clear from the small index shape subMap
1225
+ eventMap[event.index1][id] = undefined;
1226
+ }
1227
+ });
1228
+ delete eventMap[id];
1229
+ };
1230
+ _proto._sweepSingle = function _sweepSingle(geometry, pose, direction, distance, onSweep, outHitResult) {
1231
+ var _this = this, pxSweepHit = _this._pxSweepHit;
1232
+ distance = Math.min(distance, 3.4e38); // float32 max value limit in physx sweep
1233
+ var prevOnQuery = this._currentOnQuery;
1234
+ this._currentOnQuery = onSweep;
1235
+ var result;
1236
+ try {
1237
+ result = this._pxScene.sweepSingle(geometry, pose, direction, distance, pxSweepHit, this._pxRaycastSweepFilterData, this._pxQueryCallback);
1238
+ } finally{
1239
+ this._currentOnQuery = prevOnQuery;
1240
+ }
1241
+ if (result && outHitResult != undefined) {
1242
+ var position = PhysXPhysicsScene._tempPosition, normal = PhysXPhysicsScene._tempNormal;
1243
+ var pxPosition = pxSweepHit.position, pxNormal = pxSweepHit.normal;
1244
+ position.set(pxPosition.x, pxPosition.y, pxPosition.z);
1245
+ normal.set(pxNormal.x, pxNormal.y, pxNormal.z);
1246
+ outHitResult(pxSweepHit.getShape().getUUID(), pxSweepHit.distance, position, normal);
1247
+ }
1248
+ return result;
1249
+ };
1250
+ _proto._overlapMultiple = function _overlapMultiple(geometry, pose, onOverlap) {
1251
+ var prevOnQuery = this._currentOnQuery;
1252
+ this._currentOnQuery = onOverlap;
1253
+ var maxHits = 256;
1254
+ var hits;
1255
+ try {
1256
+ hits = this._pxScene.overlapMultiple(geometry, pose, maxHits, this._pxFilterData, this._pxQueryCallback);
1257
+ } finally{
1258
+ this._currentOnQuery = prevOnQuery;
1259
+ }
1260
+ var result = PhysXPhysicsScene._tempShapeIDs;
1261
+ result.length = 0;
1262
+ if (hits) {
1263
+ // PhysX overlapMultiple returns a collection with size() method
1264
+ for(var i = 0, n = hits.size(); i < n; i++){
1265
+ result.push(hits.get(i).getShape().getUUID());
1266
+ }
1267
+ }
1268
+ hits == null ? void 0 : hits.delete();
1269
+ return result;
1270
+ };
1271
+ _proto._simulate = function _simulate(elapsedTime) {
1272
+ this._pxScene.simulate(elapsedTime, true);
1273
+ };
1274
+ _proto._fetchResults = function _fetchResults(block) {
1275
+ if (block === void 0) block = true;
1276
+ this._pxScene.fetchResults(block);
1277
+ };
1278
+ _proto._getTrigger = function _getTrigger(index1, index2) {
1279
+ var event;
1280
+ if (this._triggerEventPool.length) {
1281
+ event = this._triggerEventPool.pop();
1282
+ event.index1 = index1;
1283
+ event.index2 = index2;
1284
+ } else {
1285
+ event = new TriggerEvent(index1, index2);
1286
+ }
1287
+ this._physXManager._eventMap[index1][index2] = event;
1288
+ return event;
1289
+ };
1290
+ _proto._bufferContactEvent = function _bufferContactEvent(collision, state) {
1291
+ var _this__contactEvents, _index;
1292
+ if (!this._contactEventEnabled) return;
1293
+ var index = this._contactEventCount++;
1294
+ var event = (_this__contactEvents = this._contactEvents)[_index = index] || (_this__contactEvents[_index] = new ContactEvent());
1295
+ event.shape0Id = collision.shape0Id;
1296
+ event.shape1Id = collision.shape1Id;
1297
+ event.state = state;
1298
+ // Copy contact points from PhysX (the native data is only valid during fetchResults)
1299
+ var nativeContacts = collision.getContacts();
1300
+ var count = nativeContacts.size();
1301
+ var bufferedContacts = event._bufferedContacts;
1302
+ bufferedContacts.contactCount = count;
1303
+ for(var i = 0; i < count; i++){
1304
+ var _bufferedContacts_contacts, _i;
1305
+ var src = nativeContacts.get(i);
1306
+ var dst = (_bufferedContacts_contacts = bufferedContacts.contacts)[_i = i] || (_bufferedContacts_contacts[_i] = new BufferedContactPoint());
1307
+ dst.position.copyFrom(src.position);
1308
+ dst.normal.copyFrom(src.normal);
1309
+ dst.impulse.copyFrom(src.impulse);
1310
+ dst.separation = src.separation;
1311
+ }
1312
+ };
1313
+ return PhysXPhysicsScene;
1314
+ }();
1315
+ PhysXPhysicsScene._tempPosition = new engine.Vector3();
1316
+ PhysXPhysicsScene._tempQuaternion = new engine.Quaternion();
1317
+ PhysXPhysicsScene._tempNormal = new engine.Vector3();
1318
+ PhysXPhysicsScene._tempPose = {
1319
+ translation: new engine.Vector3(),
1320
+ rotation: new engine.Quaternion()
1321
+ };
1322
+ PhysXPhysicsScene._tempShapeIDs = [];
1323
+ /**
1324
+ * Trigger event to store interactive object ids and state.
1325
+ */ var TriggerEvent = function TriggerEvent(index1, index2) {
1326
+ this.index1 = index1;
1327
+ this.index2 = index2;
1328
+ };
1329
+ /**
1330
+ * Buffered contact point data, copied from PhysX during fetchResults.
1331
+ */ var BufferedContactPoint = function BufferedContactPoint() {
1332
+ this.position = new engine.Vector3();
1333
+ this.normal = new engine.Vector3();
1334
+ this.impulse = new engine.Vector3();
1335
+ this.separation = 0;
1336
+ };
1337
+ /**
1338
+ * Contact event buffered from PhysX fetchResults callback.
1339
+ * Implements ICollision so it can be passed directly to the core layer.
1340
+ */ var BufferedContacts = /*#__PURE__*/ function() {
1341
+ function BufferedContacts() {
1342
+ this.contactCount = 0;
1343
+ this.contacts = [];
1344
+ }
1345
+ var _proto = BufferedContacts.prototype;
1346
+ _proto.size = function size() {
1347
+ return this.contactCount;
1348
+ };
1349
+ _proto.get = function get(index) {
1350
+ return this.contacts[index];
1351
+ };
1352
+ return BufferedContacts;
1353
+ }();
1354
+ var ContactEvent = /*#__PURE__*/ function() {
1355
+ function ContactEvent() {
1356
+ /** @internal */ this._bufferedContacts = new BufferedContacts();
1357
+ }
1358
+ var _proto = ContactEvent.prototype;
1359
+ _proto.getContacts = function getContacts() {
1360
+ return this._bufferedContacts;
1361
+ };
1362
+ _create_class(ContactEvent, [
1363
+ {
1364
+ key: "contactCount",
1365
+ get: function get() {
1366
+ return this._bufferedContacts.contactCount;
1367
+ }
1368
+ }
1369
+ ]);
1370
+ return ContactEvent;
1371
+ }();
1372
+
1373
+ /**
1374
+ * A static collider component that will not move.
1375
+ * @remarks Mostly used for object which always stays at the same place and never moves around.
1376
+ */ var PhysXStaticCollider = /*#__PURE__*/ function(PhysXCollider) {
1377
+ _inherits(PhysXStaticCollider, PhysXCollider);
1378
+ function PhysXStaticCollider(physXPhysics, position, rotation) {
1379
+ var _this;
1380
+ _this = PhysXCollider.call(this, physXPhysics) || this;
1381
+ _this._pxActor = physXPhysics._pxPhysics.createRigidStatic(_this._transform(position, rotation));
1382
+ return _this;
1383
+ }
1384
+ return PhysXStaticCollider;
1385
+ }(PhysXCollider);
1386
+
1387
+ /**
1388
+ * PhysX runtime mode.
1389
+ */ var PhysXRuntimeMode = /*#__PURE__*/ function(PhysXRuntimeMode) {
1390
+ /** Use WebAssembly SIMD mode first, then WebAssembly as fallback. */ PhysXRuntimeMode[PhysXRuntimeMode["Auto"] = 0] = "Auto";
1391
+ /** WebAssembly mode. */ PhysXRuntimeMode[PhysXRuntimeMode["WebAssembly"] = 1] = "WebAssembly";
1392
+ /** WebAssembly SIMD mode. */ PhysXRuntimeMode[PhysXRuntimeMode["WebAssemblySIMD"] = 2] = "WebAssemblySIMD";
1393
+ return PhysXRuntimeMode;
1394
+ }({});
1395
+
1396
+ /**
1397
+ * a base interface providing common functionality for PhysX joints
1398
+ */ var PhysXJoint = /*#__PURE__*/ function() {
1399
+ function PhysXJoint(physXPhysics) {
1400
+ this._rotation = new engine.Quaternion();
1401
+ this._breakForce = Number.MAX_VALUE;
1402
+ this._breakTorque = Number.MAX_VALUE;
1403
+ this._physXPhysics = physXPhysics;
1404
+ }
1405
+ var _proto = PhysXJoint.prototype;
1406
+ /**
1407
+ * {@inheritDoc IJoint.setConnectedCollider }
1408
+ */ _proto.setConnectedCollider = function setConnectedCollider(value) {
1409
+ var _this__collider;
1410
+ this._pxJoint.setActors(((_this__collider = this._collider) == null ? void 0 : _this__collider._pxActor) || null, (value == null ? void 0 : value._pxActor) || null);
1411
+ };
1412
+ /**
1413
+ * {@inheritDoc IJoint.setConnectedAnchor }
1414
+ */ _proto.setAnchor = function setAnchor(value) {
1415
+ this._setLocalPose(0, value, PhysXJoint._defaultQuat);
1416
+ this._anchor = value;
1417
+ };
1418
+ /**
1419
+ * {@inheritDoc IJoint.setConnectedAnchor }
1420
+ */ _proto.setConnectedAnchor = function setConnectedAnchor(value) {
1421
+ this._setLocalPose(1, value, this._rotation);
1422
+ this._connectedAnchor = value;
1423
+ };
1424
+ _proto.setRotation = function setRotation(value) {
1425
+ this._setLocalPose(1, this._connectedAnchor, value);
1426
+ this._rotation.copyFrom(value);
1427
+ };
1428
+ /**
1429
+ * {@inheritDoc IJoint.setMassScale }
1430
+ */ _proto.setMassScale = function setMassScale(value) {
1431
+ this._pxJoint.setInvMassScale0(1 / value);
1432
+ };
1433
+ /**
1434
+ * {@inheritDoc IJoint.setConnectedMassScale }
1435
+ */ _proto.setConnectedMassScale = function setConnectedMassScale(value) {
1436
+ this._pxJoint.setInvMassScale1(1 / value);
1437
+ };
1438
+ /**
1439
+ * {@inheritDoc IJoint.setInertiaScale }
1440
+ */ _proto.setInertiaScale = function setInertiaScale(value) {
1441
+ this._pxJoint.setInvInertiaScale0(value);
1442
+ };
1443
+ /**
1444
+ * {@inheritDoc IJoint.setConnectedInertiaScale }
1445
+ */ _proto.setConnectedInertiaScale = function setConnectedInertiaScale(value) {
1446
+ this._pxJoint.setInvInertiaScale1(value);
1447
+ };
1448
+ /**
1449
+ * {@inheritDoc IJoint.setBreakForce }
1450
+ */ _proto.setBreakForce = function setBreakForce(value) {
1451
+ this._breakForce = value;
1452
+ this._pxJoint.setBreakForce(this._breakForce, this._breakTorque);
1453
+ };
1454
+ /**
1455
+ * {@inheritDoc IJoint.setBreakTorque }
1456
+ */ _proto.setBreakTorque = function setBreakTorque(value) {
1457
+ this._breakTorque = value;
1458
+ this._pxJoint.setBreakForce(this._breakForce, this._breakTorque);
1459
+ };
1460
+ /**
1461
+ * {@inheritDoc IJoint.destroy }
1462
+ */ _proto.destroy = function destroy() {
1463
+ if (!this._pxJoint) return;
1464
+ this._pxJoint.release();
1465
+ this._collider = null;
1466
+ };
1467
+ /**
1468
+ * Set the joint local pose for an actor.
1469
+ * @param actor 0 for the first actor, 1 for the second actor.
1470
+ * @param position the local position for the actor this joint
1471
+ * @param rotation the local rotation for the actor this joint
1472
+ */ _proto._setLocalPose = function _setLocalPose(actor, position, rotation) {
1473
+ this._pxJoint.setLocalPose(actor, position, rotation);
1474
+ };
1475
+ return PhysXJoint;
1476
+ }();
1477
+ PhysXJoint._defaultVec = new engine.Vector3();
1478
+ PhysXJoint._defaultQuat = new engine.Quaternion();
1479
+
1480
+ /**
1481
+ * A fixed joint permits no relative movement between two colliders. ie the bodies are glued together.
1482
+ */ var PhysXFixedJoint = /*#__PURE__*/ function(PhysXJoint1) {
1483
+ _inherits(PhysXFixedJoint, PhysXJoint1);
1484
+ function PhysXFixedJoint(physXPhysics, collider) {
1485
+ var _this;
1486
+ _this = PhysXJoint1.call(this, physXPhysics) || this;
1487
+ _this._collider = collider;
1488
+ _this._pxJoint = physXPhysics._pxPhysics.createFixedJoint(collider._pxActor, PhysXJoint._defaultVec, PhysXJoint._defaultQuat, null, PhysXJoint._defaultVec, PhysXJoint._defaultQuat);
1489
+ return _this;
1490
+ }
1491
+ return PhysXFixedJoint;
1492
+ }(PhysXJoint);
1493
+
1494
+ /**
1495
+ * A joint which behaves in a similar way to a hinge or axle.
1496
+ */ var PhysXHingeJoint = /*#__PURE__*/ function(PhysXJoint1) {
1497
+ _inherits(PhysXHingeJoint, PhysXJoint1);
1498
+ function PhysXHingeJoint(physXPhysics, collider) {
1499
+ var _this;
1500
+ _this = PhysXJoint1.call(this, physXPhysics) || this, _this._axisRotationQuaternion = new engine.Quaternion(), _this._connectedAxisRotationQuaternion = new engine.Quaternion();
1501
+ _this._collider = collider;
1502
+ _this._pxJoint = physXPhysics._pxPhysics.createRevoluteJoint(collider._pxActor, PhysXJoint._defaultVec, PhysXJoint._defaultQuat, null, PhysXJoint._defaultVec, PhysXJoint._defaultQuat);
1503
+ return _this;
1504
+ }
1505
+ var _proto = PhysXHingeJoint.prototype;
1506
+ _proto.setRotation = function setRotation(value) {
1507
+ var axis = this._axis;
1508
+ this._rotation.copyFrom(value);
1509
+ axis && this.setAxis(axis);
1510
+ };
1511
+ /**
1512
+ * {@inheritDoc IHingeJoint.setAxis }
1513
+ */ _proto.setAxis = function setAxis(value) {
1514
+ this._axis = value;
1515
+ var xAxis = PhysXHingeJoint._xAxis;
1516
+ var axisRotationQuaternion = this._axisRotationQuaternion;
1517
+ xAxis.set(1, 0, 0);
1518
+ var angle = Math.acos(engine.Vector3.dot(xAxis, value));
1519
+ engine.Vector3.cross(xAxis, value, xAxis);
1520
+ engine.Quaternion.rotationAxisAngle(xAxis, angle, axisRotationQuaternion);
1521
+ this._setLocalPose(0, this._anchor, axisRotationQuaternion);
1522
+ var connectedAxisRotationQuaternion = this._connectedAxisRotationQuaternion;
1523
+ engine.Quaternion.multiply(this._rotation, axisRotationQuaternion, connectedAxisRotationQuaternion);
1524
+ this._setLocalPose(1, this._connectedAnchor, connectedAxisRotationQuaternion);
1525
+ };
1526
+ _proto.setAnchor = function setAnchor(value) {
1527
+ this._setLocalPose(0, value, this._axisRotationQuaternion);
1528
+ this._anchor = value;
1529
+ };
1530
+ /**
1531
+ * {@inheritDoc IJoint.setConnectedAnchor }
1532
+ */ _proto.setConnectedAnchor = function setConnectedAnchor(value) {
1533
+ this._setLocalPose(1, value, this._connectedAxisRotationQuaternion);
1534
+ this._connectedAnchor = value;
1535
+ };
1536
+ /**
1537
+ * {@inheritDoc IHingeJoint.getAngle }
1538
+ */ _proto.getAngle = function getAngle() {
1539
+ return engine.MathUtil.radianToDegree(this._pxJoint.getAngle());
1540
+ };
1541
+ /**
1542
+ * {@inheritDoc IHingeJoint.getVelocity }
1543
+ */ _proto.getVelocity = function getVelocity() {
1544
+ return this._pxJoint.getVelocity();
1545
+ };
1546
+ /**
1547
+ * {@inheritDoc IHingeJoint.setHardLimitCone }
1548
+ */ _proto.setHardLimit = function setHardLimit(lowerLimit, upperLimit, contactDist) {
1549
+ this._pxJoint.setHardLimit(engine.MathUtil.degreeToRadian(lowerLimit), engine.MathUtil.degreeToRadian(upperLimit), contactDist);
1550
+ };
1551
+ /**
1552
+ * {@inheritDoc IHingeJoint.setHardLimitCone }
1553
+ */ _proto.setSoftLimit = function setSoftLimit(lowerLimit, upperLimit, stiffness, damping) {
1554
+ this._pxJoint.setSoftLimit(engine.MathUtil.degreeToRadian(lowerLimit), engine.MathUtil.degreeToRadian(upperLimit), stiffness, damping);
1555
+ };
1556
+ /**
1557
+ * {@inheritDoc IHingeJoint.setDriveVelocity }
1558
+ */ _proto.setDriveVelocity = function setDriveVelocity(velocity, autowake) {
1559
+ if (autowake === void 0) autowake = true;
1560
+ this._pxJoint.setDriveVelocity(velocity, autowake);
1561
+ };
1562
+ /**
1563
+ * {@inheritDoc IHingeJoint.setDriveForceLimit }
1564
+ */ _proto.setDriveForceLimit = function setDriveForceLimit(limit) {
1565
+ this._pxJoint.setDriveForceLimit(limit);
1566
+ };
1567
+ /**
1568
+ * {@inheritDoc IHingeJoint.setDriveGearRatio }
1569
+ */ _proto.setDriveGearRatio = function setDriveGearRatio(ratio) {
1570
+ this._pxJoint.setDriveGearRatio(ratio);
1571
+ };
1572
+ /**
1573
+ * {@inheritDoc IHingeJoint.setHingeJointFlag }
1574
+ */ _proto.setHingeJointFlag = function setHingeJointFlag(flag, value) {
1575
+ this._pxJoint.setRevoluteJointFlag(flag, value);
1576
+ };
1577
+ return PhysXHingeJoint;
1578
+ }(PhysXJoint);
1579
+ PhysXHingeJoint._xAxis = new engine.Vector3(1, 0, 0);
1580
+
1581
+ /**
1582
+ * a joint that maintains an upper or lower bound (or both) on the distance between two points on different objects
1583
+ */ var PhysXSpringJoint = /*#__PURE__*/ function(PhysXJoint1) {
1584
+ _inherits(PhysXSpringJoint, PhysXJoint1);
1585
+ function PhysXSpringJoint(physXPhysics, collider) {
1586
+ var _this;
1587
+ _this = PhysXJoint1.call(this, physXPhysics) || this;
1588
+ _this._collider = collider;
1589
+ _this._pxJoint = physXPhysics._pxPhysics.createDistanceJoint(null, PhysXJoint._defaultVec, PhysXJoint._defaultQuat, collider._pxActor, PhysXJoint._defaultVec, PhysXJoint._defaultQuat);
1590
+ _this._pxJoint.setDistanceJointFlag(2, true); // enable max distance;
1591
+ _this._pxJoint.setDistanceJointFlag(4, true); // enable min distance;
1592
+ _this._pxJoint.setDistanceJointFlag(8, true); // enable spring;
1593
+ return _this;
1594
+ }
1595
+ var _proto = PhysXSpringJoint.prototype;
1596
+ /**
1597
+ * {@inheritDoc ISpringJoint.setMinDistance }
1598
+ */ _proto.setMinDistance = function setMinDistance(distance) {
1599
+ this._pxJoint.setMinDistance(distance);
1600
+ };
1601
+ /**
1602
+ * {@inheritDoc ISpringJoint.setMaxDistance }
1603
+ */ _proto.setMaxDistance = function setMaxDistance(distance) {
1604
+ this._pxJoint.setMaxDistance(distance);
1605
+ };
1606
+ /**
1607
+ * {@inheritDoc ISpringJoint.setTolerance }
1608
+ */ _proto.setTolerance = function setTolerance(tolerance) {
1609
+ this._pxJoint.setTolerance(tolerance);
1610
+ };
1611
+ /**
1612
+ * {@inheritDoc ISpringJoint.setStiffness }
1613
+ */ _proto.setStiffness = function setStiffness(stiffness) {
1614
+ this._pxJoint.setStiffness(stiffness);
1615
+ };
1616
+ /**
1617
+ * {@inheritDoc ISpringJoint.setDamping }
1618
+ */ _proto.setDamping = function setDamping(damping) {
1619
+ this._pxJoint.setDamping(damping);
1620
+ };
1621
+ return PhysXSpringJoint;
1622
+ }(PhysXJoint);
1623
+
1624
+ function _assert_this_initialized(self) {
1625
+ if (self === void 0) throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
1626
+
1627
+ return self;
1628
+ }
1629
+
1630
+ function _possible_constructor_return(self, call) {
1631
+ if (call && (_type_of(call) === "object" || typeof call === "function")) return call;
1632
+
1633
+ return _assert_this_initialized(self);
1634
+ }
1635
+
1636
+ /**
1637
+ * Mesh collider shape in PhysX.
1638
+ */ var PhysXMeshColliderShape = /*#__PURE__*/ function(PhysXColliderShape) {
1639
+ _inherits(PhysXMeshColliderShape, PhysXColliderShape);
1640
+ function PhysXMeshColliderShape(physXPhysics, uniqueID, positions, indices, isConvex, material, cookingFlags) {
1641
+ var _this;
1642
+ _this = PhysXColliderShape.call(this, physXPhysics) || this, _this._pxMesh = null;
1643
+ _this._isConvex = isConvex;
1644
+ var cooked = _this._cookMesh(positions, indices, isConvex, cookingFlags);
1645
+ if (!cooked) {
1646
+ return _possible_constructor_return(_this);
1647
+ }
1648
+ var physX = physXPhysics._physX, physics = physXPhysics._pxPhysics;
1649
+ var _this__worldScale = _this._worldScale, scaleX = _this__worldScale.x, scaleY = _this__worldScale.y, scaleZ = _this__worldScale.z;
1650
+ var shapeFlags = ShapeFlag.SCENE_QUERY_SHAPE | ShapeFlag.SIMULATION_SHAPE;
1651
+ var meshFlag = isConvex ? PhysXMeshColliderShape._tightBoundsFlag : 0;
1652
+ var createShapeFn = isConvex ? physX.createConvexMeshShape : physX.createTriMeshShape;
1653
+ _this._pxShape = createShapeFn(cooked.mesh, scaleX, scaleY, scaleZ, meshFlag, shapeFlags, material._pxMaterial, physics);
1654
+ _this._id = uniqueID;
1655
+ _this._pxMaterial = material._pxMaterial;
1656
+ _this._pxMesh = cooked.mesh;
1657
+ _this._pxGeometry = cooked.geometry;
1658
+ _this._pxShape.setUUID(uniqueID);
1659
+ _this._setLocalPose();
1660
+ return _this;
1661
+ }
1662
+ var _proto = PhysXMeshColliderShape.prototype;
1663
+ /**
1664
+ * {@inheritDoc IMeshColliderShape.setMeshData }
1665
+ */ _proto.setMeshData = function setMeshData(positions, indices, isConvex, cookingFlags) {
1666
+ var cooked = this._cookMesh(positions, indices, isConvex, cookingFlags);
1667
+ if (!cooked) {
1668
+ return false;
1669
+ }
1670
+ var oldMesh = this._pxMesh;
1671
+ var oldGeometry = this._pxGeometry;
1672
+ try {
1673
+ this._pxShape.setGeometry(cooked.geometry);
1674
+ } catch (error) {
1675
+ var _cooked_mesh, _cooked_geometry;
1676
+ (_cooked_mesh = cooked.mesh) == null ? void 0 : _cooked_mesh.release();
1677
+ (_cooked_geometry = cooked.geometry) == null ? void 0 : _cooked_geometry.delete();
1678
+ throw error;
1679
+ }
1680
+ this._pxMesh = cooked.mesh;
1681
+ this._pxGeometry = cooked.geometry;
1682
+ this._isConvex = isConvex;
1683
+ oldMesh == null ? void 0 : oldMesh.release();
1684
+ oldGeometry == null ? void 0 : oldGeometry.delete();
1685
+ return true;
1686
+ };
1687
+ /**
1688
+ * {@inheritDoc IColliderShape.setWorldScale }
1689
+ */ _proto.setWorldScale = function setWorldScale(scale) {
1690
+ PhysXColliderShape.prototype.setWorldScale.call(this, scale);
1691
+ this._updateGeometry();
1692
+ };
1693
+ /**
1694
+ * {@inheritDoc IColliderShape.destroy }
1695
+ */ _proto.destroy = function destroy() {
1696
+ var _this__pxMesh;
1697
+ (_this__pxMesh = this._pxMesh) == null ? void 0 : _this__pxMesh.release();
1698
+ PhysXColliderShape.prototype.destroy.call(this);
1699
+ };
1700
+ _proto._cookMesh = function _cookMesh(positions, indices, isConvex, cookingFlags) {
1701
+ var _this__physXPhysics = this._physXPhysics, physX = _this__physXPhysics._physX, physics = _this__physXPhysics._pxPhysics, cooking = _this__physXPhysics._pxCooking, cookingParams = _this__physXPhysics._pxCookingParams;
1702
+ // Apply per-shape cooking flags
1703
+ var preprocessFlags = 0;
1704
+ if (cookingFlags & engine.MeshColliderShapeCookingFlag.VertexWelding) {
1705
+ preprocessFlags |= 1; // eWELD_VERTICES
1706
+ }
1707
+ if (!(cookingFlags & engine.MeshColliderShapeCookingFlag.Cleaning)) {
1708
+ preprocessFlags |= 2; // eDISABLE_CLEAN_MESH
1709
+ }
1710
+ physX.setCookingMeshPreprocessParams(cookingParams, preprocessFlags);
1711
+ cooking.setParams(cookingParams);
1712
+ var verticesPtr = this._allocatePositions(positions);
1713
+ var pxMesh = null;
1714
+ if (isConvex) {
1715
+ pxMesh = cooking.createConvexMesh(verticesPtr, positions.length, physics);
1716
+ physX._free(verticesPtr);
1717
+ if (!pxMesh) {
1718
+ this._logConvexCookingError(physX);
1719
+ return null;
1720
+ }
1721
+ } else {
1722
+ if (!indices) {
1723
+ physX._free(verticesPtr);
1724
+ console.error("PhysXMeshColliderShape: Triangle mesh requires indices.");
1725
+ return null;
1726
+ }
1727
+ var isU32 = _instanceof(indices, Uint32Array);
1728
+ var indicesPtr = this._allocateIndices(indices, isU32);
1729
+ pxMesh = cooking.createTriMesh(verticesPtr, positions.length, indicesPtr, indices.length / 3, !isU32, physics);
1730
+ physX._free(verticesPtr);
1731
+ physX._free(indicesPtr);
1732
+ if (!pxMesh) {
1733
+ this._logTriMeshCookingError(physX);
1734
+ return null;
1735
+ }
1736
+ }
1737
+ var _this__worldScale = this._worldScale, scaleX = _this__worldScale.x, scaleY = _this__worldScale.y, scaleZ = _this__worldScale.z;
1738
+ var meshFlag = isConvex ? PhysXMeshColliderShape._tightBoundsFlag : 0;
1739
+ var geometry = isConvex ? physX.createConvexMeshGeometry(pxMesh, scaleX, scaleY, scaleZ, meshFlag) : physX.createTriMeshGeometry(pxMesh, scaleX, scaleY, scaleZ, meshFlag);
1740
+ return {
1741
+ mesh: pxMesh,
1742
+ geometry: geometry
1743
+ };
1744
+ };
1745
+ _proto._logConvexCookingError = function _logConvexCookingError(physX) {
1746
+ switch(physX.getLastConvexCookingResult()){
1747
+ case 1:
1748
+ console.error("PhysXMeshColliderShape: Failed to create convex mesh. Could not find 4 vertices that do not form a zero-area triangle.");
1749
+ break;
1750
+ case 2:
1751
+ console.error("PhysXMeshColliderShape: Failed to create convex mesh within the maximum polygons limit (256). Consider simplifying the mesh.");
1752
+ break;
1753
+ default:
1754
+ console.error("PhysXMeshColliderShape: Failed to create convex mesh. The input geometry may be invalid.");
1755
+ break;
1756
+ }
1757
+ };
1758
+ _proto._logTriMeshCookingError = function _logTriMeshCookingError(physX) {
1759
+ switch(physX.getLastTriMeshCookingResult()){
1760
+ case 1:
1761
+ console.error("PhysXMeshColliderShape: Failed to create triangle mesh. One of the triangles is too large. Consider tessellating large triangles.");
1762
+ break;
1763
+ default:
1764
+ console.error("PhysXMeshColliderShape: Failed to create triangle mesh. The input geometry may be invalid.");
1765
+ break;
1766
+ }
1767
+ };
1768
+ _proto._allocatePositions = function _allocatePositions(positions) {
1769
+ var physX = this._physXPhysics._physX;
1770
+ var length = positions.length;
1771
+ var ptr = physX._malloc(length * 3 * 4);
1772
+ var view = new Float32Array(physX.HEAPF32.buffer, ptr, length * 3);
1773
+ for(var i = 0, offset = 0; i < length; i++, offset += 3){
1774
+ positions[i].copyToArray(view, offset);
1775
+ }
1776
+ return ptr;
1777
+ };
1778
+ _proto._allocateIndices = function _allocateIndices(indices, isU32) {
1779
+ var physX = this._physXPhysics._physX;
1780
+ // Uint8Array and Uint16Array both write as Uint16 (PhysX minimum index size)
1781
+ var TypedArrayCtor = isU32 ? Uint32Array : Uint16Array;
1782
+ var heap = isU32 ? physX.HEAPU32 : physX.HEAPU16;
1783
+ var ptr = physX._malloc(indices.length * TypedArrayCtor.BYTES_PER_ELEMENT);
1784
+ new TypedArrayCtor(heap.buffer, ptr, indices.length).set(indices);
1785
+ return ptr;
1786
+ };
1787
+ _proto._updateGeometry = function _updateGeometry() {
1788
+ if (!this._pxMesh) return;
1789
+ var physX = this._physXPhysics._physX;
1790
+ var _this__worldScale = this._worldScale, scaleX = _this__worldScale.x, scaleY = _this__worldScale.y, scaleZ = _this__worldScale.z;
1791
+ var meshFlag = this._isConvex ? PhysXMeshColliderShape._tightBoundsFlag : 0;
1792
+ var newGeometry = this._isConvex ? physX.createConvexMeshGeometry(this._pxMesh, scaleX, scaleY, scaleZ, meshFlag) : physX.createTriMeshGeometry(this._pxMesh, scaleX, scaleY, scaleZ, meshFlag);
1793
+ var oldGeometry = this._pxGeometry;
1794
+ try {
1795
+ this._pxShape.setGeometry(newGeometry);
1796
+ } catch (error) {
1797
+ newGeometry == null ? void 0 : newGeometry.delete();
1798
+ throw error;
1799
+ }
1800
+ this._pxGeometry = newGeometry;
1801
+ oldGeometry == null ? void 0 : oldGeometry.delete();
1802
+ };
1803
+ return PhysXMeshColliderShape;
1804
+ }(PhysXColliderShape);
1805
+ PhysXMeshColliderShape._tightBoundsFlag = 1 // eTIGHT_BOUNDS = 1 (1<<0)
1806
+ ;
1807
+
1808
+ /**
1809
+ * Plane collider shape in PhysX.
1810
+ */ var PhysXPlaneColliderShape = /*#__PURE__*/ function(PhysXColliderShape1) {
1811
+ _inherits(PhysXPlaneColliderShape, PhysXColliderShape1);
1812
+ function PhysXPlaneColliderShape(physXPhysics, uniqueID, material) {
1813
+ var _this;
1814
+ _this = PhysXColliderShape1.call(this, physXPhysics) || this;
1815
+ _this._axis = new engine.Quaternion(0, 0, PhysXColliderShape.halfSqrt, PhysXColliderShape.halfSqrt);
1816
+ _this._physXRotation.copyFrom(_this._axis);
1817
+ _this._pxGeometry = new physXPhysics._physX.PxPlaneGeometry();
1818
+ _this._initialize(material, uniqueID);
1819
+ _this._setLocalPose();
1820
+ return _this;
1821
+ }
1822
+ return PhysXPlaneColliderShape;
1823
+ }(PhysXColliderShape);
1824
+
1825
+ /**
1826
+ * Sphere collider shape in PhysX.
1827
+ */ var PhysXSphereColliderShape = /*#__PURE__*/ function(PhysXColliderShape) {
1828
+ _inherits(PhysXSphereColliderShape, PhysXColliderShape);
1829
+ function PhysXSphereColliderShape(physXPhysics, uniqueID, radius, material) {
1830
+ var _this;
1831
+ _this = PhysXColliderShape.call(this, physXPhysics) || this, _this._maxScale = 1;
1832
+ _this._radius = radius;
1833
+ _this._pxGeometry = new physXPhysics._physX.PxSphereGeometry(radius * _this._maxScale);
1834
+ _this._initialize(material, uniqueID);
1835
+ _this._setLocalPose();
1836
+ return _this;
1837
+ }
1838
+ var _proto = PhysXSphereColliderShape.prototype;
1839
+ /**
1840
+ * {@inheritDoc ISphereColliderShape.setRadius }
1841
+ */ _proto.setRadius = function setRadius(value) {
1842
+ this._radius = value;
1843
+ this._pxGeometry.radius = value * this._maxScale;
1844
+ this._pxShape.setGeometry(this._pxGeometry);
1845
+ };
1846
+ /**
1847
+ * {@inheritDoc IColliderShape.setWorldScale }
1848
+ */ _proto.setWorldScale = function setWorldScale(scale) {
1849
+ PhysXColliderShape.prototype.setWorldScale.call(this, scale);
1850
+ this._maxScale = Math.max(Math.abs(scale.x), Math.abs(scale.y), Math.abs(scale.z));
1851
+ this._pxGeometry.radius = this._radius * this._maxScale;
1852
+ this._pxShape.setGeometry(this._pxGeometry);
1853
+ };
1854
+ return PhysXSphereColliderShape;
1855
+ }(PhysXColliderShape);
1856
+
1857
+ /**
1858
+ * PhysX object creation.
1859
+ */ var PhysXPhysics = /*#__PURE__*/ function() {
1860
+ function PhysXPhysics(runtimeModeOrOptions, runtimeUrls, options) {
1861
+ if (runtimeModeOrOptions === void 0) runtimeModeOrOptions = PhysXRuntimeMode.Auto;
1862
+ var _this__tolerancesScaleOptions, _this__tolerancesScaleOptions1;
1863
+ this._initializeState = 0;
1864
+ this._defaultContactOffset = 0.02;
1865
+ this._defaultSleepThreshold = 5e-3;
1866
+ var isOptionsObject = (typeof runtimeModeOrOptions === "undefined" ? "undefined" : _type_of(runtimeModeOrOptions)) === "object";
1867
+ var runtimeMode = isOptionsObject ? PhysXRuntimeMode.Auto : runtimeModeOrOptions != null ? runtimeModeOrOptions : PhysXRuntimeMode.Auto;
1868
+ var resolvedOptions = isOptionsObject ? runtimeModeOrOptions : options;
1869
+ this._runTimeMode = runtimeMode;
1870
+ var _runtimeUrls_wasmSIMDModeUrl;
1871
+ this._wasmSIMDModeUrl = (_runtimeUrls_wasmSIMDModeUrl = runtimeUrls == null ? void 0 : runtimeUrls.wasmSIMDModeUrl) != null ? _runtimeUrls_wasmSIMDModeUrl : "https://mdn.alipayobjects.com/rms/uri/file/as/apwallet/1786450043249/suyi/physx.release.simd.js";
1872
+ var _runtimeUrls_wasmModeUrl;
1873
+ this._wasmModeUrl = (_runtimeUrls_wasmModeUrl = runtimeUrls == null ? void 0 : runtimeUrls.wasmModeUrl) != null ? _runtimeUrls_wasmModeUrl : "https://mdn.alipayobjects.com/rms/uri/file/as/apwallet/1786450043249/suyi/physx.release.js";
1874
+ this._tolerancesScaleOptions = resolvedOptions == null ? void 0 : resolvedOptions.tolerancesScale;
1875
+ var _this__tolerancesScaleOptions_length;
1876
+ var length = (_this__tolerancesScaleOptions_length = (_this__tolerancesScaleOptions = this._tolerancesScaleOptions) == null ? void 0 : _this__tolerancesScaleOptions.length) != null ? _this__tolerancesScaleOptions_length : 1;
1877
+ var _this__tolerancesScaleOptions_speed;
1878
+ var speed = (_this__tolerancesScaleOptions_speed = (_this__tolerancesScaleOptions1 = this._tolerancesScaleOptions) == null ? void 0 : _this__tolerancesScaleOptions1.speed) != null ? _this__tolerancesScaleOptions_speed : 10;
1879
+ this._validateTolerancesScale(length, speed);
1880
+ this._updateScaledDefaults(length, speed);
1881
+ }
1882
+ var _proto = PhysXPhysics.prototype;
1883
+ /**
1884
+ * Initialize PhysXPhysics.
1885
+ * @param runtimeMode - Runtime mode
1886
+ * @returns Promise object
1887
+ */ _proto.initialize = function initialize() {
1888
+ var _this = this;
1889
+ if (this._initializeState === 2) {
1890
+ return Promise.resolve();
1891
+ } else if (this._initializeState === 1) {
1892
+ return this._initializePromise;
1893
+ }
1894
+ this._initializeState = 1;
1895
+ var runtimeMode = this._runTimeMode;
1896
+ var scriptPromise = new Promise(function(resolve, reject) {
1897
+ var script = document.createElement("script");
1898
+ document.body.appendChild(script);
1899
+ script.async = true;
1900
+ script.onload = resolve;
1901
+ script.onerror = reject;
1902
+ if (runtimeMode == PhysXRuntimeMode.Auto) {
1903
+ runtimeMode = engine.SystemInfo._detectSIMDSupported() ? PhysXRuntimeMode.WebAssemblySIMD : PhysXRuntimeMode.WebAssembly;
1904
+ }
1905
+ if (runtimeMode == PhysXRuntimeMode.WebAssemblySIMD) {
1906
+ script.src = _this._wasmSIMDModeUrl;
1907
+ } else {
1908
+ script.src = _this._wasmModeUrl;
1909
+ }
1910
+ });
1911
+ var initializePromise = new Promise(function(resolve, reject) {
1912
+ scriptPromise.then(function() {
1913
+ return window.PHYSX().then(function(PHYSX) {
1914
+ _this._runTimeMode = runtimeMode;
1915
+ _this._init(PHYSX);
1916
+ _this._initializeState = 2;
1917
+ _this._initializePromise = null;
1918
+ console.log("PhysX loaded.");
1919
+ resolve();
1920
+ }, reject);
1921
+ }, reject).catch(reject);
1922
+ });
1923
+ this._initializePromise = initializePromise;
1924
+ return initializePromise;
1925
+ };
1926
+ /**
1927
+ * Destroy PhysXPhysics.
1928
+ */ _proto.destroy = function destroy() {
1929
+ this._pxCooking.release();
1930
+ this._pxCookingParams.delete();
1931
+ this._physX.PxCloseExtensions();
1932
+ this._pxPhysics.release();
1933
+ this._pxFoundation.release();
1934
+ this._defaultErrorCallback.delete();
1935
+ this._allocator.delete();
1936
+ this._tolerancesScale.delete();
1937
+ };
1938
+ /**
1939
+ * {@inheritDoc IPhysics.createPhysicsManager }
1940
+ */ _proto.createPhysicsManager = function createPhysicsManager() {
1941
+ return new PhysXPhysicsManager();
1942
+ };
1943
+ /**
1944
+ * {@inheritDoc IPhysics.createPhysicsScene }
1945
+ */ _proto.createPhysicsScene = function createPhysicsScene(physicsManager) {
1946
+ var scene = new PhysXPhysicsScene(this, physicsManager);
1947
+ return scene;
1948
+ };
1949
+ /**
1950
+ * {@inheritDoc IPhysics.getDefaultContactOffset }
1951
+ */ _proto.getDefaultContactOffset = function getDefaultContactOffset() {
1952
+ return this._defaultContactOffset;
1953
+ };
1954
+ /**
1955
+ * {@inheritDoc IPhysics.getDefaultSleepThreshold }
1956
+ */ _proto.getDefaultSleepThreshold = function getDefaultSleepThreshold() {
1957
+ return this._defaultSleepThreshold;
1958
+ };
1959
+ /**
1960
+ * {@inheritDoc IPhysics.createStaticCollider }
1961
+ */ _proto.createStaticCollider = function createStaticCollider(position, rotation) {
1962
+ return new PhysXStaticCollider(this, position, rotation);
1963
+ };
1964
+ /**
1965
+ * {@inheritDoc IPhysics.createDynamicCollider }
1966
+ */ _proto.createDynamicCollider = function createDynamicCollider(position, rotation) {
1967
+ return new PhysXDynamicCollider(this, position, rotation);
1968
+ };
1969
+ /**
1970
+ * {@inheritDoc IPhysics.createCharacterController }
1971
+ */ _proto.createCharacterController = function createCharacterController() {
1972
+ return new PhysXCharacterController(this);
1973
+ };
1974
+ /**
1975
+ * {@inheritDoc IPhysics.createPhysicsMaterial }
1976
+ */ _proto.createPhysicsMaterial = function createPhysicsMaterial(staticFriction, dynamicFriction, bounciness, frictionCombine, bounceCombine) {
1977
+ return new PhysXPhysicsMaterial(this, staticFriction, dynamicFriction, bounciness, frictionCombine, bounceCombine);
1978
+ };
1979
+ /**
1980
+ * {@inheritDoc IPhysics.createBoxColliderShape }
1981
+ */ _proto.createBoxColliderShape = function createBoxColliderShape(uniqueID, size, material) {
1982
+ return new PhysXBoxColliderShape(this, uniqueID, size, material);
1983
+ };
1984
+ /**
1985
+ * {@inheritDoc IPhysics.createSphereColliderShape }
1986
+ */ _proto.createSphereColliderShape = function createSphereColliderShape(uniqueID, radius, material) {
1987
+ return new PhysXSphereColliderShape(this, uniqueID, radius, material);
1988
+ };
1989
+ /**
1990
+ * {@inheritDoc IPhysics.createPlaneColliderShape }
1991
+ */ _proto.createPlaneColliderShape = function createPlaneColliderShape(uniqueID, material) {
1992
+ return new PhysXPlaneColliderShape(this, uniqueID, material);
1993
+ };
1994
+ /**
1995
+ * {@inheritDoc IPhysics.createCapsuleColliderShape }
1996
+ */ _proto.createCapsuleColliderShape = function createCapsuleColliderShape(uniqueID, radius, height, material) {
1997
+ return new PhysXCapsuleColliderShape(this, uniqueID, radius, height, material);
1998
+ };
1999
+ /**
2000
+ * {@inheritDoc IPhysics.createMeshColliderShape }
2001
+ */ _proto.createMeshColliderShape = function createMeshColliderShape(uniqueID, positions, indices, isConvex, material, cookingFlags) {
2002
+ var shape = new PhysXMeshColliderShape(this, uniqueID, positions, indices, isConvex, material, cookingFlags);
2003
+ return shape._pxShape ? shape : null;
2004
+ };
2005
+ /**
2006
+ * {@inheritDoc IPhysics.createFixedJoint }
2007
+ */ _proto.createFixedJoint = function createFixedJoint(collider) {
2008
+ return new PhysXFixedJoint(this, collider);
2009
+ };
2010
+ /**
2011
+ * {@inheritDoc IPhysics.createHingeJoint }
2012
+ */ _proto.createHingeJoint = function createHingeJoint(collider) {
2013
+ return new PhysXHingeJoint(this, collider);
2014
+ };
2015
+ /**
2016
+ * {@inheritDoc IPhysics.createSpringJoint }
2017
+ */ _proto.createSpringJoint = function createSpringJoint(collider) {
2018
+ return new PhysXSpringJoint(this, collider);
2019
+ };
2020
+ /**
2021
+ * {@inheritDoc IPhysics.getColliderLayerCollision }
2022
+ */ _proto.getColliderLayerCollision = function getColliderLayerCollision(layer1, layer2) {
2023
+ return this._physX.getGroupCollisionFlag(layer1, layer2);
2024
+ };
2025
+ /**
2026
+ * {@inheritDoc IPhysics.setColliderLayerCollision }
2027
+ */ _proto.setColliderLayerCollision = function setColliderLayerCollision(layer1, layer2, isCollide) {
2028
+ this._physX.setGroupCollisionFlag(layer1, layer2, isCollide);
2029
+ };
2030
+ _proto._init = function _init(physX) {
2031
+ var version = physX.PX_PHYSICS_VERSION;
2032
+ var defaultErrorCallback = new physX.PxDefaultErrorCallback();
2033
+ var allocator = new physX.PxDefaultAllocator();
2034
+ var pxFoundation = physX.PxCreateFoundation(version, allocator, defaultErrorCallback);
2035
+ var tolerancesScale = new physX.PxTolerancesScale();
2036
+ this._applyTolerancesScale(tolerancesScale);
2037
+ var pxPhysics = physX.PxCreatePhysics(version, pxFoundation, tolerancesScale, false, null);
2038
+ physX.PxInitExtensions(pxPhysics, null);
2039
+ // Initialize cooking for mesh colliders
2040
+ var cookingParams = new physX.PxCookingParams(tolerancesScale);
2041
+ physX.setCookingMeshPreprocessParams(cookingParams, 1); // eWELD_VERTICES
2042
+ cookingParams.meshWeldTolerance = 0.001;
2043
+ // BVH34 midphase requires SSE2; SIMD WASM provides SSE2 via WASM SIMD
2044
+ if (this._runTimeMode === PhysXRuntimeMode.WebAssemblySIMD) {
2045
+ physX.setCookingMidphaseType(cookingParams, 1); // eBVH34
2046
+ }
2047
+ var pxCooking = physX.PxCreateCooking(version, pxFoundation, cookingParams);
2048
+ this._physX = physX;
2049
+ this._pxFoundation = pxFoundation;
2050
+ this._pxPhysics = pxPhysics;
2051
+ this._pxCooking = pxCooking;
2052
+ this._pxCookingParams = cookingParams;
2053
+ this._defaultErrorCallback = defaultErrorCallback;
2054
+ this._allocator = allocator;
2055
+ this._tolerancesScale = tolerancesScale;
2056
+ };
2057
+ _proto._applyTolerancesScale = function _applyTolerancesScale(tolerancesScale) {
2058
+ var _this__tolerancesScaleOptions, _this__tolerancesScaleOptions1;
2059
+ var _this__tolerancesScaleOptions_length;
2060
+ var length = (_this__tolerancesScaleOptions_length = (_this__tolerancesScaleOptions = this._tolerancesScaleOptions) == null ? void 0 : _this__tolerancesScaleOptions.length) != null ? _this__tolerancesScaleOptions_length : tolerancesScale.length;
2061
+ var _this__tolerancesScaleOptions_speed;
2062
+ var speed = (_this__tolerancesScaleOptions_speed = (_this__tolerancesScaleOptions1 = this._tolerancesScaleOptions) == null ? void 0 : _this__tolerancesScaleOptions1.speed) != null ? _this__tolerancesScaleOptions_speed : tolerancesScale.speed;
2063
+ this._validateTolerancesScale(length, speed);
2064
+ tolerancesScale.length = length;
2065
+ tolerancesScale.speed = speed;
2066
+ this._updateScaledDefaults(length, speed);
2067
+ };
2068
+ _proto._assertPositiveFinite = function _assertPositiveFinite(value, name) {
2069
+ if (!Number.isFinite(value) || value <= 0) {
2070
+ throw new Error("PhysXPhysics " + name + " must be a positive finite number.");
2071
+ }
2072
+ };
2073
+ _proto._validateTolerancesScale = function _validateTolerancesScale(length, speed) {
2074
+ this._assertPositiveFinite(length, "tolerancesScale.length");
2075
+ this._assertPositiveFinite(speed, "tolerancesScale.speed");
2076
+ };
2077
+ _proto._updateScaledDefaults = function _updateScaledDefaults(length, speed) {
2078
+ this._defaultContactOffset = 0.02 * length;
2079
+ this._defaultSleepThreshold = 5e-5 * speed * speed;
2080
+ };
2081
+ return PhysXPhysics;
2082
+ }();
2083
+
2084
+ //@ts-ignore
2085
+ var version = "0.0.0-experimental-2.0-migrate.7";
2086
+ console.log("Galacean Engine Physics PhysX Version: " + version);
2087
+
2088
+ exports.PhysXPhysics = PhysXPhysics;
2089
+ exports.PhysXRuntimeMode = PhysXRuntimeMode;
2090
+ exports.version = version;
2091
+
2092
+ Object.defineProperty(exports, '__esModule', { value: true });
2093
+
2094
+ }));
2095
+ //# sourceMappingURL=browser.js.map