Sidebar.Geometry.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. import * as THREE from 'three';
  2. import { UIPanel, UIRow, UIText, UIInput, UIButton, UISpan, UITextArea } from './libs/ui.js';
  3. import { SetGeometryValueCommand } from './commands/SetGeometryValueCommand.js';
  4. import { SidebarGeometryBufferGeometry } from './Sidebar.Geometry.BufferGeometry.js';
  5. import { SidebarGeometryModifiers } from './Sidebar.Geometry.Modifiers.js';
  6. import { VertexNormalsHelper } from 'three/addons/helpers/VertexNormalsHelper.js';
  7. function SidebarGeometry( editor ) {
  8. const strings = editor.strings;
  9. const signals = editor.signals;
  10. const container = new UIPanel();
  11. container.setBorderTop( '0' );
  12. container.setDisplay( 'none' );
  13. container.setPaddingTop( '20px' );
  14. let currentGeometryType = null;
  15. // Actions
  16. /*
  17. let objectActions = new UISelect().setPosition( 'absolute' ).setRight( '8px' ).setFontSize( '11px' );
  18. objectActions.setOptions( {
  19. 'Actions': 'Actions',
  20. 'Center': 'Center',
  21. 'Convert': 'Convert',
  22. 'Flatten': 'Flatten'
  23. } );
  24. objectActions.onClick( function ( event ) {
  25. event.stopPropagation(); // Avoid panel collapsing
  26. } );
  27. objectActions.onChange( function ( event ) {
  28. let action = this.getValue();
  29. let object = editor.selected;
  30. let geometry = object.geometry;
  31. if ( confirm( action + ' ' + object.name + '?' ) === false ) return;
  32. switch ( action ) {
  33. case 'Center':
  34. let offset = geometry.center();
  35. let newPosition = object.position.clone();
  36. newPosition.sub( offset );
  37. editor.execute( new SetPositionCommand( editor, object, newPosition ) );
  38. editor.signals.geometryChanged.dispatch( object );
  39. break;
  40. case 'Flatten':
  41. let newGeometry = geometry.clone();
  42. newGeometry.uuid = geometry.uuid;
  43. newGeometry.applyMatrix( object.matrix );
  44. let cmds = [ new SetGeometryCommand( editor, object, newGeometry ),
  45. new SetPositionCommand( editor, object, new THREE.Vector3( 0, 0, 0 ) ),
  46. new SetRotationCommand( editor, object, new THREE.Euler( 0, 0, 0 ) ),
  47. new SetScaleCommand( editor, object, new THREE.Vector3( 1, 1, 1 ) ) ];
  48. editor.execute( new MultiCmdsCommand( editor, cmds ), 'Flatten Geometry' );
  49. break;
  50. }
  51. this.setValue( 'Actions' );
  52. } );
  53. container.addStatic( objectActions );
  54. */
  55. // type
  56. const geometryTypeRow = new UIRow();
  57. const geometryType = new UIText();
  58. geometryTypeRow.add( new UIText( strings.getKey( 'sidebar/geometry/type' ) ).setClass( 'Label' ) );
  59. geometryTypeRow.add( geometryType );
  60. container.add( geometryTypeRow );
  61. // uuid
  62. const geometryUUIDRow = new UIRow();
  63. const geometryUUID = new UIInput().setWidth( '102px' ).setFontSize( '12px' ).setDisabled( true );
  64. const geometryUUIDRenew = new UIButton( strings.getKey( 'sidebar/geometry/new' ) ).setMarginLeft( '7px' ).onClick( function () {
  65. geometryUUID.setValue( THREE.MathUtils.generateUUID() );
  66. editor.execute( new SetGeometryValueCommand( editor, editor.selected, 'uuid', geometryUUID.getValue() ) );
  67. } );
  68. geometryUUIDRow.add( new UIText( strings.getKey( 'sidebar/geometry/uuid' ) ).setClass( 'Label' ) );
  69. geometryUUIDRow.add( geometryUUID );
  70. geometryUUIDRow.add( geometryUUIDRenew );
  71. container.add( geometryUUIDRow );
  72. // name
  73. const geometryNameRow = new UIRow();
  74. const geometryName = new UIInput().setWidth( '150px' ).setFontSize( '12px' ).onChange( function () {
  75. editor.execute( new SetGeometryValueCommand( editor, editor.selected, 'name', geometryName.getValue() ) );
  76. } );
  77. geometryNameRow.add( new UIText( strings.getKey( 'sidebar/geometry/name' ) ).setClass( 'Label' ) );
  78. geometryNameRow.add( geometryName );
  79. container.add( geometryNameRow );
  80. // parameters
  81. const parameters = new UISpan();
  82. container.add( parameters );
  83. // buffergeometry
  84. container.add( new SidebarGeometryBufferGeometry( editor ) );
  85. // Size
  86. const geometryBoundingBox = new UIText().setFontSize( '12px' );
  87. const geometryBoundingBoxRow = new UIRow();
  88. geometryBoundingBoxRow.add( new UIText( strings.getKey( 'sidebar/geometry/bounds' ) ).setClass( 'Label' ) );
  89. geometryBoundingBoxRow.add( geometryBoundingBox );
  90. container.add( geometryBoundingBoxRow );
  91. // userData
  92. const geometryUserDataRow = new UIRow();
  93. const geometryUserData = new UITextArea().setValue( '{}' ).setWidth( '150px' ).setHeight( '40px' ).setFontSize( '12px' ).onChange( function () {
  94. try {
  95. const userData = JSON.parse( geometryUserData.getValue() );
  96. if ( JSON.stringify( editor.selected.geometry.userData ) != JSON.stringify( userData ) ) {
  97. editor.execute( new SetGeometryValueCommand( editor, editor.selected, 'userData', userData ) );
  98. build();
  99. }
  100. } catch ( exception ) {
  101. console.warn( exception );
  102. }
  103. } );
  104. geometryUserData.onKeyUp( function () {
  105. try {
  106. JSON.parse( geometryUserData.getValue() );
  107. geometryUserData.dom.classList.add( 'success' );
  108. geometryUserData.dom.classList.remove( 'fail' );
  109. } catch ( error ) {
  110. geometryUserData.dom.classList.remove( 'success' );
  111. geometryUserData.dom.classList.add( 'fail' );
  112. }
  113. } );
  114. geometryUserDataRow.add( new UIText( strings.getKey( 'sidebar/geometry/userdata' ) ).setClass( 'Label' ) );
  115. geometryUserDataRow.add( geometryUserData );
  116. container.add( geometryUserDataRow );
  117. // Helpers
  118. const helpersRow = new UIRow().setMarginLeft( '120px' );
  119. container.add( helpersRow );
  120. const vertexNormalsButton = new UIButton( strings.getKey( 'sidebar/geometry/show_vertex_normals' ) );
  121. vertexNormalsButton.onClick( function () {
  122. const object = editor.selected;
  123. if ( editor.helpers[ object.id ] === undefined ) {
  124. editor.addHelper( object, new VertexNormalsHelper( object ) );
  125. } else {
  126. editor.removeHelper( object );
  127. }
  128. signals.sceneGraphChanged.dispatch();
  129. } );
  130. helpersRow.add( vertexNormalsButton );
  131. // Export JSON
  132. const exportJson = new UIButton( strings.getKey( 'sidebar/geometry/export' ) );
  133. exportJson.setMarginLeft( '120px' );
  134. exportJson.onClick( function () {
  135. const object = editor.selected;
  136. const geometry = object.geometry;
  137. let output = geometry.toJSON();
  138. try {
  139. output = JSON.stringify( output, null, '\t' );
  140. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  141. } catch ( e ) {
  142. output = JSON.stringify( output );
  143. }
  144. editor.utils.save( new Blob( [ output ] ), `${ geometryName.getValue() || 'geometry' }.json` );
  145. } );
  146. container.add( exportJson );
  147. //
  148. async function build() {
  149. const object = editor.selected;
  150. if ( object && object.geometry ) {
  151. const geometry = object.geometry;
  152. container.setDisplay( 'block' );
  153. geometryType.setValue( geometry.type );
  154. geometryUUID.setValue( geometry.uuid );
  155. geometryName.setValue( geometry.name );
  156. //
  157. if ( currentGeometryType !== geometry.type ) {
  158. parameters.clear();
  159. if ( geometry.type === 'BufferGeometry' ) {
  160. parameters.add( new SidebarGeometryModifiers( editor, object ) );
  161. } else {
  162. const { GeometryParametersPanel } = await import( `./Sidebar.Geometry.${ geometry.type }.js` );
  163. parameters.add( new GeometryParametersPanel( editor, object ) );
  164. }
  165. currentGeometryType = geometry.type;
  166. }
  167. if ( geometry.boundingBox === null ) geometry.computeBoundingBox();
  168. const boundingBox = geometry.boundingBox;
  169. const x = Math.floor( ( boundingBox.max.x - boundingBox.min.x ) * 1000 ) / 1000;
  170. const y = Math.floor( ( boundingBox.max.y - boundingBox.min.y ) * 1000 ) / 1000;
  171. const z = Math.floor( ( boundingBox.max.z - boundingBox.min.z ) * 1000 ) / 1000;
  172. geometryBoundingBox.setInnerHTML( `${x}<br/>${y}<br/>${z}` );
  173. helpersRow.setDisplay( geometry.hasAttribute( 'normal' ) ? '' : 'none' );
  174. geometryUserData.setValue( JSON.stringify( geometry.userData, null, ' ' ) );
  175. //
  176. const helper = editor.helpers[ object.id ];
  177. if ( helper !== undefined ) {
  178. editor.removeHelper( object );
  179. editor.addHelper( object, new VertexNormalsHelper( object ) );
  180. }
  181. } else {
  182. container.setDisplay( 'none' );
  183. }
  184. }
  185. signals.objectSelected.add( function () {
  186. currentGeometryType = null;
  187. build();
  188. } );
  189. signals.geometryChanged.add( build );
  190. return container;
  191. }
  192. export { SidebarGeometry };