ar-tracker.js 9.3 KB

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