Sidebar.Object3D.js 16 KB

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