add merging different colours

This commit is contained in:
casperlamboo 2017-11-17 20:47:59 +01:00
parent f3652b19bc
commit cf6a607bee

View File

@ -66,52 +66,56 @@ function createExportGeometry(shapeData, offsetSingleWalls, lineWidth) {
export function generateExportMesh(state, options = {}) { export function generateExportMesh(state, options = {}) {
const { const {
experimentalColorUnionExport = false, unionGeometry = false,
exportLineWidth = LINE_WIDTH, exportLineWidth = LINE_WIDTH,
offsetSingleWalls = true, offsetSingleWalls = true,
matrix = ROTATION_MATRIX matrix = ROTATION_MATRIX
} = options; } = options;
const geometries = [];
const materials = []; const materials = [];
let exportGeometry;
const objectMatrix = new THREE.Matrix4();
for (const id in state.objectsById) { for (const id in state.objectsById) {
const shapeData = state.objectsById[id]; const shapeData = state.objectsById[id];
if (!SHAPE_TYPE_PROPERTIES[shapeData.type].D3Visible) continue; if (!SHAPE_TYPE_PROPERTIES[shapeData.type].D3Visible) continue;
const { geometry, material } = createExportGeometry(shapeData, offsetSingleWalls, exportLineWidth); const { geometry, material } = createExportGeometry(shapeData, offsetSingleWalls || unionGeometry, exportLineWidth);
let objectGeometry = new THREE.Geometry().fromBufferGeometry(geometry); let objectGeometry = new THREE.Geometry().fromBufferGeometry(geometry);
objectGeometry.mergeVertices(); objectGeometry.mergeVertices();
objectGeometry.applyMatrix(state.spaces[shapeData.space].matrix); objectGeometry.applyMatrix(objectMatrix.multiplyMatrices(state.spaces[shapeData.space].matrix, matrix));
if (experimentalColorUnionExport) objectGeometry = new THREE_BSP(objectGeometry);
const colorHex = material.color.getHex(); const colorHex = material.color.getHex();
const index = materials.findIndex(exportMaterial => exportMaterial.color.getHex() === colorHex); let materialIndex = materials.findIndex(exportMaterial => exportMaterial.color.getHex() === colorHex);
if (index !== -1) { if (materialIndex === -1) {
if (experimentalColorUnionExport) { materialIndex = materials.length;
geometries[index] = geometries[index].union(objectGeometry);
} else {
geometries[index].merge(objectGeometry);
}
} else {
geometries.push(objectGeometry);
materials.push(material); materials.push(material);
} }
if (unionGeometry) objectGeometry = new THREE_BSP(objectGeometry, materials.length);
if (exportGeometry) {
if (unionGeometry) {
exportGeometry = exportGeometry.union(objectGeometry);
} else {
exportGeometry = exportGeometry.merge(objectGeometry, undefined, materials.length);
}
} else {
exportGeometry = objectGeometry;
}
} }
const exportGeometry = geometries.reduce((combinedGeometry, geometry, materialIndex) => { if (unionGeometry) {
if (experimentalColorUnionExport) geometry = geometry.toMesh().geometry; return exportGeometry.toMesh(materials);
combinedGeometry.merge(geometry, matrix, materialIndex); } else {
return combinedGeometry; return new THREE.Mesh(exportGeometry, materials);
}, new THREE.Geometry()); }
const exportMaterial = new THREE.MultiMaterial(materials);
return new THREE.Mesh(exportGeometry, exportMaterial);
} }
export async function createFile(objectsById, type, options) { export async function createFile(state, type, options) {
const exportMesh = generateExportMesh(objectsById, options); const exportMesh = generateExportMesh(state, options);
console.log('exportMesh: ', exportMesh);
switch (type) { switch (type) {
case 'json-string': { case 'json-string': {
@ -150,3 +154,4 @@ export async function createFile(objectsById, type, options) {
throw new Error(`did not regonize type ${type}`); throw new Error(`did not regonize type ${type}`);
} }
} }
window.createFile = createFile;