成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

Unity3D基于陀螺儀實(shí)現(xiàn)VR相機(jī)功能的方法

這篇文章主要講解了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:

Unity3D基于陀螺儀實(shí)現(xiàn)VR相機(jī)功能的方法

代碼如下:

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屬性,以將屏幕均分,如圖:

Unity3D基于陀螺儀實(shí)現(xiàn)VR相機(jī)功能的方法

3、在場(chǎng)景中創(chuàng)建一個(gè)Cube,效果如圖:

Unity3D基于陀螺儀實(shí)現(xiàn)VR相機(jī)功能的方法

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)

微信小程序開(kāi)發(fā)
芮城县| 曲麻莱县| 昆山市| 惠水县| 汾西县| 封丘县| 文安县| 喀喇沁旗| 金阳县| 德江县| 田东县| 宜丰县| 三门峡市| 德安县| 汽车| 赫章县| 鹤山市| 读书| 旬邑县| 阳山县| 呼伦贝尔市| 靖边县| 高州市| 崇信县| 六盘水市| 万年县| 嘉禾县| 留坝县| 沙湾县| 江阴市| 肇庆市| 隆回县| 广南县| 镇坪县| 柯坪县| 和田市| 辉南县| 黑山县| 大港区| 维西| 庄河市|