91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Unity3D基于陀螺儀實現VR相機功能的方法

發布時間:2020-08-04 09:22:56 來源:億速云 閱讀:210 作者:小豬 欄目:編程語言

這篇文章主要講解了Unity3D基于陀螺儀實現VR相機功能的方法,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。

Unity自帶陀螺儀功能,今天就利用陀螺儀實現一個VR相機功能。步驟如下:

1、打開Unity,創建一個新的C#腳本GyroController.cs,并掛在MainCamera游戲對象上,如圖:

Unity3D基于陀螺儀實現VR相機功能的方法

代碼如下:

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

Unity3D基于陀螺儀實現VR相機功能的方法

3、在場景中創建一個Cube,效果如圖:

Unity3D基于陀螺儀實現VR相機功能的方法

4、保存場景,打包成apk即可。即可使用手機陀螺儀控制相機旋轉了。

看完上述內容,是不是對Unity3D基于陀螺儀實現VR相機功能的方法有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

沂水县| 石嘴山市| 剑阁县| 武川县| 收藏| 青冈县| 霸州市| 介休市| 襄汾县| 灵寿县| 新泰市| 五常市| 甘肃省| 来宾市| 读书| 荆门市| 华亭县| 象山县| 永川市| 乌鲁木齐县| 陵水| 永和县| 临颍县| 墨脱县| 靖西县| 汉源县| 那曲县| 庆阳市| 西畴县| 宜君县| 江北区| 三台县| 南康市| 漳州市| 南皮县| 新和县| 于都县| 元谋县| 沛县| 柞水县| 安义县|