unity实现透视相机与正交相机的平滑切换

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

public class viewchange : MonoBehaviour {

	public static viewchange Instance=null;

	/// <summary>
	/// 相机透视改变是否触发(调用只需把此值改为true)
	/// </summary>
	public bool ChangeProjection = false;
	private bool _changing = false;
	public float ProjectionChangeTime = 0.5f;
	private float _currentT = 0.0f;

	void Awake()
	{
		Instance=this;
	}

	private void Update () { ///检测,避免变换过程中发生混乱
		if (_changing) {
			ChangeProjection = false;
		} else if (ChangeProjection) {
			_changing = true;
			_currentT = 0.0f;
		}
	}
	/// <summary>
	/// Unity3D中Update和Lateupdate的区别。Lateupdate和Update每一祯都被执行,但是执行顺序不一样,先执行Updatee然后执行lateUpdate。
	///如果你有两个脚本JS1、JS2,两个脚本中都有Update()函数, 在JS1中有 lateUpdate ,JS2中没有。那么 lateUpdate 函数会等待JS1、JS2两个脚本的Update()函数 都执行完后才执行。
	/// </summary>
	private void LateUpdate () {
		if (!_changing) {
			return;
		}
		//将当前的 是否正视图值 赋值给currentlyOrthographic变量
		bool currentlyOrthographic = Camera.main.orthographic;
		//定义变量存放当前摄像机的透视和正视矩阵信息;
		Matrix4x4 orthoMat, persMat;
		if (currentlyOrthographic) //如果当前摄像机为正视状态
		{
			orthoMat = Camera.main.projectionMatrix;

			Camera.main.orthographic = false;
			Camera.main.ResetProjectionMatrix ();
			persMat = Camera.main.projectionMatrix;
		} else //否则当前摄像机为透视状态
		{
			persMat = Camera.main.projectionMatrix;

			Camera.main.orthographic = true;
			Camera.main.ResetProjectionMatrix ();
			orthoMat = Camera.main.projectionMatrix;
		}
		Camera.main.orthographic = currentlyOrthographic;

		_currentT += (Time.deltaTime / ProjectionChangeTime);
		if (_currentT < 1.0f) {
			if (currentlyOrthographic) {
				Camera.main.projectionMatrix = MatrixLerp (orthoMat, persMat, _currentT * _currentT);
			} else {
				Camera.main.projectionMatrix = MatrixLerp (persMat, orthoMat, Mathf.Sqrt (_currentT));
			}
		} else {
			_changing = false;
			Camera.main.orthographic = !currentlyOrthographic;
			Camera.main.ResetProjectionMatrix ();
		}
	}

	private Matrix4x4 MatrixLerp (Matrix4x4 from, Matrix4x4 to, float t) {
		t = Mathf.Clamp (t, 0.0f, 1.0f);
		Matrix4x4 newMatrix = new Matrix4x4 ();
		newMatrix.SetRow (0, Vector4.Lerp (from.GetRow (0), to.GetRow (0), t));
		newMatrix.SetRow (1, Vector4.Lerp (from.GetRow (1), to.GetRow (1), t));
		newMatrix.SetRow (2, Vector4.Lerp (from.GetRow (2), to.GetRow (2), t));
		newMatrix.SetRow (3, Vector4.Lerp (from.GetRow (3), to.GetRow (3), t));
		return newMatrix;
	}

	void OnGUI () {
		// GUILayout.Label ("New Camera.main.projectionMatrix:\n" + Camera.main.projectionMatrix.ToString ());
		// if (GUILayout.Button ("更改CameraProjection")) {
		// 	ChangeProjection = true;
		// }
	}

}
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity中,多点切换相机可以通过以下步骤实现: 1. 创建多个相机对象:首先,需要在场景中创建多个相机对象,可以通过在Hierarchy面板上右键点击并选择“Create Empty”创建空对象,然后使用“Camera”组件将其转换为相机对象。 2. 设置相机的位置和属性:在Hierarchy面板中选择相机对象,可以在Inspector面板中设置相机的位置、旋转、视角和其他属性。确保每个相机的位置和视角不重叠,以便后续切换。 3. 创建脚本来切换相机:创建一个新的C#脚本,并将其附加到任意一个游戏对象上。在脚本中,可以使用`Camera.main`来访问当前激活的相机。为了切换相机,可以使用`Camera.main.enabled = false`来禁用当前相机,然后使用`otherCamera.enabled = true`来启用其他相机。 4. 检测输入:在脚本中的`Update`函数中,可以使用`Input`类来检测输入,例如按下特定的键或鼠标点击事件。当接收到切换相机的输入时,执行相机切换代码。 5. 切换相机:在切换相机的代码中,你可以使用一个整数来表示当前相机的索引,然后根据输入的不同来增加或减少索引值。使用一个整数变量追踪当前激活的相机,并将其用作相机数组的索引。然后使用禁用和启用属性来切换相机。 综上所述,通过创建多个相机对象、设置相机属性、编写切换逻辑的脚本和检测输入,就可以实现Unity中的多点切换相机功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值