热门标签 | 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文件中使用:)



  
    
    
    
  
  
    

    


推荐阅读
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • Html5-Canvas实现简易的抽奖转盘效果
    本文介绍了如何使用Html5和Canvas标签来实现简易的抽奖转盘效果,同时使用了jQueryRotate.js旋转插件。文章中给出了主要的html和css代码,并展示了实现的基本效果。 ... [详细]
  • 本文介绍了在CentOS上安装Python2.7.2的详细步骤,包括下载、解压、编译和安装等操作。同时提供了一些注意事项,以及测试安装是否成功的方法。 ... [详细]
  • 本文介绍了2015年九月八日的js学习总结及相关知识点,包括参考书《javaScript Dom编程的艺术》、js简史、Dom、DHTML、解释型程序设计和编译型程序设计等内容。同时还提到了最佳实践是将标签放到HTML文档的最后,并且对语句和注释的使用进行了说明。 ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文介绍了C#中生成随机数的三种方法,并分析了其中存在的问题。首先介绍了使用Random类生成随机数的默认方法,但在高并发情况下可能会出现重复的情况。接着通过循环生成了一系列随机数,进一步突显了这个问题。文章指出,随机数生成在任何编程语言中都是必备的功能,但Random类生成的随机数并不可靠。最后,提出了需要寻找其他可靠的随机数生成方法的建议。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • MPLS VP恩 后门链路shamlink实验及配置步骤
    本文介绍了MPLS VP恩 后门链路shamlink的实验步骤及配置过程,包括拓扑、CE1、PE1、P1、P2、PE2和CE2的配置。详细讲解了shamlink实验的目的和操作步骤,帮助读者理解和实践该技术。 ... [详细]
  • 本文由编程笔记#小编整理,主要介绍了关于数论相关的知识,包括数论的算法和百度百科的链接。文章还介绍了欧几里得算法、辗转相除法、gcd、lcm和扩展欧几里得算法的使用方法。此外,文章还提到了数论在求解不定方程、模线性方程和乘法逆元方面的应用。摘要长度:184字。 ... [详细]
  • 本文介绍了使用哈夫曼树实现文件压缩和解压的方法。首先对数据结构课程设计中的代码进行了分析,包括使用时间调用、常量定义和统计文件中各个字符时相关的结构体。然后讨论了哈夫曼树的实现原理和算法。最后介绍了文件压缩和解压的具体步骤,包括字符统计、构建哈夫曼树、生成编码表、编码和解码过程。通过实例演示了文件压缩和解压的效果。本文的内容对于理解哈夫曼树的实现原理和应用具有一定的参考价值。 ... [详细]
  • 本文讨论了在VMWARE5.1的虚拟服务器Windows Server 2008R2上安装oracle 10g客户端时出现的问题,并提供了解决方法。错误日志显示了异常访问违例,通过分析日志中的问题帧,找到了解决问题的线索。文章详细介绍了解决方法,帮助读者顺利安装oracle 10g客户端。 ... [详细]
  • Jquery 跨域问题
    为什么80%的码农都做不了架构师?JQuery1.2后getJSON方法支持跨域读取json数据,原理是利用一个叫做jsonp的概念。当然 ... [详细]
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社区 版权所有