Three.js - 如何检查对象是否对相机可见 [英] Three.js - How to check if an object is visible to the camera

查看:43
本文介绍了Three.js - 如何检查对象是否对相机可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难弄清楚检查 Object3d 是否对相机的眼睛可见的最佳方法是什么.

I'm having hard times figuring out what's the best way to check if a Object3d is visible for the eyes of the camera.

我在屏幕中间有一个球体.一些立方体随机添加在它的表面上.我需要的是一种方法来检查哪些立方体是可见的(在球体的前半部分),哪些是不可见的(在球体的后半部分)对于相机的眼睛.

I'm having a sphere in the middle of the screen. Some cubes are added on it's surface randomly. What I would need is a way to check which cubes are visible (on the front half of the sphere) and which one are invisible (on the back half of the sphere) for the eyes of the camera.

到目前为止我所发现的似乎是正确的方向 - 但我一定在 THREE.Raytracer 类中遗漏了一些东西.

What I have found so far seems to be the right direction - but I must be missing something with the THREE.Raytracer class.

这是我正在使用的代码的小提琴:jsfiddle.我已经尽量说清楚了.

Here is a fiddle of the code that I'm using: jsfiddle. I have tried to make it as clear as possible.

小提琴的这一部分可能包含错误代码:

This part of the fiddle might contain the buggy code:

var raycaster = new THREE.Raycaster();
var origin = camera.position, direction, intersects, rayGeometry = new THREE.Geometry(), g;
pointGroup.children.forEach(function(pointMesh) {
    direction = pointMesh.position.clone();
    // I THINK THIS CALCULATION MIGHT BE WRONG - BUT DON'T KNOW HOW TO CORRECT IT
    raycaster.set(origin, direction.sub(origin).normalize());
    // if the pointMesh's position is on the back half of the globe, the ray should intersect with globe first and the hit the point as second target - because the cube is hidden behind the bigger sphere object
    intersects = raycaster.intersectObject(pointMesh);
    // this is always empty - should contain objects that are located on the back of the sphere ...
    console.log(intersects);
}); 

Frustum Culling 无法按照此堆栈溢出问题中的概述工作:post1

Frustum Culling is not working as outlined in this stack overflow question here: post1

还有这个 post2 和这个 post3 很好地解释了这个主题,但不太适合这种情况.

Also this post2 and this post3 are explaining the topic really good but not quite for this situation.

感谢您的帮助!

推荐答案

您想了解遮挡剔除技术.截锥体剔除工作正常,不是你所描述的.Frustum culling 只是检查对象(或其边界框)是否在相机金字塔内.当您想要消除被视锥体内的其他对象遮挡的对象时,除了 Frustum Culling 之外,您还可以执行 Occlusion culling.但这不是一件容易的事.

You want to look at Occlusion Culling techniques. Frustum culling works fine and is not what you are describing. Frustum culling just checks if an object (or its bounding box) is inside the camera pyramid. You perform Occlusion culling in addition to Frustum Culling specially when you want to eliminate objects which are occluded by other objects inside the view frustum. But it is not an easy task.

这篇关于Three.js - 如何检查对象是否对相机可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆