How drag correctly the OBJ file?

obj-format
drag-controls
#1

Hi everyone,

I have some problem with drag object, i just want to move this with the mouse
Capture%20d%E2%80%99%C3%A9cran%20de%202019-05-17%2009-50-10

I have tried to drag with the mouse a gltf file that works well

    var loader = new THREE.GLTFLoader();
    loader.load(
        `models/toaster/scene.gltf`,
        ( gltf ) => {

            this.scene.add(gltf.scene);
            gltf.scene.scale.set(5, 5, 5);
            //gltf.scene.position.set(400,400,400)
            gltf.scene.traverse( function( object ) {
                if ( object.isMesh) objects.push( object );
            } );
        },
        null,
        null
    )

I have tried with geometry, well too

    var geometry = new THREE.BoxGeometry( 40, 40, 40 );
    var geometry = new THREE.SphereGeometry( 40, 40, 40 );
    var shape = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );

    shape.position.x = Math.random() * 1000 - 500;
    shape.position.y = Math.random() * 600 - 300;
    shape.position.z = Math.random() * 800 - 400;

    shape.castShadow = true;
    shape.receiveShadow = true;

    scene.add( shape );

But when I try with obj file i got some problem, I have tried like gltf file

   var bis;
    var mtlLoaderBis = new THREE.MTLLoader();
    mtlLoaderBis.setResourcePath('models/doppio/');
    mtlLoaderBis.setPath('models/doppio/');
    mtlLoaderBis.load('doppio.mtl', function (materials) {

        materials.preload();

        var objLoader = new THREE.OBJLoader();
        objLoader.setMaterials(materials);
        objLoader.setPath('models/doppio/');
        objLoader.load('doppio.obj', function (object) {

            scene.add(object);

            object.traverse( function (child ) {
                if (child.type === 'Mesh' ){
                  //  bis = child;
                    objects.push(child);
                }
            });

        });
    });

but that break my oject up like that

and when i try like this

    var bis;
    var mtlLoaderBis = new THREE.MTLLoader();
    mtlLoaderBis.setResourcePath('models/doppio/');
    mtlLoaderBis.setPath('models/doppio/');
    mtlLoaderBis.load('doppio.mtl', function (materials) {

        materials.preload();

        var objLoader = new THREE.OBJLoader();
        objLoader.setMaterials(materials);
        objLoader.setPath('models/doppio/');
        objLoader.load('doppio.obj', function (object) {

            scene.add(object);

            object.traverse( function (child ) {
                if (child.type === 'Mesh' ){
                    bis = child;
                }
            });

            objects.push(bis);

        });
    });

I have only one thing is moving
Capture%20d%E2%80%99%C3%A9cran%20de%202019-05-17%2009-55-31

then when i do this like simple object

    var mtlLoaderBis = new THREE.MTLLoader();
    mtlLoaderBis.setResourcePath('models/doppio/');
    mtlLoaderBis.setPath('models/doppio/');
    mtlLoaderBis.load('doppio.mtl', function (materials) {

        materials.preload();

        var objLoader = new THREE.OBJLoader();
        objLoader.setMaterials(materials);
        objLoader.setPath('models/doppio/');
        objLoader.load('doppio.obj', function (object) {

            scene.add(object);

            objects.push(object);
        });
    });

nothing happens

I don’t know how can i do to move all the block, if some one can tell me some advice.

Thanks for your time.

#2

Even if you load a single OBJ, the visual object can be composed of many individual 3D objects. Since DragControls works on object level, the mentioned result is the intended behavior.

The easiest way to fix this problem is to merge the single parts of your object in a content creation tool like Blender and then export a new OBJ. Otherwise you have to merge the single meshes into a bigger one via JavaScript. Or you change DragControls so it is also able to select the group object based on its bounding box.

2 Likes
#3

Thank you very much @Mugen87