index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. },
  83. handleARTrackerState({
  84. detail
  85. }) {
  86. const tracker = detail.value;
  87. // 获取当前状态和错误信息
  88. const {
  89. state,
  90. errorMessage
  91. } = tracker;
  92. console.log('tracker-state', tracker)
  93. if (state == 2) {
  94. wx.showToast({
  95. title: "tracker"
  96. })
  97. this.showAndPlay();
  98. }
  99. },
  100. showAndPlay() {
  101. this.scene.ar.placeHere('setitem', true);
  102. if (!this.data.isPlay) {
  103. this.scene.getNodeById('setitem').visible = true;
  104. this.setData({
  105. isPlay: true
  106. }, () => {
  107. if (this.video) {
  108. this.video.play()
  109. }
  110. })
  111. }
  112. }
  113. }
  114. })