ar-tracker.js 9.5 KB

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