Viewport.Info.js 2.2 KB

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