MeshAdapter.ts 4.1 KB

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