Sidebar.Object.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. Sidebar.Object = function ( editor ) {
  5. var signals = editor.signals;
  6. var container = new UI.Panel();
  7. container.setBorderTop( '0' );
  8. container.setPaddingTop( '20px' );
  9. container.setDisplay( 'none' );
  10. // Actions
  11. var objectActions = new UI.Select().setPosition( 'absolute' ).setRight( '8px' ).setFontSize( '11px' );
  12. objectActions.setOptions( {
  13. 'Actions': 'Actions',
  14. 'Reset Position': 'Reset Position',
  15. 'Reset Rotation': 'Reset Rotation',
  16. 'Reset Scale': 'Reset Scale'
  17. } );
  18. objectActions.onClick( function ( event ) {
  19. event.stopPropagation(); // Avoid panel collapsing
  20. } );
  21. objectActions.onChange( function ( event ) {
  22. var object = editor.selected;
  23. switch ( this.getValue() ) {
  24. case 'Reset Position':
  25. editor.execute( new SetPositionCommand( object, new THREE.Vector3( 0, 0, 0 ) ) );
  26. break;
  27. case 'Reset Rotation':
  28. editor.execute( new SetRotationCommand( object, new THREE.Euler( 0, 0, 0 ) ) );
  29. break;
  30. case 'Reset Scale':
  31. editor.execute( new SetScaleCommand( object, new THREE.Vector3( 1, 1, 1 ) ) );
  32. break;
  33. }
  34. this.setValue( 'Actions' );
  35. } );
  36. // container.addStatic( objectActions );
  37. // type
  38. var objectTypeRow = new UI.Row();
  39. var objectType = new UI.Text();
  40. objectTypeRow.add( new UI.Text( 'Type' ).setWidth( '90px' ) );
  41. objectTypeRow.add( objectType );
  42. container.add( objectTypeRow );
  43. // uuid
  44. var objectUUIDRow = new UI.Row();
  45. var objectUUID = new UI.Input().setWidth( '115px' ).setFontSize( '12px' ).setDisabled( true );
  46. var objectUUIDRenew = new UI.Button( '⟳' ).setMarginLeft( '7px' ).onClick( function () {
  47. objectUUID.setValue( THREE.Math.generateUUID() );
  48. editor.execute( new SetUuidCommand( editor.selected, objectUUID.getValue() ) );
  49. } );
  50. objectUUIDRow.add( new UI.Text( 'UUID' ).setWidth( '90px' ) );
  51. objectUUIDRow.add( objectUUID );
  52. objectUUIDRow.add( objectUUIDRenew );
  53. container.add( objectUUIDRow );
  54. // name
  55. var objectNameRow = new UI.Row();
  56. var objectName = new UI.Input().setWidth( '150px' ).setFontSize( '12px' ).onChange( function () {
  57. editor.execute( new SetValueCommand( editor.selected, 'name', objectName.getValue() ) );
  58. } );
  59. objectNameRow.add( new UI.Text( 'Name' ).setWidth( '90px' ) );
  60. objectNameRow.add( objectName );
  61. container.add( objectNameRow );
  62. /*
  63. // parent
  64. var objectParentRow = new UI.Row();
  65. var objectParent = new UI.Select().setWidth( '150px' ).setFontSize( '12px' ).onChange( update );
  66. objectParentRow.add( new UI.Text( 'Parent' ).setWidth( '90px' ) );
  67. objectParentRow.add( objectParent );
  68. container.add( objectParentRow );
  69. */
  70. // position
  71. var objectPositionRow = new UI.Row();
  72. var objectPositionX = new UI.Number().setWidth( '50px' ).onChange( update );
  73. var objectPositionY = new UI.Number().setWidth( '50px' ).onChange( update );
  74. var objectPositionZ = new UI.Number().setWidth( '50px' ).onChange( update );
  75. objectPositionRow.add( new UI.Text( 'Position' ).setWidth( '90px' ) );
  76. objectPositionRow.add( objectPositionX, objectPositionY, objectPositionZ );
  77. container.add( objectPositionRow );
  78. // rotation
  79. var objectRotationRow = new UI.Row();
  80. var objectRotationX = new UI.Number().setWidth( '50px' ).onChange( update );
  81. var objectRotationY = new UI.Number().setWidth( '50px' ).onChange( update );
  82. var objectRotationZ = new UI.Number().setWidth( '50px' ).onChange( update );
  83. objectRotationRow.add( new UI.Text( 'Rotation' ).setWidth( '90px' ) );
  84. objectRotationRow.add( objectRotationX, objectRotationY, objectRotationZ );
  85. container.add( objectRotationRow );
  86. // scale
  87. var objectScaleRow = new UI.Row();
  88. var objectScaleLock = new UI.Checkbox( true ).setPosition( 'absolute' ).setLeft( '75px' );
  89. var objectScaleX = new UI.Number( 1 ).setRange( 0.01, Infinity ).setWidth( '50px' ).onChange( updateScaleX );
  90. var objectScaleY = new UI.Number( 1 ).setRange( 0.01, Infinity ).setWidth( '50px' ).onChange( updateScaleY );
  91. var objectScaleZ = new UI.Number( 1 ).setRange( 0.01, Infinity ).setWidth( '50px' ).onChange( updateScaleZ );
  92. objectScaleRow.add( new UI.Text( 'Scale' ).setWidth( '90px' ) );
  93. objectScaleRow.add( objectScaleLock );
  94. objectScaleRow.add( objectScaleX, objectScaleY, objectScaleZ );
  95. container.add( objectScaleRow );
  96. // fov
  97. var objectFovRow = new UI.Row();
  98. var objectFov = new UI.Number().onChange( update );
  99. objectFovRow.add( new UI.Text( 'Fov' ).setWidth( '90px' ) );
  100. objectFovRow.add( objectFov );
  101. container.add( objectFovRow );
  102. // near
  103. var objectNearRow = new UI.Row();
  104. var objectNear = new UI.Number().onChange( update );
  105. objectNearRow.add( new UI.Text( 'Near' ).setWidth( '90px' ) );
  106. objectNearRow.add( objectNear );
  107. container.add( objectNearRow );
  108. // far
  109. var objectFarRow = new UI.Row();
  110. var objectFar = new UI.Number().onChange( update );
  111. objectFarRow.add( new UI.Text( 'Far' ).setWidth( '90px' ) );
  112. objectFarRow.add( objectFar );
  113. container.add( objectFarRow );
  114. // intensity
  115. var objectIntensityRow = new UI.Row();
  116. var objectIntensity = new UI.Number().setRange( 0, Infinity ).onChange( update );
  117. objectIntensityRow.add( new UI.Text( 'Intensity' ).setWidth( '90px' ) );
  118. objectIntensityRow.add( objectIntensity );
  119. container.add( objectIntensityRow );
  120. // color
  121. var objectColorRow = new UI.Row();
  122. var objectColor = new UI.Color().onChange( update );
  123. objectColorRow.add( new UI.Text( 'Color' ).setWidth( '90px' ) );
  124. objectColorRow.add( objectColor );
  125. container.add( objectColorRow );
  126. // ground color
  127. var objectGroundColorRow = new UI.Row();
  128. var objectGroundColor = new UI.Color().onChange( update );
  129. objectGroundColorRow.add( new UI.Text( 'Ground color' ).setWidth( '90px' ) );
  130. objectGroundColorRow.add( objectGroundColor );
  131. container.add( objectGroundColorRow );
  132. // distance
  133. var objectDistanceRow = new UI.Row();
  134. var objectDistance = new UI.Number().setRange( 0, Infinity ).onChange( update );
  135. objectDistanceRow.add( new UI.Text( 'Distance' ).setWidth( '90px' ) );
  136. objectDistanceRow.add( objectDistance );
  137. container.add( objectDistanceRow );
  138. // angle
  139. var objectAngleRow = new UI.Row();
  140. var objectAngle = new UI.Number().setPrecision( 3 ).setRange( 0, Math.PI / 2 ).onChange( update );
  141. objectAngleRow.add( new UI.Text( 'Angle' ).setWidth( '90px' ) );
  142. objectAngleRow.add( objectAngle );
  143. container.add( objectAngleRow );
  144. // penumbra
  145. var objectPenumbraRow = new UI.Row();
  146. var objectPenumbra = new UI.Number().setRange( 0, 1 ).onChange( update );
  147. objectPenumbraRow.add( new UI.Text( 'Penumbra' ).setWidth( '90px' ) );
  148. objectPenumbraRow.add( objectPenumbra );
  149. container.add( objectPenumbraRow );
  150. // decay
  151. var objectDecayRow = new UI.Row();
  152. var objectDecay = new UI.Number().setRange( 0, Infinity ).onChange( update );
  153. objectDecayRow.add( new UI.Text( 'Decay' ).setWidth( '90px' ) );
  154. objectDecayRow.add( objectDecay );
  155. container.add( objectDecayRow );
  156. // shadow
  157. var objectShadowRow = new UI.Row();
  158. objectShadowRow.add( new UI.Text( 'Shadow' ).setWidth( '90px' ) );
  159. var objectCastShadow = new UI.THREE.Boolean( false, 'cast' ).onChange( update );
  160. objectShadowRow.add( objectCastShadow );
  161. var objectReceiveShadow = new UI.THREE.Boolean( false, 'receive' ).onChange( update );
  162. objectShadowRow.add( objectReceiveShadow );
  163. var objectShadowRadius = new UI.Number( 1 ).onChange( update );
  164. objectShadowRow.add( objectShadowRadius );
  165. container.add( objectShadowRow );
  166. // visible
  167. var objectVisibleRow = new UI.Row();
  168. var objectVisible = new UI.Checkbox().onChange( update );
  169. objectVisibleRow.add( new UI.Text( 'Visible' ).setWidth( '90px' ) );
  170. objectVisibleRow.add( objectVisible );
  171. container.add( objectVisibleRow );
  172. // user data
  173. var timeout;
  174. var objectUserDataRow = new UI.Row();
  175. var objectUserData = new UI.TextArea().setWidth( '150px' ).setHeight( '40px' ).setFontSize( '12px' ).onChange( update );
  176. objectUserData.onKeyUp( function () {
  177. try {
  178. JSON.parse( objectUserData.getValue() );
  179. objectUserData.dom.classList.add( 'success' );
  180. objectUserData.dom.classList.remove( 'fail' );
  181. } catch ( error ) {
  182. objectUserData.dom.classList.remove( 'success' );
  183. objectUserData.dom.classList.add( 'fail' );
  184. }
  185. } );
  186. objectUserDataRow.add( new UI.Text( 'User data' ).setWidth( '90px' ) );
  187. objectUserDataRow.add( objectUserData );
  188. container.add( objectUserDataRow );
  189. //
  190. function updateScaleX() {
  191. var object = editor.selected;
  192. if ( objectScaleLock.getValue() === true ) {
  193. var scale = objectScaleX.getValue() / object.scale.x;
  194. objectScaleY.setValue( objectScaleY.getValue() * scale );
  195. objectScaleZ.setValue( objectScaleZ.getValue() * scale );
  196. }
  197. update();
  198. }
  199. function updateScaleY() {
  200. var object = editor.selected;
  201. if ( objectScaleLock.getValue() === true ) {
  202. var scale = objectScaleY.getValue() / object.scale.y;
  203. objectScaleX.setValue( objectScaleX.getValue() * scale );
  204. objectScaleZ.setValue( objectScaleZ.getValue() * scale );
  205. }
  206. update();
  207. }
  208. function updateScaleZ() {
  209. var object = editor.selected;
  210. if ( objectScaleLock.getValue() === true ) {
  211. var scale = objectScaleZ.getValue() / object.scale.z;
  212. objectScaleX.setValue( objectScaleX.getValue() * scale );
  213. objectScaleY.setValue( objectScaleY.getValue() * scale );
  214. }
  215. update();
  216. }
  217. function update() {
  218. var object = editor.selected;
  219. if ( object !== null ) {
  220. /*
  221. if ( object.parent !== null ) {
  222. var newParentId = parseInt( objectParent.getValue() );
  223. if ( object.parent.id !== newParentId && object.id !== newParentId ) {
  224. editor.execute( new MoveObjectCommand( object, editor.scene.getObjectById( newParentId ) ) );
  225. }
  226. }
  227. */
  228. var newPosition = new THREE.Vector3( objectPositionX.getValue(), objectPositionY.getValue(), objectPositionZ.getValue() );
  229. if ( object.position.distanceTo( newPosition ) >= 0.01 ) {
  230. editor.execute( new SetPositionCommand( object, newPosition ) );
  231. }
  232. var newRotation = new THREE.Euler( objectRotationX.getValue(), objectRotationY.getValue(), objectRotationZ.getValue() );
  233. if ( object.rotation.toVector3().distanceTo( newRotation.toVector3() ) >= 0.01 ) {
  234. editor.execute( new SetRotationCommand( object, newRotation ) );
  235. }
  236. var newScale = new THREE.Vector3( objectScaleX.getValue(), objectScaleY.getValue(), objectScaleZ.getValue() );
  237. if ( object.scale.distanceTo( newScale ) >= 0.01 ) {
  238. editor.execute( new SetScaleCommand( object, newScale ) );
  239. }
  240. if ( object.fov !== undefined && Math.abs( object.fov - objectFov.getValue() ) >= 0.01 ) {
  241. editor.execute( new SetValueCommand( object, 'fov', objectFov.getValue() ) );
  242. object.updateProjectionMatrix();
  243. }
  244. if ( object.near !== undefined && Math.abs( object.near - objectNear.getValue() ) >= 0.01 ) {
  245. editor.execute( new SetValueCommand( object, 'near', objectNear.getValue() ) );
  246. }
  247. if ( object.far !== undefined && Math.abs( object.far - objectFar.getValue() ) >= 0.01 ) {
  248. editor.execute( new SetValueCommand( object, 'far', objectFar.getValue() ) );
  249. }
  250. if ( object.intensity !== undefined && Math.abs( object.intensity - objectIntensity.getValue() ) >= 0.01 ) {
  251. editor.execute( new SetValueCommand( object, 'intensity', objectIntensity.getValue() ) );
  252. }
  253. if ( object.color !== undefined && object.color.getHex() !== objectColor.getHexValue() ) {
  254. editor.execute( new SetColorCommand( object, 'color', objectColor.getHexValue() ) );
  255. }
  256. if ( object.groundColor !== undefined && object.groundColor.getHex() !== objectGroundColor.getHexValue() ) {
  257. editor.execute( new SetColorCommand( object, 'groundColor', objectGroundColor.getHexValue() ) );
  258. }
  259. if ( object.distance !== undefined && Math.abs( object.distance - objectDistance.getValue() ) >= 0.01 ) {
  260. editor.execute( new SetValueCommand( object, 'distance', objectDistance.getValue() ) );
  261. }
  262. if ( object.angle !== undefined && Math.abs( object.angle - objectAngle.getValue() ) >= 0.01 ) {
  263. editor.execute( new SetValueCommand( object, 'angle', objectAngle.getValue() ) );
  264. }
  265. if ( object.penumbra !== undefined && Math.abs( object.penumbra - objectPenumbra.getValue() ) >= 0.01 ) {
  266. editor.execute( new SetValueCommand( object, 'penumbra', objectPenumbra.getValue() ) );
  267. }
  268. if ( object.decay !== undefined && Math.abs( object.decay - objectDecay.getValue() ) >= 0.01 ) {
  269. editor.execute( new SetValueCommand( object, 'decay', objectDecay.getValue() ) );
  270. }
  271. if ( object.visible !== objectVisible.getValue() ) {
  272. editor.execute( new SetValueCommand( object, 'visible', objectVisible.getValue() ) );
  273. }
  274. if ( object.castShadow !== undefined && object.castShadow !== objectCastShadow.getValue() ) {
  275. editor.execute( new SetValueCommand( object, 'castShadow', objectCastShadow.getValue() ) );
  276. }
  277. if ( object.receiveShadow !== undefined && object.receiveShadow !== objectReceiveShadow.getValue() ) {
  278. editor.execute( new SetValueCommand( object, 'receiveShadow', objectReceiveShadow.getValue() ) );
  279. object.material.needsUpdate = true;
  280. }
  281. if ( object.shadow !== undefined ) {
  282. if ( object.shadow.radius !== objectShadowRadius.getValue() ) {
  283. editor.execute( new SetValueCommand( object.shadow, 'radius', objectShadowRadius.getValue() ) );
  284. }
  285. }
  286. try {
  287. var userData = JSON.parse( objectUserData.getValue() );
  288. if ( JSON.stringify( object.userData ) != JSON.stringify( userData ) ) {
  289. editor.execute( new SetValueCommand( object, 'userData', userData ) );
  290. }
  291. } catch ( exception ) {
  292. console.warn( exception );
  293. }
  294. }
  295. }
  296. function updateRows( object ) {
  297. var properties = {
  298. // 'parent': objectParentRow,
  299. 'fov': objectFovRow,
  300. 'near': objectNearRow,
  301. 'far': objectFarRow,
  302. 'intensity': objectIntensityRow,
  303. 'color': objectColorRow,
  304. 'groundColor': objectGroundColorRow,
  305. 'distance' : objectDistanceRow,
  306. 'angle' : objectAngleRow,
  307. 'penumbra' : objectPenumbraRow,
  308. 'decay' : objectDecayRow,
  309. 'castShadow' : objectShadowRow,
  310. 'receiveShadow' : objectReceiveShadow,
  311. 'shadow': objectShadowRadius
  312. };
  313. for ( var property in properties ) {
  314. properties[ property ].setDisplay( object[ property ] !== undefined ? '' : 'none' );
  315. }
  316. }
  317. function updateTransformRows( object ) {
  318. if ( object instanceof THREE.Light ||
  319. ( object instanceof THREE.Object3D && object.userData.targetInverse ) ) {
  320. objectRotationRow.setDisplay( 'none' );
  321. objectScaleRow.setDisplay( 'none' );
  322. } else {
  323. objectRotationRow.setDisplay( '' );
  324. objectScaleRow.setDisplay( '' );
  325. }
  326. }
  327. // events
  328. signals.objectSelected.add( function ( object ) {
  329. if ( object !== null ) {
  330. container.setDisplay( 'block' );
  331. updateRows( object );
  332. updateUI( object );
  333. } else {
  334. container.setDisplay( 'none' );
  335. }
  336. } );
  337. /*
  338. signals.sceneGraphChanged.add( function () {
  339. var scene = editor.scene;
  340. var options = {};
  341. scene.traverse( function ( object ) {
  342. options[ object.id ] = object.name;
  343. } );
  344. objectParent.setOptions( options );
  345. } );
  346. */
  347. signals.objectChanged.add( function ( object ) {
  348. if ( object !== editor.selected ) return;
  349. updateUI( object );
  350. } );
  351. signals.refreshSidebarObject3D.add( function ( object ) {
  352. if ( object !== editor.selected ) return;
  353. updateUI( object );
  354. } );
  355. function updateUI( object ) {
  356. objectType.setValue( object.type );
  357. objectUUID.setValue( object.uuid );
  358. objectName.setValue( object.name );
  359. /*
  360. if ( object.parent !== null ) {
  361. objectParent.setValue( object.parent.id );
  362. }
  363. */
  364. objectPositionX.setValue( object.position.x );
  365. objectPositionY.setValue( object.position.y );
  366. objectPositionZ.setValue( object.position.z );
  367. objectRotationX.setValue( object.rotation.x );
  368. objectRotationY.setValue( object.rotation.y );
  369. objectRotationZ.setValue( object.rotation.z );
  370. objectScaleX.setValue( object.scale.x );
  371. objectScaleY.setValue( object.scale.y );
  372. objectScaleZ.setValue( object.scale.z );
  373. if ( object.fov !== undefined ) {
  374. objectFov.setValue( object.fov );
  375. }
  376. if ( object.near !== undefined ) {
  377. objectNear.setValue( object.near );
  378. }
  379. if ( object.far !== undefined ) {
  380. objectFar.setValue( object.far );
  381. }
  382. if ( object.intensity !== undefined ) {
  383. objectIntensity.setValue( object.intensity );
  384. }
  385. if ( object.color !== undefined ) {
  386. objectColor.setHexValue( object.color.getHexString() );
  387. }
  388. if ( object.groundColor !== undefined ) {
  389. objectGroundColor.setHexValue( object.groundColor.getHexString() );
  390. }
  391. if ( object.distance !== undefined ) {
  392. objectDistance.setValue( object.distance );
  393. }
  394. if ( object.angle !== undefined ) {
  395. objectAngle.setValue( object.angle );
  396. }
  397. if ( object.penumbra !== undefined ) {
  398. objectPenumbra.setValue( object.penumbra );
  399. }
  400. if ( object.decay !== undefined ) {
  401. objectDecay.setValue( object.decay );
  402. }
  403. if ( object.castShadow !== undefined ) {
  404. objectCastShadow.setValue( object.castShadow );
  405. }
  406. if ( object.receiveShadow !== undefined ) {
  407. objectReceiveShadow.setValue( object.receiveShadow );
  408. }
  409. if ( object.shadow !== undefined ) {
  410. objectShadowRadius.setValue( object.shadow.radius );
  411. }
  412. objectVisible.setValue( object.visible );
  413. try {
  414. objectUserData.setValue( JSON.stringify( object.userData, null, ' ' ) );
  415. } catch ( error ) {
  416. console.log( error );
  417. }
  418. objectUserData.setBorderColor( 'transparent' );
  419. objectUserData.setBackgroundColor( '' );
  420. updateTransformRows( object );
  421. }
  422. return container;
  423. };