c5f5f4202396229962c244c882feb56921b40cd5.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. System.register(["__unresolved_0", "cc", "__unresolved_1"], function (_export, _context) {
  2. "use strict";
  3. var _reporterNs, _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, Quat, toRadian, Vec3, Vec3Util, RotateUtil, _crd;
  4. function _reportPossibleCrUseOfVec3Util(extras) {
  5. _reporterNs.report("Vec3Util", "./Vec3Util", _context.meta, extras);
  6. }
  7. _export("RotateUtil", void 0);
  8. return {
  9. setters: [function (_unresolved_) {
  10. _reporterNs = _unresolved_;
  11. }, function (_cc) {
  12. _cclegacy = _cc.cclegacy;
  13. __checkObsolete__ = _cc.__checkObsolete__;
  14. __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
  15. Quat = _cc.Quat;
  16. toRadian = _cc.toRadian;
  17. Vec3 = _cc.Vec3;
  18. }, function (_unresolved_2) {
  19. Vec3Util = _unresolved_2.Vec3Util;
  20. }],
  21. execute: function () {
  22. _crd = true;
  23. _cclegacy._RF.push({}, "103324kr75Hi5d7RZCcRt3P", "RotateUtil", undefined);
  24. /*
  25. * @Author: dgflash
  26. * @Date: 2022-07-26 15:29:57
  27. * @LastEditors: dgflash
  28. * @LastEditTime: 2022-09-02 12:08:28
  29. */
  30. __checkObsolete__(['Node', 'Quat', 'toRadian', 'Vec3']);
  31. /** 旋转工具 */
  32. _export("RotateUtil", RotateUtil = class RotateUtil {
  33. /**
  34. * 自由旋转
  35. * @param target 旋转目标
  36. * @param axis 围绕旋转的轴
  37. * @param rad 旋转弧度
  38. */
  39. static rotateAround(target, axis, rad) {
  40. const quat = new Quat();
  41. Quat.rotateAround(quat, target.getRotation(), axis.normalize(), rad);
  42. target.setRotation(quat);
  43. }
  44. /**
  45. * 参考瞄准目标,使当前物体围绕瞄准目标旋转
  46. * 1、先通过弧度计算旋转四元数
  47. * 2、通过旋转中心点或当前目标点向量相减计算出移动方向
  48. * 3、计算起始向量旋转后的向量
  49. * 4、计算旋转后的坐标点
  50. * @param lookAt 瞄准目标
  51. * @param target 旋转目标
  52. * @param axis 围绕旋转的轴(例:Vec3.UP为Y轴)
  53. * @param rad 旋转弧度(例:delta.x * 1e-2)
  54. */
  55. static rotateAroundTarget(lookAt, target, axis, rad) {
  56. // 计算坐标
  57. const point_lookAt = lookAt.worldPosition; // 锚点坐标
  58. const point_target = target.worldPosition; // 目标坐标
  59. const quat = new Quat();
  60. const vec3 = new Vec3(); // 算出坐标点的旋转四元数
  61. Quat.fromAxisAngle(quat, axis, rad); // 计算旋转点和现有点的向量
  62. Vec3.subtract(vec3, point_target, point_lookAt); // 计算将向量做旋转操作后的向量
  63. Vec3.transformQuat(vec3, vec3, quat); // 计算目标旋转后的点
  64. Vec3.add(vec3, point_lookAt, vec3);
  65. target.setWorldPosition(vec3); // 计算目标朝向瞄准点
  66. Quat.rotateAround(quat, target.worldRotation, axis, rad);
  67. Quat.normalize(quat, quat);
  68. target.setWorldRotation(quat);
  69. }
  70. /**
  71. * 获取心半径边上的位置
  72. * @param center 圆心
  73. * @param radius 半径
  74. * @param angle 角度
  75. */
  76. static circularEdgePosition(center, radius, angle) {
  77. const edge = (_crd && Vec3Util === void 0 ? (_reportPossibleCrUseOfVec3Util({
  78. error: Error()
  79. }), Vec3Util) : Vec3Util).z.multiplyScalar(radius); // 距离圆心Z抽的距离
  80. const dir = (_crd && Vec3Util === void 0 ? (_reportPossibleCrUseOfVec3Util({
  81. error: Error()
  82. }), Vec3Util) : Vec3Util).sub(edge, center); // 初始圆心与目标位置的方向
  83. const vec3 = new Vec3();
  84. const quat = new Quat(); // 算出坐标点的旋转四元数
  85. Quat.fromAxisAngle(quat, Vec3.UP, toRadian(angle)); // 计算将向量做旋转操作后的向量
  86. Vec3.transformQuat(vec3, dir, quat); // 计算目标旋转后的点
  87. Vec3.add(vec3, center, vec3);
  88. return vec3;
  89. }
  90. });
  91. _cclegacy._RF.pop();
  92. _crd = false;
  93. }
  94. };
  95. });
  96. //# sourceMappingURL=c5f5f4202396229962c244c882feb56921b40cd5.js.map