MCPcopy Index your code
hub / github.com/Vishal-raj-1/Awesome-JavaScript-Projects / optimizeModel

Function optimizeModel

assets/js/block_ninja.js:667–757  ·  view source on GitHub ↗
(model, threshold=0.0001)

Source from the content-addressed store, hash-verified

665// and removing all polys that share the same vertices.
666// Directly mutates the model.
667function optimizeModel(model, threshold=0.0001) {
668 const { vertices, polys } = model;
669
670 const compareVertices = (v1, v2) => (
671 Math.abs(v1.x - v2.x) < threshold &&
672 Math.abs(v1.y - v2.y) < threshold &&
673 Math.abs(v1.z - v2.z) < threshold
674 );
675
676 const comparePolys = (p1, p2) => {
677 const v1 = p1.vIndexes;
678 const v2 = p2.vIndexes;
679 return (
680 (
681 v1[0] === v2[0] ||
682 v1[0] === v2[1] ||
683 v1[0] === v2[2] ||
684 v1[0] === v2[3]
685 ) && (
686 v1[1] === v2[0] ||
687 v1[1] === v2[1] ||
688 v1[1] === v2[2] ||
689 v1[1] === v2[3]
690 ) && (
691 v1[2] === v2[0] ||
692 v1[2] === v2[1] ||
693 v1[2] === v2[2] ||
694 v1[2] === v2[3]
695 ) && (
696 v1[3] === v2[0] ||
697 v1[3] === v2[1] ||
698 v1[3] === v2[2] ||
699 v1[3] === v2[3]
700 )
701 );
702 };
703
704
705 vertices.forEach((v, i) => {
706 v.originalIndexes = [i];
707 });
708
709 for (let i=vertices.length-1; i>=0; i--) {
710 for (let ii=i-1; ii>=0; ii--) {
711 const v1 = vertices[i];
712 const v2 = vertices[ii];
713 if (compareVertices(v1, v2)) {
714 vertices.splice(i, 1);
715 v2.originalIndexes.push(...v1.originalIndexes);
716 break;
717 }
718 }
719 }
720
721 vertices.forEach((v, i) => {
722 polys.forEach(p => {
723 p.vIndexes.forEach((vi, ii, arr) => {
724 const vo = v.originalIndexes;

Callers 1

getTargetOfStyleFunction · 0.85

Calls 2

compareVerticesFunction · 0.85
comparePolysFunction · 0.85

Tested by

no test coverage detected