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

首先来看一下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源码





  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值