@galacean/engine-physics-physx 2.0.0-alpha.2 → 2.0.0-alpha.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/browser.js CHANGED
@@ -10,12 +10,6 @@
10
10
  } else return left instanceof right;
11
11
  }
12
12
 
13
- function _type_of(obj) {
14
- "@swc/helpers - typeof";
15
-
16
- return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
17
- }
18
-
19
13
  function _set_prototype_of(o, p) {
20
14
  _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
21
15
  o.__proto__ = p;
@@ -827,44 +821,64 @@
827
821
  return PhysXPhysicsMaterial;
828
822
  }();
829
823
 
824
+ function _defineProperties(target, props) {
825
+ for (var i = 0; i < props.length; i++) {
826
+ var descriptor = props[i];
827
+ descriptor.enumerable = descriptor.enumerable || false;
828
+ descriptor.configurable = true;
829
+
830
+ if ("value" in descriptor) descriptor.writable = true;
831
+
832
+ Object.defineProperty(target, descriptor.key, descriptor);
833
+ }
834
+ }
835
+ function _create_class(Constructor, protoProps, staticProps) {
836
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
837
+ if (staticProps) _defineProperties(Constructor, staticProps);
838
+
839
+ return Constructor;
840
+ }
841
+
830
842
  /**
831
843
  * A manager is a collection of colliders and constraints which can interact.
832
844
  */ var PhysXPhysicsScene = /*#__PURE__*/ function() {
833
- function PhysXPhysicsScene(physXPhysics, physicsManager, onContactEnter, onContactExit, onContactStay, onTriggerEnter, onTriggerExit, onTriggerStay) {
845
+ function PhysXPhysicsScene(physXPhysics, physicsManager) {
834
846
  var _this = this;
835
847
  /** @internal */ this._pxControllerManager = null;
836
848
  // Cached geometry objects for reuse
837
849
  this._boxGeometry = null;
838
850
  this._sphereGeometry = null;
839
851
  this._capsuleGeometry = null;
840
- this._currentEvents = new engine.DisorderedArray();
841
- this._eventPool = [];
852
+ this._activeTriggers = new engine.DisorderedArray();
853
+ this._contactEvents = [];
854
+ this._contactEventCount = 0;
855
+ this._triggerEvents = [];
856
+ this._physicsEvents = {
857
+ contactEvents: [],
858
+ contactEventCount: 0,
859
+ triggerEvents: []
860
+ };
861
+ this._triggerEventPool = [];
842
862
  this._physXPhysics = physXPhysics;
843
863
  this._physXManager = physicsManager;
844
864
  var physX = physXPhysics._physX;
845
865
  this._pxRaycastHit = new physX.PxRaycastHit();
846
866
  this._pxFilterData = new physX.PxQueryFilterData();
847
867
  this._pxFilterData.flags = new physX.PxQueryFlags(1 | 2 | 4);
848
- this._onContactEnter = onContactEnter;
849
- this._onContactExit = onContactExit;
850
- this._onContactStay = onContactStay;
851
- this._onTriggerEnter = onTriggerEnter;
852
- this._onTriggerExit = onTriggerExit;
853
- this._onTriggerStay = onTriggerStay;
854
868
  var triggerCallback = {
855
869
  onContactBegin: function(collision) {
856
- _this._onContactEnter(collision);
870
+ _this._bufferContactEvent(collision, 0);
857
871
  },
858
872
  onContactEnd: function(collision) {
859
- _this._onContactExit(collision);
873
+ _this._bufferContactEvent(collision, 2);
860
874
  },
861
875
  onContactPersist: function(collision) {
862
- _this._onContactStay(collision);
876
+ _this._bufferContactEvent(collision, 1);
863
877
  },
864
878
  onTriggerBegin: function(index1, index2) {
865
879
  var event = index1 < index2 ? _this._getTrigger(index1, index2) : _this._getTrigger(index2, index1);
866
880
  event.state = 0;
867
- _this._currentEvents.add(event);
881
+ _this._activeTriggers.add(event);
868
882
  },
869
883
  onTriggerEnd: function(index1, index2) {
870
884
  var event;
@@ -942,9 +956,31 @@
942
956
  /**
943
957
  * {@inheritDoc IPhysicsScene.update }
944
958
  */ _proto.update = function update(elapsedTime) {
959
+ this._contactEventCount = 0;
945
960
  this._simulate(elapsedTime);
946
961
  this._fetchResults();
947
- this._fireEvent();
962
+ };
963
+ /**
964
+ * {@inheritDoc IPhysicsScene.updateEvents }
965
+ */ _proto.updateEvents = function updateEvents() {
966
+ var physicsEvents = this._physicsEvents;
967
+ // Collect trigger events: snapshot state for dispatch, then advance
968
+ var _this = this, triggerEventPool = _this._triggerEventPool, activeTriggers = _this._activeTriggers, triggerEvents = _this._triggerEvents;
969
+ triggerEvents.length = 0;
970
+ activeTriggers.forEach(function(event, i) {
971
+ event.dispatchState = event.state;
972
+ triggerEvents.push(event);
973
+ if (event.state === 0) {
974
+ event.state = 1;
975
+ } else if (event.state === 2) {
976
+ activeTriggers.deleteByIndex(i);
977
+ triggerEventPool.push(event);
978
+ }
979
+ });
980
+ physicsEvents.contactEvents = this._contactEvents;
981
+ physicsEvents.contactEventCount = this._contactEventCount;
982
+ physicsEvents.triggerEvents = triggerEvents;
983
+ return physicsEvents;
948
984
  };
949
985
  /**
950
986
  * {@inheritDoc IPhysicsScene.raycast }
@@ -1059,6 +1095,11 @@
1059
1095
  return this._overlapMultiple(this._capsuleGeometry, pose, onOverlap);
1060
1096
  };
1061
1097
  /**
1098
+ * {@inheritDoc IPhysicsScene.gc }
1099
+ */ _proto.gc = function gc() {
1100
+ this._contactEvents.length = this._contactEventCount;
1101
+ };
1102
+ /**
1062
1103
  * {@inheritDoc IPhysicsScene.destroy }
1063
1104
  */ _proto.destroy = function destroy() {
1064
1105
  var _this__boxGeometry, _this__sphereGeometry, _this__capsuleGeometry, // Need to release the controller manager before release the scene.
@@ -1090,15 +1131,15 @@
1090
1131
  /**
1091
1132
  * @internal
1092
1133
  */ _proto._removeColliderShape = function _removeColliderShape(id) {
1093
- var _this = this, eventPool = _this._eventPool, currentEvents = _this._currentEvents;
1134
+ var _this = this, triggerEventPool = _this._triggerEventPool, activeTriggers = _this._activeTriggers;
1094
1135
  var _this__physXManager = this._physXManager, eventMap = _this__physXManager._eventMap;
1095
- currentEvents.forEach(function(event, i) {
1136
+ activeTriggers.forEach(function(event, i) {
1096
1137
  if (event.index1 == id) {
1097
- currentEvents.deleteByIndex(i);
1098
- eventPool.push(event);
1138
+ activeTriggers.deleteByIndex(i);
1139
+ triggerEventPool.push(event);
1099
1140
  } else if (event.index2 == id) {
1100
- currentEvents.deleteByIndex(i);
1101
- eventPool.push(event);
1141
+ activeTriggers.deleteByIndex(i);
1142
+ triggerEventPool.push(event);
1102
1143
  // If the shape is big index, should clear from the small index shape subMap
1103
1144
  eventMap[event.index1][id] = undefined;
1104
1145
  }
@@ -1160,8 +1201,8 @@
1160
1201
  };
1161
1202
  _proto._getTrigger = function _getTrigger(index1, index2) {
1162
1203
  var event;
1163
- if (this._eventPool.length) {
1164
- event = this._eventPool.pop();
1204
+ if (this._triggerEventPool.length) {
1205
+ event = this._triggerEventPool.pop();
1165
1206
  event.index1 = index1;
1166
1207
  event.index2 = index2;
1167
1208
  } else {
@@ -1170,21 +1211,27 @@
1170
1211
  this._physXManager._eventMap[index1][index2] = event;
1171
1212
  return event;
1172
1213
  };
1173
- _proto._fireEvent = function _fireEvent() {
1174
- var _this = this;
1175
- var _this1 = this, eventPool = _this1._eventPool, currentEvents = _this1._currentEvents;
1176
- currentEvents.forEach(function(event, i) {
1177
- if (event.state == 0) {
1178
- _this._onTriggerEnter(event.index1, event.index2);
1179
- event.state = 1;
1180
- } else if (event.state == 1) {
1181
- _this._onTriggerStay(event.index1, event.index2);
1182
- } else if (event.state == 2) {
1183
- currentEvents.deleteByIndex(i);
1184
- _this._onTriggerExit(event.index1, event.index2);
1185
- eventPool.push(event);
1186
- }
1187
- });
1214
+ _proto._bufferContactEvent = function _bufferContactEvent(collision, state) {
1215
+ var _this__contactEvents, _index;
1216
+ var index = this._contactEventCount++;
1217
+ var event = (_this__contactEvents = this._contactEvents)[_index = index] || (_this__contactEvents[_index] = new ContactEvent());
1218
+ event.shape0Id = collision.shape0Id;
1219
+ event.shape1Id = collision.shape1Id;
1220
+ event.state = state;
1221
+ // Copy contact points from PhysX (the native data is only valid during fetchResults)
1222
+ var nativeContacts = collision.getContacts();
1223
+ var count = nativeContacts.size();
1224
+ var bufferedContacts = event._bufferedContacts;
1225
+ bufferedContacts.contactCount = count;
1226
+ for(var i = 0; i < count; i++){
1227
+ var _bufferedContacts_contacts, _i;
1228
+ var src = nativeContacts.get(i);
1229
+ var dst = (_bufferedContacts_contacts = bufferedContacts.contacts)[_i = i] || (_bufferedContacts_contacts[_i] = new BufferedContactPoint());
1230
+ dst.position.copyFrom(src.position);
1231
+ dst.normal.copyFrom(src.normal);
1232
+ dst.impulse.copyFrom(src.impulse);
1233
+ dst.separation = src.separation;
1234
+ }
1188
1235
  };
1189
1236
  return PhysXPhysicsScene;
1190
1237
  }();
@@ -1202,6 +1249,49 @@
1202
1249
  this.index1 = index1;
1203
1250
  this.index2 = index2;
1204
1251
  };
1252
+ /**
1253
+ * Buffered contact point data, copied from PhysX during fetchResults.
1254
+ */ var BufferedContactPoint = function BufferedContactPoint() {
1255
+ this.position = new engine.Vector3();
1256
+ this.normal = new engine.Vector3();
1257
+ this.impulse = new engine.Vector3();
1258
+ this.separation = 0;
1259
+ };
1260
+ /**
1261
+ * Contact event buffered from PhysX fetchResults callback.
1262
+ * Implements ICollision so it can be passed directly to the core layer.
1263
+ */ var BufferedContacts = /*#__PURE__*/ function() {
1264
+ function BufferedContacts() {
1265
+ this.contactCount = 0;
1266
+ this.contacts = [];
1267
+ }
1268
+ var _proto = BufferedContacts.prototype;
1269
+ _proto.size = function size() {
1270
+ return this.contactCount;
1271
+ };
1272
+ _proto.get = function get(index) {
1273
+ return this.contacts[index];
1274
+ };
1275
+ return BufferedContacts;
1276
+ }();
1277
+ var ContactEvent = /*#__PURE__*/ function() {
1278
+ function ContactEvent() {
1279
+ /** @internal */ this._bufferedContacts = new BufferedContacts();
1280
+ }
1281
+ var _proto = ContactEvent.prototype;
1282
+ _proto.getContacts = function getContacts() {
1283
+ return this._bufferedContacts;
1284
+ };
1285
+ _create_class(ContactEvent, [
1286
+ {
1287
+ key: "contactCount",
1288
+ get: function get() {
1289
+ return this._bufferedContacts.contactCount;
1290
+ }
1291
+ }
1292
+ ]);
1293
+ return ContactEvent;
1294
+ }();
1205
1295
 
1206
1296
  /**
1207
1297
  * A static collider component that will not move.
@@ -1220,9 +1310,9 @@
1220
1310
  /**
1221
1311
  * PhysX runtime mode.
1222
1312
  */ var PhysXRuntimeMode = /*#__PURE__*/ function(PhysXRuntimeMode) {
1223
- /** Use webAssembly mode first, if WebAssembly mode is not supported, roll back to JavaScript mode. */ PhysXRuntimeMode[PhysXRuntimeMode["Auto"] = 0] = "Auto";
1313
+ /** Use WebAssembly SIMD mode first, then WebAssembly as fallback. */ PhysXRuntimeMode[PhysXRuntimeMode["Auto"] = 0] = "Auto";
1224
1314
  /** WebAssembly mode. */ PhysXRuntimeMode[PhysXRuntimeMode["WebAssembly"] = 1] = "WebAssembly";
1225
- /** JavaScript mode. */ PhysXRuntimeMode[PhysXRuntimeMode["JavaScript"] = 2] = "JavaScript";
1315
+ /** WebAssembly SIMD mode. */ PhysXRuntimeMode[PhysXRuntimeMode["WebAssemblySIMD"] = 2] = "WebAssemblySIMD";
1226
1316
  return PhysXRuntimeMode;
1227
1317
  }({});
1228
1318
 
@@ -1454,62 +1544,62 @@
1454
1544
  return PhysXSpringJoint;
1455
1545
  }(PhysXJoint);
1456
1546
 
1457
- /** TriangleMesh flag: eDOUBLE_SIDED = 2 (1<<1) */ var DOUBLE_SIDED_FLAG = 2;
1458
- /** ConvexMesh flag: eTIGHT_BOUNDS = 1 (1<<0) */ var TIGHT_BOUNDS_FLAG = 1;
1547
+ function _assert_this_initialized(self) {
1548
+ if (self === void 0) throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
1549
+
1550
+ return self;
1551
+ }
1552
+
1553
+ function _type_of(obj) {
1554
+ "@swc/helpers - typeof";
1555
+
1556
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
1557
+ }
1558
+
1559
+ function _possible_constructor_return(self, call) {
1560
+ if (call && (_type_of(call) === "object" || typeof call === "function")) return call;
1561
+
1562
+ return _assert_this_initialized(self);
1563
+ }
1564
+
1459
1565
  /**
1460
1566
  * Mesh collider shape in PhysX.
1461
1567
  */ var PhysXMeshColliderShape = /*#__PURE__*/ function(PhysXColliderShape) {
1462
1568
  _inherits(PhysXMeshColliderShape, PhysXColliderShape);
1463
- function PhysXMeshColliderShape(physXPhysics, uniqueID, vertices, vertexCount, indices, isConvex, material) {
1569
+ function PhysXMeshColliderShape(physXPhysics, uniqueID, positions, indices, isConvex, material, cookingFlags) {
1464
1570
  var _this;
1465
- _this = PhysXColliderShape.call(this, physXPhysics) || this, _this._pxMesh = null, _this._doubleSided = false, _this._tightBounds = true;
1571
+ _this = PhysXColliderShape.call(this, physXPhysics) || this, _this._pxMesh = null;
1466
1572
  _this._isConvex = isConvex;
1467
- _this._vertices = vertices;
1468
- _this._vertexCount = vertexCount;
1469
- _this._indices = indices;
1470
- _this._createMeshAndShape(material, uniqueID);
1573
+ if (!_this._cookMesh(positions, indices, cookingFlags)) {
1574
+ return _possible_constructor_return(_this);
1575
+ }
1576
+ var physX = physXPhysics._physX, physics = physXPhysics._pxPhysics;
1577
+ var _this__worldScale = _this._worldScale, scaleX = _this__worldScale.x, scaleY = _this__worldScale.y, scaleZ = _this__worldScale.z;
1578
+ var shapeFlags = ShapeFlag.SCENE_QUERY_SHAPE | ShapeFlag.SIMULATION_SHAPE;
1579
+ var meshFlag = isConvex ? PhysXMeshColliderShape._tightBoundsFlag : 0;
1580
+ var createShapeFn = isConvex ? physX.createConvexMeshShape : physX.createTriMeshShape;
1581
+ _this._pxShape = createShapeFn(_this._pxMesh, scaleX, scaleY, scaleZ, meshFlag, shapeFlags, material._pxMaterial, physics);
1582
+ _this._id = uniqueID;
1583
+ _this._pxMaterial = material._pxMaterial;
1584
+ _this._pxShape.setUUID(uniqueID);
1471
1585
  _this._setLocalPose();
1472
1586
  return _this;
1473
1587
  }
1474
1588
  var _proto = PhysXMeshColliderShape.prototype;
1475
1589
  /**
1476
1590
  * {@inheritDoc IMeshColliderShape.setMeshData }
1477
- */ _proto.setMeshData = function setMeshData(vertices, vertexCount, indices, isConvex) {
1478
- // Save old resources
1479
- var oldMesh = this._pxMesh;
1480
- var oldGeometry = this._pxGeometry;
1481
- // Update data and create new mesh (may throw on failure)
1591
+ */ _proto.setMeshData = function setMeshData(positions, indices, isConvex, cookingFlags) {
1592
+ var _this__pxMesh, _this__pxGeometry;
1593
+ (_this__pxMesh = this._pxMesh) == null ? void 0 : _this__pxMesh.release();
1594
+ (_this__pxGeometry = this._pxGeometry) == null ? void 0 : _this__pxGeometry.delete();
1482
1595
  this._pxMesh = null;
1483
1596
  this._pxGeometry = null;
1484
1597
  this._isConvex = isConvex;
1485
- this._vertices = vertices;
1486
- this._vertexCount = vertexCount;
1487
- this._indices = indices;
1488
- this._createMesh();
1489
- this._pxShape.setGeometry(this._pxGeometry);
1490
- // Release old resources only after successful creation
1491
- if (oldMesh) {
1492
- oldMesh.release();
1493
- }
1494
- if (oldGeometry) {
1495
- oldGeometry.delete();
1496
- }
1497
- };
1498
- /**
1499
- * {@inheritDoc IMeshColliderShape.setDoubleSided }
1500
- */ _proto.setDoubleSided = function setDoubleSided(value) {
1501
- this._doubleSided = value;
1502
- if (!this._isConvex && this._pxMesh) {
1503
- this._updateGeometry();
1504
- }
1505
- };
1506
- /**
1507
- * {@inheritDoc IMeshColliderShape.setTightBounds }
1508
- */ _proto.setTightBounds = function setTightBounds(value) {
1509
- this._tightBounds = value;
1510
- if (this._isConvex && this._pxMesh) {
1511
- this._updateGeometry();
1598
+ if (!this._cookMesh(positions, indices, cookingFlags)) {
1599
+ return false;
1512
1600
  }
1601
+ this._pxShape.setGeometry(this._pxGeometry);
1602
+ return true;
1513
1603
  };
1514
1604
  /**
1515
1605
  * {@inheritDoc IColliderShape.setWorldScale }
@@ -1520,103 +1610,107 @@
1520
1610
  /**
1521
1611
  * {@inheritDoc IColliderShape.destroy }
1522
1612
  */ _proto.destroy = function destroy() {
1523
- this._releaseMesh();
1613
+ var _this__pxMesh;
1614
+ (_this__pxMesh = this._pxMesh) == null ? void 0 : _this__pxMesh.release();
1524
1615
  PhysXColliderShape.prototype.destroy.call(this);
1525
1616
  };
1526
- _proto._createMeshAndShape = function _createMeshAndShape(material, uniqueID) {
1527
- var physX = this._physXPhysics._physX;
1528
- var physics = this._physXPhysics._pxPhysics;
1529
- var shapeFlags = ShapeFlag.SCENE_QUERY_SHAPE | ShapeFlag.SIMULATION_SHAPE;
1530
- this._createMesh();
1531
- // Create shape with material
1617
+ _proto._cookMesh = function _cookMesh(positions, indices, cookingFlags) {
1618
+ var _this__physXPhysics = this._physXPhysics, physX = _this__physXPhysics._physX, physics = _this__physXPhysics._pxPhysics, cooking = _this__physXPhysics._pxCooking, cookingParams = _this__physXPhysics._pxCookingParams;
1619
+ // Apply per-shape cooking flags
1620
+ var preprocessFlags = 0;
1621
+ if (cookingFlags & engine.MeshColliderShapeCookingFlag.VertexWelding) {
1622
+ preprocessFlags |= 1; // eWELD_VERTICES
1623
+ }
1624
+ if (!(cookingFlags & engine.MeshColliderShapeCookingFlag.Cleaning)) {
1625
+ preprocessFlags |= 2; // eDISABLE_CLEAN_MESH
1626
+ }
1627
+ physX.setCookingMeshPreprocessParams(cookingParams, preprocessFlags);
1628
+ cooking.setParams(cookingParams);
1629
+ var verticesPtr = this._allocatePositions(positions);
1532
1630
  if (this._isConvex) {
1533
- this._pxShape = physX.createConvexMeshShape(this._pxMesh, this._worldScale.x, this._worldScale.y, this._worldScale.z, this._tightBounds ? TIGHT_BOUNDS_FLAG : 0, shapeFlags, material._pxMaterial, physics);
1631
+ this._pxMesh = cooking.createConvexMesh(verticesPtr, positions.length, physics);
1632
+ physX._free(verticesPtr);
1633
+ if (!this._pxMesh) {
1634
+ this._logConvexCookingError(physX);
1635
+ return false;
1636
+ }
1534
1637
  } else {
1535
- this._pxShape = physX.createTriMeshShape(this._pxMesh, this._worldScale.x, this._worldScale.y, this._worldScale.z, this._doubleSided ? DOUBLE_SIDED_FLAG : 0, shapeFlags, material._pxMaterial, physics);
1536
- }
1537
- this._id = uniqueID;
1538
- this._pxMaterial = material._pxMaterial;
1539
- this._pxShape.setUUID(uniqueID);
1540
- };
1541
- _proto._createMesh = function _createMesh() {
1542
- var physX = this._physXPhysics._physX;
1543
- var physics = this._physXPhysics._pxPhysics;
1544
- var cooking = this._physXPhysics._pxCooking;
1545
- var verticesPtr = this._allocateVertices();
1546
- var indicesPtr = 0;
1547
- try {
1548
- if (this._isConvex) {
1549
- this._pxMesh = cooking.createConvexMesh(verticesPtr, this._vertexCount, physics);
1550
- if (!this._pxMesh) {
1551
- throw new Error("Failed to create convex mesh. Check if vertex count <= 255 and geometry is valid.");
1552
- }
1553
- this._pxGeometry = physX.createConvexMeshGeometry(this._pxMesh, this._worldScale.x, this._worldScale.y, this._worldScale.z, this._tightBounds ? TIGHT_BOUNDS_FLAG : 0);
1554
- } else {
1555
- if (!this._indices) {
1556
- throw new Error("Triangle mesh requires indices");
1557
- }
1558
- var _this__allocateIndices = this._allocateIndices(), ptr = _this__allocateIndices.ptr, isU16 = _this__allocateIndices.isU16, triangleCount = _this__allocateIndices.triangleCount;
1559
- indicesPtr = ptr;
1560
- this._pxMesh = cooking.createTriMesh(verticesPtr, this._vertexCount, indicesPtr, triangleCount, isU16, physics);
1561
- if (!this._pxMesh) {
1562
- throw new Error("Failed to create triangle mesh. Check if geometry is valid.");
1563
- }
1564
- this._pxGeometry = physX.createTriMeshGeometry(this._pxMesh, this._worldScale.x, this._worldScale.y, this._worldScale.z, this._doubleSided ? DOUBLE_SIDED_FLAG : 0);
1638
+ if (!indices) {
1639
+ physX._free(verticesPtr);
1640
+ console.error("PhysXMeshColliderShape: Triangle mesh requires indices.");
1641
+ return false;
1565
1642
  }
1566
- } finally{
1643
+ var isU32 = _instanceof(indices, Uint32Array);
1644
+ var indicesPtr = this._allocateIndices(indices, isU32);
1645
+ this._pxMesh = cooking.createTriMesh(verticesPtr, positions.length, indicesPtr, indices.length / 3, !isU32, physics);
1567
1646
  physX._free(verticesPtr);
1568
- if (indicesPtr) {
1569
- physX._free(indicesPtr);
1647
+ physX._free(indicesPtr);
1648
+ if (!this._pxMesh) {
1649
+ this._logTriMeshCookingError(physX);
1650
+ return false;
1570
1651
  }
1571
- // Release JS memory after copying to WASM
1572
- this._vertices = null;
1573
- this._indices = null;
1574
1652
  }
1653
+ var _this__worldScale = this._worldScale, scaleX = _this__worldScale.x, scaleY = _this__worldScale.y, scaleZ = _this__worldScale.z;
1654
+ var meshFlag = this._isConvex ? PhysXMeshColliderShape._tightBoundsFlag : 0;
1655
+ this._pxGeometry = this._isConvex ? physX.createConvexMeshGeometry(this._pxMesh, scaleX, scaleY, scaleZ, meshFlag) : physX.createTriMeshGeometry(this._pxMesh, scaleX, scaleY, scaleZ, meshFlag);
1656
+ return true;
1575
1657
  };
1576
- _proto._allocateVertices = function _allocateVertices() {
1658
+ _proto._logConvexCookingError = function _logConvexCookingError(physX) {
1659
+ switch(physX.getLastConvexCookingResult()){
1660
+ case 1:
1661
+ console.error("PhysXMeshColliderShape: Failed to create convex mesh. Could not find 4 vertices that do not form a zero-area triangle.");
1662
+ break;
1663
+ case 2:
1664
+ console.error("PhysXMeshColliderShape: Failed to create convex mesh within the maximum polygons limit (256). Consider simplifying the mesh.");
1665
+ break;
1666
+ default:
1667
+ console.error("PhysXMeshColliderShape: Failed to create convex mesh. The input geometry may be invalid.");
1668
+ break;
1669
+ }
1670
+ };
1671
+ _proto._logTriMeshCookingError = function _logTriMeshCookingError(physX) {
1672
+ switch(physX.getLastTriMeshCookingResult()){
1673
+ case 1:
1674
+ console.error("PhysXMeshColliderShape: Failed to create triangle mesh. One of the triangles is too large. Consider tessellating large triangles.");
1675
+ break;
1676
+ default:
1677
+ console.error("PhysXMeshColliderShape: Failed to create triangle mesh. The input geometry may be invalid.");
1678
+ break;
1679
+ }
1680
+ };
1681
+ _proto._allocatePositions = function _allocatePositions(positions) {
1577
1682
  var physX = this._physXPhysics._physX;
1578
- var ptr = physX._malloc(this._vertexCount * 3 * 4);
1579
- var view = new Float32Array(physX.HEAPF32.buffer, ptr, this._vertexCount * 3);
1580
- view.set(this._vertices);
1683
+ var length = positions.length;
1684
+ var ptr = physX._malloc(length * 3 * 4);
1685
+ var view = new Float32Array(physX.HEAPF32.buffer, ptr, length * 3);
1686
+ for(var i = 0, offset = 0; i < length; i++, offset += 3){
1687
+ positions[i].copyToArray(view, offset);
1688
+ }
1581
1689
  return ptr;
1582
1690
  };
1583
- _proto._allocateIndices = function _allocateIndices() {
1691
+ _proto._allocateIndices = function _allocateIndices(indices, isU32) {
1584
1692
  var physX = this._physXPhysics._physX;
1585
- var indices = this._indices;
1586
- var isU16 = _instanceof(indices, Uint16Array);
1587
- var triangleCount = indices.length / 3;
1588
- var bytesPerIndex = isU16 ? 2 : 4;
1589
- var ptr = physX._malloc(indices.length * bytesPerIndex);
1590
- if (isU16) {
1591
- new Uint16Array(physX.HEAPU16.buffer, ptr, indices.length).set(indices);
1592
- } else {
1593
- new Uint32Array(physX.HEAPU32.buffer, ptr, indices.length).set(indices);
1594
- }
1595
- return {
1596
- ptr: ptr,
1597
- isU16: isU16,
1598
- triangleCount: triangleCount
1599
- };
1693
+ // Uint8Array and Uint16Array both write as Uint16 (PhysX minimum index size)
1694
+ var TypedArrayCtor = isU32 ? Uint32Array : Uint16Array;
1695
+ var heap = isU32 ? physX.HEAPU32 : physX.HEAPU16;
1696
+ var ptr = physX._malloc(indices.length * TypedArrayCtor.BYTES_PER_ELEMENT);
1697
+ new TypedArrayCtor(heap.buffer, ptr, indices.length).set(indices);
1698
+ return ptr;
1600
1699
  };
1601
1700
  _proto._updateGeometry = function _updateGeometry() {
1701
+ if (!this._pxMesh) return;
1602
1702
  var physX = this._physXPhysics._physX;
1603
- var newGeometry = this._isConvex ? physX.createConvexMeshGeometry(this._pxMesh, this._worldScale.x, this._worldScale.y, this._worldScale.z, this._tightBounds ? TIGHT_BOUNDS_FLAG : 0) : physX.createTriMeshGeometry(this._pxMesh, this._worldScale.x, this._worldScale.y, this._worldScale.z, this._doubleSided ? DOUBLE_SIDED_FLAG : 0);
1703
+ var _this__worldScale = this._worldScale, scaleX = _this__worldScale.x, scaleY = _this__worldScale.y, scaleZ = _this__worldScale.z;
1704
+ var meshFlag = this._isConvex ? PhysXMeshColliderShape._tightBoundsFlag : 0;
1705
+ var newGeometry = this._isConvex ? physX.createConvexMeshGeometry(this._pxMesh, scaleX, scaleY, scaleZ, meshFlag) : physX.createTriMeshGeometry(this._pxMesh, scaleX, scaleY, scaleZ, meshFlag);
1604
1706
  this._pxGeometry.delete();
1605
1707
  this._pxGeometry = newGeometry;
1606
1708
  this._pxShape.setGeometry(this._pxGeometry);
1607
1709
  };
1608
- _proto._releaseMesh = function _releaseMesh() {
1609
- if (this._pxMesh) {
1610
- this._pxMesh.release();
1611
- this._pxMesh = null;
1612
- }
1613
- if (this._pxGeometry) {
1614
- this._pxGeometry.delete();
1615
- this._pxGeometry = null;
1616
- }
1617
- };
1618
1710
  return PhysXMeshColliderShape;
1619
1711
  }(PhysXColliderShape);
1712
+ PhysXMeshColliderShape._tightBoundsFlag = 1 // eTIGHT_BOUNDS = 1 (1<<0)
1713
+ ;
1620
1714
 
1621
1715
  /**
1622
1716
  * Plane collider shape in PhysX.
@@ -1674,10 +1768,10 @@
1674
1768
  if (runtimeMode === void 0) runtimeMode = PhysXRuntimeMode.Auto;
1675
1769
  this._initializeState = 0;
1676
1770
  this._runTimeMode = runtimeMode;
1771
+ var _runtimeUrls_wasmSIMDModeUrl;
1772
+ this._wasmSIMDModeUrl = (_runtimeUrls_wasmSIMDModeUrl = runtimeUrls == null ? void 0 : runtimeUrls.wasmSIMDModeUrl) != null ? _runtimeUrls_wasmSIMDModeUrl : "https://mdn.alipayobjects.com/rms/afts/file/A*FHYHS4_ZL5UAAAAAQ4AAAAgAehQnAQ/physx.release.simd.js";
1677
1773
  var _runtimeUrls_wasmModeUrl;
1678
- this._wasmModeUrl = (_runtimeUrls_wasmModeUrl = runtimeUrls == null ? void 0 : runtimeUrls.wasmModeUrl) != null ? _runtimeUrls_wasmModeUrl : "https://mdn.alipayobjects.com/rms/afts/file/A*8pf0QJKeUXsAAAAASWAAAAgAehQnAQ/physx.release.js";
1679
- var _runtimeUrls_javaScriptModeUrl;
1680
- this._downgradeModeUrl = (_runtimeUrls_javaScriptModeUrl = runtimeUrls == null ? void 0 : runtimeUrls.javaScriptModeUrl) != null ? _runtimeUrls_javaScriptModeUrl : "https://mdn.alipayobjects.com/rms/afts/file/A*PLtBTLf8Sm0AAAAAgFAAAAgAehQnAQ/physx.release.downgrade.js";
1774
+ this._wasmModeUrl = (_runtimeUrls_wasmModeUrl = runtimeUrls == null ? void 0 : runtimeUrls.wasmModeUrl) != null ? _runtimeUrls_wasmModeUrl : "https://mdn.alipayobjects.com/rms/afts/file/A*2fv0RLMK1d0AAAAAQ4AAAAgAehQnAQ/physx.release.js";
1681
1775
  }
1682
1776
  var _proto = PhysXPhysics.prototype;
1683
1777
  /**
@@ -1691,6 +1785,7 @@
1691
1785
  } else if (this._initializeState === 1) {
1692
1786
  return this._initializePromise;
1693
1787
  }
1788
+ this._initializeState = 1;
1694
1789
  var runtimeMode = this._runTimeMode;
1695
1790
  var scriptPromise = new Promise(function(resolve, reject) {
1696
1791
  var script = document.createElement("script");
@@ -1699,30 +1794,18 @@
1699
1794
  script.onload = resolve;
1700
1795
  script.onerror = reject;
1701
1796
  if (runtimeMode == PhysXRuntimeMode.Auto) {
1702
- var supported = function() {
1703
- try {
1704
- if ((typeof WebAssembly === "undefined" ? "undefined" : _type_of(WebAssembly)) === "object" && typeof WebAssembly.instantiate === "function") {
1705
- var wasmModule = new WebAssembly.Module(Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00));
1706
- if (_instanceof(wasmModule, WebAssembly.Module)) return _instanceof(new WebAssembly.Instance(wasmModule), WebAssembly.Instance);
1707
- }
1708
- } catch (e) {}
1709
- return false;
1710
- }();
1711
- if (supported) {
1712
- runtimeMode = PhysXRuntimeMode.WebAssembly;
1713
- } else {
1714
- runtimeMode = PhysXRuntimeMode.JavaScript;
1715
- }
1797
+ runtimeMode = engine.SystemInfo._detectSIMDSupported() ? PhysXRuntimeMode.WebAssemblySIMD : PhysXRuntimeMode.WebAssembly;
1716
1798
  }
1717
- if (runtimeMode == PhysXRuntimeMode.JavaScript) {
1718
- script.src = _this._downgradeModeUrl;
1719
- } else if (runtimeMode == PhysXRuntimeMode.WebAssembly) {
1799
+ if (runtimeMode == PhysXRuntimeMode.WebAssemblySIMD) {
1800
+ script.src = _this._wasmSIMDModeUrl;
1801
+ } else {
1720
1802
  script.src = _this._wasmModeUrl;
1721
1803
  }
1722
1804
  });
1723
1805
  var initializePromise = new Promise(function(resolve, reject) {
1724
1806
  scriptPromise.then(function() {
1725
1807
  return window.PHYSX().then(function(PHYSX) {
1808
+ _this._runTimeMode = runtimeMode;
1726
1809
  _this._init(PHYSX);
1727
1810
  _this._initializeState = 2;
1728
1811
  _this._initializePromise = null;
@@ -1747,26 +1830,14 @@
1747
1830
  this._tolerancesScale.delete();
1748
1831
  };
1749
1832
  /**
1750
- * Set cooking parameters for mesh colliders.
1751
- * @param params - Cooking parameters
1752
- */ _proto.setCookingParams = function setCookingParams(params) {
1753
- var cp = this._pxCookingParams;
1754
- if (params.meshWeldTolerance !== undefined) {
1755
- cp.meshWeldTolerance = params.meshWeldTolerance;
1756
- }
1757
- if (params.meshPreprocessParams !== undefined) {
1758
- this._physX.setCookingMeshPreprocessParams(cp, params.meshPreprocessParams);
1759
- }
1760
- };
1761
- /**
1762
1833
  * {@inheritDoc IPhysics.createPhysicsManager }
1763
1834
  */ _proto.createPhysicsManager = function createPhysicsManager() {
1764
1835
  return new PhysXPhysicsManager();
1765
1836
  };
1766
1837
  /**
1767
1838
  * {@inheritDoc IPhysics.createPhysicsScene }
1768
- */ _proto.createPhysicsScene = function createPhysicsScene(physicsManager, onContactBegin, onContactEnd, onContactStay, onTriggerBegin, onTriggerEnd, onTriggerStay) {
1769
- var scene = new PhysXPhysicsScene(this, physicsManager, onContactBegin, onContactEnd, onContactStay, onTriggerBegin, onTriggerEnd, onTriggerStay);
1839
+ */ _proto.createPhysicsScene = function createPhysicsScene(physicsManager) {
1840
+ var scene = new PhysXPhysicsScene(this, physicsManager);
1770
1841
  return scene;
1771
1842
  };
1772
1843
  /**
@@ -1811,8 +1882,9 @@
1811
1882
  };
1812
1883
  /**
1813
1884
  * {@inheritDoc IPhysics.createMeshColliderShape }
1814
- */ _proto.createMeshColliderShape = function createMeshColliderShape(uniqueID, vertices, vertexCount, indices, isConvex, material) {
1815
- return new PhysXMeshColliderShape(this, uniqueID, vertices, vertexCount, indices, isConvex, material);
1885
+ */ _proto.createMeshColliderShape = function createMeshColliderShape(uniqueID, positions, indices, isConvex, material, cookingFlags) {
1886
+ var shape = new PhysXMeshColliderShape(this, uniqueID, positions, indices, isConvex, material, cookingFlags);
1887
+ return shape._pxShape ? shape : null;
1816
1888
  };
1817
1889
  /**
1818
1890
  * {@inheritDoc IPhysics.createFixedJoint }
@@ -1849,9 +1921,12 @@
1849
1921
  physX.PxInitExtensions(pxPhysics, null);
1850
1922
  // Initialize cooking for mesh colliders
1851
1923
  var cookingParams = new physX.PxCookingParams(tolerancesScale);
1852
- // Set default cooking params (WeldVertices + small tolerance)
1853
- physX.setCookingMeshPreprocessParams(cookingParams, 1); // WeldVertices = 1
1924
+ physX.setCookingMeshPreprocessParams(cookingParams, 1); // eWELD_VERTICES
1854
1925
  cookingParams.meshWeldTolerance = 0.001;
1926
+ // BVH34 midphase requires SSE2; SIMD WASM provides SSE2 via WASM SIMD
1927
+ if (this._runTimeMode === PhysXRuntimeMode.WebAssemblySIMD) {
1928
+ physX.setCookingMidphaseType(cookingParams, 1); // eBVH34
1929
+ }
1855
1930
  var pxCooking = physX.PxCreateCooking(version, pxFoundation, cookingParams);
1856
1931
  this._physX = physX;
1857
1932
  this._pxFoundation = pxFoundation;
@@ -1865,36 +1940,10 @@
1865
1940
  return PhysXPhysics;
1866
1941
  }();
1867
1942
 
1868
- /**
1869
- * Mesh preprocessing flags for cooking options.
1870
- * @remarks These flags control how the mesh is preprocessed during cooking.
1871
- */ var MeshPreprocessingFlag = /*#__PURE__*/ function(MeshPreprocessingFlag) {
1872
- /**
1873
- * When set, mesh welding is performed.
1874
- * Vertices within the meshWeldTolerance distance will be merged.
1875
- * Clean mesh must be enabled for this to work.
1876
- */ MeshPreprocessingFlag[MeshPreprocessingFlag["WeldVertices"] = 1] = "WeldVertices";
1877
- /**
1878
- * When set, mesh cleaning is disabled.
1879
- * This makes cooking faster but requires the input mesh to be valid.
1880
- * When clean mesh is disabled, vertex welding is also disabled.
1881
- */ MeshPreprocessingFlag[MeshPreprocessingFlag["DisableCleanMesh"] = 2] = "DisableCleanMesh";
1882
- /**
1883
- * When set, active edges computation is disabled.
1884
- * This makes cooking faster but may slow down contact generation.
1885
- */ MeshPreprocessingFlag[MeshPreprocessingFlag["DisableActiveEdgesPrecompute"] = 4] = "DisableActiveEdgesPrecompute";
1886
- /**
1887
- * When set, 32-bit indices will always be used regardless of triangle count.
1888
- * By default, 16-bit indices are used for meshes with <= 65535 triangles.
1889
- */ MeshPreprocessingFlag[MeshPreprocessingFlag["Force32BitIndices"] = 8] = "Force32BitIndices";
1890
- return MeshPreprocessingFlag;
1891
- }({});
1892
-
1893
1943
  //@ts-ignore
1894
- var version = "2.0.0-alpha.2";
1944
+ var version = "2.0.0-alpha.21";
1895
1945
  console.log("Galacean Engine Physics PhysX Version: " + version);
1896
1946
 
1897
- exports.MeshPreprocessingFlag = MeshPreprocessingFlag;
1898
1947
  exports.PhysXPhysics = PhysXPhysics;
1899
1948
  exports.PhysXRuntimeMode = PhysXRuntimeMode;
1900
1949
  exports.version = version;