Sidebar.Project.Image.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import * as THREE from 'three';
  2. import { UIBreak, UIButton, UIInteger, UIPanel, UIRow, UISelect, UIText } from './libs/ui.js';
  3. import { ViewportPathtracer } from './Viewport.Pathtracer.js';
  4. function SidebarProjectImage( editor ) {
  5. const strings = editor.strings;
  6. const container = new UIPanel();
  7. container.setId( 'render' );
  8. // Image
  9. container.add( new UIText( strings.getKey( 'sidebar/project/image' ) ).setTextTransform( 'uppercase' ) );
  10. container.add( new UIBreak(), new UIBreak() );
  11. // Shading
  12. const shadingRow = new UIRow();
  13. container.add( shadingRow );
  14. shadingRow.add( new UIText( strings.getKey( 'sidebar/project/shading' ) ).setClass( 'Label' ) );
  15. const shadingTypeSelect = new UISelect().setOptions( {
  16. 0: 'SOLID',
  17. 1: 'REALISTIC'
  18. } ).setWidth( '170px' ).setTextTransform( 'unset' ).onChange( refreshShadingRow ).setValue( 0 );
  19. shadingRow.add( shadingTypeSelect );
  20. const pathTracerMinSamples = 3;
  21. const pathTracerMaxSamples = 65536;
  22. const samplesNumber = new UIInteger( 16 ).setRange( pathTracerMinSamples, pathTracerMaxSamples );
  23. const samplesRow = new UIRow();
  24. samplesRow.add( new UIText( strings.getKey( 'sidebar/project/image/sampleCount' ) ).setClass( 'Label' ) );
  25. samplesRow.add( samplesNumber );
  26. container.add( samplesRow );
  27. function refreshShadingRow() {
  28. samplesRow.setHidden( shadingTypeSelect.getValue() !== '1' );
  29. }
  30. refreshShadingRow();
  31. // Resolution
  32. const resolutionRow = new UIRow();
  33. container.add( resolutionRow );
  34. resolutionRow.add( new UIText( strings.getKey( 'sidebar/project/resolution' ) ).setClass( 'Label' ) );
  35. const imageWidth = new UIInteger( 1024 ).setTextAlign( 'center' ).setWidth( '28px' );
  36. resolutionRow.add( imageWidth );
  37. resolutionRow.add( new UIText( '×' ).setTextAlign( 'center' ).setFontSize( '12px' ).setWidth( '12px' ) );
  38. const imageHeight = new UIInteger( 1024 ).setTextAlign( 'center' ).setWidth( '28px' );
  39. resolutionRow.add( imageHeight );
  40. // Render
  41. const renderButton = new UIButton( strings.getKey( 'sidebar/project/render' ) );
  42. renderButton.setWidth( '170px' );
  43. renderButton.setMarginLeft( '120px' );
  44. renderButton.onClick( async () => {
  45. const json = editor.toJSON();
  46. const project = json.project;
  47. //
  48. const loader = new THREE.ObjectLoader();
  49. const camera = loader.parse( json.camera );
  50. camera.aspect = imageWidth.getValue() / imageHeight.getValue();
  51. camera.updateProjectionMatrix();
  52. camera.updateMatrixWorld();
  53. const scene = loader.parse( json.scene );
  54. const renderer = new THREE.WebGLRenderer( { antialias: true } );
  55. renderer.setSize( imageWidth.getValue(), imageHeight.getValue() );
  56. if ( project.shadows !== undefined ) renderer.shadowMap.enabled = project.shadows;
  57. if ( project.shadowType !== undefined ) renderer.shadowMap.type = project.shadowType;
  58. if ( project.toneMapping !== undefined ) renderer.toneMapping = project.toneMapping;
  59. if ( project.toneMappingExposure !== undefined ) renderer.toneMappingExposure = project.toneMappingExposure;
  60. // popup
  61. const width = imageWidth.getValue() / window.devicePixelRatio;
  62. const height = imageHeight.getValue() / window.devicePixelRatio;
  63. const left = ( screen.width - width ) / 2;
  64. const top = ( screen.height - height ) / 2;
  65. const output = window.open( '', '_blank', `location=no,left=${left},top=${top},width=${width},height=${height}` );
  66. const meta = document.createElement( 'meta' );
  67. meta.name = 'viewport';
  68. meta.content = 'width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0';
  69. output.document.head.appendChild( meta );
  70. output.document.body.style.background = '#000';
  71. output.document.body.style.margin = '0px';
  72. output.document.body.style.overflow = 'hidden';
  73. const canvas = renderer.domElement;
  74. canvas.style.width = width + 'px';
  75. canvas.style.height = height + 'px';
  76. output.document.body.appendChild( canvas );
  77. //
  78. switch ( Number( shadingTypeSelect.getValue() ) ) {
  79. case 0: // SOLID
  80. renderer.render( scene, camera );
  81. renderer.dispose();
  82. break;
  83. case 1: // REALISTIC
  84. const status = document.createElement( 'div' );
  85. status.style.position = 'absolute';
  86. status.style.top = '10px';
  87. status.style.left = '10px';
  88. status.style.color = 'white';
  89. status.style.fontFamily = 'system-ui';
  90. status.style.fontSize = '12px';
  91. output.document.body.appendChild( status );
  92. const pathTracer = new ViewportPathtracer( renderer );
  93. pathTracer.init( scene, camera );
  94. pathTracer.setSize( imageWidth.getValue(), imageHeight.getValue() );
  95. const maxSamples = Math.max( pathTracerMinSamples, Math.min( pathTracerMaxSamples, samplesNumber.getValue() ) );
  96. function animate() {
  97. if ( output.closed === true ) return;
  98. const samples = Math.floor( pathTracer.getSamples() ) + 1;
  99. if ( samples < maxSamples ) {
  100. requestAnimationFrame( animate );
  101. }
  102. pathTracer.update();
  103. const progress = Math.floor( samples / maxSamples * 100 );
  104. status.textContent = `${ samples } / ${ maxSamples } ( ${ progress }% )`;
  105. if ( progress === 100 ) {
  106. status.textContent += ' ✓';
  107. }
  108. }
  109. animate();
  110. break;
  111. }
  112. } );
  113. container.add( renderButton );
  114. //
  115. return container;
  116. }
  117. export { SidebarProjectImage };