Incorrect distance of and uploaded stl 3d model

Hi Community,
I uploaded a file using STLLoader, which supports both Binary and ASCII files, and returns non-indexed BufferGeometry.
I have calculated the volume and distance using the following codes. When I compared the results to other results from online 3d calculators, in most cases, the volume matches. However, the distance is not matching at all.
Here is the codes:

var object;
 let material = new THREE.MeshStandardMaterial({color: 0xE5E4E2, side: THREE.DoubleSide, wireframe: false});
var loader = new THREE.STLLoader();
loader.load(`trouser_clip.stl`, function(geometry){
  object = new THREE.Mesh(geometry, material);
// object.position.set(0,-20,0);
var helper = new THREE.AxesHelper(5);
helper.position.set(0, 0, 0)



// getVolume(geometry);

volumeInput.value = getVolume(geometry).toFixed(2);



function signedVolumeOfTriangle(p1, p2, p3){
  // return vol =;
  var v321 = p3.x*p2.y*p1.z;
  var v231 = p2.x*p3.y*p1.z;
  var v312 = p3.x*p1.y*p2.z;
  var v132 = p1.x*p3.y*p2.z;
  var v213 = p2.x*p1.y*p3.z;
  var v123 = p1.x*p2.y*p3.z;

  return (-v321 + v231 + v312 - v132 - v213 + v123)/6.0;


// 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 + 2);
  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);

var dist12 = p1.distanceTo(p2);
var dist13 = p1.distanceTo(p3);
var dist23 = p2.distanceTo(p3);
distInput12.value =dist12.toFixed(2);
distInput13.value =dist13.toFixed(2);
distInput23.value =dist23.toFixed(2);

// console.log({dist12})
// console.log({dist23})
// console.log({dist13})
// console.log({sum})
// console.log(geometry)
return sum;

Can anyone help me with this please. I don’t know what I’m doing wrong.


Hi everyone,
I think I finally know how to get the right values for the size. I think it’s wrong using distanceTo to find the size of my model. I found a solution on Stackoverflow. Instead of using distanceTo, I had to compute the bounding box

//get the boundingBox
var bB = geometry.boundingBox;
// find the difference between max and min vectors
var width = bB.max.x - bB.min.x;
var height = bB.max.y - bB.min.y;
var depth = bB.max.z - bB.min.z;

I just think I should share this. I wouldn’t want anybody to spend almost two days looking for a solution like this.

Please let me know if you have a more efficient way of doing this.

It can be shorter:

let size = new THREE.Vector3();

Thanks man.
This is definitely more efficient.
I can just get the x, y, and z by writing size.x, size.y, size.z.

thanks again