【问题标题】:How to find intersection of objects in three.js?如何在three.js中找到对象的交集?
【发布时间】:2018-08-31 04:52:24
【问题描述】:

我加载了 20 个位置随机的对象。我需要使对象具有随机位置但不相交。 如何找到物体的交点并检查交点?

 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);

【问题讨论】:

  • 如果您需要 20 个相同的对象,只需加载一次并使用 .clone() 在不同位置多次附加它。对于“交叉点”,您可以compute a bounding box 并在边界框重叠时尝试新的随机位置。
  • 或者计算一个边界球并做同样的事情。

标签: javascript three.js


【解决方案1】:

阅读 THREE.js 文档:Raycaster Documentation 99% 的时间threejs.org/docs 会得到您的答案

【讨论】:

    猜你喜欢
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    • 2018-06-12
    相关资源
    最近更新 更多