fix exporting

This commit is contained in:
casperlamboo 2017-11-22 16:40:41 +01:00
parent 5d95fc63a9
commit cdb4730a7d
6 changed files with 62 additions and 45 deletions

View File

@ -73,7 +73,6 @@ store.dispatch(actions.addObject({
transform: new CAL.Matrix({ x: -10, y: -10 }) transform: new CAL.Matrix({ x: -10, y: -10 })
})); }));
// render dom // render dom
import React from 'react'; import React from 'react';
import { Provider } from 'react-redux'; import { Provider } from 'react-redux';

23
package-lock.json generated
View File

@ -36,19 +36,18 @@
"integrity": "sha512-w1+sG3ClsSaQwo3ks5wl6QLe4aWEHBe8QePq0IeAcj+lypqo770sUcWVfEZWUFBumAhSlCJg3GRc8MsycHy8LA==" "integrity": "sha512-w1+sG3ClsSaQwo3ks5wl6QLe4aWEHBe8QePq0IeAcj+lypqo770sUcWVfEZWUFBumAhSlCJg3GRc8MsycHy8LA=="
}, },
"@doodle3d/threejs-export-obj": { "@doodle3d/threejs-export-obj": {
"version": "0.0.4", "version": "0.0.5",
"resolved": "https://registry.npmjs.org/@doodle3d/threejs-export-obj/-/threejs-export-obj-0.0.4.tgz", "resolved": "https://registry.npmjs.org/@doodle3d/threejs-export-obj/-/threejs-export-obj-0.0.5.tgz",
"integrity": "sha512-7wF302lO77y7bt/pdPPoTS7wAW8TNMavW7ps60LqOCa/KmNDe0hYvgNXwsn61kGW7aKorlx22Y5stCjHQs5GRA==", "integrity": "sha512-kU3xpA77DjRQz27UBX3/PsdX6GecEJ274msqvzSrRiYZXQSs7HFIFsS0a4lcyzJd2Q8SkzMIOHvOA9h85dxWTQ==",
"requires": { "requires": {
"babel-preset-env": "1.6.1", "jszip": "3.1.5",
"jszip": "3.1.4", "three": "0.88.0"
"three": "0.87.1"
}, },
"dependencies": { "dependencies": {
"three": { "three": {
"version": "0.87.1", "version": "0.88.0",
"resolved": "https://registry.npmjs.org/three/-/three-0.87.1.tgz", "resolved": "https://registry.npmjs.org/three/-/three-0.88.0.tgz",
"integrity": "sha1-Rmo07cRUNFnO2bnX0na2Uhb+K6g=" "integrity": "sha1-QlbC/Djk+yOg0j66K2zOTfjkZtU="
} }
} }
}, },
@ -5167,9 +5166,9 @@
} }
}, },
"jszip": { "jszip": {
"version": "3.1.4", "version": "3.1.5",
"resolved": "https://registry.npmjs.org/jszip/-/jszip-3.1.4.tgz", "resolved": "https://registry.npmjs.org/jszip/-/jszip-3.1.5.tgz",
"integrity": "sha512-z6w8iYIxZ/fcgul0j/OerkYnkomH8BZigvzbxVmr2h5HkZUrPtk2kjYtLkqR9wwQxEP6ecKNoKLsbhd18jfnGA==", "integrity": "sha512-5W8NUaFRFRqTOL7ZDDrx5qWHJyBXy6velVudIzQUSoqAAYqzSh2Z7/m0Rf1QbmQJccegD0r+YZxBjzqoBiEeJQ==",
"requires": { "requires": {
"core-js": "2.3.0", "core-js": "2.3.0",
"es6-promise": "3.0.2", "es6-promise": "3.0.2",

View File

@ -17,7 +17,7 @@
"@doodle3d/clipper-js": "^1.0.7", "@doodle3d/clipper-js": "^1.0.7",
"@doodle3d/fill-path": "^1.0.7", "@doodle3d/fill-path": "^1.0.7",
"@doodle3d/potrace-js": "0.0.6", "@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/threejs-export-stl": "0.0.3",
"@doodle3d/touch-events": "0.0.7", "@doodle3d/touch-events": "0.0.7",
"babel-polyfill": "^6.26.0", "babel-polyfill": "^6.26.0",

View File

@ -16,7 +16,7 @@ export default class ShapesManager extends THREE.Object3D {
this._spaces = {}; this._spaces = {};
this.name = 'shapes-manager'; this.name = 'shapes-manager';
this._holes = new THREE_BSP(new THREE.Geometry()); this._holes = null;
// this._edges = {}; // this._edges = {};
} }

View File

@ -81,6 +81,14 @@ export const determineActiveShape2d = (state) => {
}; };
export const determineActiveShape3d = (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 || const activeTransformer = state.d2.eraser.active ||
(state.d2.transform.active && state.d2.transform.handle !== 'dragselect') || (state.d2.transform.active && state.d2.transform.handle !== 'dragselect') ||
state.d3.height.active || state.d3.height.active ||

View File

@ -4,7 +4,7 @@ import * as exportOBJ from '@doodle3d/threejs-export-obj';
import * as THREE from 'three'; import * as THREE from 'three';
import ThreeBSP from 'three-js-csg'; import ThreeBSP from 'three-js-csg';
import ClipperShape from '@doodle3d/clipper-js'; 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 { applyMatrixOnShape, pathToVectorPath } from '../utils/vectorUtils.js';
import { shapeToPoints } from '../shape/shapeToPoints.js'; import { shapeToPoints } from '../shape/shapeToPoints.js';
import { SHAPE_TYPE_PROPERTIES } from '../constants/shapeTypeProperties.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 ROTATION_MATRIX = new THREE.Matrix4().makeRotationX(Math.PI / 2);
const SCALE = 10.0; const SCALE = 10.0;
function createExportGeometry(shapeData, offsetSingleWalls, lineWidth) { function createExportShapeData(shapeData, offsetSingleWalls, lineWidth) {
let shapes = shapeToPoints(shapeData).map(({ points, holes }) => { let shapes = shapeToPoints(shapeData).map(({ points, holes }) => {
const shape = applyMatrixOnShape([points, ...holes], shapeData.transform); const shape = applyMatrixOnShape([points, ...holes], shapeData.transform);
return new ClipperShape(shape, shapeData.fill, true, false); return new ClipperShape(shape, shapeData.fill, true, false);
@ -53,57 +53,70 @@ function createExportGeometry(shapeData, offsetSingleWalls, lineWidth) {
})) }))
.map(([points, ...holes]) => ({ points, holes })); .map(([points, ...holes]) => ({ points, holes }));
const objectMesh = new ShapeMesh({ return {
...shapeData, ...shapeData,
transform: new Matrix(), transform: new Matrix(),
type: 'EXPORT_SHAPE', type: 'EXPORT_SHAPE',
fill, fill,
shapes shapes
}); };
return objectMesh;
} }
export function generateExportMesh(state, options = {}) { export function generateExportMesh(state, options = {}) {
const { const {
unionGeometry = false, unionGeometry = false,
exportLineWidth = LINE_WIDTH, lineWidth = LINE_WIDTH,
offsetSingleWalls = true, offsetSingleWalls = true,
matrix = ROTATION_MATRIX matrix = ROTATION_MATRIX
} = options; } = options;
const materials = []; const exportState = {
let exportGeometry; spaces: state.spaces,
const objectMatrix = new THREE.Matrix4(); objectsById: {}
};
for (const id in state.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); const materials = [];
let objectGeometry = new THREE.Geometry().fromBufferGeometry(geometry); const objectMatrix = new THREE.Matrix4();
objectGeometry.mergeVertices(); let exportGeometry;
objectGeometry.applyMatrix(objectMatrix.multiplyMatrices(state.spaces[shapeData.space].matrix, matrix)); 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(); console.log('mesh: ', mesh);
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); 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) { if (unionGeometry) {
exportGeometry = exportGeometry.union(objectGeometry); if (!exportGeometry) {
exportGeometry = objectGeometry;
} else {
exportGeometry = exportGeometry.union(objectGeometry);
}
} else { } else {
if (!exportGeometry) exportGeometry = new THREE.Geometry();
exportGeometry.merge(objectGeometry, undefined, materialIndex); exportGeometry.merge(objectGeometry, undefined, materialIndex);
} }
} else {
exportGeometry = objectGeometry;
} }
} });
if (unionGeometry) { if (unionGeometry) {
return exportGeometry.toMesh(materials); return exportGeometry.toMesh(materials);
@ -115,8 +128,6 @@ export function generateExportMesh(state, options = {}) {
export async function createFile(state, type, options) { export async function createFile(state, type, options) {
const exportMesh = generateExportMesh(state, options); const exportMesh = generateExportMesh(state, options);
console.log('exportMesh: ', exportMesh);
switch (type) { switch (type) {
case 'json-string': { case 'json-string': {
const object = exportMesh.geometry.toJSON().data; const object = exportMesh.geometry.toJSON().data;