pivotTools.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { Vector3, Matrix } from "../Maths/math.vector.js";
  2. /**
  3. * Class containing a set of static utilities functions for managing Pivots
  4. * @internal
  5. */
  6. export class PivotTools {
  7. /**
  8. * @internal
  9. */
  10. static _RemoveAndStorePivotPoint(mesh) {
  11. if (mesh && PivotTools._PivotCached === 0) {
  12. // Save old pivot and set pivot to 0,0,0
  13. mesh.getPivotPointToRef(PivotTools._OldPivotPoint);
  14. PivotTools._PivotPostMultiplyPivotMatrix = mesh._postMultiplyPivotMatrix;
  15. if (!PivotTools._OldPivotPoint.equalsToFloats(0, 0, 0)) {
  16. mesh.setPivotMatrix(Matrix.IdentityReadOnly);
  17. PivotTools._OldPivotPoint.subtractToRef(mesh.getPivotPoint(), PivotTools._PivotTranslation);
  18. PivotTools._PivotTmpVector.copyFromFloats(1, 1, 1);
  19. PivotTools._PivotTmpVector.subtractInPlace(mesh.scaling);
  20. PivotTools._PivotTmpVector.multiplyInPlace(PivotTools._PivotTranslation);
  21. mesh.position.addInPlace(PivotTools._PivotTmpVector);
  22. }
  23. }
  24. PivotTools._PivotCached++;
  25. }
  26. /**
  27. * @internal
  28. */
  29. static _RestorePivotPoint(mesh) {
  30. if (mesh && !PivotTools._OldPivotPoint.equalsToFloats(0, 0, 0) && PivotTools._PivotCached === 1) {
  31. mesh.setPivotPoint(PivotTools._OldPivotPoint);
  32. mesh._postMultiplyPivotMatrix = PivotTools._PivotPostMultiplyPivotMatrix;
  33. PivotTools._PivotTmpVector.copyFromFloats(1, 1, 1);
  34. PivotTools._PivotTmpVector.subtractInPlace(mesh.scaling);
  35. PivotTools._PivotTmpVector.multiplyInPlace(PivotTools._PivotTranslation);
  36. mesh.position.subtractInPlace(PivotTools._PivotTmpVector);
  37. }
  38. this._PivotCached--;
  39. }
  40. }
  41. // Stores the state of the pivot cache (_oldPivotPoint, _pivotTranslation)
  42. // store/remove pivot point should only be applied during their outermost calls
  43. PivotTools._PivotCached = 0;
  44. PivotTools._OldPivotPoint = new Vector3();
  45. PivotTools._PivotTranslation = new Vector3();
  46. PivotTools._PivotTmpVector = new Vector3();
  47. PivotTools._PivotPostMultiplyPivotMatrix = false;
  48. //# sourceMappingURL=pivotTools.js.map