Viewport.Controls.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { UIPanel, UISelect } from './libs/ui.js';
  2. function ViewportControls( editor ) {
  3. const signals = editor.signals;
  4. const container = new UIPanel();
  5. container.setPosition( 'absolute' );
  6. container.setRight( '10px' );
  7. container.setTop( '10px' );
  8. // camera
  9. const cameraSelect = new UISelect();
  10. cameraSelect.setMarginRight( '10px' );
  11. cameraSelect.onChange( function () {
  12. editor.setViewportCamera( this.getValue() );
  13. } );
  14. container.add( cameraSelect );
  15. signals.cameraAdded.add( update );
  16. signals.cameraRemoved.add( update );
  17. signals.objectChanged.add( function ( object ) {
  18. if ( object.isCamera ) {
  19. update( false );
  20. }
  21. } );
  22. // shading
  23. const shadingSelect = new UISelect();
  24. shadingSelect.setOptions( { 'realistic': 'realistic', 'solid': 'solid', 'normals': 'normals', 'wireframe': 'wireframe' } );
  25. shadingSelect.setValue( 'solid' );
  26. shadingSelect.onChange( function () {
  27. editor.setViewportShading( this.getValue() );
  28. } );
  29. container.add( shadingSelect );
  30. signals.editorCleared.add( function () {
  31. editor.setViewportCamera( editor.camera.uuid );
  32. shadingSelect.setValue( 'solid' );
  33. editor.setViewportShading( shadingSelect.getValue() );
  34. } );
  35. signals.cameraResetted.add( update );
  36. update();
  37. //
  38. function update( effect = true ) {
  39. const options = {};
  40. const cameras = editor.cameras;
  41. for ( const key in cameras ) {
  42. const camera = cameras[ key ];
  43. options[ camera.uuid ] = camera.name;
  44. }
  45. cameraSelect.setOptions( options );
  46. const selectedCamera = ( editor.viewportCamera.uuid in options )
  47. ? editor.viewportCamera
  48. : editor.camera;
  49. cameraSelect.setValue( selectedCamera.uuid );
  50. if ( effect ) editor.setViewportCamera( selectedCamera.uuid );
  51. }
  52. return container;
  53. }
  54. export { ViewportControls };
粤ICP备19079148号