update create lines

This commit is contained in:
casperlamboo 2016-05-06 19:45:03 +02:00
parent 626867e554
commit 4237195b5e

View File

@ -1,10 +1,7 @@
import THREE from 'three.js'; import THREE from 'three.js';
function addLine(geometry, lineLookup, lines, a, b) { function addLine(geometry, lineLookup, lines, a, b) {
let index = lineLookup[`${b}_${a}`]; const index = lines.length;
if (index === undefined) {
index = lines.length;
lineLookup[`${a}_${b}`] = index; lineLookup[`${a}_${b}`] = index;
lines.push({ lines.push({
@ -12,7 +9,6 @@ function addLine(geometry, lineLookup, lines, a, b) {
connects: [], connects: [],
normals: [] normals: []
}); });
}
return index; return index;
} }
@ -28,20 +24,24 @@ export default function createLines(geometry, settings) {
if (face.normal.y !== 1 && face.normal.y !== -1) { if (face.normal.y !== 1 && face.normal.y !== -1) {
const normal = new THREE.Vector2(face.normal.z, face.normal.x).normalize(); const normal = new THREE.Vector2(face.normal.z, face.normal.x).normalize();
// check for only adding unique lines const lookupA = lineLookup[`${face.b}_${face.a}`];
const lookupB = lineLookup[`${face.c}_${face.b}`];
const lookupC = lineLookup[`${face.a}_${face.c}`];
// only add unique lines
// returns index of said line // returns index of said line
const a = addLine(geometry, lineLookup, lines, face.a, face.b); const indexA = lookupA !== undefined ? lookupA : addLine(geometry, lineLookup, lines, face.a, face.b);
const b = addLine(geometry, lineLookup, lines, face.b, face.c); const indexB = lookupB !== undefined ? lookupB : addLine(geometry, lineLookup, lines, face.b, face.c);
const c = addLine(geometry, lineLookup, lines, face.c, face.a); const indexC = lookupC !== undefined ? lookupC : addLine(geometry, lineLookup, lines, face.c, face.a);
// set connecting lines (based on face) // set connecting lines (based on face)
lines[a].connects.push(b, c); lines[indexA].connects.push(indexB, indexC);
lines[b].connects.push(c, a); lines[indexB].connects.push(indexC, indexA);
lines[c].connects.push(a, b); lines[indexC].connects.push(indexA, indexB);
lines[a].normals.push(normal); lines[indexA].normals.push(normal);
lines[b].normals.push(normal); lines[indexB].normals.push(normal);
lines[c].normals.push(normal); lines[indexC].normals.push(normal);
} }
} }