index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. Component({
  2. properties: {
  3. fromScan: Boolean
  4. },
  5. data: {
  6. loaded: false,
  7. arReady: false,
  8. isPlay: 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 && (this.video = null);
  17. this.animator && (this.animator = null);
  18. this.setData({
  19. isPlay: false
  20. })
  21. }
  22. },
  23. methods: {
  24. handleReady({
  25. detail
  26. }) {
  27. const xrScene = this.scene = detail.value;
  28. this.mat = new(wx.getXrFrameSystem().Matrix4)();
  29. console.log('xr-scene', xrScene)
  30. this.triggerEvent('ready')
  31. },
  32. handleAssetsProgress: function ({
  33. detail
  34. }) {
  35. // console.log('assets progress', detail.value.progress);
  36. const progress =
  37. Math.floor(detail.value.progress * 100)
  38. this.triggerEvent('progress', progress)
  39. },
  40. handleAssetsLoaded: function ({
  41. detail
  42. }) {
  43. console.log('assets loaded', detail.value);
  44. const el = detail.value.target;
  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. console.error('show-gltf')
  67. this.showAndPlay();
  68. }
  69. this.scene.event.addOnce('touchstart', this.placeNode.bind(this));
  70. },
  71. handleGLTFLoaded({
  72. detail
  73. }) {
  74. const el = detail.value.target;
  75. console.error('handleGLTFLoaded')
  76. const gltf = el.getComponent("gltf");
  77. const video = this.scene.assets.getAsset("video-texture", "animaV");
  78. const newMat = this.scene.assets.getAsset("material", "matAnimaV");
  79. this.video = video
  80. this.video.onEnd = () => {
  81. }
  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. this.scene.ar.placeHere('setitem', true);
  110. if (!this.data.isPlay) {
  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. })