y-up-r-strategy.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { Strategy } from "./strategy.js";
  2. /**
  3. * YUp策略类
  4. */
  5. export class YUpRStrategy extends Strategy {
  6. /**
  7. * 原点姿态
  8. * @type {Object}
  9. */
  10. originPointPose = null;
  11. /**
  12. * 统一坐标系统
  13. * @param {Object} model - 模型
  14. * @param {Object} worldPose - 世界姿态
  15. */
  16. unifiedCoordinateSystem(model) {
  17. console.log("Y-up: beforeAddApplyMatrix");
  18. return model
  19. }
  20. /**
  21. * 设置本地原点姿态
  22. * @param {Object} worldPose - 世界姿态
  23. */
  24. setLocalOriginPose(worldPose) {
  25. this.originPointPose = worldPose;
  26. console.log("Y-up: convertLocalToWorld");
  27. }
  28. /**
  29. * 转换本地到世界
  30. * @param {Object} model - 模型
  31. * @param {Object} localPosition - 本地位置
  32. */
  33. convertLocalToWorld(localPosition) {
  34. var quaternion = new THREE.Quaternion(this.originPointPose.worldQuaternion.x, this.originPointPose.worldQuaternion.y, this.originPointPose.worldQuaternion.z, this.originPointPose.worldQuaternion.w);
  35. var rotationMatrix = new THREE.Matrix4().makeRotationFromQuaternion(quaternion);
  36. var translationMatrix = new THREE.Matrix4().makeTranslation(this.originPointPose.worldPosition.x, this.originPointPose.worldPosition.y, this.originPointPose.worldPosition.z);
  37. var transformationMatrix = new THREE.Matrix4().multiplyMatrices(translationMatrix, rotationMatrix);
  38. // 用转换矩阵将本地坐标转为世界坐标
  39. let worldPosition = localPosition.applyMatrix4(transformationMatrix);
  40. // console.log('======打印当前的worldPosition======', worldPosition);
  41. return worldPosition
  42. }
  43. }