Sidebar.Scene.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. var SidebarScene = function ( 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. function buildOption( object, draggable ) {
  14. var option = document.createElement( 'div' );
  15. option.draggable = draggable;
  16. option.innerHTML = buildHTML( object );
  17. option.value = object.id;
  18. return option;
  19. }
  20. function getMaterialName( material ) {
  21. if ( Array.isArray( material ) ) {
  22. var array = [];
  23. for ( var i = 0; i < material.length; i ++ ) {
  24. array.push( material[ i ].name );
  25. }
  26. return array.join( ',' );
  27. }
  28. return material.name;
  29. }
  30. function escapeHTML( html ) {
  31. return html
  32. .replace( /&/g, '&amp;' )
  33. .replace( /"/g, '&quot;' )
  34. .replace( /'/g, '&#39;' )
  35. .replace( /</g, '&lt;' )
  36. .replace( />/g, '&gt;' );
  37. }
  38. function buildHTML( object ) {
  39. var html = '<span class="type ' + object.type + '"></span> ' + escapeHTML( object.name );
  40. if ( object.isMesh ) {
  41. var geometry = object.geometry;
  42. var material = object.material;
  43. html += ' <span class="type ' + geometry.type + '"></span> ' + escapeHTML( geometry.name );
  44. html += ' <span class="type ' + material.type + '"></span> ' + escapeHTML( getMaterialName( material ) );
  45. }
  46. html += getScript( object.uuid );
  47. return html;
  48. }
  49. function getScript( uuid ) {
  50. if ( editor.scripts[ uuid ] !== undefined ) {
  51. return ' <span class="type Script"></span>';
  52. }
  53. return '';
  54. }
  55. var ignoreObjectSelectedSignal = false;
  56. var outliner = new UIOutliner( editor );
  57. outliner.setId( 'outliner' );
  58. outliner.onChange( function () {
  59. ignoreObjectSelectedSignal = true;
  60. editor.selectById( parseInt( outliner.getValue() ) );
  61. ignoreObjectSelectedSignal = false;
  62. } );
  63. outliner.onDblClick( function () {
  64. editor.focusById( parseInt( outliner.getValue() ) );
  65. } );
  66. container.add( outliner );
  67. container.add( new UIBreak() );
  68. // background
  69. function onBackgroundChanged() {
  70. signals.sceneBackgroundChanged.dispatch(
  71. backgroundType.getValue(),
  72. backgroundColor.getHexValue(),
  73. backgroundTexture.getValue()
  74. );
  75. }
  76. var backgroundRow = new UIRow();
  77. var backgroundType = new UISelect().setOptions( {
  78. 'None': 'None',
  79. 'Color': 'Color',
  80. 'Texture': 'Texture'
  81. } ).setWidth( '150px' );
  82. backgroundType.onChange( function () {
  83. onBackgroundChanged();
  84. refreshBackgroundUI();
  85. } );
  86. backgroundType.setValue( 'Color' );
  87. backgroundRow.add( new UIText( strings.getKey( 'sidebar/scene/background' ) ).setWidth( '90px' ) );
  88. backgroundRow.add( backgroundType );
  89. container.add( backgroundRow );
  90. //
  91. var colorRow = new UIRow();
  92. colorRow.setMarginLeft( '90px' );
  93. var backgroundColor = new UIColor().setValue( '#aaaaaa' ).onChange( onBackgroundChanged );
  94. colorRow.add( backgroundColor );
  95. container.add( colorRow );
  96. //
  97. var textureRow = new UIRow();
  98. textureRow.setDisplay( 'none' );
  99. textureRow.setMarginLeft( '90px' );
  100. var backgroundTexture = new UITexture().onChange( onBackgroundChanged );
  101. textureRow.add( backgroundTexture );
  102. container.add( textureRow );
  103. //
  104. function refreshBackgroundUI() {
  105. var type = backgroundType.getValue();
  106. colorRow.setDisplay( type === 'Color' ? '' : 'none' );
  107. textureRow.setDisplay( type === 'Texture' ? '' : 'none' );
  108. }
  109. // fog
  110. function onFogChanged() {
  111. signals.sceneFogChanged.dispatch(
  112. fogType.getValue(),
  113. fogColor.getHexValue(),
  114. fogNear.getValue(),
  115. fogFar.getValue(),
  116. fogDensity.getValue()
  117. );
  118. }
  119. var fogTypeRow = new UIRow();
  120. var fogType = new UISelect().setOptions( {
  121. 'None': 'None',
  122. 'Fog': 'Linear',
  123. 'FogExp2': 'Exponential'
  124. } ).setWidth( '150px' );
  125. fogType.onChange( function () {
  126. onFogChanged();
  127. refreshFogUI();
  128. } );
  129. fogTypeRow.add( new UIText( strings.getKey( 'sidebar/scene/fog' ) ).setWidth( '90px' ) );
  130. fogTypeRow.add( fogType );
  131. container.add( fogTypeRow );
  132. // fog color
  133. var fogPropertiesRow = new UIRow();
  134. fogPropertiesRow.setDisplay( 'none' );
  135. fogPropertiesRow.setMarginLeft( '90px' );
  136. container.add( fogPropertiesRow );
  137. var fogColor = new UIColor().setValue( '#aaaaaa' );
  138. fogColor.onChange( onFogChanged );
  139. fogPropertiesRow.add( fogColor );
  140. // fog near
  141. var fogNear = new UINumber( 0.1 ).setWidth( '40px' ).setRange( 0, Infinity ).onChange( onFogChanged );
  142. fogPropertiesRow.add( fogNear );
  143. // fog far
  144. var fogFar = new UINumber( 50 ).setWidth( '40px' ).setRange( 0, Infinity ).onChange( onFogChanged );
  145. fogPropertiesRow.add( fogFar );
  146. // fog density
  147. var fogDensity = new UINumber( 0.05 ).setWidth( '40px' ).setRange( 0, 0.1 ).setStep( 0.001 ).setPrecision( 3 ).onChange( onFogChanged );
  148. fogPropertiesRow.add( fogDensity );
  149. //
  150. function refreshUI() {
  151. var camera = editor.camera;
  152. var scene = editor.scene;
  153. var options = [];
  154. options.push( buildOption( camera, false ) );
  155. options.push( buildOption( scene, false ) );
  156. ( function addObjects( objects, pad ) {
  157. for ( var i = 0, l = objects.length; i < l; i ++ ) {
  158. var object = objects[ i ];
  159. var option = buildOption( object, true );
  160. option.style.paddingLeft = ( pad * 10 ) + 'px';
  161. options.push( option );
  162. addObjects( object.children, pad + 1 );
  163. }
  164. } )( scene.children, 1 );
  165. outliner.setOptions( options );
  166. if ( editor.selected !== null ) {
  167. outliner.setValue( editor.selected.id );
  168. }
  169. if ( scene.background ) {
  170. if ( scene.background.isColor ) {
  171. backgroundType.setValue( "Color" );
  172. backgroundColor.setHexValue( scene.background.getHex() );
  173. backgroundTexture.setValue( null );
  174. } else if ( scene.background.isTexture ) {
  175. backgroundType.setValue( "Texture" );
  176. backgroundTexture.setValue( scene.background );
  177. }
  178. } else {
  179. backgroundType.setValue( "None" );
  180. backgroundTexture.setValue( null );
  181. }
  182. if ( scene.fog ) {
  183. fogColor.setHexValue( scene.fog.color.getHex() );
  184. if ( scene.fog.isFog ) {
  185. fogType.setValue( "Fog" );
  186. fogNear.setValue( scene.fog.near );
  187. fogFar.setValue( scene.fog.far );
  188. } else if ( scene.fog.isFogExp2 ) {
  189. fogType.setValue( "FogExp2" );
  190. fogDensity.setValue( scene.fog.density );
  191. }
  192. } else {
  193. fogType.setValue( "None" );
  194. }
  195. refreshBackgroundUI();
  196. refreshFogUI();
  197. }
  198. function refreshFogUI() {
  199. var type = fogType.getValue();
  200. fogPropertiesRow.setDisplay( type === 'None' ? 'none' : '' );
  201. fogNear.setDisplay( type === 'Fog' ? '' : 'none' );
  202. fogFar.setDisplay( type === 'Fog' ? '' : 'none' );
  203. fogDensity.setDisplay( type === 'FogExp2' ? '' : 'none' );
  204. }
  205. refreshUI();
  206. // events
  207. signals.editorCleared.add( refreshUI );
  208. signals.sceneGraphChanged.add( refreshUI );
  209. signals.objectChanged.add( function ( object ) {
  210. var options = outliner.options;
  211. for ( var i = 0; i < options.length; i ++ ) {
  212. var option = options[ i ];
  213. if ( option.value === object.id ) {
  214. option.innerHTML = buildHTML( object );
  215. return;
  216. }
  217. }
  218. } );
  219. signals.objectSelected.add( function ( object ) {
  220. if ( ignoreObjectSelectedSignal === true ) return;
  221. outliner.setValue( object !== null ? object.id : null );
  222. } );
  223. return container;
  224. };
  225. export { SidebarScene };