最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2020年04月15日 08:53:25   作者:一縷殘陽  
這篇文章主要為大家詳細(xì)介紹了Unity3D基于陀螺儀實(shí)現(xiàn)VR相機(jī)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

Unity自帶陀螺儀功能,今天就利用陀螺儀實(shí)現(xiàn)一個(gè)VR相機(jī)功能。步驟如下:

1、打開Unity,創(chuàng)建一個(gè)新的C#腳本GyroController.cs,并掛在MainCamera游戲?qū)ο笊?,如圖:

代碼如下:

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、在場景中創(chuàng)建一個(gè)Cube,效果如圖:

4、保存場景,打包成apk即可。即可使用手機(jī)陀螺儀控制相機(jī)旋轉(zhuǎn)了。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#數(shù)據(jù)庫連接方式(類的形式)

    C#數(shù)據(jù)庫連接方式(類的形式)

    這篇文章主要介紹了C#數(shù)據(jù)庫連接方式(類的形式),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • C#實(shí)現(xiàn)自定義定時(shí)組件的方法

    C#實(shí)現(xiàn)自定義定時(shí)組件的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)自定義定時(shí)組件的方法,很實(shí)用的功能,需要的朋友可以參考下
    2014-08-08
  • C#中實(shí)現(xiàn)插入、刪除Excel分頁符的方法

    C#中實(shí)現(xiàn)插入、刪除Excel分頁符的方法

    這篇文章主要給大家介紹了關(guān)于在C#中實(shí)現(xiàn)插入、刪除Excel分頁符的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-05-05
  • WPF PasswordBox進(jìn)行數(shù)據(jù)綁定方法

    WPF PasswordBox進(jìn)行數(shù)據(jù)綁定方法

    有的時(shí)候會(huì)遇見PasswordBox的Password屬性不是依賴屬性,因此無法進(jìn)行數(shù)據(jù)綁定。本文介紹如何通過添加附加屬性解決該問題,有此問題的同學(xué)可以參考下本文
    2021-06-06
  • C#使用ZXing.Net實(shí)現(xiàn)識(shí)別二維碼和條碼

    C#使用ZXing.Net實(shí)現(xiàn)識(shí)別二維碼和條碼

    ZXing用Java實(shí)現(xiàn)的多種格式的一維二維條碼圖像處理庫,而ZXing.Net是其.Net版本的實(shí)現(xiàn),本文主要為大家詳細(xì)介紹了如何使用ZXing.Net實(shí)現(xiàn)識(shí)別二維碼和條碼,需要的可以參考下
    2024-01-01
  • 一文詳解C#中方法重載的底層玩法

    一文詳解C#中方法重載的底層玩法

    最近在看C++的方法重載,就在想C#中的重載底層是怎么玩的。畢竟很多朋友應(yīng)該知道C是不支持重載的。本文將來詳細(xì)講講C#中方法重載的底層玩法,感興趣的可以了解一下<BR>
    2022-06-06
  • 解析使用C# lock同時(shí)訪問共享數(shù)據(jù)

    解析使用C# lock同時(shí)訪問共享數(shù)據(jù)

    本篇文章是對使用C# lock同時(shí)訪問共享數(shù)據(jù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • 在C#中使用二叉樹實(shí)時(shí)計(jì)算海量用戶積分排名的實(shí)現(xiàn)詳解

    在C#中使用二叉樹實(shí)時(shí)計(jì)算海量用戶積分排名的實(shí)現(xiàn)詳解

    這篇文章主要介紹了在C#中使用二叉樹實(shí)時(shí)計(jì)算海量用戶積分排名的實(shí)現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • Visual C#類的定義及實(shí)現(xiàn)方法實(shí)例解析

    Visual C#類的定義及實(shí)現(xiàn)方法實(shí)例解析

    這篇文章主要介紹了Visual C#類的定義及實(shí)現(xiàn)方法實(shí)例解析,對于新手來說有不錯(cuò)的借鑒學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2014-07-07
  • WPF自定義實(shí)現(xiàn)雷達(dá)圖控件的示例詳解

    WPF自定義實(shí)現(xiàn)雷達(dá)圖控件的示例詳解

    雷達(dá)圖用于表示不同內(nèi)容的占比關(guān)系,在項(xiàng)目中有廣泛的應(yīng)用,但是目前未曾有封裝良好的雷達(dá)圖控件,所以本文分享了如何封裝一個(gè)通用的雷達(dá)圖控件,希望對大家有所幫助
    2023-08-08

最新評論

安图县| 日土县| 香格里拉县| 韶山市| 鄢陵县| 全椒县| 崇仁县| 吉木乃县| 万盛区| 大渡口区| 安吉县| 盱眙县| 雅安市| 寻甸| 徐水县| 新泰市| 雅安市| 定陶县| 鱼台县| 山东省| 河南省| 太保市| 泸水县| 柳河县| 永嘉县| 徐水县| 南丰县| 马龙县| 黄梅县| 芮城县| 昭觉县| 邵阳市| 团风县| 教育| 来宾市| 银川市| 永福县| 卓资县| 大田县| 馆陶县| 金寨县|