Sidebar.Project.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. import * as THREE from '../../build/three.module.js';
  5. import { UIPanel, UIRow, UIInput, UICheckbox, UIText, UIListbox, UISpan, UIButton, UISelect, UINumber } from './libs/ui.js';
  6. import { UIBoolean } from './libs/ui.three.js';
  7. import { SetMaterialCommand } from './commands/SetMaterialCommand.js';
  8. function SidebarProject( editor ) {
  9. var config = editor.config;
  10. var signals = editor.signals;
  11. var strings = editor.strings;
  12. var currentRenderer = null;
  13. var container = new UISpan();
  14. var projectsettings = new UIPanel();
  15. projectsettings.setBorderTop( '0' );
  16. projectsettings.setPaddingTop( '20px' );
  17. container.add( projectsettings );
  18. // Title
  19. var titleRow = new UIRow();
  20. var title = new UIInput( config.getKey( 'project/title' ) ).setLeft( '100px' ).setWidth( '150px' ).onChange( function () {
  21. config.setKey( 'project/title', this.getValue() );
  22. } );
  23. titleRow.add( new UIText( strings.getKey( 'sidebar/project/title' ) ).setWidth( '90px' ) );
  24. titleRow.add( title );
  25. projectsettings.add( titleRow );
  26. // Editable
  27. var editableRow = new UIRow();
  28. var editable = new UICheckbox( config.getKey( 'project/editable' ) ).setLeft( '100px' ).onChange( function () {
  29. config.setKey( 'project/editable', this.getValue() );
  30. } );
  31. editableRow.add( new UIText( strings.getKey( 'sidebar/project/editable' ) ).setWidth( '90px' ) );
  32. editableRow.add( editable );
  33. projectsettings.add( editableRow );
  34. // WebVR
  35. var vrRow = new UIRow();
  36. var vr = new UICheckbox( config.getKey( 'project/vr' ) ).setLeft( '100px' ).onChange( function () {
  37. config.setKey( 'project/vr', this.getValue() );
  38. } );
  39. vrRow.add( new UIText( strings.getKey( 'sidebar/project/vr' ) ).setWidth( '90px' ) );
  40. vrRow.add( vr );
  41. projectsettings.add( vrRow );
  42. // Renderer
  43. var rendererPanel = new UIPanel();
  44. container.add( rendererPanel );
  45. var headerRow = new UIRow();
  46. headerRow.add( new UIText( strings.getKey( 'sidebar/project/renderer' ).toUpperCase() ) );
  47. rendererPanel.add( headerRow );
  48. // Renderer / Antialias
  49. var antialiasRow = new UIRow();
  50. var antialiasBoolean = new UIBoolean( config.getKey( 'project/renderer/antialias' ) ).onChange( function () {
  51. config.setKey( 'project/renderer/antialias', this.getValue() );
  52. createRenderer();
  53. } );
  54. antialiasRow.add( new UIText( strings.getKey( 'sidebar/project/antialias' ) ).setWidth( '90px' ) );
  55. antialiasRow.add( antialiasBoolean );
  56. rendererPanel.add( antialiasRow );
  57. // Renderer / Shadows
  58. var shadowsRow = new UIRow();
  59. var shadowsBoolean = new UIBoolean( config.getKey( 'project/renderer/shadows' ) ).onChange( function () {
  60. config.setKey( 'project/renderer/shadows', this.getValue() );
  61. updateRenderer();
  62. } );
  63. shadowsRow.add( new UIText( strings.getKey( 'sidebar/project/shadows' ) ).setWidth( '90px' ) );
  64. shadowsRow.add( shadowsBoolean );
  65. var shadowTypeSelect = new UISelect().setOptions( {
  66. 0: 'Basic',
  67. 1: 'PCF',
  68. 2: 'PCF (Soft)',
  69. // 3: 'VSM'
  70. } ).setWidth( '125px' ).onChange( function () {
  71. config.setKey( 'project/renderer/shadowType', parseFloat( this.getValue() ) );
  72. updateRenderer();
  73. } );
  74. shadowTypeSelect.setValue( config.getKey( 'project/renderer/shadowType' ) );
  75. shadowsRow.add( shadowTypeSelect );
  76. rendererPanel.add( shadowsRow );
  77. // Renderer / Physically Correct lights
  78. var physicallyCorrectLightsRow = new UIRow();
  79. var physicallyCorrectLightsBoolean = new UIBoolean( config.getKey( 'project/renderer/physicallyCorrectLights' ) ).onChange( function () {
  80. config.setKey( 'project/renderer/physicallyCorrectLights', this.getValue() );
  81. updateRenderer();
  82. } );
  83. physicallyCorrectLightsRow.add( new UIText( strings.getKey( 'sidebar/project/physicallyCorrectLights' ) ).setWidth( '90px' ) );
  84. physicallyCorrectLightsRow.add( physicallyCorrectLightsBoolean );
  85. rendererPanel.add( physicallyCorrectLightsRow );
  86. // Renderer / Tonemapping
  87. var toneMappingRow = new UIRow();
  88. var toneMappingSelect = new UISelect().setOptions( {
  89. 0: 'None',
  90. 1: 'Linear',
  91. 2: 'Reinhard',
  92. 3: 'Cineon',
  93. 4: 'ACESFilmic'
  94. } ).setWidth( '120px' ).onChange( function ( value ) {
  95. var value = this.getValue();
  96. config.setKey( 'project/renderer/toneMapping', parseFloat( value ) );
  97. updateRenderer();
  98. toneMappingExposure.setDisplay( value === '0' ? 'none' : '' );
  99. } );
  100. toneMappingSelect.setValue( config.getKey( 'project/renderer/toneMapping' ) );
  101. toneMappingRow.add( new UIText( strings.getKey( 'sidebar/project/toneMapping' ) ).setWidth( '90px' ) );
  102. toneMappingRow.add( toneMappingSelect );
  103. var toneMappingExposure = new UINumber( config.getKey( 'project/renderer/toneMappingExposure' ) );
  104. toneMappingExposure.setDisplay( toneMappingSelect.getValue() === '0' ? 'none' : '' );
  105. toneMappingExposure.setWidth( '30px' ).setMarginLeft( '10px' );
  106. toneMappingExposure.setRange( 0, 10 );
  107. toneMappingExposure.onChange( function () {
  108. config.setKey( 'project/renderer/toneMappingExposure', this.getValue() );
  109. updateRenderer();
  110. } );
  111. toneMappingRow.add( toneMappingExposure );
  112. rendererPanel.add( toneMappingRow );
  113. //
  114. function updateRenderer() {
  115. currentRenderer.physicallyCorrectLights = physicallyCorrectLightsBoolean.getValue();
  116. currentRenderer.shadowMap.enabled = shadowsBoolean.getValue();
  117. currentRenderer.shadowMap.type = parseFloat( shadowTypeSelect.getValue() );
  118. currentRenderer.toneMapping = parseFloat( toneMappingSelect.getValue() );
  119. currentRenderer.toneMappingExposure = toneMappingExposure.getValue();
  120. signals.rendererUpdated.dispatch();
  121. }
  122. function createRenderer() {
  123. currentRenderer = new THREE.WebGLRenderer( { antialias: antialiasBoolean.getValue() } );
  124. currentRenderer.outputEncoding = THREE.sRGBEncoding;
  125. currentRenderer.physicallyCorrectLights = physicallyCorrectLightsBoolean.getValue();
  126. currentRenderer.shadowMap.enabled = shadowsBoolean.getValue();
  127. currentRenderer.shadowMap.type = parseFloat( shadowTypeSelect.getValue() );
  128. currentRenderer.toneMapping = parseFloat( toneMappingSelect.getValue() );
  129. currentRenderer.toneMappingExposure = toneMappingExposure.getValue();
  130. signals.rendererChanged.dispatch( currentRenderer );
  131. }
  132. createRenderer();
  133. // Materials
  134. var materials = new UIPanel();
  135. var headerRow = new UIRow();
  136. headerRow.add( new UIText( strings.getKey( 'sidebar/project/materials' ).toUpperCase() ) );
  137. materials.add( headerRow );
  138. var listbox = new UIListbox();
  139. materials.add( listbox );
  140. var buttonsRow = new UIRow();
  141. buttonsRow.setPadding( '10px 0px' );
  142. materials.add( buttonsRow );
  143. /*
  144. var addButton = new UI.Button().setLabel( 'Add' ).setMarginRight( '5px' );
  145. addButton.onClick( function () {
  146. editor.addMaterial( new THREE.MeshStandardMaterial() );
  147. } );
  148. buttonsRow.add( addButton );
  149. */
  150. var assignMaterial = new UIButton().setLabel( strings.getKey( 'sidebar/project/Assign' ) ).setMargin( '0px 5px' );
  151. assignMaterial.onClick( function () {
  152. var selectedObject = editor.selected;
  153. if ( selectedObject !== null ) {
  154. var oldMaterial = selectedObject.material;
  155. // only assing materials to objects with a material property (e.g. avoid assigning material to THREE.Group)
  156. if ( oldMaterial !== undefined ) {
  157. var material = editor.getMaterialById( parseInt( listbox.getValue() ) );
  158. if ( material !== undefined ) {
  159. editor.removeMaterial( oldMaterial );
  160. editor.execute( new SetMaterialCommand( editor, selectedObject, material ) );
  161. editor.addMaterial( material );
  162. }
  163. }
  164. }
  165. } );
  166. buttonsRow.add( assignMaterial );
  167. container.add( materials );
  168. // events
  169. signals.objectSelected.add( function ( object ) {
  170. if ( object !== null ) {
  171. var index = Object.values( editor.materials ).indexOf( object.material );
  172. listbox.selectIndex( index );
  173. }
  174. } );
  175. signals.materialAdded.add( refreshMaterialBrowserUI );
  176. signals.materialChanged.add( refreshMaterialBrowserUI );
  177. signals.materialRemoved.add( refreshMaterialBrowserUI );
  178. function refreshMaterialBrowserUI() {
  179. listbox.setItems( Object.values( editor.materials ) );
  180. }
  181. return container;
  182. }
  183. export { SidebarProject };