亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

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

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

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

昌宁县| 区。| 长顺县| 盐山县| 临夏县| 克什克腾旗| 绥滨县| 佛教| 廊坊市| 南丹县| 耒阳市| 盱眙县| 甘孜| 金乡县| 新郑市| 锦屏县| 弋阳县| 正安县| 交城县| 宁都县| 重庆市| 新巴尔虎左旗| 石棉县| 桃园县| 平乡县| 无棣县| 南召县| 大渡口区| 高唐县| 上高县| 绥芬河市| 武宁县| 磴口县| 志丹县| 宜昌市| 南昌县| 通州区| 治多县| 丰城市| 于田县| 安阳县|