spherePanel.ts 2.2 KB

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