Sidebar.Object.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. import * as THREE from 'three';
  2. import { UIPanel, UIRow, UIInput, UIButton, UIColor, UICheckbox, UIInteger, UITextArea, UIText, UINumber } from './libs/ui.js';
  3. import { UIBoolean } from './libs/ui.three.js';
  4. import { SetUuidCommand } from './commands/SetUuidCommand.js';
  5. import { SetValueCommand } from './commands/SetValueCommand.js';
  6. import { SetPositionCommand } from './commands/SetPositionCommand.js';
  7. import { SetRotationCommand } from './commands/SetRotationCommand.js';
  8. import { SetScaleCommand } from './commands/SetScaleCommand.js';
  9. import { SetColorCommand } from './commands/SetColorCommand.js';
  10. import { SidebarObjectAnimation } from './Sidebar.Object.Animation.js';
  11. function SidebarObject( editor ) {
  12. const strings = editor.strings;
  13. const signals = editor.signals;
  14. const container = new UIPanel();
  15. container.setBorderTop( '0' );
  16. container.setPaddingTop( '20px' );
  17. container.setDisplay( 'none' );
  18. // Actions
  19. /*
  20. let objectActions = new UI.Select().setPosition( 'absolute' ).setRight( '8px' ).setFontSize( '11px' );
  21. objectActions.setOptions( {
  22. 'Actions': 'Actions',
  23. 'Reset Position': 'Reset Position',
  24. 'Reset Rotation': 'Reset Rotation',
  25. 'Reset Scale': 'Reset Scale'
  26. } );
  27. objectActions.onClick( function ( event ) {
  28. event.stopPropagation(); // Avoid panel collapsing
  29. } );
  30. objectActions.onChange( function ( event ) {
  31. let object = editor.selected;
  32. switch ( this.getValue() ) {
  33. case 'Reset Position':
  34. editor.execute( new SetPositionCommand( editor, object, new Vector3( 0, 0, 0 ) ) );
  35. break;
  36. case 'Reset Rotation':
  37. editor.execute( new SetRotationCommand( editor, object, new Euler( 0, 0, 0 ) ) );
  38. break;
  39. case 'Reset Scale':
  40. editor.execute( new SetScaleCommand( editor, object, new Vector3( 1, 1, 1 ) ) );
  41. break;
  42. }
  43. this.setValue( 'Actions' );
  44. } );
  45. container.addStatic( objectActions );
  46. */
  47. // type
  48. const objectTypeRow = new UIRow();
  49. const objectType = new UIText();
  50. objectTypeRow.add( new UIText( strings.getKey( 'sidebar/object/type' ) ).setClass( 'Label' ) );
  51. objectTypeRow.add( objectType );
  52. container.add( objectTypeRow );
  53. // uuid
  54. const objectUUIDRow = new UIRow();
  55. const objectUUID = new UIInput().setWidth( '102px' ).setFontSize( '12px' ).setDisabled( true );
  56. const objectUUIDRenew = new UIButton( strings.getKey( 'sidebar/object/new' ) ).setMarginLeft( '7px' ).onClick( function () {
  57. objectUUID.setValue( THREE.MathUtils.generateUUID() );
  58. editor.execute( new SetUuidCommand( editor, editor.selected, objectUUID.getValue() ) );
  59. } );
  60. objectUUIDRow.add( new UIText( strings.getKey( 'sidebar/object/uuid' ) ).setClass( 'Label' ) );
  61. objectUUIDRow.add( objectUUID );
  62. objectUUIDRow.add( objectUUIDRenew );
  63. container.add( objectUUIDRow );
  64. // name
  65. const objectNameRow = new UIRow();
  66. const objectName = new UIInput().setWidth( '150px' ).setFontSize( '12px' ).onChange( function () {
  67. editor.execute( new SetValueCommand( editor, editor.selected, 'name', objectName.getValue() ) );
  68. } );
  69. objectNameRow.add( new UIText( strings.getKey( 'sidebar/object/name' ) ).setClass( 'Label' ) );
  70. objectNameRow.add( objectName );
  71. container.add( objectNameRow );
  72. // position
  73. const objectPositionRow = new UIRow();
  74. const objectPositionX = new UINumber().setPrecision( 3 ).setWidth( '50px' ).onChange( update );
  75. const objectPositionY = new UINumber().setPrecision( 3 ).setWidth( '50px' ).onChange( update );
  76. const objectPositionZ = new UINumber().setPrecision( 3 ).setWidth( '50px' ).onChange( update );
  77. objectPositionRow.add( new UIText( strings.getKey( 'sidebar/object/position' ) ).setClass( 'Label' ) );
  78. objectPositionRow.add( objectPositionX, objectPositionY, objectPositionZ );
  79. container.add( objectPositionRow );
  80. // rotation
  81. const objectRotationRow = new UIRow();
  82. const objectRotationX = new UINumber().setStep( 10 ).setNudge( 0.1 ).setUnit( '°' ).setWidth( '50px' ).onChange( update );
  83. const objectRotationY = new UINumber().setStep( 10 ).setNudge( 0.1 ).setUnit( '°' ).setWidth( '50px' ).onChange( update );
  84. const objectRotationZ = new UINumber().setStep( 10 ).setNudge( 0.1 ).setUnit( '°' ).setWidth( '50px' ).onChange( update );
  85. objectRotationRow.add( new UIText( strings.getKey( 'sidebar/object/rotation' ) ).setClass( 'Label' ) );
  86. objectRotationRow.add( objectRotationX, objectRotationY, objectRotationZ );
  87. container.add( objectRotationRow );
  88. // scale
  89. const objectScaleRow = new UIRow();
  90. const objectScaleX = new UINumber( 1 ).setPrecision( 3 ).setWidth( '50px' ).onChange( update );
  91. const objectScaleY = new UINumber( 1 ).setPrecision( 3 ).setWidth( '50px' ).onChange( update );
  92. const objectScaleZ = new UINumber( 1 ).setPrecision( 3 ).setWidth( '50px' ).onChange( update );
  93. objectScaleRow.add( new UIText( strings.getKey( 'sidebar/object/scale' ) ).setClass( 'Label' ) );
  94. objectScaleRow.add( objectScaleX, objectScaleY, objectScaleZ );
  95. container.add( objectScaleRow );
  96. // fov
  97. const objectFovRow = new UIRow();
  98. const objectFov = new UINumber().onChange( update );
  99. objectFovRow.add( new UIText( strings.getKey( 'sidebar/object/fov' ) ).setClass( 'Label' ) );
  100. objectFovRow.add( objectFov );
  101. container.add( objectFovRow );
  102. // left
  103. const objectLeftRow = new UIRow();
  104. const objectLeft = new UINumber().onChange( update );
  105. objectLeftRow.add( new UIText( strings.getKey( 'sidebar/object/left' ) ).setClass( 'Label' ) );
  106. objectLeftRow.add( objectLeft );
  107. container.add( objectLeftRow );
  108. // right
  109. const objectRightRow = new UIRow();
  110. const objectRight = new UINumber().onChange( update );
  111. objectRightRow.add( new UIText( strings.getKey( 'sidebar/object/right' ) ).setClass( 'Label' ) );
  112. objectRightRow.add( objectRight );
  113. container.add( objectRightRow );
  114. // top
  115. const objectTopRow = new UIRow();
  116. const objectTop = new UINumber().onChange( update );
  117. objectTopRow.add( new UIText( strings.getKey( 'sidebar/object/top' ) ).setClass( 'Label' ) );
  118. objectTopRow.add( objectTop );
  119. container.add( objectTopRow );
  120. // bottom
  121. const objectBottomRow = new UIRow();
  122. const objectBottom = new UINumber().onChange( update );
  123. objectBottomRow.add( new UIText( strings.getKey( 'sidebar/object/bottom' ) ).setClass( 'Label' ) );
  124. objectBottomRow.add( objectBottom );
  125. container.add( objectBottomRow );
  126. // near
  127. const objectNearRow = new UIRow();
  128. const objectNear = new UINumber().onChange( update );
  129. objectNearRow.add( new UIText( strings.getKey( 'sidebar/object/near' ) ).setClass( 'Label' ) );
  130. objectNearRow.add( objectNear );
  131. container.add( objectNearRow );
  132. // far
  133. const objectFarRow = new UIRow();
  134. const objectFar = new UINumber().onChange( update );
  135. objectFarRow.add( new UIText( strings.getKey( 'sidebar/object/far' ) ).setClass( 'Label' ) );
  136. objectFarRow.add( objectFar );
  137. container.add( objectFarRow );
  138. // intensity
  139. const objectIntensityRow = new UIRow();
  140. const objectIntensity = new UINumber().onChange( update );
  141. objectIntensityRow.add( new UIText( strings.getKey( 'sidebar/object/intensity' ) ).setClass( 'Label' ) );
  142. objectIntensityRow.add( objectIntensity );
  143. container.add( objectIntensityRow );
  144. // color
  145. const objectColorRow = new UIRow();
  146. const objectColor = new UIColor().onInput( update );
  147. objectColorRow.add( new UIText( strings.getKey( 'sidebar/object/color' ) ).setClass( 'Label' ) );
  148. objectColorRow.add( objectColor );
  149. container.add( objectColorRow );
  150. // ground color
  151. const objectGroundColorRow = new UIRow();
  152. const objectGroundColor = new UIColor().onInput( update );
  153. objectGroundColorRow.add( new UIText( strings.getKey( 'sidebar/object/groundcolor' ) ).setClass( 'Label' ) );
  154. objectGroundColorRow.add( objectGroundColor );
  155. container.add( objectGroundColorRow );
  156. // distance
  157. const objectDistanceRow = new UIRow();
  158. const objectDistance = new UINumber().setRange( 0, Infinity ).onChange( update );
  159. objectDistanceRow.add( new UIText( strings.getKey( 'sidebar/object/distance' ) ).setClass( 'Label' ) );
  160. objectDistanceRow.add( objectDistance );
  161. container.add( objectDistanceRow );
  162. // angle
  163. const objectAngleRow = new UIRow();
  164. const objectAngle = new UINumber().setPrecision( 3 ).setRange( 0, Math.PI / 2 ).onChange( update );
  165. objectAngleRow.add( new UIText( strings.getKey( 'sidebar/object/angle' ) ).setClass( 'Label' ) );
  166. objectAngleRow.add( objectAngle );
  167. container.add( objectAngleRow );
  168. // penumbra
  169. const objectPenumbraRow = new UIRow();
  170. const objectPenumbra = new UINumber().setRange( 0, 1 ).onChange( update );
  171. objectPenumbraRow.add( new UIText( strings.getKey( 'sidebar/object/penumbra' ) ).setClass( 'Label' ) );
  172. objectPenumbraRow.add( objectPenumbra );
  173. container.add( objectPenumbraRow );
  174. // decay
  175. const objectDecayRow = new UIRow();
  176. const objectDecay = new UINumber().setRange( 0, Infinity ).onChange( update );
  177. objectDecayRow.add( new UIText( strings.getKey( 'sidebar/object/decay' ) ).setClass( 'Label' ) );
  178. objectDecayRow.add( objectDecay );
  179. container.add( objectDecayRow );
  180. // shadow
  181. const objectShadowRow = new UIRow();
  182. objectShadowRow.add( new UIText( strings.getKey( 'sidebar/object/shadow' ) ).setClass( 'Label' ) );
  183. const objectCastShadow = new UIBoolean( false, strings.getKey( 'sidebar/object/cast' ) ).onChange( update );
  184. objectShadowRow.add( objectCastShadow );
  185. const objectReceiveShadow = new UIBoolean( false, strings.getKey( 'sidebar/object/receive' ) ).onChange( update );
  186. objectShadowRow.add( objectReceiveShadow );
  187. container.add( objectShadowRow );
  188. // shadow bias
  189. const objectShadowBiasRow = new UIRow();
  190. objectShadowBiasRow.add( new UIText( strings.getKey( 'sidebar/object/shadowBias' ) ).setClass( 'Label' ) );
  191. const objectShadowBias = new UINumber( 0 ).setPrecision( 5 ).setStep( 0.0001 ).setNudge( 0.00001 ).onChange( update );
  192. objectShadowBiasRow.add( objectShadowBias );
  193. container.add( objectShadowBiasRow );
  194. // shadow normal offset
  195. const objectShadowNormalBiasRow = new UIRow();
  196. objectShadowNormalBiasRow.add( new UIText( strings.getKey( 'sidebar/object/shadowNormalBias' ) ).setClass( 'Label' ) );
  197. const objectShadowNormalBias = new UINumber( 0 ).onChange( update );
  198. objectShadowNormalBiasRow.add( objectShadowNormalBias );
  199. container.add( objectShadowNormalBiasRow );
  200. // shadow radius
  201. const objectShadowRadiusRow = new UIRow();
  202. objectShadowRadiusRow.add( new UIText( strings.getKey( 'sidebar/object/shadowRadius' ) ).setClass( 'Label' ) );
  203. const objectShadowRadius = new UINumber( 1 ).onChange( update );
  204. objectShadowRadiusRow.add( objectShadowRadius );
  205. container.add( objectShadowRadiusRow );
  206. // visible
  207. const objectVisibleRow = new UIRow();
  208. const objectVisible = new UICheckbox().onChange( update );
  209. objectVisibleRow.add( new UIText( strings.getKey( 'sidebar/object/visible' ) ).setClass( 'Label' ) );
  210. objectVisibleRow.add( objectVisible );
  211. container.add( objectVisibleRow );
  212. // frustumCulled
  213. const objectFrustumCulledRow = new UIRow();
  214. const objectFrustumCulled = new UICheckbox().onChange( update );
  215. objectFrustumCulledRow.add( new UIText( strings.getKey( 'sidebar/object/frustumcull' ) ).setClass( 'Label' ) );
  216. objectFrustumCulledRow.add( objectFrustumCulled );
  217. container.add( objectFrustumCulledRow );
  218. // renderOrder
  219. const objectRenderOrderRow = new UIRow();
  220. const objectRenderOrder = new UIInteger().setWidth( '50px' ).onChange( update );
  221. objectRenderOrderRow.add( new UIText( strings.getKey( 'sidebar/object/renderorder' ) ).setClass( 'Label' ) );
  222. objectRenderOrderRow.add( objectRenderOrder );
  223. container.add( objectRenderOrderRow );
  224. // user data
  225. const objectUserDataRow = new UIRow();
  226. const objectUserData = new UITextArea().setWidth( '150px' ).setHeight( '40px' ).setFontSize( '12px' ).onChange( update );
  227. objectUserData.onKeyUp( function () {
  228. try {
  229. JSON.parse( objectUserData.getValue() );
  230. objectUserData.dom.classList.add( 'success' );
  231. objectUserData.dom.classList.remove( 'fail' );
  232. } catch ( error ) {
  233. objectUserData.dom.classList.remove( 'success' );
  234. objectUserData.dom.classList.add( 'fail' );
  235. }
  236. } );
  237. objectUserDataRow.add( new UIText( strings.getKey( 'sidebar/object/userdata' ) ).setClass( 'Label' ) );
  238. objectUserDataRow.add( objectUserData );
  239. container.add( objectUserDataRow );
  240. // Export JSON
  241. const exportJson = new UIButton( strings.getKey( 'sidebar/object/export' ) );
  242. exportJson.setMarginLeft( '120px' );
  243. exportJson.onClick( function () {
  244. const object = editor.selected;
  245. let output = object.toJSON();
  246. try {
  247. output = JSON.stringify( output, null, '\t' );
  248. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  249. } catch ( e ) {
  250. output = JSON.stringify( output );
  251. }
  252. const left = ( screen.width - 500 ) / 2;
  253. const top = ( screen.height - 500 ) / 2;
  254. const url = URL.createObjectURL( new Blob( [ output ], { type: 'text/plain' } ) );
  255. window.open( url, '_blank', `location=no,left=${left},top=${top},width=500,height=500` );
  256. } );
  257. container.add( exportJson );
  258. // Animations
  259. container.add( new SidebarObjectAnimation( editor ) );
  260. //
  261. function update() {
  262. const object = editor.selected;
  263. if ( object !== null ) {
  264. const newPosition = new THREE.Vector3( objectPositionX.getValue(), objectPositionY.getValue(), objectPositionZ.getValue() );
  265. if ( object.position.distanceTo( newPosition ) >= 0.01 ) {
  266. editor.execute( new SetPositionCommand( editor, object, newPosition ) );
  267. }
  268. const newRotation = new THREE.Euler( objectRotationX.getValue() * THREE.MathUtils.DEG2RAD, objectRotationY.getValue() * THREE.MathUtils.DEG2RAD, objectRotationZ.getValue() * THREE.MathUtils.DEG2RAD );
  269. if ( new THREE.Vector3().setFromEuler( object.rotation ).distanceTo( new THREE.Vector3().setFromEuler( newRotation ) ) >= 0.01 ) {
  270. editor.execute( new SetRotationCommand( editor, object, newRotation ) );
  271. }
  272. const newScale = new THREE.Vector3( objectScaleX.getValue(), objectScaleY.getValue(), objectScaleZ.getValue() );
  273. if ( object.scale.distanceTo( newScale ) >= 0.01 ) {
  274. editor.execute( new SetScaleCommand( editor, object, newScale ) );
  275. }
  276. if ( object.fov !== undefined && Math.abs( object.fov - objectFov.getValue() ) >= 0.01 ) {
  277. editor.execute( new SetValueCommand( editor, object, 'fov', objectFov.getValue() ) );
  278. object.updateProjectionMatrix();
  279. }
  280. if ( object.left !== undefined && Math.abs( object.left - objectLeft.getValue() ) >= 0.01 ) {
  281. editor.execute( new SetValueCommand( editor, object, 'left', objectLeft.getValue() ) );
  282. object.updateProjectionMatrix();
  283. }
  284. if ( object.right !== undefined && Math.abs( object.right - objectRight.getValue() ) >= 0.01 ) {
  285. editor.execute( new SetValueCommand( editor, object, 'right', objectRight.getValue() ) );
  286. object.updateProjectionMatrix();
  287. }
  288. if ( object.top !== undefined && Math.abs( object.top - objectTop.getValue() ) >= 0.01 ) {
  289. editor.execute( new SetValueCommand( editor, object, 'top', objectTop.getValue() ) );
  290. object.updateProjectionMatrix();
  291. }
  292. if ( object.bottom !== undefined && Math.abs( object.bottom - objectBottom.getValue() ) >= 0.01 ) {
  293. editor.execute( new SetValueCommand( editor, object, 'bottom', objectBottom.getValue() ) );
  294. object.updateProjectionMatrix();
  295. }
  296. if ( object.near !== undefined && Math.abs( object.near - objectNear.getValue() ) >= 0.01 ) {
  297. editor.execute( new SetValueCommand( editor, object, 'near', objectNear.getValue() ) );
  298. if ( object.isOrthographicCamera ) {
  299. object.updateProjectionMatrix();
  300. }
  301. }
  302. if ( object.far !== undefined && Math.abs( object.far - objectFar.getValue() ) >= 0.01 ) {
  303. editor.execute( new SetValueCommand( editor, object, 'far', objectFar.getValue() ) );
  304. if ( object.isOrthographicCamera ) {
  305. object.updateProjectionMatrix();
  306. }
  307. }
  308. if ( object.intensity !== undefined && Math.abs( object.intensity - objectIntensity.getValue() ) >= 0.01 ) {
  309. editor.execute( new SetValueCommand( editor, object, 'intensity', objectIntensity.getValue() ) );
  310. }
  311. if ( object.color !== undefined && object.color.getHex() !== objectColor.getHexValue() ) {
  312. editor.execute( new SetColorCommand( editor, object, 'color', objectColor.getHexValue() ) );
  313. }
  314. if ( object.groundColor !== undefined && object.groundColor.getHex() !== objectGroundColor.getHexValue() ) {
  315. editor.execute( new SetColorCommand( editor, object, 'groundColor', objectGroundColor.getHexValue() ) );
  316. }
  317. if ( object.distance !== undefined && Math.abs( object.distance - objectDistance.getValue() ) >= 0.01 ) {
  318. editor.execute( new SetValueCommand( editor, object, 'distance', objectDistance.getValue() ) );
  319. }
  320. if ( object.angle !== undefined && Math.abs( object.angle - objectAngle.getValue() ) >= 0.01 ) {
  321. editor.execute( new SetValueCommand( editor, object, 'angle', objectAngle.getValue() ) );
  322. }
  323. if ( object.penumbra !== undefined && Math.abs( object.penumbra - objectPenumbra.getValue() ) >= 0.01 ) {
  324. editor.execute( new SetValueCommand( editor, object, 'penumbra', objectPenumbra.getValue() ) );
  325. }
  326. if ( object.decay !== undefined && Math.abs( object.decay - objectDecay.getValue() ) >= 0.01 ) {
  327. editor.execute( new SetValueCommand( editor, object, 'decay', objectDecay.getValue() ) );
  328. }
  329. if ( object.visible !== objectVisible.getValue() ) {
  330. editor.execute( new SetValueCommand( editor, object, 'visible', objectVisible.getValue() ) );
  331. }
  332. if ( object.frustumCulled !== objectFrustumCulled.getValue() ) {
  333. editor.execute( new SetValueCommand( editor, object, 'frustumCulled', objectFrustumCulled.getValue() ) );
  334. }
  335. if ( object.renderOrder !== objectRenderOrder.getValue() ) {
  336. editor.execute( new SetValueCommand( editor, object, 'renderOrder', objectRenderOrder.getValue() ) );
  337. }
  338. if ( object.castShadow !== undefined && object.castShadow !== objectCastShadow.getValue() ) {
  339. editor.execute( new SetValueCommand( editor, object, 'castShadow', objectCastShadow.getValue() ) );
  340. }
  341. if ( object.receiveShadow !== objectReceiveShadow.getValue() ) {
  342. if ( object.material !== undefined ) object.material.needsUpdate = true;
  343. editor.execute( new SetValueCommand( editor, object, 'receiveShadow', objectReceiveShadow.getValue() ) );
  344. }
  345. if ( object.shadow !== undefined ) {
  346. if ( object.shadow.bias !== objectShadowBias.getValue() ) {
  347. editor.execute( new SetValueCommand( editor, object.shadow, 'bias', objectShadowBias.getValue() ) );
  348. }
  349. if ( object.shadow.normalBias !== objectShadowNormalBias.getValue() ) {
  350. editor.execute( new SetValueCommand( editor, object.shadow, 'normalBias', objectShadowNormalBias.getValue() ) );
  351. }
  352. if ( object.shadow.radius !== objectShadowRadius.getValue() ) {
  353. editor.execute( new SetValueCommand( editor, object.shadow, 'radius', objectShadowRadius.getValue() ) );
  354. }
  355. }
  356. try {
  357. const userData = JSON.parse( objectUserData.getValue() );
  358. if ( JSON.stringify( object.userData ) != JSON.stringify( userData ) ) {
  359. editor.execute( new SetValueCommand( editor, object, 'userData', userData ) );
  360. }
  361. } catch ( exception ) {
  362. console.warn( exception );
  363. }
  364. }
  365. }
  366. function updateRows( object ) {
  367. const properties = {
  368. 'fov': objectFovRow,
  369. 'left': objectLeftRow,
  370. 'right': objectRightRow,
  371. 'top': objectTopRow,
  372. 'bottom': objectBottomRow,
  373. 'near': objectNearRow,
  374. 'far': objectFarRow,
  375. 'intensity': objectIntensityRow,
  376. 'color': objectColorRow,
  377. 'groundColor': objectGroundColorRow,
  378. 'distance': objectDistanceRow,
  379. 'angle': objectAngleRow,
  380. 'penumbra': objectPenumbraRow,
  381. 'decay': objectDecayRow,
  382. 'castShadow': objectShadowRow,
  383. 'receiveShadow': objectReceiveShadow,
  384. 'shadow': [ objectShadowBiasRow, objectShadowNormalBiasRow, objectShadowRadiusRow ]
  385. };
  386. for ( const property in properties ) {
  387. const uiElement = properties[ property ];
  388. if ( Array.isArray( uiElement ) === true ) {
  389. for ( let i = 0; i < uiElement.length; i ++ ) {
  390. uiElement[ i ].setDisplay( object[ property ] !== undefined ? '' : 'none' );
  391. }
  392. } else {
  393. uiElement.setDisplay( object[ property ] !== undefined ? '' : 'none' );
  394. }
  395. }
  396. //
  397. if ( object.isLight ) {
  398. objectReceiveShadow.setDisplay( 'none' );
  399. }
  400. if ( object.isAmbientLight || object.isHemisphereLight ) {
  401. objectShadowRow.setDisplay( 'none' );
  402. }
  403. }
  404. function updateTransformRows( object ) {
  405. if ( object.isLight ||
  406. ( object.isObject3D && object.userData.targetInverse ) ) {
  407. objectRotationRow.setDisplay( 'none' );
  408. objectScaleRow.setDisplay( 'none' );
  409. } else {
  410. objectRotationRow.setDisplay( '' );
  411. objectScaleRow.setDisplay( '' );
  412. }
  413. }
  414. // events
  415. signals.objectSelected.add( function ( object ) {
  416. if ( object !== null ) {
  417. container.setDisplay( 'block' );
  418. updateRows( object );
  419. updateUI( object );
  420. } else {
  421. container.setDisplay( 'none' );
  422. }
  423. } );
  424. signals.objectChanged.add( function ( object ) {
  425. if ( object !== editor.selected ) return;
  426. updateUI( object );
  427. } );
  428. signals.refreshSidebarObject3D.add( function ( object ) {
  429. if ( object !== editor.selected ) return;
  430. updateUI( object );
  431. } );
  432. function updateUI( object ) {
  433. objectType.setValue( object.type );
  434. objectUUID.setValue( object.uuid );
  435. objectName.setValue( object.name );
  436. objectPositionX.setValue( object.position.x );
  437. objectPositionY.setValue( object.position.y );
  438. objectPositionZ.setValue( object.position.z );
  439. objectRotationX.setValue( object.rotation.x * THREE.MathUtils.RAD2DEG );
  440. objectRotationY.setValue( object.rotation.y * THREE.MathUtils.RAD2DEG );
  441. objectRotationZ.setValue( object.rotation.z * THREE.MathUtils.RAD2DEG );
  442. objectScaleX.setValue( object.scale.x );
  443. objectScaleY.setValue( object.scale.y );
  444. objectScaleZ.setValue( object.scale.z );
  445. if ( object.fov !== undefined ) {
  446. objectFov.setValue( object.fov );
  447. }
  448. if ( object.left !== undefined ) {
  449. objectLeft.setValue( object.left );
  450. }
  451. if ( object.right !== undefined ) {
  452. objectRight.setValue( object.right );
  453. }
  454. if ( object.top !== undefined ) {
  455. objectTop.setValue( object.top );
  456. }
  457. if ( object.bottom !== undefined ) {
  458. objectBottom.setValue( object.bottom );
  459. }
  460. if ( object.near !== undefined ) {
  461. objectNear.setValue( object.near );
  462. }
  463. if ( object.far !== undefined ) {
  464. objectFar.setValue( object.far );
  465. }
  466. if ( object.intensity !== undefined ) {
  467. objectIntensity.setValue( object.intensity );
  468. }
  469. if ( object.color !== undefined ) {
  470. objectColor.setHexValue( object.color.getHexString() );
  471. }
  472. if ( object.groundColor !== undefined ) {
  473. objectGroundColor.setHexValue( object.groundColor.getHexString() );
  474. }
  475. if ( object.distance !== undefined ) {
  476. objectDistance.setValue( object.distance );
  477. }
  478. if ( object.angle !== undefined ) {
  479. objectAngle.setValue( object.angle );
  480. }
  481. if ( object.penumbra !== undefined ) {
  482. objectPenumbra.setValue( object.penumbra );
  483. }
  484. if ( object.decay !== undefined ) {
  485. objectDecay.setValue( object.decay );
  486. }
  487. if ( object.castShadow !== undefined ) {
  488. objectCastShadow.setValue( object.castShadow );
  489. }
  490. if ( object.receiveShadow !== undefined ) {
  491. objectReceiveShadow.setValue( object.receiveShadow );
  492. }
  493. if ( object.shadow !== undefined ) {
  494. objectShadowBias.setValue( object.shadow.bias );
  495. objectShadowNormalBias.setValue( object.shadow.normalBias );
  496. objectShadowRadius.setValue( object.shadow.radius );
  497. }
  498. objectVisible.setValue( object.visible );
  499. objectFrustumCulled.setValue( object.frustumCulled );
  500. objectRenderOrder.setValue( object.renderOrder );
  501. try {
  502. objectUserData.setValue( JSON.stringify( object.userData, null, ' ' ) );
  503. } catch ( error ) {
  504. console.log( error );
  505. }
  506. objectUserData.setBorderColor( 'transparent' );
  507. objectUserData.setBackgroundColor( '' );
  508. updateTransformRows( object );
  509. }
  510. return container;
  511. }
  512. export { SidebarObject };