Calculating area of gltf model

Hi, i’ve been trying to calculate the area of a gltf uncompressed model by creating a BufferGeometry and then get a mesh from that geometry and finaly calculate the surfce area of the mesh. The problem is that I don’t know how to get the geometry correctly.

var manager = new THREE.LoadingManager();

new Promise( function( resolve, reject ) {

	if ( url.match( /\.gltz$/ ) ) {

		new THREE.ZipLoader().load( url ).then( function( zip ) {

			manager.setURLModifier( zip.urlResolver );

			resolve( zip.find( /\.(gltf|glb)$/i )[ 0 ] );

        } );
        //https://github.com/takahirox/THREE.ZipLoader.git

	} else {

		resolve( url );

	}

} ).then( function ( file ) {
            console.log(file);

Then a create the scene and all that stuff

var canw = 1000;
                    var canh = 600;
                    var scene = new THREE.Scene();
                     scene.background = new THREE.Color(0xdddddd);
                    var camera = new THREE.PerspectiveCamera(100, canw / canh,1,1000);
                    container = document.getElementById( 'canvas' );
                   document.body.appendChild( container );

And here is where im trying to get a geometry from that new gltf:

    gltf.scene.traverse( function ( child ) {

        if ( child.isMesh ) {
            //Setting the buffer geometry
            glTFGeometry = child.geometry;

I send the “geometry” to the Area function:

AreaG(glTFGeometry);
        }
    
    } );
    scene.add( gltf.scene );
    
    } );

} );                

And then calculate the area:

function AreaG(modelo){
            var material = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
            var mesh = new THREE.Mesh( modelo, material );
            var area = SuperficialAreaOfMesh(mesh.vertices);
            function crossVectors( a, b ) {
            var ax = a.X, ay = a.Y, az = a.Z;
            var bx = b.X, by = b.Y, bz = b.Z;
             var P={X:ay * bz - az * by,
                   Y:az * bx - ax * bz,
                   Z:ax * by - ay * bx}
     
        return P;
     }
      function SuperficialAreaOfMesh(points) {
     
        var _len =points.length(),
           _area = 0.0;
     
        if (!_len) return 0.0;
     
        var i= 0,vols=0;
        var va,vb,vc;
        do  {
           va={X:points[i],Y:points[i+1],Z:points[i+2]};
           vb={X:points[i+3],Y:points[i+4],Z:points[i+5]};
           vc={X:points[i+6],Y:points[i+7],Z:points[i+8]};
            
           vb.clone().sub(va);
           vc.clone().sub(va);
           var ab = {X:vb.X-va.X,Y:vb.Y-va.Y,Z:vb.Z-va.Z};
              
            var ac = {X:vc.X-va.X,Y:vc.Y-va.Y,Z:va.Z-vc.Z};
            var cross = new THREE.Vector3();
           cross=crossVectors( ab, ac );
           _area += Math.sqrt(Math.pow(cross.X,2)+Math.pow(cross.Y,2)+Math.pow(cross.Z,2))/2;
           i+=9;
        }
        while (i<points.length);
     
        return customRound(Math.abs(_area)/100,2);
     
     }
     
    }

This code might help?

Note that if the geometry has an index, it converts the geometry to a non-indexed geometry first.

1 Like

I just fixed it, thank you for your help, I really appreciate it. Now I found another problem about calculate the area but with the measurements given by blender when you make the glTF file. If you know something about it, i’ll be happy to see you on that ask.