Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
500 views
in Technique[技术] by (71.8m points)

three.js - Picking Object3D loaded via OBJMTLLoader

When we load an Object3D with OBJMTLLoader, it is not possible to use raycaster to pick this object with mouse. Intersection array length is always 0. Any one knows the reason? Below is the code...

The loader routine

            var loader2 = new THREE.OBJMTLLoader();
        loader2.load('/assets/unwrap/masa/dogtasmasa.obj', '/assets/unwrap/masa/dogtasmasa.mtl', function (object) {
            object.position.y = 1.5;
            object.position.x = 0;
            object.position.z = 2;
            object.rotateX(-Math.PI / 2);
            object.rotateZ(-Math.PI / 2);
            object.scale.set(0.04, 0.04, 0.04);
            object.castShadow = true;
            scene.add(object);
        });

and the picking

        function onDocumentMouseDown(event) {

        event.preventDefault();

        SCREEN_WIDTH = window.innerWidth - 5;
        SCREEN_HEIGHT = window.innerHeight - 5;

        var vector = new THREE.Vector3((event.clientX / SCREEN_WIDTH) * 2 - 1, -(event.clientY / SCREEN_HEIGHT) * 2 + 1, 0.5);
        projector.unprojectVector(vector, camera);
        var raycaster = new THREE.Raycaster(camera.position, vector.sub(camera.position).normalize());
        var intersects = raycaster.intersectObjects(scene.children);


        if (intersects.length > 0) {
            for (var i = 0; i < intersects.length; i++) {
                var obj = intersects[i].object;
                    controls.enabled = false;
                    tControls.attach();
            }
        }
        else {
            controls.enabled = true;
            tControls.detach();
        }
    }

The scene is the whole browser window. Any other mesh cerated via THREE types can be picked, but object3d not...

Thanks for all kinds of help

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Add the recursive flag like so:

var intersects = raycaster.intersectObjects( objects, true );

three.js r.66


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...