Viewport.Info.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { UIPanel, UIBreak, UIText } from './libs/ui.js';
  2. function ViewportInfo( editor ) {
  3. const signals = editor.signals;
  4. const strings = editor.strings;
  5. const container = new UIPanel();
  6. container.setId( 'info' );
  7. container.setPosition( 'absolute' );
  8. container.setLeft( '10px' );
  9. container.setBottom( '20px' );
  10. container.setFontSize( '12px' );
  11. container.setColor( '#fff' );
  12. container.setTextTransform( 'lowercase' );
  13. const objectsText = new UIText( '0' ).setTextAlign( 'right' ).setWidth( '60px' ).setMarginRight( '6px' );
  14. const verticesText = new UIText( '0' ).setTextAlign( 'right' ).setWidth( '60px' ).setMarginRight( '6px' );
  15. const trianglesText = new UIText( '0' ).setTextAlign( 'right' ).setWidth( '60px' ).setMarginRight( '6px' );
  16. const frametimeText = new UIText( '0' ).setTextAlign( 'right' ).setWidth( '60px' ).setMarginRight( '6px' );
  17. container.add( objectsText, new UIText( strings.getKey( 'viewport/info/objects' ) ), new UIBreak() );
  18. container.add( verticesText, new UIText( strings.getKey( 'viewport/info/vertices' ) ), new UIBreak() );
  19. container.add( trianglesText, new UIText( strings.getKey( 'viewport/info/triangles' ) ), new UIBreak() );
  20. container.add( frametimeText, new UIText( strings.getKey( 'viewport/info/rendertime' ) ), new UIBreak() );
  21. signals.objectAdded.add( update );
  22. signals.objectRemoved.add( update );
  23. signals.geometryChanged.add( update );
  24. signals.sceneRendered.add( updateFrametime );
  25. //
  26. function update() {
  27. const scene = editor.scene;
  28. let objects = 0, vertices = 0, triangles = 0;
  29. for ( let i = 0, l = scene.children.length; i < l; i ++ ) {
  30. const object = scene.children[ i ];
  31. object.traverseVisible( function ( object ) {
  32. objects ++;
  33. if ( object.isMesh || object.isPoints ) {
  34. const geometry = object.geometry;
  35. vertices += geometry.attributes.position.count;
  36. if ( object.isMesh ) {
  37. if ( geometry.index !== null ) {
  38. triangles += geometry.index.count / 3;
  39. } else {
  40. triangles += geometry.attributes.position.count / 3;
  41. }
  42. }
  43. }
  44. } );
  45. }
  46. objectsText.setValue( objects.format() );
  47. verticesText.setValue( vertices.format() );
  48. trianglesText.setValue( triangles.format() );
  49. }
  50. function updateFrametime( frametime ) {
  51. frametimeText.setValue( Number( frametime ).toFixed( 2 ) );
  52. }
  53. return container;
  54. }
  55. export { ViewportInfo };