简体   繁体   English

如何在three.js中找到对象的交集?

[英]How to find intersection of objects in three.js?

I load 20 objects which have random position. 我加载了20个具有随机位置的对象。 I need to make it so that the objects have random position but don't intersect. 我需要使对象具有随机位置,但不相交。 How to fint the intersection of thee objects and check intersection? 如何找到对象的交点并检查交点?

 for (var i = 0; i < 20; i++) {
        // Create a material
        var textureLoader = new THREE.TextureLoader();
        var map = textureLoader.load('./models/test_2.png');
        var material = new THREE.MeshPhongMaterial({ map: map });

        var shipMtl = new MTLLoader();
        var loader = new this.THREE.OBJLoader();

        shipMtl.load('./models/test.mtl', function(materials) {
            materials.preload();

            loader.setMaterials(materials);
            loader.load('./models/test.obj', function(object) {
                object.traverse(function(node) {
                    if (node.isMesh) node.material = material;
                });

                object.position.x = Math.random() * 500 - 250;
                object.position.y = Math.random() * 500 - 250;
                object.position.z = Math.random() * 500 - 250;
                object.scale.x = Math.random() * 2 + 40;
                object.scale.y = Math.random() * 2 + 40;
                object.scale.z = Math.random() * 2 + 40;
                object.rotation.x = Math.random() * 2 * Math.PI;
                object.rotation.y = Math.random() * 2 * Math.PI;
                object.rotation.z = Math.random() * 2 * Math.PI;
                var obj = object; // put your object as global
                scene.add(obj);
            });
        });
    }

    raycaster = new THREE.Raycaster();

    mouse = new THREE.Vector2();
    renderer = new THREE.WebGLRenderer({ antialias: true });
    renderer.setPixelRatio(window.devicePixelRatio);
    renderer.setSize(window.innerWidth, window.innerHeight);
    this.refs.figure.appendChild(renderer.domElement);

阅读THREE.js文档: Raycaster文档 99%的时间Threejs.org/docs会为您解答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM