Unity3D基于陀螺仪实现VR相机功能
Unity自带陀螺仪功能,今天就利用陀螺仪实现一个VR相机功能。步骤如下:
1、打开Unity,创建一个新的C#脚本GyroController.cs,并挂在MainCamera游戏对象上,如图:
代码如下:
using UnityEngine; using System.Collections; public class GyroController : MonoBehaviour { // Fields private readonly Quaternion baseIdentity = Quaternion.Euler(90f, 0f, 0f); private Quaternion baseOrientation = Quaternion.Euler(90f, 0f, 0f); private Quaternion baseOrientationRotationFix = Quaternion.identity; private Quaternion calibration = Quaternion.identity; private Quaternion cameraBase = Quaternion.identity; private bool debug = true; public static bool gyroAvaiable; private bool gyroEnabled = true; private Quaternion gyroInitialRotation; public static bool gyroOff; private Quaternion initialRotation; private readonly Quaternion landscapeLeft = Quaternion.Euler(0f, 0f, -90f); private readonly Quaternion landscapeRight = Quaternion.Euler(0f, 0f, 90f); private const float lowPassFilterFactor = 0.1f; private Quaternion offsetRotation; private Quaternion referanceRotation = Quaternion.identity; private readonly Quaternion upsideDown = Quaternion.Euler(0f, 0f, 180f); // Methods private void AttachGyro() { this.gyroEnabled = true; this.ResetBaseOrientation(); this.UpdateCalibration(true); this.UpdateCameraBaseRotation(true); this.RecalculateReferenceRotation(); } private void Awake() { gyroAvaiable = SystemInfo.supportsGyroscope; } private static Quaternion ConvertRotation(Quaternion q) { return new Quaternion(q.x, q.y, -q.z, -q.w); } private void DetachGyro() { this.gyroEnabled = false; } private Quaternion GetRotFix() { return Quaternion.identity; } private void RecalculateReferenceRotation() { this.referanceRotation = Quaternion.Inverse(this.baseOrientation) * Quaternion.Inverse(this.calibration); } private void ResetBaseOrientation() { this.baseOrientationRotationFix = this.GetRotFix(); this.baseOrientation = this.baseOrientationRotationFix * this.baseIdentity; } protected void Start() { Input.gyro.enabled = true; base.enabled = true; this.AttachGyro(); this.initialRotation = base.transform.localRotation; this.gyroInitialRotation = Input.gyro.attitude; } private void Update() { gyroOff = PlayerPrefs.GetInt("gyro-off") == 1; if (this.gyroEnabled ) { base.transform.localRotation = Quaternion.Slerp(base.transform.localRotation, this.cameraBase * (ConvertRotation(this.referanceRotation * Input.gyro.attitude) * this.GetRotFix()), 0.5f);//0.1f } } private void UpdateCalibration(bool onlyHorizontal) { if (onlyHorizontal) { Vector3 toDirection = (Vector3) (Input.gyro.attitude * -Vector3.forward); toDirection.z = 0f; if (toDirection == Vector3.zero) { this.calibration = Quaternion.identity; } else { this.calibration = Quaternion.FromToRotation((Vector3) (this.baseOrientationRotationFix * Vector3.up), toDirection); } } else { this.calibration = Input.gyro.attitude; } } private void UpdateCameraBaseRotation(bool onlyHorizontal) { if (onlyHorizontal) { Vector3 forward = base.transform.forward; forward.y = 0f; if (forward == Vector3.zero) { this.cameraBase = Quaternion.identity; } else { this.cameraBase = Quaternion.FromToRotation(Vector3.forward, forward); } } else { this.cameraBase = base.transform.rotation; } } }
2.在相机MainCamera下创建一个新的Camera相机,并改变两个相机的Viewport Rect属性,以将屏幕均分,如图:
3.在场景中创建一个Cube,效果如图:
4.保存场景,打包成apk即可。即可使用手机陀螺仪控制相机旋转了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
您可能感兴趣的文章
- 01-10基于C#实现简单离线注册码生成与验证
- 01-10C#基于UDP实现的P2P语音聊天工具
- 01-10C#实现基于加减按钮形式控制系统音量及静音的方法
- 01-10C#基于WebBrowser获取cookie的实现方法
- 01-10C#基于委托实现多线程之间操作的方法
- 01-10Unity3d获取系统时间
- 01-10Unity3D获取当前键盘按键及Unity3D鼠标、键盘的基本操作
- 01-10基于C#对用户密码使用MD5加密与解密
- 01-10基于C#实现简单的随机抽奖小程序
- 01-10C#基于cookie实现的购物车功能
阅读排行
本栏相关
- 01-10C#通过反射获取当前工程中所有窗体并
- 01-10关于ASP网页无法打开的解决方案
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10C#实现txt定位指定行完整实例
- 01-10WinForm实现仿视频播放器左下角滚动新
- 01-10C#停止线程的方法
- 01-10C#实现清空回收站的方法
- 01-10C#通过重写Panel改变边框颜色与宽度的
- 01-10C#实现读取注册表监控当前操作系统已
随机阅读
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 01-10delphi制作wav文件的方法
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-10C#中split用法实例总结
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-11ajax实现页面的局部加载
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 04-02jquery与jsp,用jquery
- 01-10SublimeText编译C开发环境设置