I check if any of the planes of the frustum intersects with any of the lines I have in the scene:
var line, tmpArr = [];
function initDatas() {
var geometry = new THREE.Geometry();
geometry.vertices.push(
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(0, 0, 50)
);
var material = new THREE.LineBasicMaterial({
color: 0xffff00
});
line = new THREE.Line(geometry, material);
line.frustumCulled = false;
scene.add(line);
}
function onOrbitControlsEnd() {
var frustum = new THREE.Frustum();
frustum.setFromMatrix(new THREE.Matrix4().multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse));
var vStart, vEnd;
var line3 = new THREE.Line3(), interP = new THREE.Vector3();
var resultArr = [];
for (var i = 0, len = line.geometry.vertices.length; i < len - 1; i++) {
vStart = line.geometry.vertices[i];
vEnd = line.geometry.vertices[i + 1];
line3.set(vStart, vEnd);
for (var j = 0; j < 6 ; j++) {
var plane = frustum.planes[j];
var intersect = plane.intersectLine(line3, interP);
if (intersect && frustum.containsPoint(intersect)) {
resultArr.push({ intersect: intersect.clone() });
}
}
}
if (tmpArr.length > 0) {
var len = tmpArr.length;
while (len--) {
scene.remove(tmpArr[len]);
}
tmpArr = [];
}
if (resultArr.length > 0) {
for (var k = 0, kl = resultArr.length; k < kl; k++) {
var mesh = new THREE.Mesh(new THREE.BoxBufferGeometry(1, 1, 1), new THREE.MeshBasicMaterial({ color: 0xff0000 }));
mesh.position.copy(resultArr[k].intersect);
mesh.frustumCulled = false;
scene.add(mesh);
tmpArr.push(mesh);
}
}
}
I need to find the intersection of the cone and line segment in the visible range, as shown in the figure below, but the result is always beyond my expectation. I don’t know where the problem is. Please help me. Thank you very much.