pivotTools.ts 2.0 KB

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