ar-tracker.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. // isVideoEnd1: false,
  35. isVideo12Start: false,
  36. isStartAudioPlay1: false,
  37. isStartPlay2: false,
  38. isStartPlay3: false,
  39. animator1Name: 'dd5'
  40. },
  41. /**
  42. * 组件的方法列表
  43. */
  44. methods: {
  45. handleReady({
  46. detail
  47. }) {
  48. const xrScene = this.scene = detail.value;
  49. this.mat = new(wx.getXrFrameSystem().Matrix4)();
  50. console.log('xr-scene', xrScene)
  51. this.triggerEvent('ready')
  52. // if (!this.innerAudioContext1) {
  53. // this.innerAudioContext1 = wx.createInnerAudioContext({
  54. // useWebAudioImplement: true // 是否使用 WebAudio 作为底层音频驱动,默认关闭。对于短音频、播放频繁的音频建议开启此选项,开启后将获得更优的性能表现。由于开启此选项后也会带来一定的内存增长,因此对于长音频建议关闭此选项
  55. // })
  56. // this.innerAudioContext1.src = 'https://houseoss.4dkankan.com/mini-ar-test/AR/01/audio1.MP3'
  57. // this.innerAudioContext1.onPlay(() => {
  58. // console.log('log:audio:play')
  59. // this.setData({
  60. // isStartAudioPlay1: true
  61. // })
  62. // })
  63. // this.innerAudioContext1.onPause(() => {
  64. // console.log('log:audio:pause')
  65. // this.setData({
  66. // isStartAudioPlay1: false,
  67. // })
  68. // })
  69. // this.innerAudioContext1.onEnded(() => {
  70. // if (this.animator1) {
  71. // console.log('animator1重置', this.data.animator1Name)
  72. // this.animator1.stop(this.data.animator1Name);
  73. // }
  74. // console.log('log:audio:ended')
  75. // this.setData({
  76. // isStartAudioPlay1: false,
  77. // isVideoEnd1: false,
  78. // isStartPlay1: false
  79. // })
  80. // })
  81. // console.log('this.innerAudioContext1', this.innerAudioContext1)
  82. // }
  83. },
  84. handleARReady() {
  85. console.log('handleARReady')
  86. },
  87. handleAssetsProgress: function ({
  88. detail
  89. }) {
  90. const progress =
  91. Math.floor(detail.value.progress * 100)
  92. console.log('progress', progress)
  93. this.triggerEvent('progress', progress)
  94. },
  95. handleAssetsLoaded({
  96. detail
  97. }) {
  98. console.log('assets loaded', detail.value);
  99. this.triggerEvent('loaded')
  100. this.setData({
  101. loaded: true
  102. })
  103. },
  104. handleItem1Loaded({
  105. detail
  106. }) {
  107. const el = detail.value.target;
  108. console.warn('item1 load', el)
  109. const gltf = el.getComponent("gltf");
  110. const video1 = this.scene.assets.getAsset("video-texture", "item1-v");
  111. const newMat = this.scene.assets.getAsset("material", "item1-m");
  112. const video12 = this.scene.assets.getAsset("video-texture", "item1-2-v");
  113. const newMat2 = this.scene.assets.getAsset("material", "item1-2-m");
  114. this.video1 = video1
  115. this.video12 = video12
  116. this.video1.onPlay = () => {
  117. console.warn('video1 onPlay')
  118. }
  119. this.video1.onEnd = () => {
  120. console.warn('video1 ended')
  121. }
  122. this.video12.onPlay = () => {
  123. console.warn('video12 onPlay')
  124. this.setData({
  125. isVideo12Start: true
  126. })
  127. }
  128. this.video12.onEnd = () => {
  129. this.setData({
  130. isVideo12Start: false
  131. })
  132. console.warn('video12 onEnd')
  133. this.animator1.pauseToFrame(this.data.animator1Name, 0);
  134. this.animator1.stop(this.data.animator1Name);
  135. }
  136. const animator = el.getComponent("animator");
  137. // console.warn("current el", el)
  138. this.animator1 = animator
  139. console.warn('this.animator1', this.animator1)
  140. const othergltf = this.scene.assets.getAsset("gltf", "item1")
  141. for (const mesh of othergltf.model.meshes) {
  142. if (String(mesh.name).includes('video1')) {
  143. const videoMesh1 = gltf.getPrimitivesByMeshName(mesh.name)
  144. console.warn('videoMesh1', mesh.name, videoMesh1[0].nodeName)
  145. for (const mesh of gltf.getPrimitivesByNodeName(videoMesh1[0].nodeName)) {
  146. mesh.material = newMat
  147. }
  148. }
  149. if (String(mesh.name).includes('video2')) {
  150. const videoMesh2 = gltf.getPrimitivesByMeshName(mesh.name)
  151. console.warn('videoMesh2', mesh.name, videoMesh2[0].nodeName)
  152. for (const mesh of gltf.getPrimitivesByNodeName(videoMesh2[0].nodeName)) {
  153. mesh.material = newMat2
  154. }
  155. }
  156. }
  157. },
  158. handleItem2Loaded({
  159. detail
  160. }) {
  161. const el = detail.value.target;
  162. console.warn('item2 load')
  163. const gltf = el.getComponent("gltf");
  164. const video = this.scene.assets.getAsset("video-texture", "item2-v");
  165. const newMat = this.scene.assets.getAsset("material", "item2-m");
  166. this.video2 = video
  167. for (const mesh of gltf.getPrimitivesByNodeName("video")) {
  168. console.error('2有video')
  169. mesh.material = newMat
  170. }
  171. },
  172. handleItem3Loaded({
  173. detail
  174. }) {
  175. const el = detail.value.target;
  176. console.warn('item3 load')
  177. const gltf = el.getComponent("gltf");
  178. const animator = el.getComponent("animator");
  179. this.animator3 = animator
  180. console.warn('animator3', this.animator3)
  181. },
  182. playitem1Action() {
  183. console.log('playitem1Action')
  184. if (this.video1) {
  185. this.video1.play()
  186. }
  187. if (this.animator1) {
  188. this.animator1.pauseToFrame(this.data.animator1Name, 0);
  189. this.animator1.resume(this.data.animator1Name)
  190. }
  191. if (this.video12) {
  192. setTimeout(() => {
  193. console.log('play 1-2 video -video2开始后 (8600ms) 8.6秒后播')
  194. this.video12.play()
  195. this.setData({
  196. isVideo12Start: true
  197. })
  198. }, 8600)
  199. }
  200. },
  201. playitem1() {
  202. if (!this.data.isStartPlay1) {
  203. console.warn('play 1')
  204. this.setData({
  205. isStartPlay1: true
  206. }, () => {
  207. // video1和animator同时播 video2开始后10秒后播
  208. this.playitem1Action();
  209. })
  210. } else {
  211. if (this.video12 && this.data.isVideo12Start) {
  212. console.warn('play 1 resume video')
  213. this.video12.resume()
  214. } else {
  215. console.warn('play 1 restart video')
  216. this.setData({
  217. isVideo12Start: true
  218. }, () => {
  219. this.playitem1Action();
  220. })
  221. }
  222. }
  223. },
  224. play(type) {
  225. if (this.data.loaded) {
  226. this.triggerEvent('hideGuide')
  227. if (type === 1) {
  228. this.playitem1();
  229. }
  230. if (type === 2) {
  231. console.warn('play 2')
  232. if (this.video2) {
  233. this.data.isStartPlay2 ? this.video2.resume() : this.video2.play()
  234. }
  235. this.setData({
  236. isStartPlay2: true
  237. })
  238. }
  239. if (type === 3) {
  240. console.warn('play 3')
  241. if (this.animator3) {
  242. if (!this.data.isStartPlay3) {
  243. this.animator3.play('Group21137Action', {
  244. loop: 0
  245. });
  246. this.animator3.play('polySurface1Action', {
  247. loop: 0
  248. });
  249. this.animator3.play('vlAction', {
  250. loop: 0
  251. });
  252. } else {
  253. this.animator3.resume('Group21137Action')
  254. this.animator3.resume('polySurface1Action')
  255. this.animator3.resume('vlAction')
  256. }
  257. }
  258. this.setData({
  259. isStartPlay3: true
  260. })
  261. }
  262. }
  263. },
  264. pause(type) {
  265. if (type === 1) {
  266. // if (this.innerAudioContext1 && this.data.isStartAudioPlay1) {
  267. // this.innerAudioContext1.pause()
  268. // }
  269. // if (this.animator1) {
  270. // this.animator1.pause(this.data.animator1Name)
  271. // }
  272. console.warn('pause 1', this.data.isVideo12Start, this.video12)
  273. if (this.video12 && this.data.isVideo12Start) {
  274. this.video12.pause()
  275. }
  276. }
  277. if (type === 2) {
  278. console.warn('pause 2')
  279. if (this.video2) {
  280. this.video2.pause()
  281. }
  282. }
  283. },
  284. handleARTrackerState1({
  285. detail
  286. }) {
  287. // 事件的值即为`ARTracker`实例
  288. const tracker = detail.value;
  289. // 获取当前状态和错误信息
  290. console.log('tracker', tracker)
  291. const {
  292. state,
  293. errorMessage
  294. } = tracker;
  295. if (state == 2) {
  296. this.play(1)
  297. } else {
  298. this.pause(1)
  299. }
  300. },
  301. handleARTrackerState2({
  302. detail
  303. }) {
  304. // 事件的值即为`ARTracker`实例
  305. const tracker = detail.value;
  306. // 获取当前状态和错误信息
  307. const {
  308. state,
  309. errorMessage
  310. } = tracker;
  311. if (state == 2) {
  312. this.play(2);
  313. } else {
  314. this.pause(2);
  315. }
  316. },
  317. handleARTrackerState3({
  318. detail
  319. }) {
  320. // 事件的值即为`ARTracker`实例
  321. const tracker = detail.value;
  322. // 获取当前状态和错误信息
  323. const {
  324. state,
  325. errorMessage
  326. } = tracker;
  327. if (state == 2) {
  328. this.play(3);
  329. } else {
  330. this.pause(3);
  331. }
  332. }
  333. }
  334. })