index.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. Component({
  2. behaviors: [require('../common/share-behavior').default],
  3. properties: {
  4. a: Number,
  5. },
  6. data: {
  7. loaded: false,
  8. touchingMoon: false,
  9. touchingEarth: false,
  10. θ: Math.PI,
  11. r: 10,
  12. ω: 5e-4,
  13. outerRing: 20,
  14. innerRing: 10
  15. },
  16. lifetimes: {},
  17. methods: {
  18. handleReady({detail}) {
  19. const xrScene = this.scene = detail.value;
  20. console.log('xr-scene', xrScene);
  21. },
  22. handleAssetsProgress: function({detail}) {
  23. console.log('assets progress', detail.value);
  24. },
  25. handleAssetsLoaded: function({detail}) {
  26. console.log('assets loaded', detail.value);
  27. this.setData({loaded: true});
  28. },
  29. handleTouchEarth: function() {
  30. this.setData({
  31. touchingEarth: true
  32. });
  33. },
  34. handleUntouchEarth: function() {
  35. this.setData({
  36. touchingEarth: false
  37. });
  38. },
  39. handleEarthRotation: function({detail}) {
  40. const { target, deltaX } = detail.value;
  41. target._components.transform.rotation.y += deltaX / 100;
  42. },
  43. handleDragMoon: function({detail}) {
  44. const { dir, target, camera } = detail.value;
  45. const cameraPos = camera.el._components.transform.worldPosition;
  46. const k = -cameraPos.y / dir[1];
  47. const x = cameraPos.x + k * dir[0];
  48. const z = cameraPos.z + k * dir[2];
  49. const len = Math.sqrt(x * x + z * z);
  50. if (len > this.data.innerRing) {
  51. const transform = target._components.transform;
  52. const scale = len > this.data.outerRing ? this.data.outerRing / len : 1.0;
  53. transform.position.x = x * scale;
  54. transform.position.z = z * scale;
  55. }
  56. },
  57. handleTouchMoon: function() {
  58. this.setData({touchingMoon: true});
  59. },
  60. handleUntouchMoon: function() {
  61. const moon = this.scene.getNodeById("mesh-moon");
  62. const transform = moon.el._components.transform;
  63. const x = transform.position.x;
  64. const z = transform.position.z;
  65. const len = Math.sqrt(x * x + z * z);
  66. this.setData({
  67. r: len,
  68. θ: x < 0 ? Math.atan(z / x) + Math.PI : Math.atan(z / x),
  69. ω: Math.sqrt(2.5e-4 / (len * len * len))
  70. });
  71. this.setData({touchingMoon: false});
  72. },
  73. handleTick: function({detail}) {
  74. if (this.data.touchingMoon || !this.scene) return;
  75. const deltaTime = detail.value;
  76. const moon = this.scene.getNodeById("mesh-moon");
  77. const transform = moon.el._components.transform;
  78. const x = Math.cos(this.data.θ) * this.data.r;
  79. const z = Math.sin(this.data.θ) * this.data.r;
  80. transform.position.x = x;
  81. transform.position.z = z;
  82. transform.rotation.y -= this.data.ω * deltaTime;
  83. this.setData({
  84. θ: this.data.θ + this.data.ω * deltaTime
  85. });
  86. }
  87. }
  88. })