Unity3D基于陀螺仪实现VR相机功能
程序员文章站
2022-06-14 15:30:18
unity自带陀螺仪功能,今天就利用陀螺仪实现一个vr相机功能。步骤如下:
1、打开unity,创建一个新的c#脚本gyrocontroller.cs,并挂在maincamera游戏...
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即可。即可使用手机陀螺仪控制相机旋转了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。