123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- /*
- * SolidToMeshTool.js
- *
- * @author realor
- */
- import { Tool } from './Tool.js'
- import { Selection } from '../utils/Selection.js'
- import { ObjectUtils } from '../utils/ObjectUtils.js'
- import { Solid } from '../core/Solid.js'
- import * as THREE from '../lib/three.module.js'
- class SolidToMeshTool extends Tool {
- constructor(application, options) {
- super(application)
- this.name = 'solid_to_mesh'
- this.label = 'tool.solid_to_mesh.label'
- this.help = 'tool.solid_to_mesh.help'
- this.className = 'solid_to_mesh'
- this.setOptions(options)
- this.immediate = true
- }
- execute() {
- const application = this.application
- const roots = application.selection.roots
- const newRoots = this.convert(roots)
- application.selection.set(...newRoots)
- }
- convert(roots) {
- const replacements = new Map()
- const application = this.application
- const parents = new Selection()
- for (let root of roots) {
- parents.add(root.parent)
- this.traverse(root, replacements)
- }
- application.baseObject.traverse(object => {
- if (object.builder) {
- let updated = object.builder.updateReferences(object, ref => replacements.get(ref) || null)
- if (updated) {
- object.needsRebuild = true
- }
- }
- })
- const changed = parents.roots
- application.notifyObjectsChanged(changed, this, 'structureChanged')
- return roots.map(object => replacements.get(object) || object)
- }
- traverse(object, replacements) {
- if (object instanceof Solid) {
- let solid = object
- let mesh = this.solidToMesh(solid)
- replacements.set(solid, mesh)
- } else {
- let children = object.children
- for (let child of children) {
- this.traverse(child, replacements)
- }
- }
- }
- solidToMesh(solid) {
- const parent = solid.parent
- const parentIndex = parent.children.indexOf(solid)
- let mesh = new THREE.Mesh()
- mesh.geometry.copy(solid.geometry)
- parent.children[parentIndex] = mesh
- mesh.name = solid.name
- mesh.userData = solid.userData
- mesh.visible = solid.visible && solid.facesVisible
- mesh.parent = parent
- solid.matrix.decompose(mesh.position, mesh.rotation, mesh.scale)
- mesh.updateMatrix()
- if (solid.material) mesh.material = solid.material
- solid.parent = null
- ObjectUtils.dispose(solid)
- return mesh
- }
- }
- export { SolidToMeshTool }
|