Sidebar.Scene.js 13 KB

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