MeshAdapter.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. tools.push(new BoundingBox(this));
  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(b:boolean) {
  53. // Draw axis the first time
  54. if (this._axis.length == 0) {
  55. this._drawAxis();
  56. }
  57. // Display or hide axis
  58. for (let ax of this._axis) {
  59. ax.setEnabled(b);
  60. }
  61. }
  62. /** Returns some information about this mesh */
  63. public getInfo() : string {
  64. return `${(this._obj as BABYLON.AbstractMesh).getTotalVertices()} vertices`;
  65. }
  66. /** Overrides super.highlight */
  67. public highlight(b:boolean) {
  68. this.actualObject.renderOutline = b;
  69. this.actualObject.outlineWidth = 0.25;
  70. this.actualObject.outlineColor = BABYLON.Color3.Yellow();
  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. var m = this._obj.getWorldMatrix();
  78. // Axis
  79. var x = new BABYLON.Vector3(8,0,0);
  80. var y = new BABYLON.Vector3(0,8,0);
  81. var z = new BABYLON.Vector3(0,0,8);
  82. // Draw an axis of the given color
  83. let _drawAxis = (color, start, end) : BABYLON.LinesMesh => {
  84. let axis = BABYLON.Mesh.CreateLines("###axis###", [
  85. start,
  86. end
  87. ], this._obj.getScene());
  88. axis.color = color;
  89. axis.renderingGroupId = 1;
  90. return axis;
  91. };
  92. // X axis
  93. let xAxis = _drawAxis(
  94. BABYLON.Color3.Red(),
  95. this._obj.getAbsolutePosition(),
  96. BABYLON.Vector3.TransformCoordinates(x, m));
  97. xAxis.position.subtractInPlace(this._obj.position);
  98. xAxis.parent = this._obj;
  99. this._axis.push(xAxis);
  100. // Y axis
  101. let yAxis = _drawAxis(
  102. BABYLON.Color3.Green(),
  103. this._obj.getAbsolutePosition(),
  104. BABYLON.Vector3.TransformCoordinates(y, m));
  105. yAxis.parent = this._obj;
  106. yAxis.position.subtractInPlace(this._obj.position);
  107. this._axis.push(yAxis);
  108. // Z axis
  109. let zAxis = _drawAxis(
  110. BABYLON.Color3.Blue(),
  111. this._obj.getAbsolutePosition(),
  112. BABYLON.Vector3.TransformCoordinates(z, m));
  113. zAxis.parent = this._obj;
  114. zAxis.position.subtractInPlace(this._obj.position);
  115. this._axis.push(zAxis);
  116. }
  117. }
  118. }