Unity做360度的全景照片
这里推荐两种方法,第一种是用鼠标滑动,第二种是用手机的陀螺仪进行全景查看
第一种:
1、使用Cinema4D,创建Sphere,修改分段为60,导出为Sphere.FBX;
2、Unity新建材质Material,将材质赋予Sphere,设置Sphere比例为10,设置材质的Shader类型为:Mobile/particles/Alpha Blended,然后将做好的全景图拖到材质Texture上,修改全景图MaxSize 为8192;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | using UnityEngine; using System.Collections; using UnityEngine.UI; public class GyroController_: MonoBehaviour { public float moveSpeed = 1; //物体旋转速度 public GameObject target; private Vector2 oldPosition; private Vector2 oldPosition1; private Vector2 oldPosition2; private float distance = 0; private bool flag = false ; //摄像头的位置 private float x = 0f; private float y = 0f; //左右滑动移动速度 public float xSpeed = 250f; public float ySpeed = 120f; //缩放限制系数 public float yMinLimit = -360; public float yMaxLimit = 360; //是否旋转 private bool isRotate = true ; //计数器 private float count = 0; //初始化游戏信息设置 void Start() { Vector3 angles = transform.eulerAngles; x = angles.y; y = angles.x; if (GetComponent<Rigidbody>()) GetComponent<Rigidbody>().freezeRotation = true ; } // Update is called once per frame void Update() { if (isRotate) { target.transform.Rotate(Vector3.down, Time.deltaTime * moveSpeed, Space.World); } if (!isRotate) { count += Time.deltaTime; if (count > 5) { count = 0; isRotate = true ; } } //触摸类型为移动触摸 if (Input.GetMouseButton(0)) { //根据触摸点计算X与Y位置 x += Input.GetAxis( "Mouse X" ) * xSpeed * Time.deltaTime; y -= Input.GetAxis( "Mouse Y" ) * ySpeed * Time.deltaTime; isRotate = false ; } //判断鼠标滑轮是否输入 float temp = Input.GetAxis( "Mouse ScrollWheel" ); if (temp != 0) { if (temp > 0) { // 这里的数据是根据我项目中的模型而调节的,大家可以自己任意修改 if (distance > -15) { distance -= 0.5f; } } if (temp < 0) { // 这里的数据是根据我项目中的模型而调节的,大家可以自己任意修改 if (distance < 20) { distance += 0.5f; } } } } //计算距离,判断放大还是缩小。放大返回true,缩小返回false bool IsEnlarge(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2) { //old distance float oldDistance = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y)); //new distance float newDistance = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y)); if (oldDistance < newDistance) { //zoom+ return true ; } else { //zoom- return false ; } } //每帧执行,在Update后 void LateUpdate() { if (target) { //重置摄像机的位置 y = ClampAngle(y, yMinLimit, yMaxLimit); var rotation = Quaternion.Euler(y, x, 0); var position = rotation * ( new Vector3(0.0f, 0.0f, -distance)) + target.transform.position; transform.rotation = rotation; transform.position = position; } } float ClampAngle( float angle, float min, float max) { if (angle < -360) angle += 360; if (angle > 360) angle -= 360; return Mathf.Clamp(angle, min, max); } } |
第二种:目前我没有测试,手上没有安卓机,有条件的可以进行测试一下。
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 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | using UnityEngine; /// <summary> /// Gyroscope controller that works with any device orientation. /// </summary> public class GyroController : MonoBehaviour { #region [Private fields] private bool gyroEnabled = true ; private const float lowPassFilterFactor = 0.2f; 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(); } protected void Update() { if (!gyroEnabled) return ; transform.rotation = Quaternion.Slerp(transform.rotation, cameraBase * ( ConvertRotation(referanceRotation * Input.gyro.attitude) * GetRotFix()), lowPassFilterFactor); } protected void OnGUI() { if (!debug) return ; GUILayout.Label( "Orientation: " + Screen.orientation); GUILayout.Label( "Calibration: " + calibration); GUILayout.Label( "Camera base: " + cameraBase); GUILayout.Label( "input.gyro.attitude: " + Input.gyro.attitude); GUILayout.Label( "transform.rotation: " + transform.rotation); if (GUILayout.Button( "On/off gyro: " + Input.gyro.enabled, GUILayout.Height(100))) { Input.gyro.enabled = !Input.gyro.enabled; } if (GUILayout.Button( "On/off gyro controller: " + gyroEnabled, GUILayout.Height(100))) { if (gyroEnabled) { DetachGyro(); } else { AttachGyro(); } } if (GUILayout.Button( "Update gyro calibration (Horizontal only)" , GUILayout.Height(80))) { UpdateCalibration( true ); } if (GUILayout.Button( "Update camera base rotation (Horizontal only)" , GUILayout.Height(80))) { UpdateCameraBaseRotation( true ); } if (GUILayout.Button( "Reset base orientation" , GUILayout.Height(80))) { ResetBaseOrientation(); } if (GUILayout.Button( "Reset camera rotation" , GUILayout.Height(80))) { transform.rotation = Quaternion.identity; } } #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 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!