ar-tracker.js 5.8 KB

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