spherePanel.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { Tools } from "babylonjs/Misc/tools";
  2. import { Space, Axis, Matrix, TmpVectors, Vector3 } from "babylonjs/Maths/math";
  3. import { float } from "babylonjs/types";
  4. import { VolumeBasedPanel } from "./volumeBasedPanel";
  5. import { Control3D } from "./control3D";
  6. import { Container3D } from "./container3D";
  7. /**
  8. * Class used to create a container panel deployed on the surface of a sphere
  9. */
  10. export class SpherePanel extends VolumeBasedPanel {
  11. private _radius = 5.0;
  12. /**
  13. * Gets or sets the radius of the sphere where to project controls (5 by default)
  14. */
  15. public get radius(): float {
  16. return this._radius;
  17. }
  18. public set radius(value: float) {
  19. if (this._radius === value) {
  20. return;
  21. }
  22. this._radius = value;
  23. Tools.SetImmediate(() => {
  24. this._arrangeChildren();
  25. });
  26. }
  27. protected _mapGridNode(control: Control3D, nodePosition: Vector3) {
  28. let mesh = control.mesh;
  29. if (!mesh) {
  30. return;
  31. }
  32. let newPos = this._sphericalMapping(nodePosition);
  33. control.position = newPos;
  34. switch (this.orientation) {
  35. case Container3D.FACEORIGIN_ORIENTATION:
  36. mesh.lookAt(new Vector3(2 * newPos.x, 2 * newPos.y, 2 * newPos.z));
  37. break;
  38. case Container3D.FACEORIGINREVERSED_ORIENTATION:
  39. mesh.lookAt(new Vector3(-newPos.x, -newPos.y, -newPos.z));
  40. break;
  41. case Container3D.FACEFORWARD_ORIENTATION:
  42. break;
  43. case Container3D.FACEFORWARDREVERSED_ORIENTATION:
  44. mesh.rotate(Axis.Y, Math.PI, Space.LOCAL);
  45. break;
  46. }
  47. }
  48. private _sphericalMapping(source: Vector3) {
  49. let newPos = new Vector3(0, 0, this._radius);
  50. let xAngle = (source.y / this._radius);
  51. let yAngle = -(source.x / this._radius);
  52. Matrix.RotationYawPitchRollToRef(yAngle, xAngle, 0, TmpVectors.Matrix[0]);
  53. return Vector3.TransformNormal(newPos, TmpVectors.Matrix[0]);
  54. }
  55. }