Clean up code a bit
This commit is contained in:
parent
35bb35415a
commit
1dce74a9ea
53
src/index.js
53
src/index.js
@ -38,17 +38,21 @@ for (let i = 0; i < geometry.faces.length; i += 1) {
|
|||||||
|
|
||||||
const originalVertCount = geometry.vertices.length;
|
const originalVertCount = geometry.vertices.length;
|
||||||
|
|
||||||
|
const calculateCentroid = (pa, pb, pc) => {
|
||||||
|
const vabHalf = pb.clone().sub(pa).divideScalar(2);
|
||||||
|
const pabHalf = pa.clone().add(vabHalf);
|
||||||
|
const centroid = pc.clone().sub(pabHalf).multiplyScalar(1/3).add(pabHalf);
|
||||||
|
return centroid;
|
||||||
|
}
|
||||||
|
|
||||||
const faceCentroids = {};
|
const faceCentroids = {};
|
||||||
for (let i = 0; i < geometry.faces.length; i += 1) {
|
for (let i = 0; i < geometry.faces.length; i += 1) {
|
||||||
const face = geometry.faces[i];
|
const face = geometry.faces[i];
|
||||||
const vertexA = geometry.vertices[face.a];
|
const vertexA = geometry.vertices[face.a];
|
||||||
const vertexB = geometry.vertices[face.b];
|
const vertexB = geometry.vertices[face.b];
|
||||||
const vertexC = geometry.vertices[face.c];
|
const vertexC = geometry.vertices[face.c];
|
||||||
const vabHalf = vertexB.clone().sub(vertexA).divideScalar(2);
|
|
||||||
const pabHalf = vertexA.clone().add(vabHalf);
|
|
||||||
const centroid = vertexC.clone().sub(pabHalf).multiplyScalar(1/3).add(pabHalf);
|
|
||||||
|
|
||||||
faceCentroids[i] = centroid;
|
faceCentroids[i] = calculateCentroid(vertexA, vertexB, vertexC);
|
||||||
}
|
}
|
||||||
|
|
||||||
const findAdjacentFace = (vertexIndex, faces) => {
|
const findAdjacentFace = (vertexIndex, faces) => {
|
||||||
@ -68,8 +72,21 @@ const findCenterPoint = faces => {
|
|||||||
return centerPoint;
|
return centerPoint;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const midCentroidCache = {};
|
||||||
|
const calculateMidCentroid = (vertexIndex, faces, centroid) => {
|
||||||
|
const adjFaceIndex = findAdjacentFace(vertexIndex, faces);
|
||||||
|
const adjCentroid = faceCentroids[adjFaceIndex];
|
||||||
|
const midCentroidKey = vertexIndex + ",F" + adjFaceIndex;
|
||||||
|
let midCentroid = midCentroidCache[midCentroidKey];
|
||||||
|
if (!midCentroid) {
|
||||||
|
midCentroid = centroid.clone().lerp(adjCentroid, 0.5);
|
||||||
|
midCentroidCache[midCentroidKey] = midCentroid;
|
||||||
|
}
|
||||||
|
return midCentroid;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
console.time("dual");
|
console.time("dual");
|
||||||
const midVertCache = {};
|
|
||||||
let hexCount = 0;
|
let hexCount = 0;
|
||||||
let pentCount = 0;
|
let pentCount = 0;
|
||||||
for (let i = 0; i < originalVertCount; i += 1) {
|
for (let i = 0; i < originalVertCount; i += 1) {
|
||||||
@ -79,8 +96,10 @@ for (let i = 0; i < originalVertCount; i += 1) {
|
|||||||
} else if (faces.length === 5) {
|
} else if (faces.length === 5) {
|
||||||
pentCount += 1;
|
pentCount += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
const color = new THREE.Color(Math.random(), Math.random(), Math.random());
|
const color = new THREE.Color(Math.random(), Math.random(), Math.random());
|
||||||
const centerPoint = findCenterPoint(faces);
|
const centerPoint = findCenterPoint(faces);
|
||||||
|
|
||||||
for (let j = 0; j < faces.length; j += 1) {
|
for (let j = 0; j < faces.length; j += 1) {
|
||||||
const faceIndex = faces[j];
|
const faceIndex = faces[j];
|
||||||
const face = geometry.faces[faceIndex];
|
const face = geometry.faces[faceIndex];
|
||||||
@ -88,24 +107,8 @@ for (let i = 0; i < originalVertCount; i += 1) {
|
|||||||
sortedVerts.unshift(i)
|
sortedVerts.unshift(i)
|
||||||
|
|
||||||
const centroid = faceCentroids[faceIndex];
|
const centroid = faceCentroids[faceIndex];
|
||||||
|
const midBCentroid = calculateMidCentroid(sortedVerts[1], faces, centroid);
|
||||||
const adjBFace = findAdjacentFace(sortedVerts[1], faces);
|
const midCCentroid = calculateMidCentroid(sortedVerts[2], faces, centroid);
|
||||||
const adjBCentroid = faceCentroids[adjBFace];
|
|
||||||
const midBCentroidKey = sortedVerts[1] + ",F" + adjBFace;
|
|
||||||
let midBCentroid = midVertCache[midBCentroidKey];
|
|
||||||
if (!midBCentroid) {
|
|
||||||
midBCentroid = centroid.clone().lerp(adjBCentroid, 0.5);
|
|
||||||
midVertCache[midBCentroidKey] = midBCentroid;
|
|
||||||
}
|
|
||||||
|
|
||||||
const adjCFace = findAdjacentFace(sortedVerts[2], faces);
|
|
||||||
const adjCCentroid = faceCentroids[adjCFace];
|
|
||||||
const midCCentroidKey = sortedVerts[2] + ",F" + adjCFace;
|
|
||||||
let midCCentroid = midVertCache[midCCentroidKey];
|
|
||||||
if (!midCCentroid) {
|
|
||||||
midCCentroid = centroid.clone().lerp(adjCCentroid, 0.5);
|
|
||||||
midVertCache[midCCentroidKey] = midCCentroid;
|
|
||||||
}
|
|
||||||
|
|
||||||
newVertices.push(
|
newVertices.push(
|
||||||
centerPoint.x, centerPoint.y, centerPoint.z,
|
centerPoint.x, centerPoint.y, centerPoint.z,
|
||||||
@ -138,8 +141,8 @@ function disposeArray() {
|
|||||||
|
|
||||||
console.time("find geo");
|
console.time("find geo");
|
||||||
const newGeometry = new THREE.BufferGeometry();
|
const newGeometry = new THREE.BufferGeometry();
|
||||||
newGeometry.addAttribute("position", new THREE.Float32BufferAttribute(newVertices, 3));
|
newGeometry.addAttribute("position", new THREE.Float32BufferAttribute(newVertices, 3).onUpload(disposeArray));
|
||||||
newGeometry.addAttribute("color", new THREE.Float32BufferAttribute(colors, 3));
|
newGeometry.addAttribute("color", new THREE.Float32BufferAttribute(colors, 3).onUpload(disposeArray));
|
||||||
newGeometry.computeFaceNormals();
|
newGeometry.computeFaceNormals();
|
||||||
newGeometry.computeVertexNormals();
|
newGeometry.computeVertexNormals();
|
||||||
console.timeEnd("find geo");
|
console.timeEnd("find geo");
|
||||||
|
Loading…
Reference in New Issue
Block a user