Google Cardboard with Unity 5 的单对象 3D 查看器

Single object 3D viewer for Google Cardboard with Unity 5

我正在尝试重新创建 Google Cardboard 应用的 'Exhibit' 演示的功能。即从各个侧面查看单个物体 - 向上看,你会看到物体下方,向下看,你会从上方看到它,向左或向右看,你会从侧面看到它,然后再向后看。

我已经尝试了很多方法,例如将对象设为相机的子对象,以及使用 transform.LookAt(target); 使相机聚焦在对象上,但它不起作用。

Unity5 的新手,如有任何帮助,我们将不胜感激。


更新

使用 SmoothMouseLook 脚本中的代码 (http://pastebin.com/vMFkZJAm) 这是我迄今为止最接近的代码,但它并没有真正发挥作用,而且感觉太过 'out of control'(对象一直在旋转而不是比平稳转动进行检查)并且比 'Exhibit' 演示更不可预测。我的猜测是我把事情复杂化了。有人有什么想法吗?...

在相机("Main Camera")上附上这个以保持对物体的聚焦:

 using UnityEngine;
 using System.Collections;

 public class LookAt : MonoBehaviour {
     public Transform target;

     void Update () {
         transform.LookAt(target);
     }
 }

在对象上,附加此脚本:

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


public class SmoothMouseLook : MonoBehaviour
{   
    /*
     This script is used to average the mouse input over x 
     amount of frames in order to create a smooth mouselook.
     */

    //Mouse look sensitivity    
    public float sensitivityX = 1f;
    public float sensitivityY = 1f;


    //Default mouse sensitivity
    public float defaultSensX = 1f;
    public float defaultSensY = 1f;


    //Minimum angle you can look up
    public float minimumY = -60f;
    public float maximumY = 60f;

    //Minimum angle you can look up
    public float minimumX = -60f;
    public float maximumX = 60f;


    //Number of frames to be averaged, used for smoothing mouselook
    public int frameCounterX = 35;
    public int frameCounterY = 35;


    //Mouse rotation input
    private float rotationX = 0f;
    private float rotationY = 0f;


    //Used to calculate the rotation of this object
    private Quaternion xQuaternion;
    private Quaternion yQuaternion;
    private Quaternion originalRotation;


    //Array of rotations to be averaged
    private List<float> rotArrayX = new List<float> ();
    private List<float> rotArrayY = new List<float> ();


    void Start ()
    {
        //Lock/Hide cursor

        if (GetComponent<Rigidbody>())      
            GetComponent<Rigidbody>().freezeRotation = true;


        originalRotation = transform.localRotation;

    }


    void FixedUpdate () 
    {

        //Mouse/Camera Movement Smoothing:    
        //Average rotationX for smooth mouselook

        float rotAverageX = 0f;
        //rotationX += Camera.main.transform.eulerAngles.x * sensitivityX;
        //rotationX += Cardboard.SDK.HeadRotation.eulerAngles.x * sensitivityX;
        rotationX += Cardboard.SDK.HeadPose.Orientation.x * sensitivityX;

        rotationX = ClampAngle (rotationX, minimumX, maximumX);

        //Add the current rotation to the array, at the last position
        rotArrayX.Add (rotationX);

        //Reached max number of steps?  Remove the oldest rotation from the array
        if (rotArrayX.Count >= frameCounterX) {

            rotArrayX.RemoveAt (0);

        }

        //Add all of these rotations together
        for (int i_counterX = 0; i_counterX < rotArrayX.Count; i_counterX++) {
            //Loop through the array
            rotAverageX += rotArrayX[i_counterX];
        }


        //Now divide by the number of rotations by the number of elements to get the average
        rotAverageX /= rotArrayX.Count;


        //Average rotationY, same process as above
        float rotAverageY = 0;
        //rotationY += Camera.main.transform.eulerAngles.y * sensitivityY;
        //rotationY += Cardboard.SDK.HeadRotation.eulerAngles.y * sensitivityY;
        rotationY += Cardboard.SDK.HeadPose.Orientation.y * sensitivityY;

        rotationY = ClampAngle (rotationY, minimumY, maximumY);
        rotArrayY.Add (rotationY);


        if (rotArrayY.Count >= frameCounterY) {
            rotArrayY.RemoveAt (0);
        }


        for (int i_counterY = 0; i_counterY < rotArrayY.Count; i_counterY++) {  
            rotAverageY += rotArrayY[i_counterY];
        }


        rotAverageY /= rotArrayY.Count;


        //Apply and rotate this object
        xQuaternion = Quaternion.AngleAxis (rotAverageX, Vector3.up);
        yQuaternion = Quaternion.AngleAxis (rotAverageY, Vector3.left);

        transform.localRotation = originalRotation * xQuaternion * yQuaternion;

    }



    private float ClampAngle (float angle, float min, float max)
    {
        if (angle < -360f)  
            angle += 360f;
        if (angle > 360f)
            angle -= 360f;

        return Mathf.Clamp (angle, min, max);

    }
}

对于该特定用例,您不需要脚本。假设您使用的是 CardboardMain 预制件,请执行以下操作:

  • 将对象放在原点,CardboardMain 也放在原点。
  • 在纸板设置中,将颈部模型比例设置为 0。
  • 打开 Head 对象下的 CardboardMain 和 select Main Camera。
  • 将其 Transform Position Z 值设置为负值(远到足以看到对象)。

(您可以将其视为 "selfie-stick" 相机型号。)