AutoRotate.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import XrFrame from 'XrFrame';
  2. const xrFrameSystem = wx.getXrFrameSystem();
  3. export interface IAutoRotateData {
  4. speed: number[];
  5. }
  6. const AutoRotateSchema: XrFrame.IComponentSchema = {
  7. speed: {type: 'number-array', defaultValue: [1, 1, 1]}
  8. }
  9. xrFrameSystem.registerComponent('auto-rotate', class AutoRotate extends xrFrameSystem.Component<IAutoRotateData> {
  10. public readonly schema: XrFrame.IComponentSchema = AutoRotateSchema;
  11. private _speedX: number = 1;
  12. private _speedY: number = 1;
  13. private _speedZ: number = 1;
  14. public onAdd(parent: XrFrame.Element, data: IAutoRotateData): void {
  15. this._processData(data);
  16. }
  17. public onUpdate(data: IAutoRotateData, preData: IAutoRotateData): void {
  18. this._processData(data);
  19. }
  20. public onTick(delta: number, data: IAutoRotateData) {
  21. const trs = this.el.getComponent(xrFrameSystem.Transform);
  22. if (!trs) {
  23. return;
  24. }
  25. trs.rotation.x += this._speedX * 0.1;
  26. trs.rotation.y += this._speedY * 0.1;
  27. trs.rotation.z += this._speedZ * 0.1;
  28. }
  29. public onRemove(parent: XrFrame.Element, data: IAutoRotateData): void {
  30. }
  31. private _processData(data: IAutoRotateData) {
  32. this._speedX = data.speed?.[0] !== undefined ? data.speed[0] : 1;
  33. this._speedY = data.speed?.[1] !== undefined ? data.speed[1] : 1;
  34. this._speedZ = data.speed?.[2] !== undefined ? data.speed[2] : 1;
  35. }
  36. });