index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. const xrFrameSystem = wx.getXrFrameSystem();
  2. Component({
  3. // behaviors: [require('../common/share-behavior').default],
  4. properties: {
  5. fromScan: Boolean
  6. },
  7. data: {
  8. loaded: false,
  9. arReady: false,
  10. isPlay: false
  11. },
  12. lifetimes: {
  13. async attached() {
  14. console.log('data', this.data)
  15. },
  16. async detached() {
  17. console.error('detached', this.video)
  18. this.animator && (this.animator = null);
  19. this.setData({
  20. isPlay: false,
  21. fromScan: false
  22. })
  23. }
  24. },
  25. methods: {
  26. handleReady({
  27. detail
  28. }) {
  29. const xrScene = this.scene = detail.value;
  30. this.mat = new(wx.getXrFrameSystem().Matrix4)();
  31. console.log('xr-scene', xrScene)
  32. this.triggerEvent('ready')
  33. },
  34. handleAssetsProgress: function ({
  35. detail
  36. }) {
  37. const progress =
  38. Math.floor(detail.value.progress * 100)
  39. this.triggerEvent('progress', progress)
  40. },
  41. handleAssetsLoaded: function ({
  42. detail
  43. }) {
  44. console.log('assets loaded', detail.value);
  45. const el = detail.value.target;
  46. // this.setData({loaded: true});
  47. this.scene.event.addOnce('touchstart', this.placeNode.bind(this));
  48. this.triggerEvent('loaded')
  49. },
  50. handleARReady: function ({
  51. detail
  52. }) {
  53. console.log('arReady', this.scene.ar.arVersion);
  54. },
  55. placeNode(event) {
  56. const {
  57. clientX,
  58. clientY
  59. } = event.touches[0];
  60. const {
  61. frameWidth: width,
  62. frameHeight: height
  63. } = this.scene;
  64. if (clientY / height > 0.8 && clientX / width < 0.2) {
  65. this.scene.getNodeById('setitem').visible = false;
  66. this.scene.ar.resetPlane();
  67. } else {
  68. console.error('show-gltf', this.data.isPlay)
  69. this.showAndPlay();
  70. }
  71. this.scene.event.addOnce('touchstart', this.placeNode.bind(this));
  72. },
  73. handleGLTFLoaded({
  74. detail
  75. }) {
  76. const el = detail.value.target;
  77. const animator = el.getComponent("animator");
  78. this.animator = animator
  79. console.warn('handleGLTFLoaded', animator)
  80. // if (this.data.fromScan) {
  81. // console.log('fromScan', this.data.fromScan);
  82. // this.showAndPlay();
  83. // }
  84. },
  85. handleARTrackerState({
  86. detail
  87. }) {
  88. const tracker = detail.value;
  89. // 获取当前状态和错误信息
  90. const {
  91. state,
  92. errorMessage
  93. } = tracker;
  94. console.log('tracker-state', tracker)
  95. if (state == 2) {
  96. wx.showToast({
  97. title: "tracker"
  98. })
  99. this.showAndPlay();
  100. }
  101. },
  102. showAndPlay() {
  103. console.log('showAndPlay');
  104. this.scene.ar.placeHere('setitem', true);
  105. if (!this.data.isPlay) {
  106. this.scene.getNodeById('setitem').visible = true;
  107. this.setData({
  108. isPlay: true
  109. }, () => {
  110. if (this.animator) {
  111. if (this.animator) {
  112. this.animator.play('Group21137Action', {
  113. loop: 0
  114. });
  115. this.animator.play('polySurface1Action', {
  116. loop: 0
  117. });
  118. this.animator.play('vlAction', {
  119. loop: 0
  120. });
  121. }
  122. }
  123. })
  124. }
  125. }
  126. }
  127. })