Sidebar.Project.Resources.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. import { UIPanel, UIText, UITabbedPanel, UIListbox, UIButton } from './libs/ui.js';
  2. import { SetGeometryCommand } from './commands/SetGeometryCommand.js';
  3. import { SetMaterialCommand } from './commands/SetMaterialCommand.js';
  4. function SidebarProjectResources( editor ) {
  5. const signals = editor.signals;
  6. const strings = editor.strings;
  7. const container = new UITabbedPanel();
  8. // Geometries
  9. const geometriesTab = new UIPanel();
  10. geometriesTab.dom.style.borderTop = 'none';
  11. const geometriesListbox = new UIListbox();
  12. geometriesListbox.dom.style.height = '140px';
  13. geometriesListbox.dom.style.resize = 'vertical';
  14. geometriesListbox.dom.style.marginBottom = '10px';
  15. geometriesTab.add( geometriesListbox );
  16. const geometriesAssign = new UIButton( strings.getKey( 'sidebar/project/Assign' ) );
  17. geometriesTab.add( geometriesAssign );
  18. const geometriesInfo = new UIText();
  19. geometriesInfo.dom.style.float = 'right';
  20. geometriesTab.add( geometriesInfo );
  21. geometriesAssign.onClick( function () {
  22. const selectedObject = editor.selected;
  23. if ( selectedObject !== null && selectedObject.geometry !== undefined ) {
  24. const selectedId = geometriesListbox.getValue();
  25. const geometries = Object.values( editor.geometries );
  26. const geometry = geometries.find( g => g.id === parseInt( selectedId ) );
  27. if ( geometry !== undefined ) {
  28. editor.execute( new SetGeometryCommand( editor, selectedObject, geometry ) );
  29. }
  30. }
  31. } );
  32. container.addTab( 'geometries', strings.getKey( 'sidebar/project/geometries' ), geometriesTab );
  33. // Materials
  34. const materialsTab = new UIPanel();
  35. materialsTab.dom.style.borderTop = 'none';
  36. const materialsListbox = new UIListbox();
  37. materialsListbox.dom.style.height = '140px';
  38. materialsListbox.dom.style.resize = 'vertical';
  39. materialsListbox.dom.style.marginBottom = '10px';
  40. materialsTab.add( materialsListbox );
  41. const materialsAssign = new UIButton( strings.getKey( 'sidebar/project/Assign' ) );
  42. materialsTab.add( materialsAssign );
  43. const materialsInfo = new UIText();
  44. materialsInfo.dom.style.float = 'right';
  45. materialsTab.add( materialsInfo );
  46. materialsAssign.onClick( function () {
  47. const selectedObject = editor.selected;
  48. if ( selectedObject !== null && selectedObject.material !== undefined ) {
  49. const selectedId = materialsListbox.getValue();
  50. const materials = Object.values( editor.materials );
  51. const material = materials.find( m => m.id === parseInt( selectedId ) );
  52. if ( material !== undefined ) {
  53. editor.execute( new SetMaterialCommand( editor, selectedObject, material ) );
  54. }
  55. }
  56. } );
  57. container.addTab( 'materials', strings.getKey( 'sidebar/project/materials' ), materialsTab );
  58. // Textures
  59. const texturesTab = new UIPanel();
  60. texturesTab.dom.style.borderTop = 'none';
  61. const texturesListbox = new UIListbox();
  62. texturesListbox.dom.style.height = '140px';
  63. texturesListbox.dom.style.resize = 'vertical';
  64. texturesListbox.dom.style.marginBottom = '10px';
  65. texturesTab.add( texturesListbox );
  66. const texturesInfo = new UIText();
  67. texturesInfo.dom.style.float = 'right';
  68. texturesTab.add( texturesInfo );
  69. container.addTab( 'textures', strings.getKey( 'sidebar/project/textures' ), texturesTab );
  70. container.select( 'geometries' );
  71. // Signals
  72. function refreshGeometriesUI() {
  73. const geometries = Object.values( editor.geometries );
  74. geometriesListbox.setItems( geometries );
  75. geometriesInfo.setValue( geometries.length + ' ' + strings.getKey( 'sidebar/project/geometries' ).toLowerCase() );
  76. }
  77. function refreshMaterialsUI() {
  78. const materials = Object.values( editor.materials );
  79. materialsListbox.setItems( materials );
  80. materialsInfo.setValue( materials.length + ' ' + strings.getKey( 'sidebar/project/materials' ).toLowerCase() );
  81. }
  82. function refreshTexturesUI() {
  83. const textures = [];
  84. const types = [];
  85. const texturesUsed = new Set();
  86. const materials = Object.values( editor.materials );
  87. for ( const material of materials ) {
  88. for ( const property in material ) {
  89. const value = material[ property ];
  90. if ( value !== null && value !== undefined && value.isTexture === true ) {
  91. if ( texturesUsed.has( value.uuid ) === false ) {
  92. textures.push( value );
  93. types.push( getTextureType( value ) );
  94. texturesUsed.add( value.uuid );
  95. }
  96. }
  97. }
  98. }
  99. texturesListbox.setItems( textures, types );
  100. texturesInfo.setValue( textures.length + ' ' + strings.getKey( 'sidebar/project/textures' ).toLowerCase() );
  101. }
  102. function getTextureType( texture ) {
  103. if ( texture.isCanvasTexture ) return 'CanvasTexture';
  104. if ( texture.isVideoTexture ) return 'VideoTexture';
  105. if ( texture.isCubeDepthTexture ) return 'CubeDepthTexture';
  106. if ( texture.isDepthTexture ) return 'DepthTexture';
  107. if ( texture.isCompressedArrayTexture ) return 'CompressedArrayTexture';
  108. if ( texture.isCompressedCubeTexture ) return 'CompressedCubeTexture';
  109. if ( texture.isCompressedTexture ) return 'CompressedTexture';
  110. if ( texture.isCubeTexture ) return 'CubeTexture';
  111. if ( texture.isData3DTexture ) return 'Data3DTexture';
  112. if ( texture.isDataArrayTexture ) return 'DataArrayTexture';
  113. if ( texture.isDataTexture ) return 'DataTexture';
  114. if ( texture.isFramebufferTexture ) return 'FramebufferTexture';
  115. return 'Texture';
  116. }
  117. function refreshUI() {
  118. refreshGeometriesUI();
  119. refreshMaterialsUI();
  120. refreshTexturesUI();
  121. }
  122. let timeout;
  123. function refreshUIDelayed() {
  124. clearTimeout( timeout );
  125. timeout = setTimeout( refreshUI, 100 );
  126. }
  127. signals.editorCleared.add( refreshUIDelayed );
  128. signals.sceneGraphChanged.add( refreshUIDelayed );
  129. signals.geometryChanged.add( refreshUIDelayed );
  130. signals.materialAdded.add( refreshUIDelayed );
  131. signals.materialChanged.add( refreshUIDelayed );
  132. signals.materialRemoved.add( refreshUIDelayed );
  133. signals.objectSelected.add( function ( object ) {
  134. if ( object !== null ) {
  135. const geometries = Object.values( editor.geometries );
  136. const materials = Object.values( editor.materials );
  137. if ( object.geometry !== undefined ) {
  138. const geometryIndex = geometries.indexOf( object.geometry );
  139. geometriesListbox.selectIndex( geometryIndex );
  140. }
  141. if ( object.material !== undefined ) {
  142. const material = Array.isArray( object.material ) ? object.material[ 0 ] : object.material;
  143. const materialIndex = materials.indexOf( material );
  144. materialsListbox.selectIndex( materialIndex );
  145. }
  146. } else {
  147. geometriesListbox.selectIndex( - 1 );
  148. materialsListbox.selectIndex( - 1 );
  149. }
  150. } );
  151. return container;
  152. }
  153. export { SidebarProjectResources };
粤ICP备19079148号