index.js 3.2 KB

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