index.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. Component({
  2. behaviors: [require('../../common/share-behavior').default],
  3. properties: {
  4. a: Number,
  5. },
  6. data: {
  7. loaded: false,
  8. arReady: false,
  9. markerMatch: false
  10. },
  11. lifetimes: {
  12. async attached() {
  13. console.log('data', this.data)
  14. }
  15. },
  16. methods: {
  17. handleReady({detail}) {
  18. const xrScene = this.scene = detail.value;
  19. this.mat = new (wx.getXrFrameSystem().Matrix4)();
  20. console.log('xr-scene', xrScene);
  21. },
  22. handleARReady: async function({detail}) {
  23. console.log('arReady', this.scene.ar.arVersion);
  24. const xr = wx.getXrFrameSystem();
  25. // shadow root
  26. this.root = this.scene.getElementById('root');
  27. // 动态创建添加tracker
  28. const lockTrackerEl = this.scene.createElement(xr.XRNode);
  29. const lockTracker = lockTrackerEl.addComponent(xr.ARTracker, {
  30. mode: 'Marker',
  31. src: 'https://mmbizwxaminiprogram-1258344707.cos.ap-guangzhou.myqcloud.com/xr-frame/demo/marker/2dmarker-test.jpg',
  32. });
  33. this.root.addChild(lockTrackerEl);
  34. let waiting = false;
  35. lockTrackerEl.event.add('ar-tracker-state', tracker => {
  36. // 获取当前状态和错误信息
  37. const {state, errorMessage} = tracker;
  38. if (state === 2 && !waiting) {
  39. console.log('match')
  40. waiting = true;
  41. // 识别成功后切换到世界坐标
  42. // 延时保证坐标已经设置
  43. setTimeout(() => {
  44. this.setData({
  45. markerMatch: true
  46. });
  47. // 去除tracker监听
  48. this.root.removeChild(lockTrackerEl);
  49. }, 30);
  50. }
  51. })
  52. }
  53. }
  54. })