调用手机陀螺仪

在制作移动端VR应用的时候,基本上都会用到手机陀螺仪数据的采集。这里我之前一朋友给了我一个他从网上找到的脚本,如果侵犯到哪位大佬的原创了,请联系我将其删掉。


using System.Collections;
using System.Collections.Generic;
using UnityEngine;



/// <summary>

/// Gyroscope controller that works with any device orientation.

///此脚本是陀螺仪控制脚本,使用时放到需要响应陀螺仪的GameObject就可以 

/// </summary>

public class GyroController : MonoBehaviour
{

    #region [Private fields]



    private bool gyroEnabled = true;

   private const float lowPassFilterFactor = 0.2f;
    // private const float lowPassFilterFactor = 0.5f;



    private readonly Quaternion baseIdentity = Quaternion.Euler(90, 0, 0);

/*    private readonly Quaternion landscapeRight = Quaternion.Euler(0, 0, 90);*/

/*    private readonly Quaternion landscapeLeft = Quaternion.Euler(0, 0, -90);*/

/*    private readonly Quaternion upsideDown = Quaternion.Euler(0, 0, 180);*/



    private Quaternion cameraBase = Quaternion.identity;

    private Quaternion calibration = Quaternion.identity;

    private Quaternion baseOrientation = Quaternion.Euler(90, 0, 0);

    private Quaternion baseOrientationRotationFix = Quaternion.identity;



    private Quaternion referanceRotation = Quaternion.identity;

/*    private bool debug = true;*/ 



    #endregion



    #region [Unity events]



    protected void Start()
    {

        AttachGyro();
        Input.gyro.enabled = true;
    }



    protected void Update()
    {

        if (!gyroEnabled)

            return;

        transform.rotation = Quaternion.Slerp(transform.rotation,

            cameraBase * (ConvertRotation(referanceRotation * Input.gyro.attitude) * GetRotFix()),lowPassFilterFactor);
       

    }

    #endregion



    #region [Public methods]



    /// <summary>

    /// Attaches gyro controller to the transform.

    /// </summary>

    private void AttachGyro()
    {

        gyroEnabled = true;

        ResetBaseOrientation();

        UpdateCalibration(true);

        UpdateCameraBaseRotation(true);

        RecalculateReferenceRotation();

    }



    /// <summary>

    /// Detaches gyro controller from the transform

    /// </summary>

    private void DetachGyro()
    {

        gyroEnabled = false;

    }



    #endregion



    #region [Private methods]



    /// <summary>

    /// Update the gyro calibration.

    /// </summary>

    private void UpdateCalibration(bool onlyHorizontal)
    {

        if (onlyHorizontal)
        {

            var fw = (Input.gyro.attitude) * (-Vector3.forward);

            fw.z = 0;

            if (fw == Vector3.zero)
            {

                calibration = Quaternion.identity;

            }

            else
            {

                calibration = (Quaternion.FromToRotation(baseOrientationRotationFix * Vector3.up, fw));

            }

        }

        else
        {

            calibration = Input.gyro.attitude;

        }

    }



    /// <summary>

    /// Update the camera base rotation.

    /// </summary>

    /// <param name='onlyHorizontal'>

    /// Only y rotation.

    /// </param>

    private void UpdateCameraBaseRotation(bool onlyHorizontal)
    {

        if (onlyHorizontal)
        {

            var fw = transform.forward;

            fw.y = 0;

            if (fw == Vector3.zero)
            {

                cameraBase = Quaternion.identity;

            }

            else
            {

                cameraBase = Quaternion.FromToRotation(Vector3.forward, fw);

            }

        }

        else
        {

            cameraBase = transform.rotation;

        }

    }



    /// <summary>

    /// Converts the rotation from right handed to left handed.

    /// </summary>

    /// <returns>

    /// The result rotation.

    /// </returns>

    /// <param name='q'>

    /// The rotation to convert.

    /// </param>

    private static Quaternion ConvertRotation(Quaternion q)
    {

        return new Quaternion(q.x, q.y, -q.z, -q.w);

    }



    /// <summary>

    /// Gets the rot fix for different orientations.

    /// </summary>

    /// <returns>

    /// The rot fix.

    /// </returns>

    private Quaternion GetRotFix()
    {

#if UNITY_3_5

        if (Screen.orientation == ScreenOrientation.Portrait)

            return Quaternion.identity;

           

        if (Screen.orientation == ScreenOrientation.LandscapeLeft || Screen.orientation == ScreenOrientation.Landscape)

            return landscapeLeft;

                   

        if (Screen.orientation == ScreenOrientation.LandscapeRight)

            return landscapeRight;

                   

        if (Screen.orientation == ScreenOrientation.PortraitUpsideDown)

            return upsideDown;

        return Quaternion.identity;

#else

        return Quaternion.identity;

#endif

    }



    /// <summary>

    /// Recalculates reference system.

    /// </summary>

    private void ResetBaseOrientation()
    {

        baseOrientationRotationFix = GetRotFix();

        baseOrientation = baseOrientationRotationFix * baseIdentity;

    }



    /// <summary>

    /// Recalculates reference rotation.

    /// </summary>

    private void RecalculateReferenceRotation()
    {

        referanceRotation = Quaternion.Inverse(baseOrientation) * Quaternion.Inverse(calibration);

    }



    #endregion

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个小坑货

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值