@galacean/engine-physics-physx 1.6.12 → 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/module.js CHANGED
@@ -32,6 +32,14 @@ function _inherits(subClass, superClass) {
32
32
  if (superClass) _set_prototype_of(subClass, superClass);
33
33
  }
34
34
 
35
+ /**
36
+ * Flags which affect the behavior of Shapes.
37
+ */ var ShapeFlag = /*#__PURE__*/ function(ShapeFlag) {
38
+ /** The shape will partake in collision in the physical simulation. */ ShapeFlag[ShapeFlag["SIMULATION_SHAPE"] = 1] = "SIMULATION_SHAPE";
39
+ /** The shape will partake in scene queries (ray casts, overlap tests, sweeps, ...). */ ShapeFlag[ShapeFlag["SCENE_QUERY_SHAPE"] = 2] = "SCENE_QUERY_SHAPE";
40
+ /** The shape is a trigger which can send reports whenever other shapes enter/leave its volume. */ ShapeFlag[ShapeFlag["TRIGGER_SHAPE"] = 4] = "TRIGGER_SHAPE";
41
+ return ShapeFlag;
42
+ }({});
35
43
  /**
36
44
  * Abstract class for collider shapes.
37
45
  */ var PhysXColliderShape = /*#__PURE__*/ function() {
@@ -118,8 +126,9 @@ function _inherits(subClass, superClass) {
118
126
  /**
119
127
  * {@inheritDoc IColliderShape.destroy }
120
128
  */ _proto.destroy = function destroy() {
129
+ var _this__pxGeometry;
121
130
  this._pxShape.release();
122
- this._pxGeometry.delete();
131
+ (_this__pxGeometry = this._pxGeometry) == null ? void 0 : _this__pxGeometry.delete();
123
132
  };
124
133
  /**
125
134
  * @internal
@@ -673,13 +682,17 @@ PhysXCollider._tempTransform = {
673
682
  switch(value){
674
683
  case 1:
675
684
  this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eENABLE_CCD, true);
685
+ this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eENABLE_CCD_FRICTION, false);
686
+ this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eENABLE_SPECULATIVE_CCD, false);
676
687
  break;
677
688
  case 2:
678
- this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eENABLE_CCD, false);
689
+ this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eENABLE_CCD, true);
679
690
  this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eENABLE_CCD_FRICTION, true);
691
+ this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eENABLE_SPECULATIVE_CCD, false);
680
692
  break;
681
693
  case 3:
682
694
  this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eENABLE_CCD, false);
695
+ this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eENABLE_CCD_FRICTION, false);
683
696
  this._pxActor.setRigidBodyFlag(physX.PxRigidBodyFlag.eENABLE_SPECULATIVE_CCD, true);
684
697
  break;
685
698
  case 0:
@@ -1437,6 +1450,170 @@ PhysXHingeJoint._xAxis = new Vector3(1, 0, 0);
1437
1450
  return PhysXSpringJoint;
1438
1451
  }(PhysXJoint);
1439
1452
 
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;
1455
+ /**
1456
+ * Mesh collider shape in PhysX.
1457
+ */ var PhysXMeshColliderShape = /*#__PURE__*/ function(PhysXColliderShape) {
1458
+ _inherits(PhysXMeshColliderShape, PhysXColliderShape);
1459
+ function PhysXMeshColliderShape(physXPhysics, uniqueID, vertices, vertexCount, indices, isConvex, material) {
1460
+ var _this;
1461
+ _this = PhysXColliderShape.call(this, physXPhysics) || this, _this._pxMesh = null, _this._doubleSided = false, _this._tightBounds = true;
1462
+ _this._isConvex = isConvex;
1463
+ _this._vertices = vertices;
1464
+ _this._vertexCount = vertexCount;
1465
+ _this._indices = indices;
1466
+ _this._createMeshAndShape(material, uniqueID);
1467
+ _this._setLocalPose();
1468
+ return _this;
1469
+ }
1470
+ var _proto = PhysXMeshColliderShape.prototype;
1471
+ /**
1472
+ * {@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)
1478
+ this._pxMesh = null;
1479
+ this._pxGeometry = null;
1480
+ 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();
1508
+ }
1509
+ };
1510
+ /**
1511
+ * {@inheritDoc IColliderShape.setWorldScale }
1512
+ */ _proto.setWorldScale = function setWorldScale(scale) {
1513
+ PhysXColliderShape.prototype.setWorldScale.call(this, scale);
1514
+ this._updateGeometry();
1515
+ };
1516
+ /**
1517
+ * {@inheritDoc IColliderShape.destroy }
1518
+ */ _proto.destroy = function destroy() {
1519
+ this._releaseMesh();
1520
+ PhysXColliderShape.prototype.destroy.call(this);
1521
+ };
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
1528
+ 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);
1530
+ } 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);
1561
+ }
1562
+ } finally{
1563
+ physX._free(verticesPtr);
1564
+ if (indicesPtr) {
1565
+ physX._free(indicesPtr);
1566
+ }
1567
+ // Release JS memory after copying to WASM
1568
+ this._vertices = null;
1569
+ this._indices = null;
1570
+ }
1571
+ };
1572
+ _proto._allocateVertices = function _allocateVertices() {
1573
+ 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);
1577
+ return ptr;
1578
+ };
1579
+ _proto._allocateIndices = function _allocateIndices() {
1580
+ 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
+ };
1596
+ };
1597
+ _proto._updateGeometry = function _updateGeometry() {
1598
+ 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);
1600
+ this._pxGeometry.delete();
1601
+ this._pxGeometry = newGeometry;
1602
+ this._pxShape.setGeometry(this._pxGeometry);
1603
+ };
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
+ return PhysXMeshColliderShape;
1615
+ }(PhysXColliderShape);
1616
+
1440
1617
  /**
1441
1618
  * Plane collider shape in PhysX.
1442
1619
  */ var PhysXPlaneColliderShape = /*#__PURE__*/ function(PhysXColliderShape1) {
@@ -1494,9 +1671,9 @@ PhysXHingeJoint._xAxis = new Vector3(1, 0, 0);
1494
1671
  this._initializeState = 0;
1495
1672
  this._runTimeMode = runtimeMode;
1496
1673
  var _runtimeUrls_wasmModeUrl;
1497
- 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";
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";
1498
1675
  var _runtimeUrls_javaScriptModeUrl;
1499
- 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";
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";
1500
1677
  }
1501
1678
  var _proto = PhysXPhysics.prototype;
1502
1679
  /**
@@ -1556,6 +1733,8 @@ PhysXHingeJoint._xAxis = new Vector3(1, 0, 0);
1556
1733
  /**
1557
1734
  * Destroy PhysXPhysics.
1558
1735
  */ _proto.destroy = function destroy() {
1736
+ this._pxCooking.release();
1737
+ this._pxCookingParams.delete();
1559
1738
  this._physX.PxCloseExtensions();
1560
1739
  this._pxPhysics.release();
1561
1740
  this._pxFoundation.release();
@@ -1564,6 +1743,18 @@ PhysXHingeJoint._xAxis = new Vector3(1, 0, 0);
1564
1743
  this._tolerancesScale.delete();
1565
1744
  };
1566
1745
  /**
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
+ /**
1567
1758
  * {@inheritDoc IPhysics.createPhysicsManager }
1568
1759
  */ _proto.createPhysicsManager = function createPhysicsManager() {
1569
1760
  return new PhysXPhysicsManager();
@@ -1615,6 +1806,11 @@ PhysXHingeJoint._xAxis = new Vector3(1, 0, 0);
1615
1806
  return new PhysXCapsuleColliderShape(this, uniqueID, radius, height, material);
1616
1807
  };
1617
1808
  /**
1809
+ * {@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);
1812
+ };
1813
+ /**
1618
1814
  * {@inheritDoc IPhysics.createFixedJoint }
1619
1815
  */ _proto.createFixedJoint = function createFixedJoint(collider) {
1620
1816
  return new PhysXFixedJoint(this, collider);
@@ -1647,9 +1843,17 @@ PhysXHingeJoint._xAxis = new Vector3(1, 0, 0);
1647
1843
  var tolerancesScale = new physX.PxTolerancesScale();
1648
1844
  var pxPhysics = physX.PxCreatePhysics(version, pxFoundation, tolerancesScale, false, null);
1649
1845
  physX.PxInitExtensions(pxPhysics, null);
1846
+ // Initialize cooking for mesh colliders
1847
+ var cookingParams = new physX.PxCookingParams(tolerancesScale);
1848
+ // Set default cooking params (WeldVertices + small tolerance)
1849
+ physX.setCookingMeshPreprocessParams(cookingParams, 1); // WeldVertices = 1
1850
+ cookingParams.meshWeldTolerance = 0.001;
1851
+ var pxCooking = physX.PxCreateCooking(version, pxFoundation, cookingParams);
1650
1852
  this._physX = physX;
1651
1853
  this._pxFoundation = pxFoundation;
1652
1854
  this._pxPhysics = pxPhysics;
1855
+ this._pxCooking = pxCooking;
1856
+ this._pxCookingParams = cookingParams;
1653
1857
  this._defaultErrorCallback = defaultErrorCallback;
1654
1858
  this._allocator = allocator;
1655
1859
  this._tolerancesScale = tolerancesScale;
@@ -1657,9 +1861,34 @@ PhysXHingeJoint._xAxis = new Vector3(1, 0, 0);
1657
1861
  return PhysXPhysics;
1658
1862
  }();
1659
1863
 
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
+
1660
1889
  //@ts-ignore
1661
- var version = "1.6.12";
1890
+ var version = "2.0.0-alpha.0";
1662
1891
  console.log("Galacean Engine Physics PhysX Version: " + version);
1663
1892
 
1664
- export { PhysXPhysics, PhysXRuntimeMode, version };
1893
+ export { MeshPreprocessingFlag, PhysXPhysics, PhysXRuntimeMode, version };
1665
1894
  //# sourceMappingURL=module.js.map