Volume of an uploaded stl file returning 0 after calculation

Hi everyone,
I have managed to calculate the voule of the stl file I uploaded. I can get the distance between two points using p1.distanceTo(p2). However, I’m unable to get the volume of the geometry. It always return 0. Here is my

var object        
var loader = new THREE.STLLoader();
    loader.load(box.stl, function(geometry){
    var geo = new THREE.Geometry().fromBufferGeometry(geometry);
    var material = new THREE.MeshStandardMaterial({color: 0xffff, side: THREE.DoubleSide, wireframe:true});
    object = new THREE.Mesh(geo, material);

// getVolume(geometry);

function signedVolumeOfTriangle(p1, p2, p3){
// return vol = p1.dot(p2.cross(p3))/6.0;
var v321 = p3.xp2.yp1.z;
var v231 = p2.xp3.yp1.z;
var v312 = p3.xp1.yp2.z;
var v132 = p1.xp3.yp2.z;
var v213 = p2.xp1.yp3.z;
var v123 = p1.xp2.yp3.z;
return (1.0/6.0)*(-v321 + v231 + v312 - v132 - v213 + v123);


// Get volume
function getVolume(geometry){
console.log("‘geometry’ must be an indexed or non-indexed buffer geometry");
return 0;
var isIndexed = geometry.index !==null;
let position = geometry.attributes.position;
let p1 = new THREE.Vector3(),
p2= new THREE.Vector3(),
p3 = new THREE.Vector3();

let sum = 0;
let faces = position.count / 3;
for(let i=0; i<faces; i++){
p1.fromBufferAttribute(position, i*3 + 0);
p2.fromBufferAttribute(position, i * 3 + 1);
p3.fromBufferAttribute(position, i * 3 + 0);
sum += signedVolumeOfTriangle(p1, p2, p3);

} else {
let index = geometry.index;
let faces = index.count / 3;
for (let i = 0; i < faces; i++){
p1.fromBufferAttribute(position, index.array[i * 3 + 0]);
p2.fromBufferAttribute(position, index.array[i * 3 + 1]);
p3.fromBufferAttribute(position, index.array[i * 3 + 2]);
sum += signedVolumeOfTriangle(p1, p2, p3);
return sum;

I’m creating a web app where a customer can load and stl file and, the volume and other dimensions can be generated. The generated data will be used in determine the price for 3d printing.
Any help will be much appreciated.

I have seen my mistake in the code. it’s at the p3.fromBufferAttribute(position, i * 3 + 0);
I have fixed it, and the volume calculation is correct when compared to other 3d model calculators.

The problem I’m having now is the result of p1.distanceTo(p3) is not correct when I compared it the other 3d model calculators. I don’t know where the problem is.

How exactly are you calculating the volume - can you share a doc or some brief explanation? Why are you calculating triangle volume based only on the z-axis in signedVolumeOfTriangle ? Wouldn’t in this case (nearly-) vertical faces have (nearly-) 0 volume, even though they’d still require filament to print :thinking: ?

(If it’s just a cubic volume - you may also use using Box3, but I’m assuming it won’t be that simple. :’) )

There is that topic: Volume of THREE.BufferGeometry()

1 Like

From what I’ve read so far on calculating volume of 3d model of BufferGeometry, both index and non-index, the formula in the signedVolumeOfTriangle is the one that have been used mostly. From what I understand, the surface of the model is made up of triangle and the volume can be calculated by calculating the volume of each triangle.
I’m new to threejs, so I don’t have much knowledge about all the calculation involves.

But after comparing my result to other models, the volume is correct for two different stl files I uploaded.

The problem I’m having now is getting the correct distance