《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---运动追踪与输入设备
运动追踪设备 第二种可以使人脑相信它真实处于虚拟世界的关键技术就是运动追踪技术,它可以通过追踪头部的运动状态实时更新渲染的场景.这与我们在真实世界中观看周围非常类似. 高速的惯性测量单元( ...
随机推荐
- linux环境下使用百度云网盘
linux下经常需要备份一些文件到云端,现在能用的也就只有度娘的百度云网盘了,在github上发现一个挺好的项目,bypy,用来在linux下使用百度云. 项目地址:https://github.co ...
- java基础第十三篇之Collection
常见的几种数据结构: * 1.堆栈:先进后出 * 2.队列:先进先出 * 3.数组:查找快,增删慢 * 4.链表:查找慢,增删快 import java.util.LinkedList; /* * ...
- 移动IM开发指南2:心跳指令详解
<移动IM开发指南>系列文章将会介绍一个IM APP的方方面面,包括技术选型.登陆优化等.此外,本文作者会结合他在网易云信多年iOS IM SDK开发的经验,深度分析实际开发中的各种常见问 ...
- javascript中中文转码的方法
js对文字进行编码涉及3个函数: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent ...
- Matplotlib快速入门
Matplotlib 可能还有小伙伴不知道Matplotlib是什么,下面是维基百科的介绍. Matplotlib 是Python编程语言的一个绘图库及其数值数学扩展 NumPy.它为利用通用的图形用 ...
- Jenkins+Python+GitLab持续集成
创建任务 登录Jenkins,点击左侧列表的新建选项.输入任务名称,选择构建一个自由风格的软件项目,点击确定. 配置 在任务配置界面,可以设置General标签中的丢弃旧的构建选项,设置保持构建的天数 ...
- 24 | 紧跟时代步伐:微服务模式下API测试要怎么做?
- 17 | 精益求精:聊聊提高GUI测试稳定性的关键技术
- mysql计算日期之间相差的天数
TO_DAYS(NOW()) - TO_DAYS(createTime) as dayFactor,
- 前端笔记之React(四)生命周期&Virtual DOM和Diff算法&日历组件开发
一.React生命周期 一个组件从出生到消亡,在各个阶段React提供给我们调用的接口,就是生命周期. 生命周期这个东西,必须有项目,才知道他们干嘛的. 1.1 Mouting阶段[装载过程] 这个阶 ...