index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. Component({
  2. // behaviors: [require('../common/share-behavior').default],
  3. properties: {
  4. fromScan: Boolean
  5. },
  6. data: {
  7. loaded: false,
  8. arReady: false,
  9. },
  10. lifetimes: {
  11. async attached() {
  12. console.log('data', this.data)
  13. },
  14. async detached() {
  15. console.error('detached', this.video)
  16. this.video = null;
  17. this.setData({
  18. isPlay: false
  19. })
  20. }
  21. },
  22. methods: {
  23. handleReady({
  24. detail
  25. }) {
  26. const xrScene = this.scene = detail.value;
  27. this.mat = new(wx.getXrFrameSystem().Matrix4)();
  28. console.log('xr-scene', xrScene)
  29. this.triggerEvent('ready')
  30. },
  31. handleAssetsProgress: function ({
  32. detail
  33. }) {
  34. // console.log('assets progress', detail.value.progress);
  35. const progress =
  36. Math.floor(detail.value.progress * 100)
  37. this.triggerEvent('progress', progress)
  38. },
  39. handleAssetsLoaded: function ({
  40. detail
  41. }) {
  42. console.log('assets loaded', detail.value);
  43. const el = detail.value.target;
  44. // this.setData({loaded: true});
  45. this.scene.event.addOnce('touchstart', this.placeNode.bind(this));
  46. this.triggerEvent('loaded')
  47. },
  48. handleARReady: function ({
  49. detail
  50. }) {
  51. console.log('arReady', this.scene.ar.arVersion);
  52. },
  53. placeNode(event) {
  54. const {
  55. clientX,
  56. clientY
  57. } = event.touches[0];
  58. const {
  59. frameWidth: width,
  60. frameHeight: height
  61. } = this.scene;
  62. if (clientY / height > 0.8 && clientX / width < 0.2) {
  63. this.scene.getNodeById('setitem').visible = false;
  64. this.scene.ar.resetPlane();
  65. } else {
  66. this.showAndPlay();
  67. }
  68. this.scene.event.addOnce('touchstart', this.placeNode.bind(this));
  69. },
  70. handleGLTFLoaded({
  71. detail
  72. }) {
  73. const el = detail.value.target;
  74. console.error('handleGLTFLoaded')
  75. const gltf = el.getComponent("gltf");
  76. const video = this.scene.assets.getAsset("video-texture", "cat");
  77. const newMat = this.scene.assets.getAsset("material", "catMat");
  78. this.video = video
  79. for (const mesh of gltf.getPrimitivesByNodeName("video")) {
  80. mesh.material = newMat
  81. }
  82. if (this.data.fromScan) {
  83. console.log('fromScan', this.data.fromScan);
  84. this.scene.ar.resetPlane();
  85. this.showAndPlay();
  86. }
  87. },
  88. handleARTrackerState({
  89. detail
  90. }) {
  91. const tracker = detail.value;
  92. // 获取当前状态和错误信息
  93. const {
  94. state,
  95. errorMessage
  96. } = tracker;
  97. console.log('tracker-state', tracker)
  98. if (state == 2) {
  99. wx.showToast({
  100. title: "tracker"
  101. })
  102. this.showAndPlay();
  103. }
  104. },
  105. showAndPlay() {
  106. this.scene.ar.placeHere('setitem', true);
  107. if (!this.data.isPlay) {
  108. this.scene.getNodeById('setitem').visible = true;
  109. this.setData({
  110. isPlay: true
  111. }, () => {
  112. if (this.video) {
  113. this.video.play()
  114. }
  115. })
  116. }
  117. }
  118. }
  119. })