Sidebar.Geometry.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import * as THREE from 'three';
  2. import { UIPanel, UIRow, UIText, UIInput, UIButton, UISpan } 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' ) ).setWidth( '90px' ) );
  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' ) ).setWidth( '90px' ) );
  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' ) ).setWidth( '90px' ) );
  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' ) ).setWidth( '90px' ) );
  89. geometryBoundingBoxRow.add( geometryBoundingBox );
  90. container.add( geometryBoundingBoxRow );
  91. // Helpers
  92. const helpersRow = new UIRow().setPaddingLeft( '90px' );
  93. container.add( helpersRow );
  94. const vertexNormalsButton = new UIButton( strings.getKey( 'sidebar/geometry/show_vertex_normals' ) );
  95. vertexNormalsButton.onClick( function () {
  96. const object = editor.selected;
  97. if ( editor.helpers[ object.id ] === undefined ) {
  98. editor.addHelper( object, new VertexNormalsHelper( object ) );
  99. } else {
  100. editor.removeHelper( object );
  101. }
  102. signals.sceneGraphChanged.dispatch();
  103. } );
  104. helpersRow.add( vertexNormalsButton );
  105. async function build() {
  106. const object = editor.selected;
  107. if ( object && object.geometry ) {
  108. const geometry = object.geometry;
  109. container.setDisplay( 'block' );
  110. geometryType.setValue( geometry.type );
  111. geometryUUID.setValue( geometry.uuid );
  112. geometryName.setValue( geometry.name );
  113. //
  114. if ( currentGeometryType !== geometry.type ) {
  115. parameters.clear();
  116. if ( geometry.type === 'BufferGeometry' ) {
  117. parameters.add( new SidebarGeometryModifiers( editor, object ) );
  118. } else {
  119. const { GeometryParametersPanel } = await import( `./Sidebar.Geometry.${ geometry.type }.js` );
  120. parameters.add( new GeometryParametersPanel( editor, object ) );
  121. }
  122. currentGeometryType = geometry.type;
  123. }
  124. if ( geometry.boundingBox === null ) geometry.computeBoundingBox();
  125. const boundingBox = geometry.boundingBox;
  126. const x = Math.floor( ( boundingBox.max.x - boundingBox.min.x ) * 1000 ) / 1000;
  127. const y = Math.floor( ( boundingBox.max.y - boundingBox.min.y ) * 1000 ) / 1000;
  128. const z = Math.floor( ( boundingBox.max.z - boundingBox.min.z ) * 1000 ) / 1000;
  129. geometryBoundingBox.setInnerHTML( `${x}<br/>${y}<br/>${z}` );
  130. helpersRow.setDisplay( geometry.hasAttribute( 'normal' ) ? '' : 'none' );
  131. } else {
  132. container.setDisplay( 'none' );
  133. }
  134. }
  135. signals.objectSelected.add( function () {
  136. currentGeometryType = null;
  137. build();
  138. } );
  139. signals.geometryChanged.add( build );
  140. return container;
  141. }
  142. export { SidebarGeometry };