index.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. Component({
  2. behaviors: [require('../../common/share-behavior').default],
  3. innerInterval: 0,
  4. videoId: -1,
  5. properties: {
  6. markerListRaw: {
  7. type: Array,
  8. value: []
  9. },
  10. },
  11. data: {
  12. loaded: true,
  13. arReady: false,
  14. gltfLoaded: false,
  15. videoLoaded: false,
  16. videoRatioLoaded: false,
  17. markerWidth: 1,
  18. markerHeight: 1,
  19. },
  20. observers: {
  21. markerListRaw(newVal) {
  22. this.setData({
  23. markerList: newVal,
  24. })
  25. },
  26. },
  27. lifetimes: {
  28. detached() {
  29. // 关闭时候去除计时器
  30. clearInterval(this.innerInterval);
  31. }
  32. },
  33. methods: {
  34. handleReady({detail}) {
  35. const xrScene = this.scene = detail.value;
  36. console.log('xr-scene', xrScene);
  37. // 加载场景资源
  38. const gltfResList = [];
  39. const videoResList = [];
  40. const markerList = this.data.markerList;
  41. for(let i = 0; i < markerList.length; i++) {
  42. const marker = markerList[i];
  43. switch (marker.renderType) {
  44. case 'gltf':
  45. gltfResList.push(marker);
  46. break;
  47. case 'video':
  48. videoResList.push(marker);
  49. break;
  50. }
  51. }
  52. try {
  53. this.loadGLTF(gltfResList)
  54. } catch (err) {
  55. console.log('[gltf load] error: ', err)
  56. }
  57. },
  58. handleARReady: function() {
  59. console.log('arReady')
  60. this.setData({ arReady: true })
  61. },
  62. async loadGLTF(gltfList) {
  63. const scene = this.scene
  64. const gltfModel = await Promise.all(gltfList.map(gltfItem => scene.assets.loadAsset({
  65. type: 'gltf',
  66. assetId: 'gltf-' + gltfItem.id,
  67. src: gltfItem.src,
  68. })))
  69. console.log('glTF asset loaded')
  70. this.setData({ gltfLoaded: true })
  71. },
  72. async loadVideoSingle(videoItem) {
  73. const scene = this.scene
  74. const videoTexture = await scene.assets.loadAsset({
  75. type: 'video-texture',
  76. assetId: `video-` + videoItem.id,
  77. src: videoItem.src,
  78. options: { loop: true, autoPlay: true },
  79. });
  80. // console.log('videoTexture', videoTexture);
  81. const videoMat = scene.createMaterial(
  82. scene.assets.getAsset('effect', 'simple'),
  83. { u_baseColorMap: videoTexture.value.texture }
  84. )
  85. scene.assets.addAsset('material', `video-mat-${videoItem.id}`, videoMat)
  86. console.log('video asset loaded')
  87. this.setData({
  88. videoLoaded: true,
  89. })
  90. },
  91. releaseVideo(id) {
  92. if (id !== -1) {
  93. // 存在纹理才进行释放
  94. const scene = this.scene
  95. // 释放加载过的资源
  96. scene.assets.releaseAsset('video-texture', `video-${id}`);
  97. scene.assets.releaseAsset('material', `video-mat-${id}`);
  98. }
  99. },
  100. handleTrackerSwitch({ detail }) {
  101. console.log('tracked match')
  102. if (this.data.loaded) {
  103. const element = detail.el;
  104. const active = detail.value;
  105. const data = this.data;
  106. const markerList = data.markerList;
  107. for (let i = 0; i < markerList.length; i++) {
  108. const markerInfo = markerList[i];
  109. const markerTracker = this.scene.getElementById(`marker-${markerInfo.id}`)
  110. if (element === markerTracker) {
  111. // 处理视频纹理
  112. this.releaseVideo(this.videoId);
  113. this.videoId = -1;
  114. // 匹配 tracker
  115. switch (markerInfo.renderType) {
  116. case 'scan': // scan
  117. this.scanHandler(markerTracker, markerInfo, active);
  118. break;
  119. case 'video': // video
  120. if (active) {
  121. this.videoHanler(markerInfo);
  122. }
  123. break;
  124. case 'gltf': // gltf
  125. break;
  126. }
  127. this.triggerEvent('trackerchange', {
  128. name: markerInfo.name,
  129. active: active,
  130. type: markerInfo.renderType,
  131. })
  132. }
  133. }
  134. }
  135. },
  136. scanHandler(markerTracker, markerInfo, active) {
  137. const renderType = markerInfo.renderType
  138. const xrFrameSystem = wx.getXrFrameSystem()
  139. const camera = this.scene.getElementById('camera').getComponent(xrFrameSystem.Camera);
  140. const trackerTRS = markerTracker.getComponent(xrFrameSystem.Transform)
  141. const rightTRS = this.scene.getElementById(`normal-right-${markerInfo.id}`).getComponent(xrFrameSystem.Transform);
  142. const trackerPos = camera.convertWorldPositionToClip(trackerTRS.worldPosition)
  143. const rightPos = camera.convertWorldPositionToClip(rightTRS.worldPosition)
  144. const posX = ((trackerPos.x + 1) / 2)
  145. const posY = (1 - (trackerPos.y + 1) / 2)
  146. const rightPosX = ((rightPos.x + 1) / 2)
  147. // 获取识别图,图片比例
  148. let widthDivideHeight = 1
  149. wx.getImageInfo({
  150. src: markerInfo.markerImage,
  151. success: res => {
  152. const {
  153. width,
  154. height
  155. } = res
  156. widthDivideHeight = width / height
  157. },
  158. fail: res => {
  159. console.error(res)
  160. }
  161. })
  162. // 通知上层
  163. this.triggerEvent('trackerchange', {
  164. name: markerInfo.name,
  165. active: active,
  166. type: renderType,
  167. trackerInfo: {
  168. x: posX,
  169. y: posY,
  170. halfWidth: rightPosX - posX,
  171. widthDivideHeight
  172. }
  173. })
  174. if (active) {
  175. // 激活态开启tracker位置同步
  176. let preX = posX;
  177. let preY = posY;
  178. let preR = rightPosX
  179. this.innerInterval = setInterval(()=>{
  180. const tPos = camera.convertWorldPositionToClip(trackerTRS.worldPosition)
  181. const rPos = camera.convertWorldPositionToClip(rightTRS.worldPosition)
  182. const pX = ((tPos.x + 1) / 2)
  183. const pY = (1 - (tPos.y + 1) / 2)
  184. const rX = ((rPos.x + 1) / 2)
  185. const dX = Math.abs(pX - preX)
  186. const dY = Math.abs(pY - preY)
  187. const dR = Math.abs(rX - preR)
  188. if (dX > 0.005 || dY > 0.005 || dR > 0.005) {
  189. preX = pX;
  190. preY = pY;
  191. preR = rX
  192. this.triggerEvent('trackermove', {
  193. active: true,
  194. type: 'scan',
  195. trackerInfo: {
  196. x: pX,
  197. y: pY,
  198. halfWidth: rX - pX,
  199. widthDivideHeight
  200. }
  201. })
  202. }
  203. }, 40)
  204. } else {
  205. // 取消态去除tracker位置同步
  206. clearInterval(this.innerInterval)
  207. this.triggerEvent('trackermove', {
  208. active: false,
  209. type: 'scan',
  210. trackerInfo: {
  211. x: 0,
  212. y: 0,
  213. halfWidth: 0
  214. }
  215. })
  216. }
  217. },
  218. videoHanler(markerInfo) {
  219. this.setData({
  220. videoLoaded: false,
  221. videoRatioLoaded: false,
  222. })
  223. // 获取识别图,图片比例
  224. wx.getImageInfo({
  225. src: markerInfo.markerImage,
  226. success: res => {
  227. const {
  228. width,
  229. height
  230. } = res
  231. const widthDivideHeight = width / height;
  232. this.setData({
  233. markerWidth: 1,
  234. markerHeight: (1 / widthDivideHeight).toFixed(2),
  235. videoRatioLoaded: true,
  236. });
  237. },
  238. fail: res => {
  239. console.error(res)
  240. }
  241. })
  242. try {
  243. this.loadVideoSingle(markerInfo)
  244. } catch (err) {
  245. console.log('[video load] error: ', err)
  246. }
  247. this.videoId = markerInfo.id;
  248. }
  249. }
  250. })