Sidebar.Object3D.js 16 KB

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