index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. Component({
  2. behaviors: [require('../../common/share-behavior').default],
  3. properties: {
  4. a: Number,
  5. },
  6. data: {
  7. loaded: false,
  8. arReady: false,
  9. },
  10. lifetimes: {
  11. async attached() {
  12. console.log('data', this.data)
  13. }
  14. },
  15. methods: {
  16. handleReady({detail}) {
  17. const xrScene = this.scene = detail.value;
  18. this.mat = new (wx.getXrFrameSystem().Matrix4)();
  19. console.log('xr-scene', xrScene);
  20. },
  21. handleARReady: async function({detail}) {
  22. console.log('arReady', this.scene.ar.arVersion);
  23. const xr = wx.getXrFrameSystem();
  24. // shadow root
  25. this.root = this.scene.getElementById('root');
  26. // 动态创建添加tracker
  27. const lockTrackerEl = this.scene.createElement(xr.XRNode);
  28. const lockTracker = lockTrackerEl.addComponent(xr.ARTracker, {
  29. mode: 'Marker',
  30. src: 'https://mmbizwxaminiprogram-1258344707.cos.ap-guangzhou.myqcloud.com/xr-frame/demo/marker/2dmarker-test.jpg',
  31. });
  32. this.root.addChild(lockTrackerEl);
  33. // 动态改动模型根节点
  34. const lockItemEle = this.scene.createElement(xr.XRNode, {
  35. position: '10000 0 0',
  36. });
  37. const {value: model} = await this.scene.assets.loadAsset({type: 'gltf', assetId: 'butterfly', src: 'https://mmbizwxaminiprogram-1258344707.cos.ap-guangzhou.myqcloud.com/xr-frame/demo/butterfly/index.glb'});
  38. // 添加蝴蝶
  39. const gltf1 = this.scene.createElement(xr.XRGLTF, {
  40. position: '0.2 0 -0.2',
  41. scale: '0.6 0.6 0.6',
  42. rotation: '0 -50 0',
  43. 'anim-autoplay': '',
  44. });
  45. lockItemEle.addChild(gltf1);
  46. gltf1.getComponent(xr.GLTF).setData({
  47. model: model,
  48. });
  49. const gltf2 = this.scene.createElement(xr.XRGLTF, {
  50. position: '0.4 0 0.3',
  51. scale: '0.5 0.5 0.5',
  52. rotation: '0 -50 0',
  53. 'anim-autoplay': '',
  54. });
  55. lockItemEle.addChild(gltf2);
  56. gltf2.getComponent(xr.GLTF).setData({
  57. model: model,
  58. });
  59. const gltf3 = this.scene.createElement(xr.XRGLTF, {
  60. position: '-0.3 0 0.3',
  61. scale: '0.4 0.4 0.4',
  62. rotation: '0 -50 0',
  63. 'anim-autoplay': '',
  64. });
  65. lockItemEle.addChild(gltf3);
  66. gltf3.getComponent(xr.GLTF).setData({
  67. model: model,
  68. });
  69. // 先挂到场上,但是可以放在离屏
  70. this.root.addChild(lockItemEle);
  71. let waiting = false;
  72. lockTrackerEl.event.add('ar-tracker-state', tracker => {
  73. // 获取当前状态和错误信息
  74. const {state, errorMessage} = tracker;
  75. if (state === 2 && !waiting) {
  76. console.log('match')
  77. waiting = true;
  78. // 识别成功后切换到世界坐标
  79. // 延时保证坐标已经设置
  80. setTimeout(() => {
  81. // 将 lockTrackerEl 的世界矩阵信息同步到 lockItemEle
  82. const lockTrackerTrs = lockTrackerEl.getComponent(xr.Transform)
  83. const lockItemTrs = lockItemEle.getComponent(xr.Transform)
  84. lockItemTrs.setLocalMatrix(lockTrackerTrs.worldMatrix);
  85. // 去除tracker监听
  86. this.root.removeChild(lockTrackerEl);
  87. }, 30);
  88. }
  89. })
  90. }
  91. }
  92. })