ar-tracker.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. // console.error('meshes', gltf.meshes)
  119. // mesh_video1_16978video1
  120. //mesh_video2_16942video2
  121. // console.log('1', gltf.getPrimitivesByMeshName('mesh_video1_16978video1'))
  122. // console.log('2', gltf.getPrimitivesByMeshName('mesh_video2_16942video2'))
  123. // console.error('othergltf', othergltf)
  124. // for (const mesh of gltf.meshes) {
  125. // // mesh.material = newMat
  126. // console.warn('gltf-m', mesh)
  127. // }
  128. // for (const mesh of othergltf.model.meshes) {
  129. // // mesh.material = newMat
  130. // console.warn('othergltf-m', mesh.name)
  131. // }
  132. for (const mesh of gltf.getPrimitivesByNodeName('node_video1_-7064')) {
  133. mesh.material = newMat
  134. // console.warn('mesh1', mesh)
  135. }
  136. for (const mesh of gltf.getPrimitivesByNodeName('node_video2_-7072')) {
  137. mesh.material = newMat2
  138. // console.warn('mesh2', mesh)
  139. }
  140. },
  141. handleItem2Loaded({
  142. detail
  143. }) {
  144. const el = detail.value.target;
  145. console.warn('item2 load')
  146. const gltf = el.getComponent("gltf");
  147. const video = this.scene.assets.getAsset("video-texture", "item2-v");
  148. const newMat = this.scene.assets.getAsset("material", "item2-m");
  149. this.video2 = video
  150. for (const mesh of gltf.getPrimitivesByNodeName("video")) {
  151. console.error('2有video')
  152. mesh.material = newMat
  153. }
  154. },
  155. handleItem3Loaded({
  156. detail
  157. }) {
  158. const el = detail.value.target;
  159. console.warn('item3 load')
  160. const gltf = el.getComponent("gltf");
  161. const animator = el.getComponent("animator");
  162. this.animator3 = animator
  163. console.warn('animator3', this.animator3)
  164. },
  165. play(type) {
  166. if (this.data.loaded) {
  167. if (type === 1) {
  168. console.warn('play 1')
  169. if (this.video1) {
  170. if (this.data.isStartAudioPlay1) {
  171. this.data.isStartPlay1 && this.innerAudioContext1.play()
  172. } else {
  173. console.log('play 1 video', this.data.isStartPlay1, this.data.isVideoEnd1)
  174. this.data.isStartPlay1 ? !this.data.isVideoEnd1 ? this.video1.resume() : this.video1.play() : this.video1.play()
  175. }
  176. }
  177. if (this.animator1) {
  178. this.data.isStartPlay1 ? this.animator1.resume('dd2') : this.animator1.play('dd2')
  179. }
  180. this.setData({
  181. isStartPlay1: true
  182. })
  183. }
  184. if (type === 2) {
  185. console.warn('play 2')
  186. if (this.video2) {
  187. this.data.isStartPlay2 ? this.video2.resume() : this.video2.play()
  188. }
  189. this.setData({
  190. isStartPlay2: true
  191. })
  192. }
  193. if (type === 3) {
  194. console.warn('play 3')
  195. if (this.animator3) {
  196. if (!this.data.isStartPlay3) {
  197. this.animator3.play('Group21137Action', {
  198. loop: 0
  199. });
  200. this.animator3.play('polySurface1Action', {
  201. loop: 0
  202. });
  203. this.animator3.play('vlAction', {
  204. loop: 0
  205. });
  206. } else {
  207. this.animator3.resume('Group21137Action')
  208. this.animator3.resume('polySurface1Action')
  209. this.animator3.resume('vlAction')
  210. }
  211. }
  212. this.setData({
  213. isStartPlay3: true
  214. })
  215. }
  216. }
  217. },
  218. pause(type) {
  219. if (type === 1) {
  220. console.warn('pause 1')
  221. if (this.innerAudioContext1 && this.data.isStartAudioPlay1) {
  222. this.innerAudioContext1.pause()
  223. }
  224. if (this.animator1) {
  225. this.animator1.pause('dd2')
  226. }
  227. }
  228. if (type === 2) {
  229. console.warn('pause 2')
  230. if (this.video2) {
  231. this.video2.pause()
  232. }
  233. }
  234. },
  235. handleARTrackerState1({
  236. detail
  237. }) {
  238. // 事件的值即为`ARTracker`实例
  239. const tracker = detail.value;
  240. // 获取当前状态和错误信息
  241. const {
  242. state,
  243. errorMessage
  244. } = tracker;
  245. if (state == 2) {
  246. this.play(1)
  247. } else {
  248. this.data.isStartPlay1 && this.pause(1)
  249. }
  250. },
  251. handleARTrackerState2({
  252. detail
  253. }) {
  254. // 事件的值即为`ARTracker`实例
  255. const tracker = detail.value;
  256. // 获取当前状态和错误信息
  257. const {
  258. state,
  259. errorMessage
  260. } = tracker;
  261. if (state == 2) {
  262. this.play(2);
  263. } else {
  264. this.pause(2);
  265. }
  266. },
  267. handleARTrackerState3({
  268. detail
  269. }) {
  270. // 事件的值即为`ARTracker`实例
  271. const tracker = detail.value;
  272. // 获取当前状态和错误信息
  273. const {
  274. state,
  275. errorMessage
  276. } = tracker;
  277. if (state == 2) {
  278. this.play(3);
  279. } else {
  280. this.pause(3);
  281. }
  282. }
  283. }
  284. })