ar-tracker.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // components/ar-tracker/ar-tracker.ts
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. properties: {
  7. },
  8. /**
  9. * 组件的初始数据
  10. */
  11. data: {
  12. loaded: false,
  13. isStartPlay1: false,
  14. },
  15. /**
  16. * 组件的方法列表
  17. */
  18. methods: {
  19. handleReady({
  20. detail
  21. }) {
  22. const xrScene = this.scene = detail.value;
  23. this.mat = new(wx.getXrFrameSystem().Matrix4)();
  24. console.log('xr-scene', xrScene)
  25. this.triggerEvent('ready')
  26. },
  27. handleARReady() {
  28. console.log('handleARReady')
  29. },
  30. handleAssetsProgress: function ({
  31. detail
  32. }) {
  33. const progress =
  34. Math.floor(detail.value.progress * 100)
  35. console.log('progress', progress)
  36. this.triggerEvent('progress', progress)
  37. },
  38. handleAssetsLoaded({
  39. detail
  40. }) {
  41. console.log('assets loaded', detail.value);
  42. this.triggerEvent('loaded')
  43. this.setData({
  44. loaded: true
  45. })
  46. },
  47. handleItem1Loaded({
  48. detail
  49. }) {
  50. const el = detail.value.target;
  51. console.warn('item1 load')
  52. const gltf = el.getComponent("gltf");
  53. const video = this.scene.assets.getAsset("video-texture", "item1-v");
  54. const newMat = this.scene.assets.getAsset("material", "item1-m");
  55. this.video1 = video
  56. // this.video1.onEnd = () => {}
  57. const animator = el.getComponent("animator");
  58. this.animator1 = animator
  59. console.warn('animator1', this.animator1)
  60. for (const mesh of gltf.getPrimitivesByNodeName("video")) {
  61. mesh.material = newMat
  62. }
  63. },
  64. handleItem2Loaded({
  65. detail
  66. }) {
  67. const el = detail.value.target;
  68. console.warn('item2 load')
  69. const gltf = el.getComponent("gltf");
  70. const video = this.scene.assets.getAsset("video-texture", "item2-v");
  71. const newMat = this.scene.assets.getAsset("material", "item2-m");
  72. this.video2 = video
  73. for (const mesh of gltf.getPrimitivesByNodeName("video")) {
  74. mesh.material = newMat
  75. }
  76. },
  77. handleItem3Loaded({
  78. detail
  79. }) {
  80. const el = detail.value.target;
  81. console.warn('item3 load')
  82. const gltf = el.getComponent("gltf");
  83. const animator = el.getComponent("animator");
  84. this.animator3 = animator
  85. console.warn('animator3', this.animator3)
  86. },
  87. play(type) {
  88. if (this.data.loaded) {
  89. if (type === 1) {
  90. console.warn('play 1')
  91. if (this.video1) {
  92. this.data.isStartPlay1 ? this.video1.resume() : this.video1.play()
  93. }
  94. if (this.animator1) {
  95. this.data.isStartPlay1 ? this.animator1.resume('Animation') : this.animator1.play('Animation')
  96. }
  97. this.setData({
  98. isStartPlay1: true
  99. })
  100. }
  101. if (type === 2) {
  102. console.warn('play 2')
  103. if (this.video2) {
  104. this.data.isStartPlay2 ? this.video2.resume() : this.video2.play()
  105. }
  106. this.setData({
  107. isStartPlay2: true
  108. })
  109. }
  110. }
  111. },
  112. pause(type) {
  113. if (type === 1) {
  114. console.warn('pause 1')
  115. if (this.video1) {
  116. this.video1.pause()
  117. }
  118. if (this.animator1) {
  119. this.animator1.pause('Animation')
  120. }
  121. }
  122. if (type === 2) {
  123. console.warn('pause 2')
  124. if (this.video2) {
  125. this.video2.pause()
  126. }
  127. }
  128. },
  129. handleARTrackerState1({
  130. detail
  131. }) {
  132. // 事件的值即为`ARTracker`实例
  133. const tracker = detail.value;
  134. // 获取当前状态和错误信息
  135. const {
  136. state,
  137. errorMessage
  138. } = tracker;
  139. if (state == 2) {
  140. this.play(1)
  141. } else {
  142. this.data.isStartPlay1 && this.pause(1)
  143. }
  144. },
  145. handleARTrackerState2({
  146. detail
  147. }) {
  148. // 事件的值即为`ARTracker`实例
  149. const tracker = detail.value;
  150. // 获取当前状态和错误信息
  151. const {
  152. state,
  153. errorMessage
  154. } = tracker;
  155. if (state == 2) {
  156. this.play(2);
  157. } else {
  158. this.pause(2);
  159. }
  160. },
  161. handleARTrackerState3({
  162. detail
  163. }) {
  164. // 事件的值即为`ARTracker`实例
  165. const tracker = detail.value;
  166. // 获取当前状态和错误信息
  167. const {
  168. state,
  169. errorMessage
  170. } = tracker;
  171. if (state == 2) {}
  172. }
  173. }
  174. })