Unity做360度的全景照片

这里推荐两种方法,第一种是用鼠标滑动,第二种是用手机的陀螺仪进行全景查看

第一种:

1、新建一Sphere,然后为其赋予材质,注意材质的Shader类型为:Mobile/particles/Alpha Blended,然后将做好的全景图贴上去

2、在摄像机上附加以下脚本:代码来源参考自

  1 using UnityEngine;
  2 using System.Collections;
  3 using UnityEngine.UI;
  4 
  5 public class GyroController_01 : MonoBehaviour
  6 {
  7     public float moveSpeed = 1;//物体旋转速度  
  8     public GameObject target;
  9 
 10     private Vector2 oldPosition;
 11     private Vector2 oldPosition1;
 12     private Vector2 oldPosition2;
 13 
 14 
 15     private float distance = 0;
 16     private bool flag = false;
 17     //摄像头的位置
 18     private float x = 0f;
 19     private float y = 0f;
 20     //左右滑动移动速度
 21     public float xSpeed = 250f;
 22     public float ySpeed = 120f;
 23     //缩放限制系数
 24     public float yMinLimit = -360;
 25     public float yMaxLimit = 360;
 26     //是否旋转
 27     private bool isRotate = true;
 28     //计数器
 29     private float count = 0;
 30 
 31     //初始化游戏信息设置
 32     void Start()
 33     {
 34         Vector3 angles = transform.eulerAngles;
 35         x = angles.y;
 36         y = angles.x;
 37         if (GetComponent<Rigidbody>())
 38             GetComponent<Rigidbody>().freezeRotation = true;
 39     }
 40 
 41 
 42 
 43     // Update is called once per frame  
 44     void Update()
 45     {
 46 
 47         if (isRotate)
 48         {
 49 
 50             target.transform.Rotate(Vector3.down, Time.deltaTime * moveSpeed, Space.World);
 51 
 52         }
 53         if (!isRotate)
 54         {
 55             count += Time.deltaTime;
 56             if (count > 5)
 57             {
 58                 count = 0;
 59                 isRotate = true;
 60             }
 61         }
 62 
 63         //触摸类型为移动触摸
 64         if (Input.GetMouseButton(0))
 65         {
 66             //根据触摸点计算X与Y位置
 67             x += Input.GetAxis("Mouse X") * xSpeed * Time.deltaTime;
 68             y -= Input.GetAxis("Mouse Y") * ySpeed * Time.deltaTime;
 69             isRotate = false;
 70         }
 71         //判断鼠标滑轮是否输入
 72         float temp = Input.GetAxis("Mouse ScrollWheel");
 73         if (temp != 0)
 74         {
 75             if (temp > 0)
 76             {
 77                 // 这里的数据是根据我项目中的模型而调节的,大家可以自己任意修改
 78                 if (distance > -15)
 79                 {
 80                     distance -= 0.5f;
 81                 }
 82             }
 83             if (temp < 0)
 84             {
 85                 // 这里的数据是根据我项目中的模型而调节的,大家可以自己任意修改
 86                 if (distance < 20)
 87                 {
 88                     distance += 0.5f;
 89                 }
 90             }
 91         }
 92 
 93     }
 94 
 95     //计算距离,判断放大还是缩小。放大返回true,缩小返回false  
 96     bool IsEnlarge(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2)
 97     {
 98         //old distance  
 99         float oldDistance = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
100         //new distance  
101         float newDistance = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));
102 
103         if (oldDistance < newDistance)
104         {
105             //zoom+  
106             return true;
107         }
108         else
109         {
110             //zoom-  
111             return false;
112         }
113     }
114 
115     //每帧执行,在Update后  
116     void LateUpdate()
117     {
118         if (target)
119         {
120             //重置摄像机的位置
121             y = ClampAngle(y, yMinLimit, yMaxLimit);
122             var rotation = Quaternion.Euler(y, x, 0);
123             var position = rotation * (new Vector3(0.0f, 0.0f, -distance)) + target.transform.position;
124 
125             transform.rotation = rotation;
126             transform.position = position;
127         }
128     }
129     float ClampAngle(float angle, float min, float max)
130     {
131         if (angle < -360)
132             angle += 360;
133         if (angle > 360)
134             angle -= 360;
135         return Mathf.Clamp(angle, min, max);
136 
137     }
138 
139 }

3、然后在游戏运行的时候左右、上下滑动鼠标即可,也可以滚动鼠标滚轮

第二种:目前我没有测试,手上没有安卓机,有条件的可以进行测试一下。

1、首先我们先了解移动端手机陀螺仪的向量方向。

Unity中重力感应的取值范围时 -1.0~1.0
X轴:home按键在下手机面朝天
        向右旋转90度重力分量为1.0
        向左旋转90度重力分量为-1.0
        
Y轴:Home按键在上手机背面朝自己重力分量为1.0
     Home按键在下手机面朝自己重力分量为-1.0
    
Z轴:手机面朝地面重力分量为1.0
     手机面朝天空重力分量为1.0

                    

2、新建一Sphere,然后为其赋予材质,注意材质的Shader类型为:Mobile/particles/Alpha Blended,然后将做好的全景图贴上去

3、在摄像机上附加以下脚本,并将相机作为Sphere的子物体即可 代码来源参考自

  1 // ***********************************************************
  2 // Written by Heyworks Unity Studio http://unity.heyworks.com/
  3 // ***********************************************************
  4 using UnityEngine;
  5    
  6 /// <summary>
  7 /// Gyroscope controller that works with any device orientation.
  8 /// </summary>
  9 public class GyroController : MonoBehaviour 
 10 {
 11     #region [Private fields]
 12    
 13     private bool gyroEnabled = true;
 14     private const float lowPassFilterFactor = 0.2f;
 15    
 16     private readonly Quaternion baseIdentity =  Quaternion.Euler(90, 0, 0);
 17     private readonly Quaternion landscapeRight =  Quaternion.Euler(0, 0, 90);
 18     private readonly Quaternion landscapeLeft =  Quaternion.Euler(0, 0, -90);
 19     private readonly Quaternion upsideDown =  Quaternion.Euler(0, 0, 180);
 20        
 21     private Quaternion cameraBase =  Quaternion.identity;
 22     private Quaternion calibration =  Quaternion.identity;
 23     private Quaternion baseOrientation =  Quaternion.Euler(90, 0, 0);
 24     private Quaternion baseOrientationRotationFix =  Quaternion.identity;
 25    
 26     private Quaternion referanceRotation = Quaternion.identity;
 27     private bool debug = true;
 28    
 29     #endregion
 30    
 31     #region [Unity events]
 32    
 33     protected void Start () 
 34     {
 35         AttachGyro();
 36     }
 37    
 38     protected void Update() 
 39     {
 40         if (!gyroEnabled)
 41             return;
 42         transform.rotation = Quaternion.Slerp(transform.rotation,
 43             cameraBase * ( ConvertRotation(referanceRotation * Input.gyro.attitude) * GetRotFix()), lowPassFilterFactor);
 44     }
 45 
 46     protected void OnGUI()
 47     {
 48         if (!debug)
 49             return;
 50         GUILayout.Label("Orientation: " + Screen.orientation);
 51         GUILayout.Label("Calibration: " + calibration);
 52         GUILayout.Label("Camera base: " + cameraBase);
 53         GUILayout.Label("input.gyro.attitude: " + Input.gyro.attitude);
 54         GUILayout.Label("transform.rotation: " + transform.rotation);
 55 
 56         if (GUILayout.Button("On/off gyro: " + Input.gyro.enabled, GUILayout.Height(100)))
 57         {
 58             Input.gyro.enabled = !Input.gyro.enabled;
 59         }
 60 
 61         if (GUILayout.Button("On/off gyro controller: " + gyroEnabled, GUILayout.Height(100)))
 62         {
 63             if (gyroEnabled)
 64             {
 65                 DetachGyro();
 66             }
 67             else
 68             {
 69                 AttachGyro();
 70             }
 71         }
 72 
 73         if (GUILayout.Button("Update gyro calibration (Horizontal only)", GUILayout.Height(80)))
 74         {
 75             UpdateCalibration(true);
 76         }
 77 
 78         if (GUILayout.Button("Update camera base rotation (Horizontal only)", GUILayout.Height(80)))
 79         {
 80             UpdateCameraBaseRotation(true);
 81         }
 82 
 83         if (GUILayout.Button("Reset base orientation", GUILayout.Height(80)))
 84         {
 85             ResetBaseOrientation();
 86         }
 87 
 88         if (GUILayout.Button("Reset camera rotation", GUILayout.Height(80)))
 89         {
 90             transform.rotation = Quaternion.identity;
 91         }
 92     }
 93 
 94     #endregion
 95 
 96     #region [Public methods]
 97 
 98     /// <summary>
 99     /// Attaches gyro controller to the transform.
100     /// </summary>
101     private void AttachGyro()
102     {
103         gyroEnabled = true;
104         ResetBaseOrientation();
105         UpdateCalibration(true);
106         UpdateCameraBaseRotation(true);
107         RecalculateReferenceRotation();
108     }
109    
110     /// <summary>
111     /// Detaches gyro controller from the transform
112     /// </summary>
113     private void DetachGyro()
114     {
115         gyroEnabled = false;
116     }
117    
118     #endregion
119    
120     #region [Private methods]
121    
122     /// <summary>
123     /// Update the gyro calibration.
124     /// </summary>
125     private void UpdateCalibration(bool onlyHorizontal)
126     {
127         if (onlyHorizontal)
128         {
129             var fw = (Input.gyro.attitude) * (-Vector3.forward);
130             fw.z = 0;
131             if (fw == Vector3.zero)
132             {
133                 calibration = Quaternion.identity;
134             }
135             else
136             {
137                 calibration = (Quaternion.FromToRotation(baseOrientationRotationFix * Vector3.up, fw));
138             }
139         }
140         else
141         {
142             calibration = Input.gyro.attitude;
143         }
144     }
145        
146     /// <summary>
147     /// Update the camera base rotation.
148     /// </summary>
149     /// <param name='onlyHorizontal'>
150     /// Only y rotation.
151     /// </param>
152     private void UpdateCameraBaseRotation(bool onlyHorizontal)
153     {
154         if (onlyHorizontal)
155         {
156             var fw = transform.forward;
157             fw.y = 0;
158             if (fw == Vector3.zero)
159             {
160                 cameraBase = Quaternion.identity;
161             }
162             else
163             {
164                 cameraBase = Quaternion.FromToRotation(Vector3.forward, fw);
165             }
166         }
167         else
168         {
169             cameraBase = transform.rotation;
170         }
171     }
172        
173     /// <summary>
174     /// Converts the rotation from right handed to left handed.
175     /// </summary>
176     /// <returns>
177     /// The result rotation.
178     /// </returns>
179     /// <param name='q'>
180     /// The rotation to convert.
181     /// </param>
182     private static Quaternion ConvertRotation(Quaternion q)
183     {
184         return new Quaternion(q.x, q.y, -q.z, -q.w);    
185     }
186        
187     /// <summary>
188     /// Gets the rot fix for different orientations.
189     /// </summary>
190     /// <returns>
191     /// The rot fix.
192     /// </returns>
193     private Quaternion GetRotFix()
194     {
195 #if UNITY_3_5
196         if (Screen.orientation == ScreenOrientation.Portrait)
197             return Quaternion.identity;
198            
199         if (Screen.orientation == ScreenOrientation.LandscapeLeft || Screen.orientation == ScreenOrientation.Landscape)
200             return landscapeLeft;
201                    
202         if (Screen.orientation == ScreenOrientation.LandscapeRight)
203             return landscapeRight;
204                    
205         if (Screen.orientation == ScreenOrientation.PortraitUpsideDown)
206             return upsideDown;
207         return Quaternion.identity;
208 #else
209         return Quaternion.identity;
210 #endif
211     }
212        
213     /// <summary>
214     /// Recalculates reference system.
215     /// </summary>
216     private void ResetBaseOrientation()
217     {
218         baseOrientationRotationFix = GetRotFix();
219         baseOrientation = baseOrientationRotationFix * baseIdentity;
220     }
221    
222     /// <summary>
223     /// Recalculates reference rotation.
224     /// </summary>
225     private void RecalculateReferenceRotation()
226     {
227         referanceRotation = Quaternion.Inverse(baseOrientation)*Quaternion.Inverse(calibration);
228     }
229    
230     #endregion
231 }
原文地址:https://www.cnblogs.com/zhh19981104/p/9692184.html