ar-tracker.js 8.2 KB

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