Sidebar.Project.Image.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. 'solid': 'SOLID',
  17. 'realistic': 'REALISTIC'
  18. } ).setWidth( '170px' ).onChange( refreshShadingRow ).setValue( 'solid' );
  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/samples' ) ).setClass( 'Label' ) );
  25. samplesRow.add( samplesNumber );
  26. container.add( samplesRow );
  27. function refreshShadingRow() {
  28. samplesRow.setHidden( shadingTypeSelect.getValue() !== 'realistic' );
  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. if ( shadingTypeSelect.getValue() === 'realistic' ) {
  46. let isMaterialsValid = true;
  47. editor.scene.traverseVisible( ( object ) => {
  48. if ( object.isMesh ) {
  49. const materials = Array.isArray( object.material ) ? object.material : [ object.material ];
  50. for ( let i = 0; i < materials.length; i ++ ) {
  51. const material = materials[ i ];
  52. if ( ! material.isMeshStandardMaterial ) {
  53. isMaterialsValid = false;
  54. return;
  55. }
  56. }
  57. }
  58. } );
  59. if ( isMaterialsValid === false ) {
  60. alert( strings.getKey( 'prompt/rendering/realistic/unsupportedMaterial' ) );
  61. return;
  62. }
  63. }
  64. //
  65. const json = editor.toJSON();
  66. const project = json.project;
  67. //
  68. const loader = new THREE.ObjectLoader();
  69. const camera = loader.parse( json.camera );
  70. camera.aspect = imageWidth.getValue() / imageHeight.getValue();
  71. camera.updateProjectionMatrix();
  72. camera.updateMatrixWorld();
  73. const scene = loader.parse( json.scene );
  74. const renderer = new THREE.WebGLRenderer( { antialias: true } );
  75. renderer.setSize( imageWidth.getValue(), imageHeight.getValue() );
  76. if ( project.shadows !== undefined ) renderer.shadowMap.enabled = project.shadows;
  77. if ( project.shadowType !== undefined ) renderer.shadowMap.type = project.shadowType;
  78. if ( project.toneMapping !== undefined ) renderer.toneMapping = project.toneMapping;
  79. if ( project.toneMappingExposure !== undefined ) renderer.toneMappingExposure = project.toneMappingExposure;
  80. // popup
  81. const width = imageWidth.getValue() / window.devicePixelRatio;
  82. const height = imageHeight.getValue() / window.devicePixelRatio;
  83. const left = ( screen.width - width ) / 2;
  84. const top = ( screen.height - height ) / 2;
  85. const output = window.open( '', '_blank', `location=no,left=${left},top=${top},width=${width},height=${height}` );
  86. const meta = document.createElement( 'meta' );
  87. meta.name = 'viewport';
  88. meta.content = 'width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0';
  89. output.document.head.appendChild( meta );
  90. output.document.body.style.background = '#000';
  91. output.document.body.style.margin = '0px';
  92. output.document.body.style.overflow = 'hidden';
  93. const canvas = renderer.domElement;
  94. canvas.style.width = width + 'px';
  95. canvas.style.height = height + 'px';
  96. output.document.body.appendChild( canvas );
  97. //
  98. switch ( shadingTypeSelect.getValue() ) {
  99. case 'solid':
  100. renderer.render( scene, camera );
  101. renderer.dispose();
  102. break;
  103. case 'realistic':
  104. const status = document.createElement( 'div' );
  105. status.style.position = 'absolute';
  106. status.style.top = '10px';
  107. status.style.left = '10px';
  108. status.style.color = 'white';
  109. status.style.fontFamily = 'system-ui';
  110. status.style.fontSize = '12px';
  111. output.document.body.appendChild( status );
  112. const pathTracer = new ViewportPathtracer( renderer );
  113. pathTracer.init( scene, camera );
  114. pathTracer.setSize( imageWidth.getValue(), imageHeight.getValue() );
  115. const maxSamples = Math.max( pathTracerMinSamples, Math.min( pathTracerMaxSamples, samplesNumber.getValue() ) );
  116. function animate() {
  117. if ( output.closed === true ) return;
  118. const samples = Math.floor( pathTracer.getSamples() ) + 1;
  119. if ( samples < maxSamples ) {
  120. requestAnimationFrame( animate );
  121. }
  122. pathTracer.update();
  123. const progress = Math.floor( samples / maxSamples * 100 );
  124. status.textContent = `${ samples } / ${ maxSamples } ( ${ progress }% )`;
  125. if ( progress === 100 ) {
  126. status.textContent += ' ✓';
  127. }
  128. }
  129. animate();
  130. break;
  131. }
  132. } );
  133. container.add( renderButton );
  134. //
  135. return container;
  136. }
  137. export { SidebarProjectImage };