ar-tracker.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. if (type === 3) {
  111. console.warn('play 3')
  112. if (this.animator3) {
  113. if (!this.data.isStartPlay3) {
  114. this.animator3.play('Group21137Action', {
  115. loop: 0
  116. });
  117. this.animator3.play('polySurface1Action', {
  118. loop: 0
  119. });
  120. this.animator3.play('vlAction', {
  121. loop: 0
  122. });
  123. } else {
  124. this.animator3.resume('Group21137Action')
  125. this.animator3.resume('polySurface1Action')
  126. this.animator3.resume('vlAction')
  127. }
  128. }
  129. this.setData({
  130. isStartPlay3: true
  131. })
  132. }
  133. }
  134. },
  135. pause(type) {
  136. if (type === 1) {
  137. console.warn('pause 1')
  138. if (this.video1) {
  139. this.video1.pause()
  140. }
  141. if (this.animator1) {
  142. this.animator1.pause('Animation')
  143. }
  144. }
  145. if (type === 2) {
  146. console.warn('pause 2')
  147. if (this.video2) {
  148. this.video2.pause()
  149. }
  150. }
  151. },
  152. handleARTrackerState1({
  153. detail
  154. }) {
  155. // 事件的值即为`ARTracker`实例
  156. const tracker = detail.value;
  157. // 获取当前状态和错误信息
  158. const {
  159. state,
  160. errorMessage
  161. } = tracker;
  162. if (state == 2) {
  163. this.play(1)
  164. } else {
  165. this.data.isStartPlay1 && this.pause(1)
  166. }
  167. },
  168. handleARTrackerState2({
  169. detail
  170. }) {
  171. // 事件的值即为`ARTracker`实例
  172. const tracker = detail.value;
  173. // 获取当前状态和错误信息
  174. const {
  175. state,
  176. errorMessage
  177. } = tracker;
  178. if (state == 2) {
  179. this.play(2);
  180. } else {
  181. this.pause(2);
  182. }
  183. },
  184. handleARTrackerState3({
  185. detail
  186. }) {
  187. // 事件的值即为`ARTracker`实例
  188. const tracker = detail.value;
  189. // 获取当前状态和错误信息
  190. const {
  191. state,
  192. errorMessage
  193. } = tracker;
  194. if (state == 2) {
  195. this.play(3);
  196. } else {
  197. this.pause(3);
  198. }
  199. }
  200. }
  201. })