diff --git a/index.js b/index.js index e6fd242..e51158f 100644 --- a/index.js +++ b/index.js @@ -73,7 +73,6 @@ store.dispatch(actions.addObject({ transform: new CAL.Matrix({ x: -10, y: -10 }) })); - // render dom import React from 'react'; import { Provider } from 'react-redux'; diff --git a/package-lock.json b/package-lock.json index d31e29e..57b954b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -36,19 +36,18 @@ "integrity": "sha512-w1+sG3ClsSaQwo3ks5wl6QLe4aWEHBe8QePq0IeAcj+lypqo770sUcWVfEZWUFBumAhSlCJg3GRc8MsycHy8LA==" }, "@doodle3d/threejs-export-obj": { - "version": "0.0.4", - "resolved": "https://registry.npmjs.org/@doodle3d/threejs-export-obj/-/threejs-export-obj-0.0.4.tgz", - "integrity": "sha512-7wF302lO77y7bt/pdPPoTS7wAW8TNMavW7ps60LqOCa/KmNDe0hYvgNXwsn61kGW7aKorlx22Y5stCjHQs5GRA==", + "version": "0.0.5", + "resolved": "https://registry.npmjs.org/@doodle3d/threejs-export-obj/-/threejs-export-obj-0.0.5.tgz", + "integrity": "sha512-kU3xpA77DjRQz27UBX3/PsdX6GecEJ274msqvzSrRiYZXQSs7HFIFsS0a4lcyzJd2Q8SkzMIOHvOA9h85dxWTQ==", "requires": { - "babel-preset-env": "1.6.1", - "jszip": "3.1.4", - "three": "0.87.1" + "jszip": "3.1.5", + "three": "0.88.0" }, "dependencies": { "three": { - "version": "0.87.1", - "resolved": "https://registry.npmjs.org/three/-/three-0.87.1.tgz", - "integrity": "sha1-Rmo07cRUNFnO2bnX0na2Uhb+K6g=" + "version": "0.88.0", + "resolved": "https://registry.npmjs.org/three/-/three-0.88.0.tgz", + "integrity": "sha1-QlbC/Djk+yOg0j66K2zOTfjkZtU=" } } }, @@ -5167,9 +5166,9 @@ } }, "jszip": { - "version": "3.1.4", - "resolved": "https://registry.npmjs.org/jszip/-/jszip-3.1.4.tgz", - "integrity": "sha512-z6w8iYIxZ/fcgul0j/OerkYnkomH8BZigvzbxVmr2h5HkZUrPtk2kjYtLkqR9wwQxEP6ecKNoKLsbhd18jfnGA==", + "version": "3.1.5", + "resolved": "https://registry.npmjs.org/jszip/-/jszip-3.1.5.tgz", + "integrity": "sha512-5W8NUaFRFRqTOL7ZDDrx5qWHJyBXy6velVudIzQUSoqAAYqzSh2Z7/m0Rf1QbmQJccegD0r+YZxBjzqoBiEeJQ==", "requires": { "core-js": "2.3.0", "es6-promise": "3.0.2", diff --git a/package.json b/package.json index 3bec2e4..cab9402 100755 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "@doodle3d/clipper-js": "^1.0.7", "@doodle3d/fill-path": "^1.0.7", "@doodle3d/potrace-js": "0.0.6", - "@doodle3d/threejs-export-obj": "0.0.4", + "@doodle3d/threejs-export-obj": "0.0.5", "@doodle3d/threejs-export-stl": "0.0.3", "@doodle3d/touch-events": "0.0.7", "babel-polyfill": "^6.26.0", diff --git a/src/d3/ShapesManager.js b/src/d3/ShapesManager.js index cdd5e0a..7427c5b 100644 --- a/src/d3/ShapesManager.js +++ b/src/d3/ShapesManager.js @@ -16,7 +16,7 @@ export default class ShapesManager extends THREE.Object3D { this._spaces = {}; this.name = 'shapes-manager'; - this._holes = new THREE_BSP(new THREE.Geometry()); + this._holes = null; // this._edges = {}; } diff --git a/src/shape/shapeDataUtils.js b/src/shape/shapeDataUtils.js index 0364260..a7e6274 100644 --- a/src/shape/shapeDataUtils.js +++ b/src/shape/shapeDataUtils.js @@ -81,6 +81,14 @@ export const determineActiveShape2d = (state) => { }; export const determineActiveShape3d = (state) => { + if (!state.d2 || !state.d3) { + const activeShapes = {}; + for (const id in state.objectsById) { + activeShapes[id] = false; + } + return activeShapes; + } + const activeTransformer = state.d2.eraser.active || (state.d2.transform.active && state.d2.transform.handle !== 'dragselect') || state.d3.height.active || diff --git a/src/utils/exportUtils.js b/src/utils/exportUtils.js index a12737c..cbdeb6e 100644 --- a/src/utils/exportUtils.js +++ b/src/utils/exportUtils.js @@ -4,7 +4,7 @@ import * as exportOBJ from '@doodle3d/threejs-export-obj'; import * as THREE from 'three'; import ThreeBSP from 'three-js-csg'; import ClipperShape from '@doodle3d/clipper-js'; -import ShapeMesh from '../d3/ShapeMesh.js'; +import ShapesManager from '../d3/ShapesManager.js'; import { applyMatrixOnShape, pathToVectorPath } from '../utils/vectorUtils.js'; import { shapeToPoints } from '../shape/shapeToPoints.js'; import { SHAPE_TYPE_PROPERTIES } from '../constants/shapeTypeProperties.js'; @@ -17,7 +17,7 @@ const THREE_BSP = ThreeBSP(THREE); const ROTATION_MATRIX = new THREE.Matrix4().makeRotationX(Math.PI / 2); const SCALE = 10.0; -function createExportGeometry(shapeData, offsetSingleWalls, lineWidth) { +function createExportShapeData(shapeData, offsetSingleWalls, lineWidth) { let shapes = shapeToPoints(shapeData).map(({ points, holes }) => { const shape = applyMatrixOnShape([points, ...holes], shapeData.transform); return new ClipperShape(shape, shapeData.fill, true, false); @@ -53,57 +53,70 @@ function createExportGeometry(shapeData, offsetSingleWalls, lineWidth) { })) .map(([points, ...holes]) => ({ points, holes })); - const objectMesh = new ShapeMesh({ + return { ...shapeData, transform: new Matrix(), type: 'EXPORT_SHAPE', fill, shapes - }); - - return objectMesh; + }; } export function generateExportMesh(state, options = {}) { const { unionGeometry = false, - exportLineWidth = LINE_WIDTH, + lineWidth = LINE_WIDTH, offsetSingleWalls = true, matrix = ROTATION_MATRIX } = options; - const materials = []; - let exportGeometry; - const objectMatrix = new THREE.Matrix4(); + const exportState = { + spaces: state.spaces, + objectsById: {} + }; + for (const id in state.objectsById) { - const shapeData = state.objectsById[id]; + exportState.objectsById[id] = createExportShapeData(state.objectsById[id], offsetSingleWalls || unionGeometry, lineWidth); + } - if (!SHAPE_TYPE_PROPERTIES[shapeData.type].D3Visible) continue; + const shapesManager = new ShapesManager({ toonShader: false }); + shapesManager.update(exportState); - const { geometry, material } = createExportGeometry(shapeData, offsetSingleWalls || unionGeometry, exportLineWidth); - let objectGeometry = new THREE.Geometry().fromBufferGeometry(geometry); - objectGeometry.mergeVertices(); - objectGeometry.applyMatrix(objectMatrix.multiplyMatrices(state.spaces[shapeData.space].matrix, matrix)); + const materials = []; + const objectMatrix = new THREE.Matrix4(); + let exportGeometry; + shapesManager.traverse(mesh => { + const shapeData = exportState.objectsById[mesh.name]; + if (mesh instanceof THREE.Mesh && shapeData.solid) { + const { geometry, material } = mesh; - const colorHex = material.color.getHex(); - let materialIndex = materials.findIndex(exportMaterial => exportMaterial.color.getHex() === colorHex); - if (materialIndex === -1) { - materialIndex = materials.length; - materials.push(material); - } + console.log('mesh: ', mesh); - if (unionGeometry) objectGeometry = new THREE_BSP(objectGeometry, materialIndex); + let objectGeometry = geometry.clone(); + objectGeometry.mergeVertices(); + objectGeometry.applyMatrix(objectMatrix.multiplyMatrices(state.spaces[shapeData.space].matrix, matrix)); + + const colorHex = material.color.getHex(); + let materialIndex = materials.findIndex(exportMaterial => exportMaterial.color.getHex() === colorHex); + if (materialIndex === -1) { + materialIndex = materials.length; + materials.push(material); + } + + if (unionGeometry) objectGeometry = new THREE_BSP(objectGeometry, materialIndex); - if (exportGeometry) { if (unionGeometry) { - exportGeometry = exportGeometry.union(objectGeometry); + if (!exportGeometry) { + exportGeometry = objectGeometry; + } else { + exportGeometry = exportGeometry.union(objectGeometry); + } } else { + if (!exportGeometry) exportGeometry = new THREE.Geometry(); exportGeometry.merge(objectGeometry, undefined, materialIndex); } - } else { - exportGeometry = objectGeometry; } - } + }); if (unionGeometry) { return exportGeometry.toMesh(materials); @@ -115,8 +128,6 @@ export function generateExportMesh(state, options = {}) { export async function createFile(state, type, options) { const exportMesh = generateExportMesh(state, options); - console.log('exportMesh: ', exportMesh); - switch (type) { case 'json-string': { const object = exportMesh.geometry.toJSON().data;