b96b47e1de4520b3e54fe6923b0d5370c7a1552f.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. System.register(["cc"], function (_export, _context) {
  2. "use strict";
  3. var _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, _decorator, Component, Node, quat, tween, Tween, Vec3, _dec, _dec2, _dec3, _dec4, _class, _class2, _descriptor, _descriptor2, _descriptor3, _crd, ccclass, property, ModelRotator;
  4. function _initializerDefineProperty(target, property, descriptor, context) { if (!descriptor) return; Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 }); }
  5. function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object.defineProperty(target, property, desc); desc = null; } return desc; }
  6. function _initializerWarningHelper(descriptor, context) { throw new Error('Decorating class property failed. Please ensure that ' + 'transform-class-properties is enabled and runs after the decorators transform.'); }
  7. return {
  8. setters: [function (_cc) {
  9. _cclegacy = _cc.cclegacy;
  10. __checkObsolete__ = _cc.__checkObsolete__;
  11. __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
  12. _decorator = _cc._decorator;
  13. Component = _cc.Component;
  14. Node = _cc.Node;
  15. quat = _cc.quat;
  16. tween = _cc.tween;
  17. Tween = _cc.Tween;
  18. Vec3 = _cc.Vec3;
  19. }],
  20. execute: function () {
  21. _crd = true;
  22. _cclegacy._RF.push({}, "70003AYwQ5EK6B4bgpBRvvR", "ModelRotator", undefined);
  23. __checkObsolete__(['_decorator', 'Component', 'EventTouch', 'Input', 'input', 'Node', 'quat', 'Quat', 'tween', 'Tween', 'Vec3']);
  24. ({
  25. ccclass,
  26. property
  27. } = _decorator);
  28. _export("ModelRotator", ModelRotator = (_dec = ccclass('ModelRotator'), _dec2 = property({
  29. tooltip: '初始旋转速度(度/秒)'
  30. }), _dec3 = property({
  31. tooltip: '触摸后的减速系数(0-1)'
  32. }), _dec4 = property({
  33. tooltip: '斜角旋转的X轴角度(度)'
  34. }), _dec(_class = (_class2 = class ModelRotator extends Component {
  35. constructor() {
  36. super(...arguments);
  37. _initializerDefineProperty(this, "speed", _descriptor, this);
  38. _initializerDefineProperty(this, "touchDeceleration", _descriptor2, this);
  39. _initializerDefineProperty(this, "tiltAngle", _descriptor3, this);
  40. // 改为36度斜角
  41. // 新增Canvas节点引用
  42. this.canvasNode = null;
  43. this._targetQuat = quat();
  44. this._currentSpeed = 0;
  45. this._touchStartX = 0;
  46. this._isDragging = false;
  47. this._baseRotation = new Vec3();
  48. }
  49. start() {
  50. this.canvasNode = this.node.parent.parent; // 设置斜角旋转(X轴36度 + Z轴旋转)
  51. this._baseRotation.set(this.tiltAngle, 0, -this.tiltAngle); // 增加Z轴倾斜
  52. this.node.setRotationFromEuler(this._baseRotation); // 删除冗余的全局input监听
  53. this.canvasNode.on(Node.EventType.TOUCH_START, this.onTouchStart, this);
  54. this.canvasNode.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
  55. this.canvasNode.on(Node.EventType.TOUCH_END, this.onTouchEnd, this); // 初始化自动旋转
  56. this._currentSpeed = this.speed;
  57. this.startAutoRotation();
  58. }
  59. startAutoRotation() {
  60. tween(this.node).by(1, {
  61. //改为绕Z轴旋转
  62. eulerAngles: new Vec3(this._currentSpeed, 0, -this._currentSpeed)
  63. }).repeatForever().start();
  64. }
  65. onTouchMove(event) {
  66. if (!this._isDragging) return; // 改进差值计算方式
  67. var currentX = event.getUILocation().x;
  68. var deltaX = currentX - this._touchStartX;
  69. this._touchStartX = currentX; // 改为同时更新Z轴旋转
  70. var currentEuler = this.node.eulerAngles.clone();
  71. currentEuler.z += deltaX * 0.5; // 绕Z轴旋转
  72. this.node.setRotationFromEuler(new Vec3(this.tiltAngle, // 保持X轴斜角
  73. 0, // 固定Y轴
  74. currentEuler.z // Z轴旋转
  75. ));
  76. }
  77. onTouchStart(event) {
  78. this._touchStartX = event.getLocationX();
  79. this._isDragging = true;
  80. this._currentSpeed = 0; // 停止自动旋转
  81. Tween.stopAllByTarget(this.node);
  82. }
  83. onTouchEnd() {
  84. this._isDragging = false; // 添加惯性滑动效果
  85. this._currentSpeed = this.speed * this.touchDeceleration;
  86. this.startAutoRotation();
  87. }
  88. onDestroy() {
  89. //改为Canvas节点的事件解绑
  90. if (this.canvasNode) {
  91. this.canvasNode.off(Node.EventType.TOUCH_START, this.onTouchStart, this);
  92. this.canvasNode.off(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
  93. this.canvasNode.off(Node.EventType.TOUCH_END, this.onTouchEnd, this);
  94. }
  95. }
  96. }, (_descriptor = _applyDecoratedDescriptor(_class2.prototype, "speed", [_dec2], {
  97. configurable: true,
  98. enumerable: true,
  99. writable: true,
  100. initializer: function initializer() {
  101. return 45;
  102. }
  103. }), _descriptor2 = _applyDecoratedDescriptor(_class2.prototype, "touchDeceleration", [_dec3], {
  104. configurable: true,
  105. enumerable: true,
  106. writable: true,
  107. initializer: function initializer() {
  108. return 0.95;
  109. }
  110. }), _descriptor3 = _applyDecoratedDescriptor(_class2.prototype, "tiltAngle", [_dec4], {
  111. configurable: true,
  112. enumerable: true,
  113. writable: true,
  114. initializer: function initializer() {
  115. return 36;
  116. }
  117. })), _class2)) || _class));
  118. _cclegacy._RF.pop();
  119. _crd = false;
  120. }
  121. };
  122. });
  123. //# sourceMappingURL=b96b47e1de4520b3e54fe6923b0d5370c7a1552f.js.map