TextureTab.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. module INSPECTOR {
  2. export class TextureTab extends Tab {
  3. private _inspector: Inspector;
  4. /** The panel containing a list of items */
  5. protected _treePanel: HTMLElement;
  6. protected _treeItems: Array<TreeItem> = [];
  7. /* Panel containing the texture image */
  8. private _imagePanel: HTMLElement;
  9. constructor(tabbar: TabBar, inspector: Inspector) {
  10. super(tabbar, 'Textures');
  11. this._inspector = inspector;
  12. // Build the properties panel : a div that will contains the tree and the detail panel
  13. this._panel = Helpers.CreateDiv('tab-panel') as HTMLDivElement;
  14. // Build the treepanel
  15. this._treePanel = Helpers.CreateDiv('insp-tree', this._panel);
  16. this._imagePanel = Helpers.CreateDiv('insp-details', this._panel) as HTMLDivElement;
  17. Split([this._treePanel, this._imagePanel], {
  18. blockDrag: this._inspector.popupMode,
  19. direction: 'vertical'
  20. });
  21. this.update();
  22. }
  23. public dispose() {
  24. // Nothing to dispose
  25. }
  26. public update(_items?: Array<TreeItem>) {
  27. let items;
  28. if (_items) {
  29. items = _items;
  30. } else {
  31. // Rebuild the tree
  32. this._treeItems = this._getTree();
  33. items = this._treeItems;
  34. }
  35. // Clean the tree
  36. Helpers.CleanDiv(this._treePanel);
  37. Helpers.CleanDiv(this._imagePanel);
  38. // Sort items alphabetically
  39. items.sort((item1, item2) => {
  40. return item1.compareTo(item2);
  41. });
  42. // Display items
  43. for (let item of items) {
  44. this._treePanel.appendChild(item.toHtml());
  45. }
  46. }
  47. /* Overrides super */
  48. private _getTree(): Array<TreeItem> {
  49. let arr = [];
  50. // get all cameras from the first scene
  51. let instances = this._inspector.scene;
  52. for (let tex of instances.textures) {
  53. arr.push(new TreeItem(this, new TextureAdapter(tex)));
  54. }
  55. return arr;
  56. }
  57. /** Display the details of the given item */
  58. public displayDetails(item: TreeItem) {
  59. // Remove active state on all items
  60. this.activateNode(item);
  61. Helpers.CleanDiv(this._imagePanel);
  62. // Get the texture object
  63. let texture = item.adapter.object;
  64. let imgs: HTMLImageElement[] = [];
  65. let img = Helpers.CreateElement('img', 'texture-image', this._imagePanel) as HTMLImageElement;
  66. imgs.push(img);
  67. //Create five other images elements
  68. for(let i = 0; i<5; i++){
  69. imgs.push(Helpers.CreateElement('img', 'texture-image', this._imagePanel) as HTMLImageElement);
  70. }
  71. if (texture instanceof BABYLON.MapTexture) {
  72. // instance of Map texture
  73. texture.bindTextureForPosSize(new BABYLON.Vector2(0, 0), new BABYLON.Size(texture.getSize().width, texture.getSize().height), false);
  74. BABYLON.Tools.DumpFramebuffer(texture.getSize().width, texture.getSize().height, this._inspector.scene.getEngine(), (data) => img.src = data);
  75. texture.unbindTexture();
  76. }
  77. else if (texture instanceof BABYLON.RenderTargetTexture) {
  78. // RenderTarget textures
  79. let scene = this._inspector.scene;
  80. let engine = scene.getEngine();
  81. let size = texture.getSize();
  82. // Clone the texture
  83. let screenShotTexture = texture.clone();
  84. screenShotTexture.activeCamera = texture.activeCamera;
  85. screenShotTexture.onBeforeRender = texture.onBeforeRender;
  86. screenShotTexture.onAfterRender = texture.onAfterRender;
  87. screenShotTexture.onBeforeRenderObservable = texture.onBeforeRenderObservable;
  88. // To display the texture after rendering
  89. screenShotTexture.onAfterRenderObservable.add((faceIndex: number) => {
  90. BABYLON.Tools.DumpFramebuffer(size.width, size.height, engine,
  91. (data) => imgs[faceIndex].src = data, "image/png");
  92. });
  93. // Render the texture
  94. scene.incrementRenderId();
  95. scene.resetCachedMaterial();
  96. screenShotTexture.render();
  97. screenShotTexture.dispose();
  98. }else if(texture instanceof BABYLON.CubeTexture){
  99. // Display all textures of the CubeTexture
  100. let i: number = 0;
  101. for(let filename of (texture as BABYLON.CubeTexture)['_files']){
  102. imgs[i].src = filename;
  103. i++;
  104. }
  105. }
  106. else if (texture.url) {
  107. // If an url is present, the texture is an image
  108. img.src = texture.url;
  109. } else if (texture['_canvas']) {
  110. // Dynamic texture
  111. let base64Image = texture['_canvas'].toDataURL("image/png");
  112. img.src = base64Image;
  113. }
  114. }
  115. /** Select an item in the tree */
  116. public select(item: TreeItem) {
  117. // Active the node
  118. this.activateNode(item);
  119. // Display its details
  120. this.displayDetails(item);
  121. }
  122. /** Set the given item as active in the tree */
  123. public activateNode(item: TreeItem) {
  124. if (this._treeItems) {
  125. for (let node of this._treeItems) {
  126. node.active(false);
  127. }
  128. }
  129. item.active(true);
  130. }
  131. }
  132. }