简体   繁体   English

Three.js - 视图的宽度

[英]Three.js - Width of view

Is there a way to find out the width of a rendered portion of the scene?有没有办法找出场景渲染部分的宽度?

For example, if we have a mesh of width 100, but rendered with a certain level of zoom...how can I calculate the width of the portion of the mesh that is being rendered in the screen?例如,如果我们有一个宽度为 100 的网格,但以一定的缩放级别进行渲染……我如何计算正在屏幕中渲染的网格部分的宽度?

You have to be precise here.你必须在这里精确。

You can calculate the visible rectangular region given the camera's field-of-view, camera.fov , and a given distance, dist , from the camera.您可以在给定相机的视野、 camera.fov和与相机的给定距离dist下计算可见矩形区域。

Since the object presumably has depth, you have to pick one plane through the mesh, and do the calculation at that distance.由于对象可能具有深度,因此您必须通过网格选择一个平面,并在该距离处进行计算。

Here is how to calculate the visible height and width for a given distance dist from the camera.以下是如何计算距相机给定距离dist的可见heightwidth

var vFOV = THREE.MathUtils.degToRad( camera.fov ); // convert vertical fov to radians

var height = 2 * Math.tan( vFOV / 2 ) * dist; // visible height

var width = height * camera.aspect;           // visible width

three.js r.117三.js r.117

For a (perfect) spherical object, it will cover more of the view close up as you need to take the tangent into account ie you can't see the 'poles'.对于(完美的)球形物体,它将覆盖更多的近距离视图,因为您需要考虑切线,即您看不到“极点”。

Use the following for a spherical object:对球形对象使用以下内容:

// we're using the following triangle: camera - tangent point on sphere - centre of sphere
var targetAngle = 2 * Math.asin(objectRadius / distanceToCamera);

var vFOV = THREE.Math.degToRad(this.fov);

var normalizedHeight = targetAngle / vFOV;

Multiply this with your viewport width to get screen height.将此与您的视口宽度相乘以获得屏幕高度。 For width you need to apply hFOV - see: 90 degree field of view without distortion in THREE.PerspectiveCamera对于宽度,您需要应用 hFOV - 请参阅: THREE.PerspectiveCamera 中无失真的 90 度视野

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM