index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. Component({
  2. behaviors: [require('../common/share-behavior').default],
  3. properties: {
  4. throwing: {
  5. type: Boolean,
  6. value: false,
  7. observer: function(newVal, oldVal) {
  8. if (newVal !== oldVal) {
  9. if (newVal) this.startThrowing();
  10. else this.endThrowing();
  11. }
  12. }
  13. }
  14. },
  15. data: {
  16. showTarget: false
  17. },
  18. lifetimes: {},
  19. methods: {
  20. handleReady({detail}) {
  21. const xrScene = this.scene = detail.value;
  22. this.vQueueLength = 5;
  23. this.vQueue = [];
  24. this.vQueueHead = 0;
  25. },
  26. handleAssetsProgress: function({detail}) {
  27. console.log('assets progress', detail.value);
  28. },
  29. handleAssetsLoaded: function({detail}) {
  30. console.log('assets loaded', detail.value);
  31. this.setData({loaded: true});
  32. },
  33. handleARTrackerState({detail}) {
  34. // 事件的值即为`ARTracker`实例
  35. const tracker = detail.value;
  36. // 获取当前状态和错误信息
  37. const {state, errorMessage} = tracker;
  38. if (state == 2) {
  39. this.handleARDetected();
  40. }
  41. },
  42. handleARDetected() {
  43. this.scene.event.addOnce('touchstart', this.placeTarget.bind(this));
  44. },
  45. placeTarget() {
  46. this.scene.ar.placeHere("targetTransform");
  47. this.setData({
  48. showTarget: true
  49. });
  50. this.triggerEvent("ar_detected");
  51. },
  52. startThrowing() {
  53. if (!this.data.showTarget) return;
  54. this.makeBall();
  55. },
  56. endThrowing() {
  57. this.releaseBall();
  58. },
  59. makeBall() {
  60. const xr = wx.getXrFrameSystem();
  61. const el = this.scene.createElement(xr.XRMesh, {
  62. geometry: "sphere",
  63. scale: "0.1 0.1 0.1",
  64. "sphere-shape": "autoFit: true",
  65. rigidbody: "disabled: true",
  66. "shape-interact": "collide: true; bounciness: 0.5;"
  67. });
  68. const root = this.scene.getElementById("ballRoot");
  69. root.addChild(el);
  70. this.placeBall(el.getComponent("transform"));
  71. this.currentBall = el;
  72. },
  73. placeBall(transform) {
  74. const camera = this.scene.getElementById("camera");
  75. const cam_trans = camera.getComponent("transform");
  76. transform.position.set(cam_trans.position.add(cam_trans.worldForward.scale(-1)));
  77. },
  78. handleTick: function({detail}) {
  79. if (this.currentBall) {
  80. this.placeBall(this.currentBall.getComponent("transform"));
  81. this.recordPosition(detail);
  82. }
  83. },
  84. releaseBall() {
  85. if (!this.currentBall) return;
  86. this.currentBall.getComponent("rigidbody").setData({
  87. disabled: false
  88. });
  89. const r1 = this.vQueue[(this.vQueueHead - 1 + this.vQueueLength) % this.vQueueLength];
  90. const r2 = this.vQueue[this.vQueueHead];
  91. if (r1 && r2) {
  92. const vscale = 1.3; // 初始速度调节
  93. const v = r1[0].sub(r2[0]).scale(vscale * 1000 / (r1[1] - r2[1]));
  94. this.currentBall.getComponent("rigidbody").velocity = v;
  95. }
  96. this.currentBall = undefined;
  97. this.vQueue = [];
  98. this.vQueueHead = 0;
  99. },
  100. recordPosition() {
  101. const transform = this.currentBall.getComponent("transform");
  102. this.vQueue[this.vQueueHead] = [transform.position.clone(), Date.now()];
  103. this.vQueueHead = (this.vQueueHead + 1) % this.vQueueLength;
  104. }
  105. }
  106. })