ar-tracker.js 8.7 KB

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