Sidebar.Scene.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. import { UIPanel, UIBreak, UIRow, UIColor, UISelect, UIText, UINumber } from './libs/ui.js';
  5. import { UIOutliner, UITexture } from './libs/ui.three.js';
  6. function SidebarScene( editor ) {
  7. var signals = editor.signals;
  8. var strings = editor.strings;
  9. var container = new UIPanel();
  10. container.setBorderTop( '0' );
  11. container.setPaddingTop( '20px' );
  12. // outliner
  13. var nodeStates = new WeakMap();
  14. function buildOption( object, draggable ) {
  15. var option = document.createElement( 'div' );
  16. option.draggable = draggable;
  17. option.innerHTML = buildHTML( object );
  18. option.value = object.id;
  19. // opener
  20. if ( nodeStates.has( object ) ) {
  21. var state = nodeStates.get( object );
  22. var opener = document.createElement( 'span' );
  23. opener.classList.add( 'opener' );
  24. if ( object.children.length > 0 ) {
  25. opener.classList.add( state ? 'open' : 'closed' );
  26. }
  27. opener.addEventListener( 'click', function () {
  28. nodeStates.set( object, nodeStates.get( object ) === false ); // toggle
  29. refreshUI();
  30. }, false );
  31. option.insertBefore( opener, option.firstChild );
  32. }
  33. return option;
  34. }
  35. function getMaterialName( material ) {
  36. if ( Array.isArray( material ) ) {
  37. var array = [];
  38. for ( var i = 0; i < material.length; i ++ ) {
  39. array.push( material[ i ].name );
  40. }
  41. return array.join( ',' );
  42. }
  43. return material.name;
  44. }
  45. function escapeHTML( html ) {
  46. return html
  47. .replace( /&/g, '&amp;' )
  48. .replace( /"/g, '&quot;' )
  49. .replace( /'/g, '&#39;' )
  50. .replace( /</g, '&lt;' )
  51. .replace( />/g, '&gt;' );
  52. }
  53. function getObjectType( object ) {
  54. if ( object.isScene ) return 'Scene';
  55. if ( object.isCamera ) return 'Camera';
  56. if ( object.isLight ) return 'Light';
  57. if ( object.isMesh ) return 'Mesh';
  58. if ( object.isLine ) return 'Line';
  59. if ( object.isPoints ) return 'Points';
  60. return 'Object3D';
  61. }
  62. function buildHTML( object ) {
  63. var html = `<span class="type ${ getObjectType( object ) }"></span> ${ escapeHTML( object.name ) }`;
  64. if ( object.isMesh ) {
  65. var geometry = object.geometry;
  66. var material = object.material;
  67. html += ` <span class="type Geometry"></span> ${ escapeHTML( geometry.name ) }`;
  68. html += ` <span class="type Material"></span> ${ escapeHTML( getMaterialName( material ) ) }`;
  69. }
  70. html += getScript( object.uuid );
  71. return html;
  72. }
  73. function getScript( uuid ) {
  74. if ( editor.scripts[ uuid ] !== undefined ) {
  75. return ' <span class="type Script"></span>';
  76. }
  77. return '';
  78. }
  79. var ignoreObjectSelectedSignal = false;
  80. var outliner = new UIOutliner( editor );
  81. outliner.setId( 'outliner' );
  82. outliner.onChange( function () {
  83. ignoreObjectSelectedSignal = true;
  84. editor.selectById( parseInt( outliner.getValue() ) );
  85. ignoreObjectSelectedSignal = false;
  86. } );
  87. outliner.onDblClick( function () {
  88. editor.focusById( parseInt( outliner.getValue() ) );
  89. } );
  90. container.add( outliner );
  91. container.add( new UIBreak() );
  92. // background
  93. function onBackgroundChanged() {
  94. signals.sceneBackgroundChanged.dispatch(
  95. backgroundType.getValue(),
  96. backgroundColor.getHexValue(),
  97. backgroundTexture.getValue(),
  98. backgroundEquirectangularTexture.getValue(),
  99. environmentType.getValue()
  100. );
  101. }
  102. var backgroundRow = new UIRow();
  103. var backgroundType = new UISelect().setOptions( {
  104. 'None': 'None',
  105. 'Color': 'Color',
  106. 'Texture': 'Texture',
  107. 'Equirectangular': 'Equirect'
  108. } ).setWidth( '150px' );
  109. backgroundType.onChange( function () {
  110. onBackgroundChanged();
  111. refreshBackgroundUI();
  112. } );
  113. backgroundType.setValue( 'Color' );
  114. backgroundRow.add( new UIText( strings.getKey( 'sidebar/scene/background' ) ).setWidth( '90px' ) );
  115. backgroundRow.add( backgroundType );
  116. var backgroundColor = new UIColor().setValue( '#000000' ).setMarginLeft( '8px' ).onChange( onBackgroundChanged );
  117. backgroundRow.add( backgroundColor );
  118. var backgroundTexture = new UITexture().setMarginLeft( '8px' ).onChange( onBackgroundChanged );
  119. backgroundTexture.setDisplay( 'none' );
  120. backgroundRow.add( backgroundTexture );
  121. var backgroundEquirectangularTexture = new UITexture().setMarginLeft( '8px' ).onChange( onBackgroundChanged );
  122. backgroundEquirectangularTexture.setDisplay( 'none' );
  123. backgroundRow.add( backgroundEquirectangularTexture );
  124. container.add( backgroundRow );
  125. //
  126. function refreshBackgroundUI() {
  127. var type = backgroundType.getValue();
  128. backgroundType.setWidth( type === 'None' ? '150px' : '110px' );
  129. backgroundColor.setDisplay( type === 'Color' ? '' : 'none' );
  130. backgroundTexture.setDisplay( type === 'Texture' ? '' : 'none' );
  131. backgroundEquirectangularTexture.setDisplay( type === 'Equirectangular' ? '' : 'none' );
  132. }
  133. // environment
  134. var environmentRow = new UIRow();
  135. var environmentType = new UISelect().setOptions( {
  136. 'None': 'None',
  137. 'Background': 'Background'
  138. } ).setWidth( '150px' );
  139. environmentType.setValue( 'None' );
  140. environmentType.onChange( function () {
  141. signals.sceneEnvironmentChanged.dispatch( environmentType.getValue() );
  142. } );
  143. environmentRow.add( new UIText( strings.getKey( 'sidebar/scene/environment' ) ).setWidth( '90px' ) );
  144. environmentRow.add( environmentType );
  145. container.add( environmentRow );
  146. // fog
  147. function onFogChanged() {
  148. signals.sceneFogChanged.dispatch(
  149. fogType.getValue(),
  150. fogColor.getHexValue(),
  151. fogNear.getValue(),
  152. fogFar.getValue(),
  153. fogDensity.getValue()
  154. );
  155. }
  156. var fogTypeRow = new UIRow();
  157. var fogType = new UISelect().setOptions( {
  158. 'None': 'None',
  159. 'Fog': 'Linear',
  160. 'FogExp2': 'Exponential'
  161. } ).setWidth( '150px' );
  162. fogType.onChange( function () {
  163. onFogChanged();
  164. refreshFogUI();
  165. } );
  166. fogTypeRow.add( new UIText( strings.getKey( 'sidebar/scene/fog' ) ).setWidth( '90px' ) );
  167. fogTypeRow.add( fogType );
  168. container.add( fogTypeRow );
  169. // fog color
  170. var fogPropertiesRow = new UIRow();
  171. fogPropertiesRow.setDisplay( 'none' );
  172. fogPropertiesRow.setMarginLeft( '90px' );
  173. container.add( fogPropertiesRow );
  174. var fogColor = new UIColor().setValue( '#aaaaaa' );
  175. fogColor.onChange( onFogChanged );
  176. fogPropertiesRow.add( fogColor );
  177. // fog near
  178. var fogNear = new UINumber( 0.1 ).setWidth( '40px' ).setRange( 0, Infinity ).onChange( onFogChanged );
  179. fogPropertiesRow.add( fogNear );
  180. // fog far
  181. var fogFar = new UINumber( 50 ).setWidth( '40px' ).setRange( 0, Infinity ).onChange( onFogChanged );
  182. fogPropertiesRow.add( fogFar );
  183. // fog density
  184. var fogDensity = new UINumber( 0.05 ).setWidth( '40px' ).setRange( 0, 0.1 ).setStep( 0.001 ).setPrecision( 3 ).onChange( onFogChanged );
  185. fogPropertiesRow.add( fogDensity );
  186. //
  187. function refreshUI() {
  188. var camera = editor.camera;
  189. var scene = editor.scene;
  190. var options = [];
  191. options.push( buildOption( camera, false ) );
  192. options.push( buildOption( scene, false ) );
  193. ( function addObjects( objects, pad ) {
  194. for ( var i = 0, l = objects.length; i < l; i ++ ) {
  195. var object = objects[ i ];
  196. if ( nodeStates.has( object ) === false ) {
  197. nodeStates.set( object, false );
  198. }
  199. var option = buildOption( object, true );
  200. option.style.paddingLeft = ( pad * 18 ) + 'px';
  201. options.push( option );
  202. if ( nodeStates.get( object ) === true ) {
  203. addObjects( object.children, pad + 1 );
  204. }
  205. }
  206. } )( scene.children, 0 );
  207. outliner.setOptions( options );
  208. if ( editor.selected !== null ) {
  209. outliner.setValue( editor.selected.id );
  210. }
  211. if ( scene.background ) {
  212. if ( scene.background.isColor ) {
  213. backgroundType.setValue( "Color" );
  214. backgroundColor.setHexValue( scene.background.getHex() );
  215. backgroundTexture.setValue( null );
  216. backgroundEquirectangularTexture.setValue( null );
  217. }
  218. // TODO: Add Texture/EquirectangularTexture support
  219. } else {
  220. backgroundType.setValue( "None" );
  221. backgroundTexture.setValue( null );
  222. backgroundEquirectangularTexture.setValue( null );
  223. }
  224. if ( scene.environment ) {
  225. // TODO
  226. } else {
  227. environmentType.setValue( "None" );
  228. }
  229. if ( scene.fog ) {
  230. fogColor.setHexValue( scene.fog.color.getHex() );
  231. if ( scene.fog.isFog ) {
  232. fogType.setValue( "Fog" );
  233. fogNear.setValue( scene.fog.near );
  234. fogFar.setValue( scene.fog.far );
  235. } else if ( scene.fog.isFogExp2 ) {
  236. fogType.setValue( "FogExp2" );
  237. fogDensity.setValue( scene.fog.density );
  238. }
  239. } else {
  240. fogType.setValue( "None" );
  241. }
  242. refreshBackgroundUI();
  243. refreshFogUI();
  244. }
  245. function refreshFogUI() {
  246. var type = fogType.getValue();
  247. fogPropertiesRow.setDisplay( type === 'None' ? 'none' : '' );
  248. fogNear.setDisplay( type === 'Fog' ? '' : 'none' );
  249. fogFar.setDisplay( type === 'Fog' ? '' : 'none' );
  250. fogDensity.setDisplay( type === 'FogExp2' ? '' : 'none' );
  251. }
  252. refreshUI();
  253. // events
  254. signals.editorCleared.add( refreshUI );
  255. signals.sceneGraphChanged.add( refreshUI );
  256. /*
  257. signals.objectChanged.add( function ( object ) {
  258. var options = outliner.options;
  259. for ( var i = 0; i < options.length; i ++ ) {
  260. var option = options[ i ];
  261. if ( option.value === object.id ) {
  262. option.innerHTML = buildHTML( object );
  263. return;
  264. }
  265. }
  266. } );
  267. */
  268. signals.objectSelected.add( function ( object ) {
  269. if ( ignoreObjectSelectedSignal === true ) return;
  270. if ( object !== null ) {
  271. let needsRefresh = false;
  272. let parent = object.parent;
  273. while ( parent !== editor.scene ) {
  274. if ( nodeStates.get( parent ) !== true ) {
  275. nodeStates.set( parent, true );
  276. needsRefresh = true;
  277. }
  278. parent = parent.parent;
  279. }
  280. if ( needsRefresh ) refreshUI();
  281. outliner.setValue( object.id );
  282. } else {
  283. outliner.setValue( null );
  284. }
  285. } );
  286. return container;
  287. }
  288. export { SidebarScene };