index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. Component({
  2. behaviors: [require('../common/share-behavior').default],
  3. properties: {
  4. markerImg: {
  5. type: String
  6. },
  7. },
  8. data: {
  9. loaded: false,
  10. arReady: false,
  11. isStartPlay1: false,
  12. adlScale: '0 0 0',
  13. adlPos: '-0.1 -0.5 -0.05'
  14. },
  15. lifetimes: {
  16. attached() {
  17. }
  18. },
  19. methods: {
  20. handleReady({
  21. detail
  22. }) {
  23. const xrScene = this.scene = detail.value;
  24. // console.log('xr-scene', xrScene);
  25. },
  26. handleAssetsProgress: function ({
  27. detail
  28. }) {
  29. // console.log('assets progress', detail.value);
  30. },
  31. handleAssetsLoaded: function ({
  32. detail
  33. }) {
  34. console.log('assets loaded', detail.value);
  35. this.setData({
  36. loaded: true
  37. });
  38. },
  39. handleARReady: function ({
  40. detail
  41. }) {
  42. console.log('arReady');
  43. this.setData({
  44. arReady: true
  45. })
  46. },
  47. handleItem1Loaded({ detail }) {
  48. const el = detail.value.target;
  49. const animator = el.getComponent("animator");
  50. this.animator1 = animator
  51. console.log('animator1', animator)
  52. },
  53. handleItem2Loaded({ detail }) {
  54. const el = detail.value.target;
  55. const animator = el.getComponent("animator");
  56. this.animator2 = animator;
  57. },
  58. handleARTrackerState1({
  59. detail
  60. }) {
  61. // 事件的值即为`ARTracker`实例
  62. const tracker = detail.value;
  63. // 获取当前状态和错误信息
  64. console.log('tracker', tracker)
  65. const {
  66. state,
  67. } = tracker;
  68. if (state == 2) {
  69. this.play()
  70. } else {
  71. this.pause()
  72. }
  73. },
  74. play() {
  75. if (!this.data.loaded) return
  76. if (!this.data.isStartPlay1) {
  77. this.setData({
  78. isStartPlay1: true
  79. }, () => {
  80. this.playitem1Action()
  81. })
  82. }
  83. },
  84. pause() {
  85. },
  86. playitem1Action() {
  87. if (!this.animator1 || !this.animator2) return;
  88. console.log('start animator1');
  89. this.animator1.play('All Animations', {
  90. loop: 0
  91. });
  92. setTimeout(() => {
  93. console.log('start animator2');
  94. this.animator2.pause();
  95. const duration = 1500;
  96. const startTime = Date.now();
  97. const startScale = 0;
  98. const endScale = 0.38;
  99. const startPX = -0.1
  100. const endPX = 0
  101. const startPY = -0.5
  102. const endPY = 0
  103. const startPZ = -0.05
  104. const endPZ = 0
  105. const step = () => {
  106. const now = Date.now();
  107. let t = (now - startTime) / duration;
  108. if (t > 1) t = 1;
  109. const s = startScale + (endScale - startScale) * t;
  110. const scaleStr = `${s} ${s} ${s}`;
  111. const p = `${startPX + (endPX - startPX) * t} ${startPY + (endPY - startPY) * t} ${startPZ + (endPZ - startPZ) * t}`;
  112. if (t >= 0.5 && !this.isStartPlay2) {
  113. this.isStartPlay2 = true;
  114. this.animator2.play('All Animations', {
  115. loop: 0
  116. });
  117. }
  118. this.setData({
  119. adlScale: scaleStr,
  120. adlPos: p
  121. });
  122. if (t < 1) {
  123. setTimeout(step, 16); // 60fps
  124. } else {
  125. const audioUrl = 'https://houseoss.4dkankan.com/project/hq-eduction-vr/public/%E5%AE%89%E5%BE%B7%E7%83%88.mp3';
  126. if (!this.audioCtx) {
  127. const audioCtx = wx.createInnerAudioContext();
  128. audioCtx.src = audioUrl;
  129. audioCtx.play();
  130. audioCtx.onEnded(() => {
  131. const hideDuration = 1000;
  132. const hideStartTime = Date.now();
  133. const startScaleHide = 0.38;
  134. const endScaleHide = 0;
  135. const hideStep = () => {
  136. const now = Date.now();
  137. let t = (now - hideStartTime) / hideDuration;
  138. if (t > 1) t = 1;
  139. const s = startScaleHide + (endScaleHide - startScaleHide) * t;
  140. const scaleStr = `${s} ${s} ${s}`;
  141. this.setData({
  142. adlScale: scaleStr
  143. });
  144. if (t < 1) {
  145. setTimeout(hideStep, 16);
  146. } else {
  147. this.animator2 = null;
  148. if (this.audioCtx) {
  149. this.audioCtx.destroy();
  150. this.audioCtx = null;
  151. }
  152. }
  153. };
  154. hideStep();
  155. });
  156. }
  157. }
  158. };
  159. this.setData({
  160. adlScale: '0 0 0'
  161. });
  162. step();
  163. }, 1500);
  164. }
  165. }
  166. })