MeshAdapter.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. module INSPECTOR {
  2. export class MeshAdapter
  3. extends Adapter
  4. implements IToolVisible, IToolDebug, IToolBoundingBox, IToolInfo {
  5. /** Keep track of the axis of the actual object */
  6. private _axesViewer: BABYLON.Nullable<any>;
  7. private onBeforeRenderObserver: BABYLON.Nullable<BABYLON.Observer<BABYLON.Scene>>;
  8. constructor(mesh: BABYLON.Node) {
  9. super(mesh);
  10. new BABYLON.Debug.AxesViewer(mesh.getScene());
  11. }
  12. /** Returns the name displayed in the tree */
  13. public id(): string {
  14. let str = '';
  15. if (this._obj.name) {
  16. str = this._obj.name;
  17. } // otherwise nothing displayed
  18. return str;
  19. }
  20. /** Returns the type of this object - displayed in the tree */
  21. public type(): string {
  22. return Helpers.GET_TYPE(this._obj);
  23. }
  24. /** Returns the list of properties to be displayed for this adapter */
  25. public getProperties(): Array<PropertyLine> {
  26. return Helpers.GetAllLinesProperties(this._obj);
  27. }
  28. public getTools(): Array<AbstractTreeTool> {
  29. let tools = [];
  30. tools.push(new Checkbox(this));
  31. tools.push(new DebugArea(this));
  32. if (this._obj instanceof BABYLON.AbstractMesh) {
  33. if ((this._obj as BABYLON.AbstractMesh).getTotalVertices() > 0) {
  34. tools.push(new BoundingBox(this));
  35. }
  36. }
  37. tools.push(new Info(this));
  38. return tools;
  39. }
  40. public setVisible(b: boolean) {
  41. this._obj.setEnabled(b);
  42. this._obj.isVisible = b;
  43. }
  44. public isVisible(): boolean {
  45. return this._obj.isEnabled() && (this._obj.isVisible === undefined || this._obj.isVisible);
  46. }
  47. public isBoxVisible(): boolean {
  48. return (this._obj as BABYLON.AbstractMesh).showBoundingBox;
  49. }
  50. public setBoxVisible(b: boolean) {
  51. return (this._obj as BABYLON.AbstractMesh).showBoundingBox = b;
  52. }
  53. public debug(enable: boolean) {
  54. // Draw axis the first time
  55. if (!this._axesViewer) {
  56. this._drawAxis();
  57. }
  58. // Display or hide axis
  59. if (!enable && this._axesViewer) {
  60. let mesh = this._obj as BABYLON.AbstractMesh;
  61. mesh.getScene().onBeforeRenderObservable.remove(this.onBeforeRenderObserver);
  62. this._axesViewer.dispose();
  63. this._axesViewer = null;
  64. }
  65. }
  66. /** Returns some information about this mesh */
  67. public getInfo(): string {
  68. if (this._obj instanceof BABYLON.AbstractMesh) {
  69. return `${(this._obj as BABYLON.AbstractMesh).getTotalVertices()} vertices`;
  70. }
  71. return '0 vertices';
  72. }
  73. /** Draw X, Y and Z axis for the actual object if this adapter.
  74. * Should be called only one time as it will fill this._axis
  75. */
  76. private _drawAxis() {
  77. this._obj.computeWorldMatrix();
  78. // Axis
  79. var x = new BABYLON.Vector3(1, 0, 0);
  80. var y = new BABYLON.Vector3(0, 1, 0);
  81. var z = new BABYLON.Vector3(0, 0, 1);
  82. this._axesViewer = new BABYLON.Debug.AxesViewer(this._obj.getScene());
  83. let mesh = this._obj as BABYLON.TransformNode;
  84. this.onBeforeRenderObserver = mesh.getScene().onBeforeRenderObservable.add(() => {
  85. let matrix = mesh.getWorldMatrix();
  86. let extend = new BABYLON.Vector3(1, 1, 1);
  87. if (mesh instanceof BABYLON.AbstractMesh) {
  88. extend = mesh.getBoundingInfo().boundingBox.extendSizeWorld;
  89. }
  90. this._axesViewer!.scaleLines = Math.max(extend.x, extend.y, extend.z) * 2;
  91. this._axesViewer!.update(this._obj.position, BABYLON.Vector3.TransformNormal(x, matrix), BABYLON.Vector3.TransformNormal(y, matrix), BABYLON.Vector3.TransformNormal(z, matrix));
  92. });
  93. }
  94. }
  95. }