這篇文章主要講解了Unity3D基于陀螺儀實(shí)現(xiàn)VR相機(jī)功能的方法,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。
成都創(chuàng)新互聯(lián)公司專(zhuān)注于海曙網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供海曙營(yíng)銷(xiāo)型網(wǎng)站建設(shè),海曙網(wǎng)站制作、海曙網(wǎng)頁(yè)設(shè)計(jì)、海曙網(wǎng)站官網(wǎng)定制、成都微信小程序服務(wù),打造海曙網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供海曙網(wǎng)站排名全網(wǎng)營(yíng)銷(xiāo)落地服務(wù)。
Unity自帶陀螺儀功能,今天就利用陀螺儀實(shí)現(xiàn)一個(gè)VR相機(jī)功能。步驟如下:
1、打開(kāi)Unity,創(chuàng)建一個(gè)新的C#腳本GyroController.cs,并掛在MainCamera游戲?qū)ο笊希鐖D:
代碼如下:
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、在相機(jī)MainCamera下創(chuàng)建一個(gè)新的Camera相機(jī),并改變兩個(gè)相機(jī)的Viewport Rect屬性,以將屏幕均分,如圖:
3、在場(chǎng)景中創(chuàng)建一個(gè)Cube,效果如圖:
4、保存場(chǎng)景,打包成apk即可。即可使用手機(jī)陀螺儀控制相機(jī)旋轉(zhuǎn)了。
看完上述內(nèi)容,是不是對(duì)Unity3D基于陀螺儀實(shí)現(xiàn)VR相機(jī)功能的方法有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文標(biāo)題:Unity3D基于陀螺儀實(shí)現(xiàn)VR相機(jī)功能的方法
分享地址:http://jinyejixie.com/article42/pdsdhc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、網(wǎng)站策劃、網(wǎng)站營(yíng)銷(xiāo)、微信公眾號(hào)、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)