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

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