using System; using UnityEngine; namespace Thousandto.Plugins.Common.UniScene { //摄像机混合脚本 public class CameraBlendRender : MonoBehaviour { #region//私有变量 private float _blendTime = 1f; private float _timer = 0f; private Action _finishCallBack = null; private Shader _shader = null; private Material _material = null; private Camera _targetCamera = null; private RenderTexture _targetTex = null; #endregion #region//公有函数 public void PlayBlend(Camera target, float time, Action finishCallBack) { if(_shader == null) { _shader = Core.Base.ShaderEx.Find("Ares/Camera/CameraBlendPass"); } if(_material == null) { _material = new Material(_shader); } _timer = 0f; _blendTime = time; if(_targetCamera != null) { _targetCamera.targetTexture = null; } if(_targetTex == null) { _targetTex = RenderTexture.GetTemporary(Screen.width, Screen.height, 24, RenderTextureFormat.ARGB32); _targetTex.name = "RenderTexture9"; _targetTex.antiAliasing = 1; } _targetCamera = target; _targetCamera.targetTexture = _targetTex; //渲染一次,防止出现白色 _targetCamera.Render(); _finishCallBack = finishCallBack; Update(); } #endregion #region//脚本函数 private void Update() { _timer += Time.deltaTime; _material.SetTexture("_TargetText", _targetTex); _material.SetFloat("_LerpValue", _timer / _blendTime); if (_timer >= _blendTime) { _finishCallBack(); } //同步摄像机位置,防止重影 _targetCamera.transform.localPosition = transform.localPosition; } private void OnRenderImage(RenderTexture source, RenderTexture destination) { Graphics.Blit(source, destination, _material); } private void OnDestroy() { GameObject.DestroyImmediate(_material); RenderTexture.ReleaseTemporary(_targetTex); _targetCamera.targetTexture = null; } #endregion } }