- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
浏览器窗口大小改变后,我需要获取正确的 Three.JS 相机 FOV。我已查看以下问题,但似乎找不到我的问题的答案:
我的相机设置如下(“this”指我设置的 gameCamera 对象):
const CAMERA_DIST = 8000;
-other stuff-
this.camera = new THREE.PerspectiveCamera(
45, //FOV parameter
window.innerWidth / window.innerHeight, //aspect ratio parameter
1, //frustum near plane parameter
CAMERA_DIST //frustum far plane parameter
);
当用户调整浏览器窗口大小时,将调用以下更新代码。我包含了在这里找到的代码:( How to calculate fov for the Perspective camera in three js? ),用于尝试计算新的 FOV ('aFOV')。
function onWindowResize() {
gameCamera.camera.aspect = window.innerWidth / window.innerHeight;
console.log(gameCamera.camera.fov);
gameCamera.camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
console.log(gameCamera.camera.fov);
let aFOV = 2*Math.atan((window.innerHeight)/(2*CAMERA_DIST)) * (180/Pi);
console.log(aFOV);
windowHalfX = window.innerWidth / 2;
windowHalfY = window.innerHeight / 2;
} //onWindowResize()
但是好像不行。调整窗口大小后,例如将其拖动 500 像素宽,我可以看到渲染的 3D 场景的宽度要大得多。渲染的 View 似乎没有扭曲(即没有或多或少的“鱼眼”外观)。但camera.fov值没有改变(在控制台日志中,我之前得到“45”作为FOV,之后得到“45”作为FOV)并且我计算的FOV根本不正确——值为“6.33189” ...'。
因此在我看来,FOV是用来设置projectionMatrix的,但是当调用updateProjectionMatrix()时,并没有进行反向计算来更新FOV。我正在使用 THREE.JS r87(修订版 87)。
这是我在此 ( How to calculate fov for the Perspective camera in three js? ) 链接中找到的用于计算 FOV 的原始代码:
var height = 500;
var distance = 1000;
var fov = 2 * Math.atan((height) / (2 * distance)) * (180 / Math.PI);
itsLeftCamera = new THREE.PerspectiveCamera(fov , 400 / 500, 1.0, 1000);
该链接上的注释似乎表明该公式是正确的。
问题:
提前致谢
--- 编辑 ----
问完这个问题后,我决定尝试看看是否能解决这个问题。
我从@rabbid76 的优秀图表开始:Calculating frustum FOV for a PerspectiveCamera如果知道远平面尺寸和相机距离,我修改了图像以显示计算 FOV 的公式的推导。
现在我明白了公式的推导。我发现,如果给定起始垂直 FOV,那么我可以计算远平面宽度和高度,如下所示:
this.cameraDist = CAMERA_DIST;
this.aspectRatio = window.innerWidth / window.innerHeight;
this.farPlaneHeight = Math.tan(this.vertFOV/2) * this.cameraDist;
this.farPlaneWidth = this.Height * this.aspectRatio;
但我还是被困住了。我不明白渲染窗口大小(即浏览器窗口)和远平面大小之间的相关性。如果我的cameraDist很大(例如1,000,000),我的远平面也会很大。
我假设我的渲染窗口位于近平面和远平面之间。所以我需要的是到渲染平面的距离。
我仍然不知道在更改 window.innerHeight 或 window.innerWidth 后如何确定新的相机 FOV。
-- 编辑 2--
@rabbid76 在评论中建议了正确答案。我想理解它,所以在考虑时做了一些图表:
该图显示,随着视口(viewport)平面尺寸的变化 (h1 --> h2),我们可以计算出有效视野 (FOV) 的变化。
如果视口(viewport)不是方形的,那么在已知垂直 FOV 且纵横比已知的情况下,也可以使用此公式来计算水平 FOV。
为了将这一切作为最终答案,我使用以下代码:
//Below is code used when initializing the perspective camera
this.viewportWidth = window.innerWidth;
this.viewportHeight = window.innerHeight;
this.aspectRatio = window.innerWidth / window.innerHeight;
this.vertFOV = params.FOV || CAMERA_FOV
this.horizFOV = this.calculateHorizFOV();
this.camera = new THREE.PerspectiveCamera(this.vertFOV, this.aspectRatio, 1, this.cameraDist);
...
calculateHorizFOV() {
let radVertFOV = this.vertFOV * Pi/180;
let radHhorizFOV = 2 * Math.atan( Math.tan(radVertFOV/2) * this.aspectRatio);
let horizFOV = radHorizFOV * 180/Pi;
return horizFOV;
}
然后,当用户调整屏幕大小时,我使用此代码。
function onWindowResize() {
let oldHeight = gameCamera.viewportHeight;
let oldWidth = gameCamera.viewportWidth;
let newHeight = window.innerHeight;
let newWidth = window.innerWidth;
gameCamera.viewportHeight = newHeight;
gameCamera.viewportWidth = newWidth;
gameCamera.aspectRatio = newWidth / newHeight;
let oldRadFOV = gameCamera.vertFOV * Pi/180;
let newRadVertFOV = 2*Math.atan( Math.tan(oldRadFOV/2) * newHeight/oldHeight);
gameCamera.vertFOV = newRadVertFOV * 180/Pi;
gameCamera.calculateHorizFOV();
gameCamera.camera.aspect = gameCamera.aspectRatio;
gameCamera.camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
} //onWindowResize()
最佳答案
如果你想知道,如果一个点位于 View 体积中并且未被剪切,那么你必须将该点投影到视口(viewport)上。
使用Vector3.project
为此:
camera = THREE.PerspectiveCamera
pt_world = Three.Vector3
pt_ndc = new THREE.Vector3()
pt_ndc.copy(pt_world).project(camera)
结果是 Cartesian coordinate如果结果位于标准化设备空间中,则该点位于 View 体积中(在视口(viewport)上)。归一化的设备空间的范围是从(-1,-1,-1)到(1,1,1),形成一个完美的立方体体积。(参见Transpose z-position from perspective to orthographic camera in three.js)
请注意,投影矩阵描述了从场景的 3D 点到视口(viewport)的 2D 点的映射。投影矩阵从 View 空间变换到剪辑空间,并且剪辑空间中的坐标变换为范围为 (-1, -1, -1) 到 (1, 1, 1) 的归一化设备坐标 (NDC)除以剪辑坐标的 w 分量。
关于javascript - 三JS : Calculating FOV for perspective camera after browser window resize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47184264/
我知道 Magical Record 支持聚合操作,例如 sum:、max: 但是有没有办法进行一些简单的计算,例如: 总和:属性 * other_attributes 如果我们知道这些属性的值为 N
我有一个项目可以计算一些关于用户表现的“统计数据”,然后将其展示给他们。所有这些统计数据最终都来自一个记录用户与网站交互的大型“交互”表。目前,所有这些统计数据都是通过查看这些数据来计算的。我们广泛使
我正在试着用熊猫和NumPy来计算蟒蛇中的Connors RSI。我想用ConnorsRSI的默认值(3,2,100)来计算它。。Connors RSI的公式为:[RSI(Close,3)+RSI(S
我对某种 mean() 计算有疑问。我使用带有两个标识符“ID”和“year”的面板数据集(使用 plm pkg) 我想计算变量“y”的分组平均值,但省略了第一年的计算条目,然后仅填写用于计算它的年份
我不知道这是否是微不足道的或实际上很棘手:是否可以捕获 VBA 中的“计算工作表 (shift+f9)”和“计算工作簿”事件? 我想隐藏一些操作几千行的进程,只显示一些关键值。我正在计算分布,数千行,
我和#1895500有同样的问题, 但使用 PostgreSQL 而不是 MySQL。 如何定义具有计算字段的 View ,例如: (mytable.col1 * 2) AS times_two .
如何定义具有两个计算字段的 View ,例如... ('TableName'.'BlueSquares' + 'TableName'.'RedSquares') AS TotalSquares, (
CALCULATE(m, x=red) 和 CALCULATE(m, KEEPFILTERS(x=red)) 之间有什么区别 显然它们不一样。我找到了文档和解释,但我仍然不明白。 https://le
我正在尝试从命令提示符运行我的 Java 类文件,当我尝试这样做时,我收到此错误 C:\Users\New User\workspace\myproject\bin\apackage>java cal
我正在尝试根据用户的输入显示文本。例如输入单词 APPLE 应该让它显示 BANANA。 这段代码工作正常: :Input X :If X=APPLE :Disp "BANANA" 但我不确定如何以此
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-topic
我们正在尝试实现自己的自定义购物篮计算规则集并注册新的结果 View 来获取购物篮计算结果,但我们无法找到一些如何注册新结果 View 类的信息? 我们使用这里的示例:https://support.
数字变量是否遵循 TI 计算器上的记录标准? 我真的很惊讶地注意到我的 TI 83 Premium CE 测试实际上返回了 true(即 1): 0.1 -> X 0.1 -> Y 0.01 -> Z
大约两天前,我收到了我的 TI-82 STATS 可编程计算器(实际上更像是一个 TI-83) - 并想用内置的 TI-BASIC 语言编写一个贪吃蛇游戏。 虽然我不得不找出:TI-BASIC 是 极
作为家庭作业,我们有一个基本的计算器,它只能进行+运算,我们必须实现更多的功能。我们必须实现括号运算符、符号运算符和最小最大函数。最后的任务之一是扩展最小/最大函数以计算具有两个以上参数的最小/最大,
如何从 Excel 的单元格中选择一列,然后仅计算该列?我只知道 SHIFT + F9 可以计算整个工作表,F9 可以计算整个工作簿。 谢谢你们;) 最佳答案 我认为仅使用标准 Excel 无法做到这
我已经为计算器编写了代码,但它还不能 100% 可靠地工作。每次我进行计算时,例如:“1+1=2”,并且我想要进行另一次计算,我必须关闭小程序并重新启动它。我怎样才能让它回到开始的地方。 这是代码:
意图:该程序要求用户提供其银行帐户中当前的金额、年利率和年数。输出是金额的开始和结束,显示用户指定年份的累计利息。 问题:我正在尝试找到一种正确添加利息的方法,截至目前,在指定的年份里,我所做的就是乘
我怎么让第一次点击不接受操作返回“0” 这是我的功能 $(document).ready(function(){ $('button').on('click', function(){
题目地址:https://leetcode.com/problems/basic-calculator/description/ 题目描述 Implement a basic calculator
我是一名优秀的程序员,十分优秀!