@galacean/engine-physics-physx 2.0.0-alpha.17 → 2.0.0-alpha.19

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
@@ -821,44 +821,64 @@
821
821
  return PhysXPhysicsMaterial;
822
822
  }();
823
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
+
824
842
  /**
825
843
  * A manager is a collection of colliders and constraints which can interact.
826
844
  */ var PhysXPhysicsScene = /*#__PURE__*/ function() {
827
- function PhysXPhysicsScene(physXPhysics, physicsManager, onContactEnter, onContactExit, onContactStay, onTriggerEnter, onTriggerExit, onTriggerStay) {
845
+ function PhysXPhysicsScene(physXPhysics, physicsManager) {
828
846
  var _this = this;
829
847
  /** @internal */ this._pxControllerManager = null;
830
848
  // Cached geometry objects for reuse
831
849
  this._boxGeometry = null;
832
850
  this._sphereGeometry = null;
833
851
  this._capsuleGeometry = null;
834
- this._currentEvents = new engine.DisorderedArray();
835
- 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 = [];
836
862
  this._physXPhysics = physXPhysics;
837
863
  this._physXManager = physicsManager;
838
864
  var physX = physXPhysics._physX;
839
865
  this._pxRaycastHit = new physX.PxRaycastHit();
840
866
  this._pxFilterData = new physX.PxQueryFilterData();
841
867
  this._pxFilterData.flags = new physX.PxQueryFlags(1 | 2 | 4);
842
- this._onContactEnter = onContactEnter;
843
- this._onContactExit = onContactExit;
844
- this._onContactStay = onContactStay;
845
- this._onTriggerEnter = onTriggerEnter;
846
- this._onTriggerExit = onTriggerExit;
847
- this._onTriggerStay = onTriggerStay;
848
868
  var triggerCallback = {
849
869
  onContactBegin: function(collision) {
850
- _this._onContactEnter(collision);
870
+ _this._bufferContactEvent(collision, 0);
851
871
  },
852
872
  onContactEnd: function(collision) {
853
- _this._onContactExit(collision);
873
+ _this._bufferContactEvent(collision, 2);
854
874
  },
855
875
  onContactPersist: function(collision) {
856
- _this._onContactStay(collision);
876
+ _this._bufferContactEvent(collision, 1);
857
877
  },
858
878
  onTriggerBegin: function(index1, index2) {
859
879
  var event = index1 < index2 ? _this._getTrigger(index1, index2) : _this._getTrigger(index2, index1);
860
880
  event.state = 0;
861
- _this._currentEvents.add(event);
881
+ _this._activeTriggers.add(event);
862
882
  },
863
883
  onTriggerEnd: function(index1, index2) {
864
884
  var event;
@@ -936,9 +956,31 @@
936
956
  /**
937
957
  * {@inheritDoc IPhysicsScene.update }
938
958
  */ _proto.update = function update(elapsedTime) {
959
+ this._contactEventCount = 0;
939
960
  this._simulate(elapsedTime);
940
961
  this._fetchResults();
941
- 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;
942
984
  };
943
985
  /**
944
986
  * {@inheritDoc IPhysicsScene.raycast }
@@ -1053,6 +1095,11 @@
1053
1095
  return this._overlapMultiple(this._capsuleGeometry, pose, onOverlap);
1054
1096
  };
1055
1097
  /**
1098
+ * {@inheritDoc IPhysicsScene.gc }
1099
+ */ _proto.gc = function gc() {
1100
+ this._contactEvents.length = this._contactEventCount;
1101
+ };
1102
+ /**
1056
1103
  * {@inheritDoc IPhysicsScene.destroy }
1057
1104
  */ _proto.destroy = function destroy() {
1058
1105
  var _this__boxGeometry, _this__sphereGeometry, _this__capsuleGeometry, // Need to release the controller manager before release the scene.
@@ -1084,15 +1131,15 @@
1084
1131
  /**
1085
1132
  * @internal
1086
1133
  */ _proto._removeColliderShape = function _removeColliderShape(id) {
1087
- var _this = this, eventPool = _this._eventPool, currentEvents = _this._currentEvents;
1134
+ var _this = this, triggerEventPool = _this._triggerEventPool, activeTriggers = _this._activeTriggers;
1088
1135
  var _this__physXManager = this._physXManager, eventMap = _this__physXManager._eventMap;
1089
- currentEvents.forEach(function(event, i) {
1136
+ activeTriggers.forEach(function(event, i) {
1090
1137
  if (event.index1 == id) {
1091
- currentEvents.deleteByIndex(i);
1092
- eventPool.push(event);
1138
+ activeTriggers.deleteByIndex(i);
1139
+ triggerEventPool.push(event);
1093
1140
  } else if (event.index2 == id) {
1094
- currentEvents.deleteByIndex(i);
1095
- eventPool.push(event);
1141
+ activeTriggers.deleteByIndex(i);
1142
+ triggerEventPool.push(event);
1096
1143
  // If the shape is big index, should clear from the small index shape subMap
1097
1144
  eventMap[event.index1][id] = undefined;
1098
1145
  }
@@ -1154,8 +1201,8 @@
1154
1201
  };
1155
1202
  _proto._getTrigger = function _getTrigger(index1, index2) {
1156
1203
  var event;
1157
- if (this._eventPool.length) {
1158
- event = this._eventPool.pop();
1204
+ if (this._triggerEventPool.length) {
1205
+ event = this._triggerEventPool.pop();
1159
1206
  event.index1 = index1;
1160
1207
  event.index2 = index2;
1161
1208
  } else {
@@ -1164,21 +1211,27 @@
1164
1211
  this._physXManager._eventMap[index1][index2] = event;
1165
1212
  return event;
1166
1213
  };
1167
- _proto._fireEvent = function _fireEvent() {
1168
- var _this = this;
1169
- var _this1 = this, eventPool = _this1._eventPool, currentEvents = _this1._currentEvents;
1170
- currentEvents.forEach(function(event, i) {
1171
- if (event.state == 0) {
1172
- _this._onTriggerEnter(event.index1, event.index2);
1173
- event.state = 1;
1174
- } else if (event.state == 1) {
1175
- _this._onTriggerStay(event.index1, event.index2);
1176
- } else if (event.state == 2) {
1177
- currentEvents.deleteByIndex(i);
1178
- _this._onTriggerExit(event.index1, event.index2);
1179
- eventPool.push(event);
1180
- }
1181
- });
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
+ }
1182
1235
  };
1183
1236
  return PhysXPhysicsScene;
1184
1237
  }();
@@ -1196,6 +1249,49 @@
1196
1249
  this.index1 = index1;
1197
1250
  this.index2 = index2;
1198
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
+ }();
1199
1295
 
1200
1296
  /**
1201
1297
  * A static collider component that will not move.
@@ -1740,8 +1836,8 @@
1740
1836
  };
1741
1837
  /**
1742
1838
  * {@inheritDoc IPhysics.createPhysicsScene }
1743
- */ _proto.createPhysicsScene = function createPhysicsScene(physicsManager, onContactBegin, onContactEnd, onContactStay, onTriggerBegin, onTriggerEnd, onTriggerStay) {
1744
- 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);
1745
1841
  return scene;
1746
1842
  };
1747
1843
  /**
@@ -1845,7 +1941,7 @@
1845
1941
  }();
1846
1942
 
1847
1943
  //@ts-ignore
1848
- var version = "2.0.0-alpha.17";
1944
+ var version = "2.0.0-alpha.19";
1849
1945
  console.log("Galacean Engine Physics PhysX Version: " + version);
1850
1946
 
1851
1947
  exports.PhysXPhysics = PhysXPhysics;