Sidebar.Geometry.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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' ) ).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. // Helpers
  92. const helpersRow = new UIRow().setMarginLeft( '120px' );
  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. // Export JSON
  106. const exportJson = new UIButton( strings.getKey( 'sidebar/geometry/export' ) );
  107. exportJson.setMarginLeft( '120px' );
  108. exportJson.onClick( function () {
  109. const object = editor.selected;
  110. const geometry = object.geometry;
  111. let output = geometry.toJSON();
  112. try {
  113. output = JSON.stringify( output, null, '\t' );
  114. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  115. } catch ( e ) {
  116. output = JSON.stringify( output );
  117. }
  118. const left = ( screen.width - 500 ) / 2;
  119. const top = ( screen.height - 500 ) / 2;
  120. const url = URL.createObjectURL( new Blob( [ output ], { type: 'text/plain' } ) );
  121. window.open( url, '_blank', `location=no,left=${left},top=${top},width=500,height=500` );
  122. } );
  123. container.add( exportJson );
  124. //
  125. async function build() {
  126. const object = editor.selected;
  127. if ( object && object.geometry ) {
  128. const geometry = object.geometry;
  129. container.setDisplay( 'block' );
  130. geometryType.setValue( geometry.type );
  131. geometryUUID.setValue( geometry.uuid );
  132. geometryName.setValue( geometry.name );
  133. //
  134. if ( currentGeometryType !== geometry.type ) {
  135. parameters.clear();
  136. if ( geometry.type === 'BufferGeometry' ) {
  137. parameters.add( new SidebarGeometryModifiers( editor, object ) );
  138. } else {
  139. const { GeometryParametersPanel } = await import( `./Sidebar.Geometry.${ geometry.type }.js` );
  140. parameters.add( new GeometryParametersPanel( editor, object ) );
  141. }
  142. currentGeometryType = geometry.type;
  143. }
  144. if ( geometry.boundingBox === null ) geometry.computeBoundingBox();
  145. const boundingBox = geometry.boundingBox;
  146. const x = Math.floor( ( boundingBox.max.x - boundingBox.min.x ) * 1000 ) / 1000;
  147. const y = Math.floor( ( boundingBox.max.y - boundingBox.min.y ) * 1000 ) / 1000;
  148. const z = Math.floor( ( boundingBox.max.z - boundingBox.min.z ) * 1000 ) / 1000;
  149. geometryBoundingBox.setInnerHTML( `${x}<br/>${y}<br/>${z}` );
  150. helpersRow.setDisplay( geometry.hasAttribute( 'normal' ) ? '' : 'none' );
  151. } else {
  152. container.setDisplay( 'none' );
  153. }
  154. }
  155. signals.objectSelected.add( function () {
  156. currentGeometryType = null;
  157. build();
  158. } );
  159. signals.geometryChanged.add( build );
  160. return container;
  161. }
  162. export { SidebarGeometry };