ar-tracker.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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.video12 && (this.video12 = null);
  16. this.video2 && (this.video2 = null);
  17. this.animator1 && (this.animator1 = null);
  18. this.animator2 && (this.animator2 = null);
  19. this.animator3 && (this.animator3 = null);
  20. this.innerAudioContext1 && this.innerAudioContext1.destroy() && (this.innerAudioContext1 = null)
  21. this.setData({
  22. isStartPlay1: false,
  23. isStartPlay2: false,
  24. isStartPlay3: false,
  25. })
  26. }
  27. },
  28. /**
  29. * 组件的初始数据
  30. */
  31. data: {
  32. loaded: false,
  33. isStartPlay1: false,
  34. isStartPlay2: false,
  35. isStartPlay3: false,
  36. },
  37. /**
  38. * 组件的方法列表
  39. */
  40. methods: {
  41. handleReady({
  42. detail
  43. }) {
  44. const xrScene = this.scene = detail.value;
  45. this.mat = new(wx.getXrFrameSystem().Matrix4)();
  46. console.log('xr-scene', xrScene)
  47. this.triggerEvent('ready')
  48. this.innerAudioContext1 = wx.createInnerAudioContext({
  49. useWebAudioImplement: false // 是否使用 WebAudio 作为底层音频驱动,默认关闭。对于短音频、播放频繁的音频建议开启此选项,开启后将获得更优的性能表现。由于开启此选项后也会带来一定的内存增长,因此对于长音频建议关闭此选项
  50. })
  51. this.innerAudioContext1.src = 'https://houseoss.4dkankan.com/mini-ar-test/AR/01/audio1.MP3'
  52. },
  53. handleARReady() {
  54. console.log('handleARReady')
  55. },
  56. handleAssetsProgress: function ({
  57. detail
  58. }) {
  59. const progress =
  60. Math.floor(detail.value.progress * 100)
  61. console.log('progress', progress)
  62. this.triggerEvent('progress', progress)
  63. },
  64. handleAssetsLoaded({
  65. detail
  66. }) {
  67. console.log('assets loaded', detail.value);
  68. this.triggerEvent('loaded')
  69. this.setData({
  70. loaded: true
  71. })
  72. },
  73. handleItem1Loaded({
  74. detail
  75. }) {
  76. const el = detail.value.target;
  77. console.warn('item1 load', el)
  78. const gltf = el.getComponent("gltf");
  79. const video = this.scene.assets.getAsset("video-texture", "item1-v");
  80. const video12 = this.scene.assets.getAsset("video-texture", "item12-v");
  81. const newMat = this.scene.assets.getAsset("material", "item1-m");
  82. const newMat2 = this.scene.assets.getAsset("material", "item12-m");
  83. this.video1 = video
  84. this.video12 = video12
  85. this.video1.onEnd = () => {
  86. this.innerAudioContext1 && this.innerAudioContext1.play()
  87. this.video12 && this.video12.play();
  88. }
  89. const animator = el.getComponent("animator");
  90. this.animator1 = animator
  91. // getPrimitivesByNodeName
  92. const othergltf = this.scene.assets.getAsset("gltf", "item1")
  93. console.error('meshes', gltf.meshes)
  94. // console.error('othergltf', othergltf)
  95. for (const mesh of gltf.meshes) {
  96. console.log(mesh.name)
  97. if (mesh.name === 'mesh_video_16024video1') {
  98. mesh.material = newMat
  99. console.log('mesh', mesh)
  100. }
  101. // mesh.material = newMat
  102. }
  103. // for (const mesh of gltf.getPrimitivesByMeshName("video2")) {
  104. // console.error('2有video2')
  105. // mesh.material = newMat2
  106. // }
  107. },
  108. handleItem2Loaded({
  109. detail
  110. }) {
  111. const el = detail.value.target;
  112. console.warn('item2 load')
  113. const gltf = el.getComponent("gltf");
  114. const video = this.scene.assets.getAsset("video-texture", "item2-v");
  115. const newMat = this.scene.assets.getAsset("material", "item2-m");
  116. this.video2 = video
  117. for (const mesh of gltf.getPrimitivesByNodeName("video")) {
  118. console.error('2有video')
  119. mesh.material = newMat
  120. }
  121. },
  122. handleItem3Loaded({
  123. detail
  124. }) {
  125. const el = detail.value.target;
  126. console.warn('item3 load')
  127. const gltf = el.getComponent("gltf");
  128. const animator = el.getComponent("animator");
  129. this.animator3 = animator
  130. console.warn('animator3', this.animator3)
  131. },
  132. play(type) {
  133. if (this.data.loaded) {
  134. if (type === 1) {
  135. console.warn('play 1')
  136. if (this.video1) {
  137. // this.data.isStartPlay1 ? this.video1.resume() : this.video1.play()
  138. // this.data.isStartPlay1 ? this.innerAudioContext1.resume() : this.innerAudioContext1.play()
  139. // this.video1.play()
  140. }
  141. if (this.animator1) {
  142. // this.data.isStartPlay1 ? this.animator1.resume('dd') : this.animator1.play('dd')
  143. this.animator1.play('dd')
  144. }
  145. this.setData({
  146. isStartPlay1: true
  147. })
  148. }
  149. if (type === 2) {
  150. console.warn('play 2')
  151. if (this.video2) {
  152. this.data.isStartPlay2 ? this.video2.resume() : this.video2.play()
  153. }
  154. this.setData({
  155. isStartPlay2: true
  156. })
  157. }
  158. if (type === 3) {
  159. console.warn('play 3')
  160. if (this.animator3) {
  161. if (!this.data.isStartPlay3) {
  162. this.animator3.play('Group21137Action', {
  163. loop: 0
  164. });
  165. this.animator3.play('polySurface1Action', {
  166. loop: 0
  167. });
  168. this.animator3.play('vlAction', {
  169. loop: 0
  170. });
  171. } else {
  172. this.animator3.resume('Group21137Action')
  173. this.animator3.resume('polySurface1Action')
  174. this.animator3.resume('vlAction')
  175. }
  176. }
  177. this.setData({
  178. isStartPlay3: true
  179. })
  180. }
  181. }
  182. },
  183. pause(type) {
  184. if (type === 1) {
  185. console.warn('pause 1')
  186. // if (this.video1) {
  187. // this.video1.pause()
  188. // }
  189. // if (this.animator1) {
  190. // this.animator1.pause('Animation')
  191. // }
  192. }
  193. if (type === 2) {
  194. console.warn('pause 2')
  195. if (this.video2) {
  196. this.video2.pause()
  197. }
  198. }
  199. },
  200. handleARTrackerState1({
  201. detail
  202. }) {
  203. // 事件的值即为`ARTracker`实例
  204. const tracker = detail.value;
  205. // 获取当前状态和错误信息
  206. const {
  207. state,
  208. errorMessage
  209. } = tracker;
  210. if (state == 2) {
  211. this.play(1)
  212. } else {
  213. this.data.isStartPlay1 && this.pause(1)
  214. }
  215. },
  216. handleARTrackerState2({
  217. detail
  218. }) {
  219. // 事件的值即为`ARTracker`实例
  220. const tracker = detail.value;
  221. // 获取当前状态和错误信息
  222. const {
  223. state,
  224. errorMessage
  225. } = tracker;
  226. if (state == 2) {
  227. this.play(2);
  228. } else {
  229. this.pause(2);
  230. }
  231. },
  232. handleARTrackerState3({
  233. detail
  234. }) {
  235. // 事件的值即为`ARTracker`实例
  236. const tracker = detail.value;
  237. // 获取当前状态和错误信息
  238. const {
  239. state,
  240. errorMessage
  241. } = tracker;
  242. if (state == 2) {
  243. this.play(3);
  244. } else {
  245. this.pause(3);
  246. }
  247. }
  248. }
  249. })