《VR入门系列教程》之18---Oculus代码剖析
代码剖析
Anchor 是关键所在,它们分别带有一个相机,用来分别渲染左右眼视图。这两个相机在Inspector中的参数都是默认值,它们的参数会在程序运行的时候有所改变。
- #if !UNITY_ANDROID || UNITY_EDITOR
- privatevoid LateUpdate()
- #else
- privatevoid Update()
- #endif
- {
- EnsureGameObjectIntegrity();
- if(!Application.isPlaying)
- return;
- UpdateCameras();
- UpdateAnchors();
- }

#if !UNITY_ANDROID || UNITY_EDITOR
privatevoid LateUpdate()
#else
privatevoid Update()
#endif
{
EnsureGameObjectIntegrity();
if(!Application.isPlaying)
return;
UpdateCameras();
UpdateAnchors();
}
- privatevoid UpdateCameras()
- {
- if(needsCameraConfigure)
- {
- leftEyeCamera = ConfigureCamera(OVREye.Left);
- rightEyeCamera = ConfigureCamera(OVREye.Right);
- #if !UNITY_ANDROID || UNITY_EDITOR
- needsCameraConfigure = false;
- #endif
- }
- }
privatevoid UpdateCameras()
{
if(needsCameraConfigure)
{
leftEyeCamera = ConfigureCamera(OVREye.Left);
rightEyeCamera = ConfigureCamera(OVREye.Right);
#if !UNITY_ANDROID || UNITY_EDITOR
needsCameraConfigure = false;
#endif
}
}
- privateCamera ConfigureCamera(OVREye eye)
- {
- Transform anchor = (eye == OVREye.Left) ? leftEyeAnchor : rightEyeAnchor;
- Camera cam = anchor.GetComponent<Camera>();
- OVRDisplay.EyeRenderDesc eyeDesc = OVRManager.display.GetEyeRenderDesc(eye);
- cam.fieldOfView = eyeDesc.fov.y;
- cam.aspect = eyeDesc.resolution.x / eyeDesc.resolution.y;
- cam.rect = newRect(0f, 0f, OVRManager.instance.virtualTextureScale, OVRManager.instance.virtualTextureScale);
- cam.targetTexture = OVRManager.display.GetEyeTexture(eye);
- cam.hdr = OVRManager.instance.hdr;
- ...
- returncam;
- }

privateCamera ConfigureCamera(OVREye eye)
{
Transform anchor = (eye == OVREye.Left) ? leftEyeAnchor : rightEyeAnchor;
Camera cam = anchor.GetComponent<Camera>();
OVRDisplay.EyeRenderDesc eyeDesc = OVRManager.display.GetEyeRenderDesc(eye);
cam.fieldOfView = eyeDesc.fov.y;
cam.aspect = eyeDesc.resolution.x / eyeDesc.resolution.y;
cam.rect = newRect(0f, 0f, OVRManager.instance.virtualTextureScale, OVRManager.instance.virtualTextureScale);
cam.targetTexture = OVRManager.display.GetEyeTexture(eye);
cam.hdr = OVRManager.instance.hdr;
...
returncam;
}
- privatevoid UpdateAnchors()
- {
- boolmonoscopic = OVRManager.instance.monoscopic;
- OVRPose tracker = OVRManager.tracker.GetPose();
- OVRPose hmdLeftEye = OVRManager.display.GetEyePose(OVREye.Left);
- OVRPose hmdRightEye = OVRManager.display.GetEyePose(OVREye.Right);
- trackerAnchor.localRotation = tracker.orientation;
- centerEyeAnchor.localRotation = hmdLeftEye.orientation; // using left eye for now
- leftEyeAnchor.localRotation = monoscopic ? centerEyeAnchor.localRotation : hmdLeftEye.orientation;
- rightEyeAnchor.localRotation = monoscopic ? centerEyeAnchor.localRotation : hmdRightEye.orientation;
- trackerAnchor.localPosition = tracker.position;
- centerEyeAnchor.localPosition = 0.5f * (hmdLeftEye.position + hmdRightEye.position);
- leftEyeAnchor.localPosition = monoscopic ? centerEyeAnchor.localPosition : hmdLeftEye.position;
- rightEyeAnchor.localPosition = monoscopic ? centerEyeAnchor.localPosition : hmdRightEye.position;
- if(UpdatedAnchors != null)
- {
- UpdatedAnchors(this);
- }
- }
privatevoid UpdateAnchors()
{
boolmonoscopic = OVRManager.instance.monoscopic;
OVRPose tracker = OVRManager.tracker.GetPose();
OVRPose hmdLeftEye = OVRManager.display.GetEyePose(OVREye.Left);
OVRPose hmdRightEye = OVRManager.display.GetEyePose(OVREye.Right);
trackerAnchor.localRotation = tracker.orientation;
centerEyeAnchor.localRotation = hmdLeftEye.orientation; // using left eye for now
leftEyeAnchor.localRotation = monoscopic ? centerEyeAnchor.localRotation : hmdLeftEye.orientation;
rightEyeAnchor.localRotation = monoscopic ? centerEyeAnchor.localRotation : hmdRightEye.orientation;
trackerAnchor.localPosition = tracker.position;
centerEyeAnchor.localPosition = 0.5f * (hmdLeftEye.position + hmdRightEye.position);
leftEyeAnchor.localPosition = monoscopic ? centerEyeAnchor.localPosition : hmdLeftEye.position;
rightEyeAnchor.localPosition = monoscopic ? centerEyeAnchor.localPosition : hmdRightEye.position;
if(UpdatedAnchors != null)
{
UpdatedAnchors(this);
}
}
《VR入门系列教程》之18---Oculus代码剖析的更多相关文章
- 《VR入门系列教程》之22---GearVR SDK代码剖析
GearVR SDK代码剖析 接下来我们来了解一下GearVR开发包的底层代码,它底层的代码和之前在第三章中讲的桌面SDK代码非常类似,当然,也有许多不同的地方. 首先,我们看看如何构 ...
- 《VR入门系列教程》之19---GearVR开发初识
本章我们来介绍一下如何在移动VR设备的佼佼者GearVR上进行开发,之前我们在桌面端的VR开发想法可以直接应用在移动端,但是仍然会有些不一样的技术需要注意.这次,我们仍然采用Unity3D引擎构建示例 ...
- 《VR入门系列教程》之16---第一个OculusVR应用
第一个VR应用 之前我们已经将Oculus的开发包导入到空工程中了,现在我们来构建第一个桌面VR的示例.开发包中已经有一个示例场景,只需要几步就可以让这个场景运行起来.我们将要构建的这个Demo ...
- 《VR入门系列教程》之20---使用Oculus移动端SDK
使用Oculus移动端SDK 在基于安卓系统的GearVR上开发应用需要用到Oculus的移动端SDK,下面的网址可以下载SDK:http://developer.oculus.com ...
- 《VR入门系列教程》之15---配置Oculus的开发环境
安装Oculus SDK 在使用类似Unity3D之类的引擎开发Oculus Rift应用之前,你必须先安装Oculus的SDK,在Oculus的官网上可以下载:http://develope ...
- 《VR入门系列教程》之14---面向大众的Unity3D
大众化的游戏引擎--Unity3D 并不是所有VR应用都是游戏,然而现在做VR开发的几乎都会用专业游戏引擎来做,因为游戏引擎既满足了一个引擎的要求又可以方便地制作出高品质的VR应用.一个游戏引 ...
- 《VR入门系列教程》之10---3D图形学初识
第三章 基于Oculus Rift开发桌面端VR应用 接下来的几个章节中我们会进行VR开发的实际操练,本章就从Oculus Rift开发开始,我们会介绍如何开发一个桌面端的VR应用.虽然只是介 ...
- 《VR入门系列教程》之4---运行平台
运行平台 大多数的VR应用都可以在目前多数的PC和手机上运行,基本上一个不太旧的PC或者配置好点的笔记本电脑都可以正常运行Oculus Rift,如果手机的CPU和显卡不错的话也可以有很好的V ...
- 《VR入门系列教程》之3---运动追踪与输入设备
运动追踪设备 第二种可以使人脑相信它真实处于虚拟世界的关键技术就是运动追踪技术,它可以通过追踪头部的运动状态实时更新渲染的场景.这与我们在真实世界中观看周围非常类似. 高速的惯性测量单元( ...
随机推荐
- 基于SAP HANA平台的多团队产品研发
工欲善其事必先利其器.要提高多团队的开发效率,而且还是在SAP HANA平台上,建议大家还是本着“慢就是快”的原则,不要急功近利,在没有准备好团队开发的架构时就匆忙开始功能的开发.匆忙功能开发就算了, ...
- 一个基于jQuery写的弹窗效果(附源码)
最近项目中频繁遇到需要弹出窗口的功能,一直使用浏览器默认的Alert和Confirm弹窗,感觉视觉效果不是那么好,而从网上下载的话又找不到合适的,找到的话有些也是十分臃肿,有时候感觉学习配置的功夫自己 ...
- 高可用的zookeeper
Install zookeeper wget http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.13/zookeeper-3.4.13 ...
- 自己动手写jQuery插件---Tip(提示框)
对jQuery相信很多同学和我一样平时都是拿来主义,没办法,要怪只能怪jQuery太火了,各种插件基本能满足平时的要求.但是这毕竟不是长久之道,古人云:“授之以鱼,不如授之以渔”. 为了方便之前没有接 ...
- sso和oauth2.0的简单了解学习
sso,单点登录,single sign on 缩写.sso多用于多个应用之间的切换,例如百度论坛.百度知道.百度云.百度文库等,在其中一个系统中登录,(登录有效期内)切换到另一个系统的时候,不必再次 ...
- java反射机制获取自定义注解值和方法
由于工作需求要应用到java反射机制,就做了一下功能demo想到这些就做了一下记录 这个demo目的是实现动态获取到定时器的方法好注解名称,废话不多说了直接上源码 1.首先需要自定义注解类 /** * ...
- JavaWeb入门_模仿天猫整站Tmall_SSM实践项目
Tmall_SSM 技术栈 Spring MVC+ Mybatis + Spring + Jsp + Tomcat , 是 Java Web 入门非常好的练手项目 效果展示: 模仿天猫前台 模仿天猫后 ...
- 【MYSQL】mysql大数据量分页性能优化
转载地址: http://www.cnblogs.com/lpfuture/p/5772055.html https://www.cnblogs.com/shiwenhu/p/5757250.html ...
- ElasticStack学习(二):ElasticStack安装与运行
一.ElasticSearch的安装与运行 1.由于ElasticSearch是由Java语言开发的,若要运行ElasticSearch,需要安装并配置JDK,并要设置$JAVA_HOME环境变量. ...
- CCPC2019江西省赛-Problem G.Traffic
题目描述: /*纯手打题面*/ Avin is observing the cars at a crossroads.He finds that there are n cars running in ...