MeshTab.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { Node, TransformNode } from "babylonjs";
  2. import { MeshAdapter } from "../adapters/MeshAdapter";
  3. import { Helpers } from "../helpers/Helpers";
  4. import { Inspector } from "../Inspector";
  5. import { TreeItem } from "../tree/TreeItem";
  6. import { PropertyTab } from "./PropertyTab";
  7. import { TabBar } from "./TabBar";
  8. export class MeshTab extends PropertyTab {
  9. constructor(tabbar: TabBar, inspector: Inspector) {
  10. super(tabbar, 'Mesh', inspector);
  11. }
  12. /* Overrides super */
  13. protected _getTree(): Array<TreeItem> {
  14. let arr = new Array<TreeItem>();
  15. // Tab containing mesh already in results
  16. let alreadyIn = new Array<Node>();
  17. // Recursive method building the tree panel
  18. let createNode = (obj: Node) => {
  19. let descendants = obj.getDescendants(true);
  20. let node = new TreeItem(this, new MeshAdapter(obj));
  21. if (descendants.length > 0) {
  22. for (let child of descendants) {
  23. if (child instanceof TransformNode) {
  24. if (!Helpers.IsSystemName(child.name)) {
  25. let n = createNode(child);
  26. node.add(n);
  27. }
  28. }
  29. }
  30. node.update();
  31. }
  32. // Retrieve the root node if the mesh is actually child of another mesh
  33. // This can hapen if the child mesh has been created before the parent mesh
  34. if (obj.parent != null && alreadyIn.indexOf(obj) != -1) {
  35. let i: number = 0;
  36. let notFound: boolean = true;
  37. // Find and delete the root node standing for this mesh
  38. while (i < arr.length && notFound) {
  39. if (obj.name === arr[i].id) {
  40. arr.splice(i, 1);
  41. notFound = false;
  42. }
  43. i++;
  44. }
  45. }
  46. alreadyIn.push(obj);
  47. return node;
  48. };
  49. // get all meshes from the first scene
  50. let instances = this._inspector.scene;
  51. // Find top of hierarchy for meshes...
  52. let meshWithoutAnyParent: Array<Node> = [];
  53. for (let mesh of instances.meshes) {
  54. // Not already in the array, not system name and no parent
  55. if (meshWithoutAnyParent.indexOf(mesh) == -1 && !Helpers.IsSystemName(mesh.name) && !mesh.parent) {
  56. meshWithoutAnyParent.push(mesh);
  57. }
  58. }
  59. // ... and for transforms
  60. for (let tn of instances.transformNodes) {
  61. // Not already in the array, not system name and no parent
  62. if (meshWithoutAnyParent.indexOf(tn) == -1 && !Helpers.IsSystemName(tn.name) && !tn.parent) {
  63. meshWithoutAnyParent.push(tn);
  64. }
  65. }
  66. for (let mesh of meshWithoutAnyParent) {
  67. if (alreadyIn.indexOf(mesh) == -1 && !Helpers.IsSystemName(mesh.name)) {
  68. let node = createNode(mesh);
  69. arr.push(node);
  70. }
  71. }
  72. return arr;
  73. }
  74. }