SmoothEdgesTool.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * SmoothEdgesTool.js
  3. *
  4. * @author realor
  5. */
  6. import { Tool } from './Tool.js'
  7. import { Selection } from '../utils/Selection.js'
  8. import { ObjectUtils } from '../utils/ObjectUtils.js'
  9. import { Solid } from '../core/Solid.js'
  10. import { Controls } from '../ui/Controls.js'
  11. import * as THREE from '../lib/three.module.js'
  12. class SmoothEdgesTool extends Tool {
  13. constructor(application, options) {
  14. super(application)
  15. this.name = 'smooth_edges'
  16. this.label = 'tool.smooth_edges.label'
  17. this.className = 'smooth_edges'
  18. this.setOptions(options)
  19. this.createPanel()
  20. }
  21. createPanel() {
  22. const application = this.application
  23. this.panel = application.createPanel(this.label, 'left', 'panel_smooth_edges')
  24. this.panel.preferredHeight = 100
  25. this.smoothAngleElem = Controls.addNumberField(this.panel.bodyElem, 'smooth_angle', 'label.smooth_angle', 20, 'row')
  26. this.smoothAngleElem.style.width = '50px'
  27. this.smoothAngleElem.min = 0
  28. this.smoothAngleElem.max = 180
  29. this.applyButton = Controls.addButton(this.panel.bodyElem, 'apply_smooth', 'button.apply', () => this.applySmooth())
  30. }
  31. activate() {
  32. this.panel.visible = true
  33. }
  34. deactivate() {
  35. this.panel.visible = false
  36. }
  37. applySmooth() {
  38. let smoothAngle = parseFloat(this.smoothAngleElem.value)
  39. if (smoothAngle < 0) smoothAngle = 0
  40. else if (smoothAngle > 180) smoothAngle = 180
  41. const changed = []
  42. let roots = this.application.selection.roots
  43. for (let root of roots) {
  44. this.traverse(root, smoothAngle, changed)
  45. }
  46. this.application.notifyObjectsChanged(changed, this)
  47. }
  48. traverse(object, smoothAngle, changed) {
  49. if (object instanceof Solid) {
  50. let objectChanged = false
  51. if (object.geometry.smoothAngle !== smoothAngle) {
  52. object.geometry.smoothAngle = smoothAngle
  53. object.geometry.updateBuffers()
  54. objectChanged = true
  55. }
  56. if (object.builder && object.builder.smoothAngle !== undefined) {
  57. if (object.builder.smoothAngle !== smoothAngle) {
  58. object.builder.smoothAngle = smoothAngle
  59. objectChanged = true
  60. }
  61. }
  62. if (objectChanged) changed.push(object)
  63. } else {
  64. let children = object.children
  65. for (let child of children) {
  66. this.traverse(child, smoothAngle, changed)
  67. }
  68. }
  69. }
  70. }
  71. export { SmoothEdgesTool }