Webgl基于Three.js的开发——利用Frustum来判断相交或者包含

版权声明: https://blog.csdn.net/niuge8905/article/details/80898522

首先来看一下Frustum的文档:Frustum帮助文档

根据文档创建一个Frustum:

function initFrustum()
        {
            //定义锥形物体最远的四个点
            var topLeftPoint = new THREE.Vector3(-2000,2000,-1000);
            var topRightPoint= new THREE.Vector3(2000,2000,-1000);
            var bottomLeftPoint = new THREE.Vector3(-2000, -2000, -1000);
            var bottomRightPoint = new THREE.Vector3(2000, -2000, -1000);
            //定义锥形物体最近的四个点
            var nearTopLeftPoint = new THREE.Vector3(-2000, 2000, 1000);
            var nearTopRightPoint = new THREE.Vector3(2000, 2000, 1000);
            var nearBottomLeftPoint = new THREE.Vector3(-2000, -2000, 1000);
            var nearBottomRightPoint = new THREE.Vector3(2000, -2000, 1000);
            topPlane = new THREE.Plane();
            bottomPlane = new THREE.Plane();
            rightPlane = new THREE.Plane();
            leftPlane = new THREE.Plane();
            nearPlane = new THREE.Plane();
            farPlane = new THREE.Plane();
            topPlane.setFromCoplanarPoints(nearTopLeftPoint, topLeftPoint, topRightPoint);
            leftPlane.setFromCoplanarPoints(nearTopLeftPoint, topLeftPoint, bottomLeftPoint);
            bottomPlane.setFromCoplanarPoints(nearBottomLeftPoint, bottomLeftPoint, bottomRightPoint);
            rightPlane.setFromCoplanarPoints(nearBottomRightPoint, bottomRightPoint, topRightPoint);
            farPlane.setFromCoplanarPoints(topLeftPoint, topRightPoint, bottomLeftPoint);
            nearPlane.setFromCoplanarPoints(nearTopLeftPoint,nearTopRightPoint,nearBottomLeftPoint);
            frustum = new THREE.Frustum(topPlane,leftPlane,bottomPlane,rightPlane,farPlane,nearPlane);
        }

接着前面两章,把判断选中的改为:

frustum.intersectsObject(intersects[0].object)

就可以直观的看到相交的结果。



最后可以直观的看到结果:相交的时候返回True.从源码中可以看到相交和包含返回一样的值。如果想把相交和包含区分开来,需要进一步处理。另外,这里的结果不一定正确,这里是取的是包含物体sphere,所以有些情况下,实际没有相交,会被判断为相交。

Frustum源码





阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页