我加载了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);
.clone()
在不同的位置附加它们即可。对于“交叉”部分,您可以计算边界框,如果边界框重叠,则尝试一个新的随机位置。 - Don McCurdy