Sidebar.Scene.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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, UICubeTexture } 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. backgroundCubeTexture.getValue()
  75. );
  76. }
  77. var backgroundRow = new UIRow();
  78. var backgroundType = new UISelect().setOptions( {
  79. 'None': 'None',
  80. 'Color': 'Color',
  81. 'Texture': 'Texture',
  82. 'CubeTexture': 'CubeTexture'
  83. } ).setWidth( '150px' );
  84. backgroundType.onChange( function () {
  85. onBackgroundChanged();
  86. refreshBackgroundUI();
  87. } );
  88. backgroundType.setValue( 'Color' );
  89. backgroundRow.add( new UIText( strings.getKey( 'sidebar/scene/background' ) ).setWidth( '90px' ) );
  90. backgroundRow.add( backgroundType );
  91. container.add( backgroundRow );
  92. //
  93. var colorRow = new UIRow();
  94. colorRow.setMarginLeft( '90px' );
  95. var backgroundColor = new UIColor().setValue( '#aaaaaa' ).onChange( onBackgroundChanged );
  96. colorRow.add( backgroundColor );
  97. container.add( colorRow );
  98. //
  99. var textureRow = new UIRow();
  100. textureRow.setDisplay( 'none' );
  101. textureRow.setMarginLeft( '90px' );
  102. var backgroundTexture = new UITexture().onChange( onBackgroundChanged );
  103. textureRow.add( backgroundTexture );
  104. container.add( textureRow );
  105. //
  106. var cubeTextureRow = new UIRow();
  107. cubeTextureRow.setDisplay( 'none' );
  108. cubeTextureRow.setMarginLeft( '90px' );
  109. var backgroundCubeTexture = new UICubeTexture().onChange( onBackgroundChanged );
  110. cubeTextureRow.add( backgroundCubeTexture );
  111. container.add( cubeTextureRow );
  112. //
  113. function refreshBackgroundUI() {
  114. var type = backgroundType.getValue();
  115. colorRow.setDisplay( type === 'Color' ? '' : 'none' );
  116. textureRow.setDisplay( type === 'Texture' ? '' : 'none' );
  117. cubeTextureRow.setDisplay( type === 'CubeTexture' ? '' : 'none' );
  118. }
  119. // fog
  120. function onFogChanged() {
  121. signals.sceneFogChanged.dispatch(
  122. fogType.getValue(),
  123. fogColor.getHexValue(),
  124. fogNear.getValue(),
  125. fogFar.getValue(),
  126. fogDensity.getValue()
  127. );
  128. }
  129. var fogTypeRow = new UIRow();
  130. var fogType = new UISelect().setOptions( {
  131. 'None': 'None',
  132. 'Fog': 'Linear',
  133. 'FogExp2': 'Exponential'
  134. } ).setWidth( '150px' );
  135. fogType.onChange( function () {
  136. onFogChanged();
  137. refreshFogUI();
  138. } );
  139. fogTypeRow.add( new UIText( strings.getKey( 'sidebar/scene/fog' ) ).setWidth( '90px' ) );
  140. fogTypeRow.add( fogType );
  141. container.add( fogTypeRow );
  142. // fog color
  143. var fogPropertiesRow = new UIRow();
  144. fogPropertiesRow.setDisplay( 'none' );
  145. fogPropertiesRow.setMarginLeft( '90px' );
  146. container.add( fogPropertiesRow );
  147. var fogColor = new UIColor().setValue( '#aaaaaa' );
  148. fogColor.onChange( onFogChanged );
  149. fogPropertiesRow.add( fogColor );
  150. // fog near
  151. var fogNear = new UINumber( 0.1 ).setWidth( '40px' ).setRange( 0, Infinity ).onChange( onFogChanged );
  152. fogPropertiesRow.add( fogNear );
  153. // fog far
  154. var fogFar = new UINumber( 50 ).setWidth( '40px' ).setRange( 0, Infinity ).onChange( onFogChanged );
  155. fogPropertiesRow.add( fogFar );
  156. // fog density
  157. var fogDensity = new UINumber( 0.05 ).setWidth( '40px' ).setRange( 0, 0.1 ).setStep( 0.001 ).setPrecision( 3 ).onChange( onFogChanged );
  158. fogPropertiesRow.add( fogDensity );
  159. //
  160. function refreshUI() {
  161. var camera = editor.camera;
  162. var scene = editor.scene;
  163. var options = [];
  164. options.push( buildOption( camera, false ) );
  165. options.push( buildOption( scene, false ) );
  166. ( function addObjects( objects, pad ) {
  167. for ( var i = 0, l = objects.length; i < l; i ++ ) {
  168. var object = objects[ i ];
  169. var option = buildOption( object, true );
  170. option.style.paddingLeft = ( pad * 10 ) + 'px';
  171. options.push( option );
  172. addObjects( object.children, pad + 1 );
  173. }
  174. } )( scene.children, 1 );
  175. outliner.setOptions( options );
  176. if ( editor.selected !== null ) {
  177. outliner.setValue( editor.selected.id );
  178. }
  179. if ( scene.background ) {
  180. if ( scene.background.isColor ) {
  181. backgroundType.setValue( "Color" );
  182. backgroundColor.setHexValue( scene.background.getHex() );
  183. backgroundTexture.setValue( null );
  184. backgroundCubeTexture.setValue( null );
  185. } else if ( scene.background.isTexture ) {
  186. backgroundType.setValue( "Texture" );
  187. backgroundTexture.setValue( scene.background );
  188. backgroundCubeTexture.setValue( null );
  189. } else if ( scene.background.isCubeTexture ) {
  190. backgroundType.setValue( "CubeTexture" );
  191. backgroundCubeTexture.setValue( scene.background );
  192. backgroundTexture.setValue( null );
  193. }
  194. } else {
  195. backgroundType.setValue( "None" );
  196. backgroundTexture.setValue( null );
  197. }
  198. if ( scene.fog ) {
  199. fogColor.setHexValue( scene.fog.color.getHex() );
  200. if ( scene.fog.isFog ) {
  201. fogType.setValue( "Fog" );
  202. fogNear.setValue( scene.fog.near );
  203. fogFar.setValue( scene.fog.far );
  204. } else if ( scene.fog.isFogExp2 ) {
  205. fogType.setValue( "FogExp2" );
  206. fogDensity.setValue( scene.fog.density );
  207. }
  208. } else {
  209. fogType.setValue( "None" );
  210. }
  211. refreshBackgroundUI();
  212. refreshFogUI();
  213. }
  214. function refreshFogUI() {
  215. var type = fogType.getValue();
  216. fogPropertiesRow.setDisplay( type === 'None' ? 'none' : '' );
  217. fogNear.setDisplay( type === 'Fog' ? '' : 'none' );
  218. fogFar.setDisplay( type === 'Fog' ? '' : 'none' );
  219. fogDensity.setDisplay( type === 'FogExp2' ? '' : 'none' );
  220. }
  221. refreshUI();
  222. // events
  223. signals.editorCleared.add( refreshUI );
  224. signals.sceneGraphChanged.add( refreshUI );
  225. signals.objectChanged.add( function ( object ) {
  226. var options = outliner.options;
  227. for ( var i = 0; i < options.length; i ++ ) {
  228. var option = options[ i ];
  229. if ( option.value === object.id ) {
  230. option.innerHTML = buildHTML( object );
  231. return;
  232. }
  233. }
  234. } );
  235. signals.objectSelected.add( function ( object ) {
  236. if ( ignoreObjectSelectedSignal === true ) return;
  237. outliner.setValue( object !== null ? object.id : null );
  238. } );
  239. return container;
  240. };
  241. export { SidebarScene };
粤ICP备19079148号