Sidebar.Object.js 18 KB

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