MeshAdapter.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 _axis: Array<BABYLON.Mesh> = [];
  7. constructor(obj: BABYLON.AbstractMesh) {
  8. super(obj);
  9. }
  10. /** Returns the name displayed in the tree */
  11. public id(): string {
  12. let str = '';
  13. if (this._obj.name) {
  14. str = this._obj.name;
  15. } // otherwise nothing displayed
  16. return str;
  17. }
  18. /** Returns the type of this object - displayed in the tree */
  19. public type(): string {
  20. return Helpers.GET_TYPE(this._obj);
  21. }
  22. /** Returns the list of properties to be displayed for this adapter */
  23. public getProperties(): Array<PropertyLine> {
  24. return Helpers.GetAllLinesProperties(this._obj);
  25. }
  26. public getTools(): Array<AbstractTreeTool> {
  27. let tools = [];
  28. tools.push(new Checkbox(this));
  29. tools.push(new DebugArea(this));
  30. if ((this._obj as BABYLON.AbstractMesh).getTotalVertices() > 0) {
  31. tools.push(new BoundingBox(this));
  32. }
  33. tools.push(new Info(this));
  34. return tools;
  35. }
  36. public setVisible(b: boolean) {
  37. this._obj.setEnabled(b);
  38. this._obj.isVisible = b;
  39. }
  40. public isVisible(): boolean {
  41. return this._obj.isEnabled() && this._obj.isVisible;
  42. }
  43. public isBoxVisible(): boolean {
  44. return (this._obj as BABYLON.AbstractMesh).showBoundingBox;
  45. }
  46. public setBoxVisible(b: boolean) {
  47. return (this._obj as BABYLON.AbstractMesh).showBoundingBox = b;
  48. }
  49. public debug(b: boolean) {
  50. // Draw axis the first time
  51. if (this._axis.length == 0) {
  52. this._drawAxis();
  53. }
  54. // Display or hide axis
  55. for (let ax of this._axis) {
  56. ax.setEnabled(b);
  57. }
  58. }
  59. /** Returns some information about this mesh */
  60. public getInfo(): string {
  61. return `${(this._obj as BABYLON.AbstractMesh).getTotalVertices()} vertices`;
  62. }
  63. /** Draw X, Y and Z axis for the actual object if this adapter.
  64. * Should be called only one time as it will fill this._axis
  65. */
  66. private _drawAxis() {
  67. this._obj.computeWorldMatrix();
  68. var m = this._obj.getWorldMatrix();
  69. // Axis
  70. var x = new BABYLON.Vector3(8 / (this._obj as BABYLON.AbstractMesh).scaling.x, 0, 0);
  71. var y = new BABYLON.Vector3(0, 8 / (this._obj as BABYLON.AbstractMesh).scaling.y, 0);
  72. var z = new BABYLON.Vector3(0, 0, 8 / (this._obj as BABYLON.AbstractMesh).scaling.z);
  73. // Draw an axis of the given color
  74. let _drawAxis = (color, start, end): BABYLON.LinesMesh => {
  75. let axis = BABYLON.Mesh.CreateLines("###axis###", [
  76. start,
  77. end
  78. ], this._obj.getScene());
  79. axis.color = color;
  80. axis.renderingGroupId = 1;
  81. return axis;
  82. };
  83. // X axis
  84. let xAxis = _drawAxis(
  85. BABYLON.Color3.Red(),
  86. BABYLON.Vector3.Zero(),
  87. x);
  88. xAxis.parent = this._obj;
  89. this._axis.push(xAxis);
  90. // Y axis
  91. let yAxis = _drawAxis(
  92. BABYLON.Color3.Green(),
  93. BABYLON.Vector3.Zero(),
  94. y);
  95. yAxis.parent = this._obj;
  96. this._axis.push(yAxis);
  97. // Z axis
  98. let zAxis = _drawAxis(
  99. BABYLON.Color3.Blue(),
  100. BABYLON.Vector3.Zero(),
  101. z);
  102. zAxis.parent = this._obj;
  103. this._axis.push(zAxis);
  104. }
  105. }
  106. }