热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

三。js设置并读取相机的lookvector-three.jssetandreadcameralookvector

Insteadofrotatingthecamerawithcamera.rotationorwiththelookAt()functionIdliketopassa

Instead of rotating the camera with camera.rotation or with the lookAt() function I'd like to pass a look vector directly to the camera... Is it possible to set a camera look vector directly and is it possible to read the look vector from the camera?

而不是用照相机旋转照相机。旋转或使用lookAt()函数,我想将一个look矢量直接传递给相机……是否可以直接设置摄像头的视向量,是否可以从摄像头中读出视向量?

4 个解决方案

#1


25  

The camera does not have a "look vector", so you cannot set it.

相机没有“look vector”,所以你不能设置它。

You can, however, construct a point to look at by adding your look vector to the camera's position, and then call

但是,你可以通过在相机的位置添加你的外观向量来构造一个点,然后调用。

camera.lookAt( point );

Here is how to determine the direction in which the camera is looking -- assuming the camera either has no parent (other than the scene).

这里是如何确定相机的方向——假设相机没有父(除了场景)。

The camera is looking down it's internal negative z-axis, so create a vector pointing down the negative z-axis:

相机向下看它的内部负z轴,所以创建一个指向负z轴的矢量:

var vector = new THREE.Vector3( 0, 0, - 1 );

Now, apply the same rotation to the vector that is applied to the camera:

现在,将同样的旋转应用到相机上的矢量:

vector.applyQuaternion( camera.quaternion );

The resulting vector will be pointing in the direction that the camera is looking.

由此产生的矢量将指向相机的方向。

Alternatively, you can use the following method, which works even if the camera is a child of another object:

或者,您也可以使用以下方法,即使相机是另一个对象的子对象,也可以使用以下方法:

camera.getWorldDirection( dirVector );

three.js r.73

三。js r.73

#2


8  

The above answer wrapped as a util, this is what I do with my Three Utils:

上面的答案是一个util,这是我用我的3个util做的:

THREE.Utils = {
    cameraLookDir: function(camera) {
        var vector = new THREE.Vector3(0, 0, -1);
        vector.applyEuler(camera.rotation, camera.eulerOrder);
        return vector;
    }
};

Call it with THREE.Utils.cameraLookDir(camera);

叫它与THREE.Utils.cameraLookDir(相机);

#3


5  

These other answers are very insightful, but not completely correct. The code returns a vector that points in the same direction that the camera is pointing at. That's a great start!

这些其他的答案很有见地,但并不完全正确。代码返回一个指向摄像机指向的方向的向量。这是一个好的开始!

But unless the camera is at the origin (0, 0, 0) (or lies exactly on the line segment that connects the origin to the rotated vector point without passing beyond that point so that the point is behind the camera) the camera won't be looking at that point. Clearly -- just common sense -- the position of the camera also influences what points are being looked at. Just think about it!!

但是,除非相机在原点(0,0,0)(或者正好位于将原点与旋转矢量点相连的线段上,而不经过那个点,这样这个点就在相机后面),否则相机不会看到那个点。很明显,仅仅是常识,相机的位置也会影响到所观察到的点。想想吧! !

The camera method lookAt() looks at a point in 3D space regardless of where the camera is. So to get a point that the camera is looking at you need to adjust for the camera position by calling:

camera方法lookAt()查看3D空间中的一个点,而不管摄像头在哪里。因此,要得到相机正在观察的点,你需要通过调用:

vec.add( camera.position );

矢量。添加(相机。位置);

It is also worth mentioning that the camera is looking not at a single point but is looking down a line of an infinite number of points, each at a different distance from the camera. The code from the other answers returns a vector that is exactly one unit in length because the application of a quaternion to the normalized z-axis vector (0, 0, -1) returns another normalized vector (in a different direction). To calculate the look at point at an arbitrary distance x from the camera use:

值得一提的是,相机并不是盯着一个点,而是盯着一条无限多的点,每一个点与相机的距离都不同。来自其他答案的代码返回一个长度为一个单位的向量,因为四元数对归一化的z轴向量的应用(0,0,-1)返回另一个归一化的向量(在另一个方向上)。计算从相机使用的任意距离x的角度:

THREE.Vector3( 0, 0, -x ).applyQuaternion( camera.quaternion ).add( camera.position );

三。向量(0,0,-x)applyQuaternion(相机。四元数)。添加(相机。位置);

This takes a point along the z-axis at a distance x from the origin, rotates it to the direction of the camera and then creates a "world point" by adding the camera's position. We want a "world point" (and not just a "relative to the camera point") since camera.lookAt() also takes a "world point" as a parameter.

它沿着z轴取一个点,距离原点x,将它旋转到相机的方向,然后通过增加相机的位置来创建一个“世界点”。我们需要一个“world point”(而不仅仅是“相对于camera point”),因为camerat . lookat()也将“world point”作为参数。

#4


-1  

What I did was to use method lookAt(Vector) just before render the scene like in below code , just try it using it on a new html file :)

我所做的就是在呈现场景之前使用method lookAt(Vector),如下面的代码所示,在一个新的html文件中使用:)



  
    
    
    
  
  
    

    


推荐阅读
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • 导航栏样式练习:项目实例解析
    本文详细介绍了如何创建一个具有动态效果的导航栏,包括HTML、CSS和JavaScript代码的实现,并附有详细的说明和效果图。 ... [详细]
  • 本文详细介绍了如何解决Uploadify插件在Internet Explorer(IE)9和10版本中遇到的点击失效及JQuery运行时错误问题。通过修改相关JavaScript代码,确保上传功能在不同浏览器环境中的一致性和稳定性。 ... [详细]
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • 深入理解 Oracle 存储函数:计算员工年收入
    本文介绍如何使用 Oracle 存储函数查询特定员工的年收入。我们将详细解释存储函数的创建过程,并提供完整的代码示例。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 本文详细介绍如何使用Python进行配置文件的读写操作,涵盖常见的配置文件格式(如INI、JSON、TOML和YAML),并提供具体的代码示例。 ... [详细]
  • 本文介绍了如何利用JavaScript或jQuery来判断网页中的文本框是否处于焦点状态,以及如何检测鼠标是否悬停在指定的HTML元素上。 ... [详细]
  • 本文介绍了如何使用JQuery实现省市二级联动和表单验证。首先,通过change事件监听用户选择的省份,并动态加载对应的城市列表。其次,详细讲解了使用Validation插件进行表单验证的方法,包括内置规则、自定义规则及实时验证功能。 ... [详细]
  • 本文介绍了一款用于自动化部署 Linux 服务的 Bash 脚本。该脚本不仅涵盖了基本的文件复制和目录创建,还处理了系统服务的配置和启动,确保在多种 Linux 发行版上都能顺利运行。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • QUIC协议:快速UDP互联网连接
    QUIC(Quick UDP Internet Connections)是谷歌开发的一种旨在提高网络性能和安全性的传输层协议。它基于UDP,并结合了TLS级别的安全性,提供了更高效、更可靠的互联网通信方式。 ... [详细]
  • 本文介绍如何在 Android 中通过代码模拟用户的点击和滑动操作,包括参数说明、事件生成及处理逻辑。详细解析了视图(View)对象、坐标偏移量以及不同类型的滑动方式。 ... [详细]
  • 本文详细介绍了Java中org.eclipse.ui.forms.widgets.ExpandableComposite类的addExpansionListener()方法,并提供了多个实际代码示例,帮助开发者更好地理解和使用该方法。这些示例来源于多个知名开源项目,具有很高的参考价值。 ... [详细]
author-avatar
三生石512606
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有