Sidebar.Geometry.ExtrudeGeometry.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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( refreshUI );
  97. //
  98. function update() {
  99. updateBevelRow( enabled.getValue() );
  100. editor.execute( new SetGeometryCommand( editor, object, new THREE.ExtrudeGeometry(
  101. parameters.shapes,
  102. {
  103. curveSegments: curveSegments.getValue(),
  104. steps: steps.getValue(),
  105. depth: depth.getValue(),
  106. bevelEnabled: enabled.getValue(),
  107. bevelThickness: thickness !== undefined ? thickness.getValue() : options.bevelThickness,
  108. bevelSize: size !== undefined ? size.getValue() : options.bevelSize,
  109. bevelOffset: offset !== undefined ? offset.getValue() : options.bevelOffset,
  110. bevelSegments: segments !== undefined ? segments.getValue() : options.bevelSegments
  111. }
  112. ) ) );
  113. }
  114. function toShape() {
  115. editor.execute( new SetGeometryCommand( editor, object, new THREE.ShapeGeometry(
  116. parameters.shapes,
  117. options.curveSegments
  118. ) ) );
  119. }
  120. return container;
  121. }
  122. export { GeometryParametersPanel };