MeshAdapter.ts 4.1 KB

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