欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

1、打開Unity,創(chuàng)建一個新的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)建一個新的Camera相機(jī),并改變兩個相機(jī)的Viewport Rect屬性,以將屏幕均分,如圖:

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

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

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

相關(guān)文章

  • C#寫入XML文檔

    C#寫入XML文檔

    這篇文章介紹了C#寫入XML文檔的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • C#中的靜態(tài)字段double.Epsilon實(shí)例詳解

    C#中的靜態(tài)字段double.Epsilon實(shí)例詳解

    double.Epsilon 是C#中的一個靜態(tài)字段,表示 double 數(shù)據(jù)類型的最小可表示的正數(shù)值,這篇文章主要介紹了C#中的靜態(tài)字段double.Epsilon的相關(guān)知識,需要的朋友可以參考下
    2024-01-01
  • 動態(tài)webservice調(diào)用接口并讀取解析返回結(jié)果

    動態(tài)webservice調(diào)用接口并讀取解析返回結(jié)果

    webservice的 發(fā)布一般都是使用WSDL(web service descriptive language)文件的樣式來發(fā)布的,在WSDL文件里面,包含這個webservice暴露在外面可供使用的接口。今天我們來詳細(xì)討論下如何動態(tài)調(diào)用以及讀取解析返回結(jié)果
    2015-06-06
  • WinForm的延時加載控件概述

    WinForm的延時加載控件概述

    這篇文章主要介紹了WinForm的延時加載控件,很實(shí)用的技巧,在C#程序設(shè)計(jì)中有著比較廣泛的應(yīng)用,需要的朋友可以參考下
    2014-08-08
  • C#中神器類BlockingCollection的實(shí)現(xiàn)詳解

    C#中神器類BlockingCollection的實(shí)現(xiàn)詳解

    如果你想玩轉(zhuǎn)C#?里面多線程,工廠模式,生產(chǎn)者/消費(fèi)者,隊(duì)列等高級操作,就可以和我一起探索這個強(qiáng)大的線程安全提供阻塞和限制功能的C#神器類BlockingCollection吧
    2023-02-02
  • C#調(diào)用存儲過程詳解(帶返回值、參數(shù)輸入輸出等)

    C#調(diào)用存儲過程詳解(帶返回值、參數(shù)輸入輸出等)

    這篇文章主要介紹了C#調(diào)用存儲過程的方法,結(jié)合實(shí)例形式詳細(xì)分析了各種常用的存儲過程調(diào)用方法,包括帶返回值、參數(shù)輸入輸出等,需要的朋友可以參考下
    2016-06-06
  • 詳解C#多線程之線程同步

    詳解C#多線程之線程同步

    本文主要介紹了C#線程同步的相關(guān)知識。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-01-01
  • C#通過流寫入數(shù)據(jù)到文件的方法

    C#通過流寫入數(shù)據(jù)到文件的方法

    這篇文章主要介紹了C#通過流寫入數(shù)據(jù)到文件的方法,涉及C#通過字節(jié)流讀寫文件的相關(guān)技巧,需要的朋友可以參考下
    2015-07-07
  • C#字體池技術(shù)實(shí)現(xiàn)代碼詳解

    C#字體池技術(shù)實(shí)現(xiàn)代碼詳解

    在本篇文章里小編給大家整理的是關(guān)于C#字體池技術(shù)實(shí)現(xiàn)代碼詳解內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。
    2019-11-11
  • 使用GPS經(jīng)緯度定位附近地點(diǎn)(某一點(diǎn)范圍內(nèi)查詢)

    使用GPS經(jīng)緯度定位附近地點(diǎn)(某一點(diǎn)范圍內(nèi)查詢)

    目前的工作是需要手機(jī)查找附近N米以內(nèi)的商戶,致想法是已知一個中心點(diǎn),一個半徑,求圓包含于圓拋物線里所有的點(diǎn),經(jīng)緯度是一個點(diǎn),半徑是一個距離,不能直接加減,下面提供C#的解決方法
    2013-12-12

最新評論