Cloner.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * Cloner.js
  3. *
  4. * @author realor
  5. */
  6. import { Solid } from '../core/Solid.js'
  7. import { Profile } from '../core/Profile.js'
  8. import { Cord } from '../core/Cord.js'
  9. import { ObjectBuilder } from './ObjectBuilder.js'
  10. import * as THREE from '../lib/three.module.js'
  11. class Cloner extends ObjectBuilder {
  12. constructor(objectToClone) {
  13. super()
  14. this.objectToClone = objectToClone
  15. this.cloneVisible = true
  16. }
  17. traverseDependencies(object, action) {
  18. if (this.objectToClone) {
  19. action(this.objectToClone)
  20. }
  21. }
  22. updateReferences(object, update) {
  23. let newObjectToClone = update(this.objectToClone)
  24. if (newObjectToClone !== null) {
  25. this.objectToClone = newObjectToClone
  26. return true
  27. }
  28. return false
  29. }
  30. performBuild(object) {
  31. if (this.objectToClone === undefined) return false
  32. const clone = object => {
  33. let clonedObject = object.clone(false)
  34. if (this.cloneVisible) clonedObject.visible = true
  35. if (object instanceof Solid || object instanceof Profile || object instanceof Cord) {
  36. // do not clone children
  37. } // object is Group or Object3D
  38. else {
  39. for (let child of object.children) {
  40. clonedObject.add(clone(child))
  41. }
  42. }
  43. return clonedObject
  44. }
  45. object.clear()
  46. object.add(clone(this.objectToClone))
  47. object.updateMatrix()
  48. return true
  49. }
  50. isGeometryBuilder(object) {
  51. return false
  52. }
  53. isChildrenBuilder(object) {
  54. return true
  55. }
  56. copy(source) {
  57. this.objectToClone = source.objectToClone
  58. this.cloneVisible = source.cloneVisible
  59. return this
  60. }
  61. }
  62. ObjectBuilder.addClass(Cloner)
  63. export { Cloner }