getGeometryVolume.js
888 Bytes
import { Vector3 } from 'three';
const signedVolumeOfTriangle = (p1, p2, p3) => {
return p1.dot(p2.cross(p3)) / 6.0;
};
export const getGeometryVolume = (geometry) => {
const { position } = geometry.attributes;
let sum = 0;
const p1 = new Vector3();
const p2 = new Vector3();
const p3 = new Vector3();
const indices = geometry.getIndex();
let a = 0;
let b = 0;
let c = 0;
for (let i = 0; i < indices.count / 3; i += 1) {
a = indices.array[i * 3 + 0];
b = indices.array[i * 3 + 1];
c = indices.array[i * 3 + 2];
p1.set(position.getX(a), position.getY(a), position.getZ(a));
p2.set(position.getX(b), position.getY(b), position.getZ(b));
p3.set(position.getX(c), position.getY(c), position.getZ(c));
sum += signedVolumeOfTriangle(p1, p2, p3);
}
return Math.round(sum, 2);
};