unity 相机fade脚本示例

  1 //Class to fade from camera 0 to 1, and back from 1 to 0
  2 //This class assumes there are only two scene cameras
  3 //---------------------------------------
  4 using UnityEngine;
  5 using System.Collections;
  6 //---------------------------------------
  7 public class CameraFader : MonoBehaviour
  8 {
  9     //---------------------------------------
 10     //Reference to cameras (all cameras in the scene to be composited)
 11     public Camera[] Cameras;
 12 
 13     //Reference to camera color (color to multiply with render)
 14     public Color[] CamCols = null;
 15 
 16     //Fade in/out time in seconds (total time for a fade in one direction)
 17     public float FadeTime = 2.0f;
 18 
 19     //Final material to apply to render (Can be used to apply a shader to final rendered pixels)
 20     public Material Mat = null;
 21     //---------------------------------------
 22     // Use this for initialization
 23     void Start () 
 24     {
 25         //Assign render textures to each camera
 26         foreach(Camera C in Cameras)
 27             C.targetTexture = new RenderTexture(Screen.width, Screen.height, 24); //Create texture
 28     }
 29     //---------------------------------------
 30     //This function is called once per frame after the camera has 
 31     //finished rendering but before the render is shown
 32     //It has a companion function OnPreRender (which is called before rendering)
 33     void OnPostRender()
 34     {
 35         //Define screen rect
 36         Rect ScreenRct = new Rect(0,0,Screen.width,Screen.height);
 37 
 38         //Source Rect
 39         Rect SourceRect = new Rect(0,1,1,-1);
 40 
 41         //Render each camera to their target texture
 42         for(int i = 0; i<Cameras.Length; i++)
 43         {
 44             //Render camera
 45             Cameras[i].Render();
 46 
 47             //Draw camera textures to screen using this camera
 48             GL.PushMatrix();
 49             GL.LoadPixelMatrix(); //Get pixel space matrix
 50             Graphics.DrawTexture(ScreenRct, Cameras[i].targetTexture, SourceRect, 0,0,0,0, CamCols[i]); //Draws each camera as layer
 51             GL.PopMatrix(); //Reset matrix
 52         }
 53     }
 54     //---------------------------------------
 55     //This function is called afer OnPostRender
 56     //And when final pixels are to be shown on screen
 57     //src = current render from camera
 58     //dst = texture to be shown on screen
 59     void OnRenderImage(RenderTexture src, RenderTexture dst)
 60     {
 61         //Frame finished rendering, now push final pixels to screen with Mat applied (can apply custom shader here)
 62         Graphics.Blit(src, dst, Mat);
 63     }
 64     //---------------------------------------
 65     //Function to lerp between color From to Color To over period TotalTime
 66     //This function is used to fade alpha for topmost rendered camera CamCols[1]
 67     public IEnumerator Fade(Color From, Color To, float TotalTime)
 68     {
 69         float ElapsedTime = 0f;
 70 
 71         //Loop while total time is not met
 72         while(ElapsedTime <= TotalTime)
 73         {
 74             //Update color
 75             CamCols[1] = Color.Lerp(From, To, ElapsedTime/TotalTime);
 76 
 77             //Wait until next frame
 78             yield return null;
 79 
 80             //Update Time
 81             ElapsedTime += Time.deltaTime;
 82         }
 83 
 84         //Apply final color
 85         CamCols[1] = Color.Lerp(From, To, 1f);
 86     }
 87     //---------------------------------------
 88     //Sample update function for testing camera functionality
 89     //Press space bar to fade in and out between cameras
 90     void Update()
 91     {
 92         //Fade camera in or out when space is pressed
 93         if(Input.GetKeyDown(KeyCode.Space))
 94         {
 95             StopAllCoroutines();
 96 
 97             //Should we fade out or in
 98             if(CamCols[1].a <= 0f)
 99                 StartCoroutine(Fade(CamCols[1], new Color(0.5f,0.5f,0.5f,1f), FadeTime)); //Fade in
100             else
101                 StartCoroutine(Fade(CamCols[1], new Color(0.5f,0.5f,0.5f,0f), FadeTime)); //Fade out
102         }
103     }
104     //---------------------------------------
105 }

 

posted @ 2020-03-15 22:17  漏船载酒泛中流  阅读(462)  评论(0)    收藏  举报