ar-tracker.js 9.1 KB

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