Sidebar.Object3D.js 16 KB

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