@galacean/engine-physics-physx 1.6.13 → 2.0.0-alpha.0

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
@@ -36,6 +36,14 @@
36
36
  if (superClass) _set_prototype_of(subClass, superClass);
37
37
  }
38
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
+ }({});
39
47
  /**
40
48
  * Abstract class for collider shapes.
41
49
  */ var PhysXColliderShape = /*#__PURE__*/ function() {
@@ -122,8 +130,9 @@
122
130
  /**
123
131
  * {@inheritDoc IColliderShape.destroy }
124
132
  */ _proto.destroy = function destroy() {
133
+ var _this__pxGeometry;
125
134
  this._pxShape.release();
126
- this._pxGeometry.delete();
135
+ (_this__pxGeometry = this._pxGeometry) == null ? void 0 : _this__pxGeometry.delete();
127
136
  };
128
137
  /**
129
138
  * @internal
@@ -677,13 +686,17 @@
677
686
  switch(value){
678
687
  case 1:
679
688
  this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eENABLE_CCD, true);
689
+ this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eENABLE_CCD_FRICTION, false);
690
+ this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eENABLE_SPECULATIVE_CCD, false);
680
691
  break;
681
692
  case 2:
682
- this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eENABLE_CCD, false);
693
+ this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eENABLE_CCD, true);
683
694
  this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eENABLE_CCD_FRICTION, true);
695
+ this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eENABLE_SPECULATIVE_CCD, false);
684
696
  break;
685
697
  case 3:
686
698
  this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eENABLE_CCD, false);
699
+ this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eENABLE_CCD_FRICTION, false);
687
700
  this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eENABLE_SPECULATIVE_CCD, true);
688
701
  break;
689
702
  case 0:
@@ -1441,6 +1454,170 @@
1441
1454
  return PhysXSpringJoint;
1442
1455
  }(PhysXJoint);
1443
1456
 
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;
1459
+ /**
1460
+ * Mesh collider shape in PhysX.
1461
+ */ var PhysXMeshColliderShape = /*#__PURE__*/ function(PhysXColliderShape) {
1462
+ _inherits(PhysXMeshColliderShape, PhysXColliderShape);
1463
+ function PhysXMeshColliderShape(physXPhysics, uniqueID, vertices, vertexCount, indices, isConvex, material) {
1464
+ var _this;
1465
+ _this = PhysXColliderShape.call(this, physXPhysics) || this, _this._pxMesh = null, _this._doubleSided = false, _this._tightBounds = true;
1466
+ _this._isConvex = isConvex;
1467
+ _this._vertices = vertices;
1468
+ _this._vertexCount = vertexCount;
1469
+ _this._indices = indices;
1470
+ _this._createMeshAndShape(material, uniqueID);
1471
+ _this._setLocalPose();
1472
+ return _this;
1473
+ }
1474
+ var _proto = PhysXMeshColliderShape.prototype;
1475
+ /**
1476
+ * {@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)
1482
+ this._pxMesh = null;
1483
+ this._pxGeometry = null;
1484
+ 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();
1512
+ }
1513
+ };
1514
+ /**
1515
+ * {@inheritDoc IColliderShape.setWorldScale }
1516
+ */ _proto.setWorldScale = function setWorldScale(scale) {
1517
+ PhysXColliderShape.prototype.setWorldScale.call(this, scale);
1518
+ this._updateGeometry();
1519
+ };
1520
+ /**
1521
+ * {@inheritDoc IColliderShape.destroy }
1522
+ */ _proto.destroy = function destroy() {
1523
+ this._releaseMesh();
1524
+ PhysXColliderShape.prototype.destroy.call(this);
1525
+ };
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
1532
+ 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);
1534
+ } 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);
1565
+ }
1566
+ } finally{
1567
+ physX._free(verticesPtr);
1568
+ if (indicesPtr) {
1569
+ physX._free(indicesPtr);
1570
+ }
1571
+ // Release JS memory after copying to WASM
1572
+ this._vertices = null;
1573
+ this._indices = null;
1574
+ }
1575
+ };
1576
+ _proto._allocateVertices = function _allocateVertices() {
1577
+ 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);
1581
+ return ptr;
1582
+ };
1583
+ _proto._allocateIndices = function _allocateIndices() {
1584
+ 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
+ };
1600
+ };
1601
+ _proto._updateGeometry = function _updateGeometry() {
1602
+ 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);
1604
+ this._pxGeometry.delete();
1605
+ this._pxGeometry = newGeometry;
1606
+ this._pxShape.setGeometry(this._pxGeometry);
1607
+ };
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
+ return PhysXMeshColliderShape;
1619
+ }(PhysXColliderShape);
1620
+
1444
1621
  /**
1445
1622
  * Plane collider shape in PhysX.
1446
1623
  */ var PhysXPlaneColliderShape = /*#__PURE__*/ function(PhysXColliderShape1) {
@@ -1498,9 +1675,9 @@
1498
1675
  this._initializeState = 0;
1499
1676
  this._runTimeMode = runtimeMode;
1500
1677
  var _runtimeUrls_wasmModeUrl;
1501
- this._wasmModeUrl = (_runtimeUrls_wasmModeUrl = runtimeUrls == null ? void 0 : runtimeUrls.wasmModeUrl) != null ? _runtimeUrls_wasmModeUrl : "https://mdn.alipayobjects.com/rms/afts/file/A*m04iQojeKRgAAAAASWAAAAgAehQnAQ/physx.release.js";
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";
1502
1679
  var _runtimeUrls_javaScriptModeUrl;
1503
- this._downgradeModeUrl = (_runtimeUrls_javaScriptModeUrl = runtimeUrls == null ? void 0 : runtimeUrls.javaScriptModeUrl) != null ? _runtimeUrls_javaScriptModeUrl : "https://mdn.alipayobjects.com/rms/afts/file/A*13gEToqpJWcAAAAAgEAAAAgAehQnAQ/physx.release.downgrade.js";
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";
1504
1681
  }
1505
1682
  var _proto = PhysXPhysics.prototype;
1506
1683
  /**
@@ -1560,6 +1737,8 @@
1560
1737
  /**
1561
1738
  * Destroy PhysXPhysics.
1562
1739
  */ _proto.destroy = function destroy() {
1740
+ this._pxCooking.release();
1741
+ this._pxCookingParams.delete();
1563
1742
  this._physX.PxCloseExtensions();
1564
1743
  this._pxPhysics.release();
1565
1744
  this._pxFoundation.release();
@@ -1568,6 +1747,18 @@
1568
1747
  this._tolerancesScale.delete();
1569
1748
  };
1570
1749
  /**
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
+ /**
1571
1762
  * {@inheritDoc IPhysics.createPhysicsManager }
1572
1763
  */ _proto.createPhysicsManager = function createPhysicsManager() {
1573
1764
  return new PhysXPhysicsManager();
@@ -1619,6 +1810,11 @@
1619
1810
  return new PhysXCapsuleColliderShape(this, uniqueID, radius, height, material);
1620
1811
  };
1621
1812
  /**
1813
+ * {@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);
1816
+ };
1817
+ /**
1622
1818
  * {@inheritDoc IPhysics.createFixedJoint }
1623
1819
  */ _proto.createFixedJoint = function createFixedJoint(collider) {
1624
1820
  return new PhysXFixedJoint(this, collider);
@@ -1651,9 +1847,17 @@
1651
1847
  var tolerancesScale = new physX.PxTolerancesScale();
1652
1848
  var pxPhysics = physX.PxCreatePhysics(version, pxFoundation, tolerancesScale, false, null);
1653
1849
  physX.PxInitExtensions(pxPhysics, null);
1850
+ // Initialize cooking for mesh colliders
1851
+ var cookingParams = new physX.PxCookingParams(tolerancesScale);
1852
+ // Set default cooking params (WeldVertices + small tolerance)
1853
+ physX.setCookingMeshPreprocessParams(cookingParams, 1); // WeldVertices = 1
1854
+ cookingParams.meshWeldTolerance = 0.001;
1855
+ var pxCooking = physX.PxCreateCooking(version, pxFoundation, cookingParams);
1654
1856
  this._physX = physX;
1655
1857
  this._pxFoundation = pxFoundation;
1656
1858
  this._pxPhysics = pxPhysics;
1859
+ this._pxCooking = pxCooking;
1860
+ this._pxCookingParams = cookingParams;
1657
1861
  this._defaultErrorCallback = defaultErrorCallback;
1658
1862
  this._allocator = allocator;
1659
1863
  this._tolerancesScale = tolerancesScale;
@@ -1661,10 +1865,36 @@
1661
1865
  return PhysXPhysics;
1662
1866
  }();
1663
1867
 
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
+
1664
1893
  //@ts-ignore
1665
- var version = "1.6.13";
1894
+ var version = "2.0.0-alpha.0";
1666
1895
  console.log("Galacean Engine Physics PhysX Version: " + version);
1667
1896
 
1897
+ exports.MeshPreprocessingFlag = MeshPreprocessingFlag;
1668
1898
  exports.PhysXPhysics = PhysXPhysics;
1669
1899
  exports.PhysXRuntimeMode = PhysXRuntimeMode;
1670
1900
  exports.version = version;