Sidebar.Geometry.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. import * as THREE from '../../build/three.module.js';
  5. import { UIPanel, UIRow, UIText, UIInput, UIButton, UISpan } from './libs/ui.js';
  6. import { SetGeometryValueCommand } from './commands/SetGeometryValueCommand.js';
  7. import { SidebarGeometryGeometry } from './Sidebar.Geometry.Geometry.js';
  8. import { SidebarGeometryBufferGeometry } from './Sidebar.Geometry.BufferGeometry.js';
  9. import { SidebarGeometryModifiers } from './Sidebar.Geometry.Modifiers.js';
  10. import { SidebarGeometryBoxGeometry } from './Sidebar.Geometry.BoxGeometry.js';
  11. import { SidebarGeometryCircleGeometry } from './Sidebar.Geometry.CircleGeometry.js';
  12. import { SidebarGeometryCylinderGeometry } from './Sidebar.Geometry.CylinderGeometry.js';
  13. import { SidebarGeometryDodecahedronGeometry } from './Sidebar.Geometry.DodecahedronGeometry.js';
  14. import { SidebarGeometryExtrudeGeometry } from './Sidebar.Geometry.ExtrudeGeometry.js';
  15. import { SidebarGeometryIcosahedronGeometry } from './Sidebar.Geometry.IcosahedronGeometry.js';
  16. import { SidebarGeometryLatheGeometry } from './Sidebar.Geometry.LatheGeometry.js';
  17. import { SidebarGeometryOctahedronGeometry } from './Sidebar.Geometry.OctahedronGeometry.js';
  18. import { SidebarGeometryPlaneGeometry } from './Sidebar.Geometry.PlaneGeometry.js';
  19. import { SidebarGeometryRingGeometry } from './Sidebar.Geometry.RingGeometry.js';
  20. import { SidebarGeometryShapeGeometry } from './Sidebar.Geometry.ShapeGeometry.js';
  21. import { SidebarGeometrySphereGeometry } from './Sidebar.Geometry.SphereGeometry.js';
  22. import { SidebarGeometryTeapotBufferGeometry } from './Sidebar.Geometry.TeapotBufferGeometry.js';
  23. import { SidebarGeometryTetrahedronGeometry } from './Sidebar.Geometry.TetrahedronGeometry.js';
  24. import { SidebarGeometryTorusGeometry } from './Sidebar.Geometry.TorusGeometry.js';
  25. import { SidebarGeometryTorusKnotGeometry } from './Sidebar.Geometry.TorusKnotGeometry.js';
  26. import { SidebarGeometryTubeGeometry } from './Sidebar.Geometry.TubeGeometry.js';
  27. var geometryUIClasses = {
  28. 'BoxBufferGeometry': SidebarGeometryBoxGeometry,
  29. 'CircleBufferGeometry': SidebarGeometryCircleGeometry,
  30. 'CylinderBufferGeometry': SidebarGeometryCylinderGeometry,
  31. 'DodecahedronBufferGeometry': SidebarGeometryDodecahedronGeometry,
  32. 'ExtrudeBufferGeometry': SidebarGeometryExtrudeGeometry,
  33. 'IcosahedronBufferGeometry': SidebarGeometryIcosahedronGeometry,
  34. 'LatheBufferGeometry': SidebarGeometryLatheGeometry,
  35. 'OctahedronBufferGeometry': SidebarGeometryOctahedronGeometry,
  36. 'PlaneBufferGeometry': SidebarGeometryPlaneGeometry,
  37. 'RingBufferGeometry': SidebarGeometryRingGeometry,
  38. 'ShapeBufferGeometry': SidebarGeometryShapeGeometry,
  39. 'SphereBufferGeometry': SidebarGeometrySphereGeometry,
  40. 'TeapotBufferGeometry': SidebarGeometryTeapotBufferGeometry,
  41. 'TetrahedronBufferGeometry': SidebarGeometryTetrahedronGeometry,
  42. 'TorusBufferGeometry': SidebarGeometryTorusGeometry,
  43. 'TorusKnotBufferGeometry': SidebarGeometryTorusKnotGeometry,
  44. 'TubeBufferGeometry': SidebarGeometryTubeGeometry
  45. };
  46. var SidebarGeometry = function ( editor ) {
  47. var strings = editor.strings;
  48. var signals = editor.signals;
  49. var container = new UIPanel();
  50. container.setBorderTop( '0' );
  51. container.setDisplay( 'none' );
  52. container.setPaddingTop( '20px' );
  53. var currentGeometryType = null;
  54. // Actions
  55. /*
  56. var objectActions = new UISelect().setPosition( 'absolute' ).setRight( '8px' ).setFontSize( '11px' );
  57. objectActions.setOptions( {
  58. 'Actions': 'Actions',
  59. 'Center': 'Center',
  60. 'Convert': 'Convert',
  61. 'Flatten': 'Flatten'
  62. } );
  63. objectActions.onClick( function ( event ) {
  64. event.stopPropagation(); // Avoid panel collapsing
  65. } );
  66. objectActions.onChange( function ( event ) {
  67. var action = this.getValue();
  68. var object = editor.selected;
  69. var geometry = object.geometry;
  70. if ( confirm( action + ' ' + object.name + '?' ) === false ) return;
  71. switch ( action ) {
  72. case 'Center':
  73. var offset = geometry.center();
  74. var newPosition = object.position.clone();
  75. newPosition.sub( offset );
  76. editor.execute( new SetPositionCommand( editor, object, newPosition ) );
  77. editor.signals.geometryChanged.dispatch( object );
  78. break;
  79. case 'Convert':
  80. if ( geometry && geometry.isGeometry ) {
  81. editor.execute( new SetGeometryCommand( editor, object, new THREE.BufferGeometry().fromGeometry( geometry ) ) );
  82. }
  83. break;
  84. case 'Flatten':
  85. var newGeometry = geometry.clone();
  86. newGeometry.uuid = geometry.uuid;
  87. newGeometry.applyMatrix( object.matrix );
  88. var cmds = [ new SetGeometryCommand( editor, object, newGeometry ),
  89. new SetPositionCommand( editor, object, new THREE.Vector3( 0, 0, 0 ) ),
  90. new SetRotationCommand( editor, object, new THREE.Euler( 0, 0, 0 ) ),
  91. new SetScaleCommand( editor, object, new THREE.Vector3( 1, 1, 1 ) ) ];
  92. editor.execute( new MultiCmdsCommand( editor, cmds ), 'Flatten Geometry' );
  93. break;
  94. }
  95. this.setValue( 'Actions' );
  96. } );
  97. container.addStatic( objectActions );
  98. */
  99. // type
  100. var geometryTypeRow = new UIRow();
  101. var geometryType = new UIText();
  102. geometryTypeRow.add( new UIText( strings.getKey( 'sidebar/geometry/type' ) ).setWidth( '90px' ) );
  103. geometryTypeRow.add( geometryType );
  104. container.add( geometryTypeRow );
  105. // uuid
  106. var geometryUUIDRow = new UIRow();
  107. var geometryUUID = new UIInput().setWidth( '102px' ).setFontSize( '12px' ).setDisabled( true );
  108. var geometryUUIDRenew = new UIButton( strings.getKey( 'sidebar/geometry/new' ) ).setMarginLeft( '7px' ).onClick( function () {
  109. geometryUUID.setValue( THREE.MathUtils.generateUUID() );
  110. editor.execute( new SetGeometryValueCommand( editor, editor.selected, 'uuid', geometryUUID.getValue() ) );
  111. } );
  112. geometryUUIDRow.add( new UIText( strings.getKey( 'sidebar/geometry/uuid' ) ).setWidth( '90px' ) );
  113. geometryUUIDRow.add( geometryUUID );
  114. geometryUUIDRow.add( geometryUUIDRenew );
  115. container.add( geometryUUIDRow );
  116. // name
  117. var geometryNameRow = new UIRow();
  118. var geometryName = new UIInput().setWidth( '150px' ).setFontSize( '12px' ).onChange( function () {
  119. editor.execute( new SetGeometryValueCommand( editor, editor.selected, 'name', geometryName.getValue() ) );
  120. } );
  121. geometryNameRow.add( new UIText( strings.getKey( 'sidebar/geometry/name' ) ).setWidth( '90px' ) );
  122. geometryNameRow.add( geometryName );
  123. container.add( geometryNameRow );
  124. // parameters
  125. var parameters = new UISpan();
  126. container.add( parameters );
  127. // geometry
  128. container.add( new SidebarGeometryGeometry( editor ) );
  129. // buffergeometry
  130. container.add( new SidebarGeometryBufferGeometry( editor ) );
  131. // size
  132. var geometryBoundingSphere = new UIText();
  133. container.add( new UIText( strings.getKey( 'sidebar/geometry/bounds' ) ).setWidth( '90px' ) );
  134. container.add( geometryBoundingSphere );
  135. //
  136. function build() {
  137. var object = editor.selected;
  138. if ( object && object.geometry ) {
  139. var geometry = object.geometry;
  140. container.setDisplay( 'block' );
  141. geometryType.setValue( geometry.type );
  142. geometryUUID.setValue( geometry.uuid );
  143. geometryName.setValue( geometry.name );
  144. //
  145. if ( currentGeometryType !== geometry.type ) {
  146. parameters.clear();
  147. if ( geometry.type === 'BufferGeometry' || geometry.type === 'Geometry' ) {
  148. parameters.add( new SidebarGeometryModifiers( editor, object ) );
  149. } else if ( geometryUIClasses[ geometry.type ] !== undefined ) {
  150. parameters.add( new geometryUIClasses[ geometry.type ]( editor, object ) );
  151. }
  152. currentGeometryType = geometry.type;
  153. }
  154. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  155. geometryBoundingSphere.setValue( Math.floor( geometry.boundingSphere.radius * 1000 ) / 1000 );
  156. } else {
  157. container.setDisplay( 'none' );
  158. }
  159. }
  160. signals.objectSelected.add( function () {
  161. currentGeometryType = null;
  162. build();
  163. } );
  164. signals.geometryChanged.add( build );
  165. return container;
  166. };
  167. export { SidebarGeometry };