MeshAdapter.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. let propertiesLines: Array<PropertyLine> = [];
  25. for (let dirty of PROPERTIES['Mesh'].properties) {
  26. let infos = new Property(dirty, this._obj);
  27. propertiesLines.push(new PropertyLine(infos));
  28. }
  29. return propertiesLines;
  30. }
  31. public getTools(): Array<AbstractTreeTool> {
  32. let tools = [];
  33. tools.push(new Checkbox(this));
  34. tools.push(new DebugArea(this));
  35. if ((this._obj as BABYLON.AbstractMesh).getTotalVertices() > 0) {
  36. tools.push(new BoundingBox(this));
  37. }
  38. tools.push(new Info(this));
  39. return tools;
  40. }
  41. public setVisible(b: boolean) {
  42. this._obj.setEnabled(b);
  43. this._obj.isVisible = b;
  44. }
  45. public isVisible(): boolean {
  46. return this._obj.isEnabled() && this._obj.isVisible;
  47. }
  48. public isBoxVisible(): boolean {
  49. return (this._obj as BABYLON.AbstractMesh).showBoundingBox;
  50. }
  51. public setBoxVisible(b: boolean) {
  52. return (this._obj as BABYLON.AbstractMesh).showBoundingBox = b;
  53. }
  54. public debug(b: boolean) {
  55. // Draw axis the first time
  56. if (this._axis.length == 0) {
  57. this._drawAxis();
  58. }
  59. // Display or hide axis
  60. for (let ax of this._axis) {
  61. ax.setEnabled(b);
  62. }
  63. }
  64. /** Returns some information about this mesh */
  65. public getInfo(): string {
  66. return `${(this._obj as BABYLON.AbstractMesh).getTotalVertices()} vertices`;
  67. }
  68. /** Overrides super.highlight */
  69. public highlight(b: boolean) {
  70. this.actualObject.renderOutline = b;
  71. this.actualObject.outlineWidth = 0.25;
  72. this.actualObject.outlineColor = BABYLON.Color3.Yellow();
  73. }
  74. /** Draw X, Y and Z axis for the actual object if this adapter.
  75. * Should be called only one time as it will fill this._axis
  76. */
  77. private _drawAxis() {
  78. this._obj.computeWorldMatrix();
  79. var m = this._obj.getWorldMatrix();
  80. // Axis
  81. var x = new BABYLON.Vector3(8, 0, 0);
  82. var y = new BABYLON.Vector3(0, 8, 0);
  83. var z = new BABYLON.Vector3(0, 0, 8);
  84. // Draw an axis of the given color
  85. let _drawAxis = (color, start, end): BABYLON.LinesMesh => {
  86. let axis = BABYLON.Mesh.CreateLines("###axis###", [
  87. start,
  88. end
  89. ], this._obj.getScene());
  90. axis.color = color;
  91. axis.renderingGroupId = 1;
  92. return axis;
  93. };
  94. // X axis
  95. let xAxis = _drawAxis(
  96. BABYLON.Color3.Red(),
  97. this._obj.getAbsolutePosition(),
  98. BABYLON.Vector3.TransformCoordinates(x, m));
  99. xAxis.position.subtractInPlace(this._obj.position);
  100. xAxis.parent = this._obj;
  101. this._axis.push(xAxis);
  102. // Y axis
  103. let yAxis = _drawAxis(
  104. BABYLON.Color3.Green(),
  105. this._obj.getAbsolutePosition(),
  106. BABYLON.Vector3.TransformCoordinates(y, m));
  107. yAxis.parent = this._obj;
  108. yAxis.position.subtractInPlace(this._obj.position);
  109. this._axis.push(yAxis);
  110. // Z axis
  111. let zAxis = _drawAxis(
  112. BABYLON.Color3.Blue(),
  113. this._obj.getAbsolutePosition(),
  114. BABYLON.Vector3.TransformCoordinates(z, m));
  115. zAxis.parent = this._obj;
  116. zAxis.position.subtractInPlace(this._obj.position);
  117. this._axis.push(zAxis);
  118. }
  119. }
  120. }