Sidebar.Geometry.ExtrudeGeometry.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import * as THREE from 'three';
  2. import { UIDiv, UIRow, UIText, UIInteger, UICheckbox, UIButton, UINumber } from './libs/ui.js';
  3. import { SetGeometryCommand } from './commands/SetGeometryCommand.js';
  4. function GeometryParametersPanel( editor, object ) {
  5. const strings = editor.strings;
  6. const signals = editor.signals;
  7. const container = new UIDiv();
  8. const geometry = object.geometry;
  9. const parameters = geometry.parameters;
  10. const options = parameters.options;
  11. options.curveSegments = options.curveSegments != undefined ? options.curveSegments : 12;
  12. options.steps = options.steps != undefined ? options.steps : 1;
  13. options.depth = options.depth != undefined ? options.depth : 1;
  14. const bevelThickness = options.bevelThickness !== undefined ? options.bevelThickness : 0.2;
  15. options.bevelThickness = bevelThickness;
  16. options.bevelSize = options.bevelSize !== undefined ? options.bevelSize : bevelThickness - 0.1;
  17. options.bevelOffset = options.bevelOffset !== undefined ? options.bevelOffset : 0;
  18. options.bevelSegments = options.bevelSegments !== undefined ? options.bevelSegments : 3;
  19. // curveSegments
  20. const curveSegmentsRow = new UIRow();
  21. const curveSegments = new UIInteger( options.curveSegments ).onChange( update ).setRange( 1, Infinity );
  22. curveSegmentsRow.add( new UIText( strings.getKey( 'sidebar/geometry/extrude_geometry/curveSegments' ) ).setClass( 'Label' ) );
  23. curveSegmentsRow.add( curveSegments );
  24. container.add( curveSegmentsRow );
  25. // steps
  26. const stepsRow = new UIRow();
  27. const steps = new UIInteger( options.steps ).onChange( update ).setRange( 1, Infinity );
  28. stepsRow.add( new UIText( strings.getKey( 'sidebar/geometry/extrude_geometry/steps' ) ).setClass( 'Label' ) );
  29. stepsRow.add( steps );
  30. container.add( stepsRow );
  31. // depth
  32. const depthRow = new UIRow();
  33. const depth = new UINumber( options.depth ).onChange( update ).setRange( 1, Infinity );
  34. depthRow.add( new UIText( strings.getKey( 'sidebar/geometry/extrude_geometry/depth' ) ).setClass( 'Label' ) );
  35. depthRow.add( depth );
  36. container.add( depthRow );
  37. // enabled
  38. const enabledRow = new UIRow();
  39. const enabled = new UICheckbox( options.bevelEnabled ).onChange( update );
  40. enabledRow.add( new UIText( strings.getKey( 'sidebar/geometry/extrude_geometry/bevelEnabled' ) ).setClass( 'Label' ) );
  41. enabledRow.add( enabled );
  42. container.add( enabledRow );
  43. // thickness
  44. const thicknessRow = new UIRow();
  45. const thickness = new UINumber( options.bevelThickness ).onChange( update );
  46. thicknessRow.add( new UIText( strings.getKey( 'sidebar/geometry/extrude_geometry/bevelThickness' ) ).setClass( 'Label' ) );
  47. thicknessRow.add( thickness );
  48. container.add( thicknessRow );
  49. // size
  50. const sizeRow = new UIRow();
  51. const size = new UINumber( options.bevelSize ).onChange( update );
  52. sizeRow.add( new UIText( strings.getKey( 'sidebar/geometry/extrude_geometry/bevelSize' ) ).setClass( 'Label' ) );
  53. sizeRow.add( size );
  54. container.add( sizeRow );
  55. // offset
  56. const offsetRow = new UIRow();
  57. const offset = new UINumber( options.bevelOffset ).onChange( update );
  58. offsetRow.add( new UIText( strings.getKey( 'sidebar/geometry/extrude_geometry/bevelOffset' ) ).setClass( 'Label' ) );
  59. offsetRow.add( offset );
  60. container.add( offsetRow );
  61. // segments
  62. const segmentsRow = new UIRow();
  63. const segments = new UIInteger( options.bevelSegments ).onChange( update ).setRange( 0, Infinity );
  64. segmentsRow.add( new UIText( strings.getKey( 'sidebar/geometry/extrude_geometry/bevelSegments' ) ).setClass( 'Label' ) );
  65. segmentsRow.add( segments );
  66. container.add( segmentsRow );
  67. updateBevelRow( options.bevelEnabled );
  68. const button = new UIButton( strings.getKey( 'sidebar/geometry/extrude_geometry/shape' ) ).onClick( toShape ).setClass( 'Label' ).setMarginLeft( '120px' );
  69. container.add( button );
  70. //
  71. function updateBevelRow( enabled ) {
  72. if ( enabled === true ) {
  73. thicknessRow.setDisplay( '' );
  74. sizeRow.setDisplay( '' );
  75. offsetRow.setDisplay( '' );
  76. segmentsRow.setDisplay( '' );
  77. } else {
  78. thicknessRow.setDisplay( 'none' );
  79. sizeRow.setDisplay( 'none' );
  80. offsetRow.setDisplay( 'none' );
  81. segmentsRow.setDisplay( 'none' );
  82. }
  83. }
  84. function refreshUI() {
  85. const options = object.geometry.parameters.options;
  86. curveSegments.setValue( options.curveSegments );
  87. steps.setValue( options.steps );
  88. depth.setValue( options.depth );
  89. enabled.setValue( options.bevelEnabled );
  90. thickness.setValue( options.bevelThickness );
  91. size.setValue( options.bevelSize );
  92. offset.setValue( options.bevelOffset );
  93. segments.setValue( options.bevelSegments );
  94. updateBevelRow( options.bevelEnabled );
  95. }
  96. signals.geometryChanged.add( function ( mesh ) {
  97. if ( mesh === object ) {
  98. refreshUI();
  99. }
  100. } );
  101. //
  102. function update() {
  103. updateBevelRow( enabled.getValue() );
  104. editor.execute( new SetGeometryCommand( editor, object, new THREE.ExtrudeGeometry(
  105. parameters.shapes,
  106. {
  107. curveSegments: curveSegments.getValue(),
  108. steps: steps.getValue(),
  109. depth: depth.getValue(),
  110. bevelEnabled: enabled.getValue(),
  111. bevelThickness: thickness !== undefined ? thickness.getValue() : options.bevelThickness,
  112. bevelSize: size !== undefined ? size.getValue() : options.bevelSize,
  113. bevelOffset: offset !== undefined ? offset.getValue() : options.bevelOffset,
  114. bevelSegments: segments !== undefined ? segments.getValue() : options.bevelSegments
  115. }
  116. ) ) );
  117. }
  118. function toShape() {
  119. editor.execute( new SetGeometryCommand( editor, object, new THREE.ShapeGeometry(
  120. parameters.shapes,
  121. options.curveSegments
  122. ) ) );
  123. }
  124. return container;
  125. }
  126. export { GeometryParametersPanel };