CrossFadePro

From Unify Community Wiki
Revision as of 19:17, 19 October 2009 by NCarter (Talk | contribs)

Jump to: navigation, search


Author: Eric Haines (Eric5h5)

Description

Makes a smooth cross-fade from one camera view to another. Unlike the original CrossFade, the image in the first camera can be moving during the transition. Also it's faster since it doesn't require manually reading the entire screen into a texture and applying it, but uses a rendertexture like the full-screen image effects. Requires Unity Pro and Unity 2.1+.

Usage

Make a camera (GameObject -> Create Other -> Camera), and attach this script to it. Then drag the two cameras which you want to fade between onto the appropriate slots in the Inspector (the cameras must be in the same scene, of course). You can disable the camera component of the camera that you just made; it activates when necessary. The script has a fadeTime public variable, which is the time, in seconds, that it will take for the wipe to complete.

In actual use, you'd want to modify the script slightly: get rid of the Update function, and call ScreenCrossFade() appropriately instead. See the attached file for an example scene.

Notes:

If you see objects turning transparent or disappearing during the fade when they shouldn't, check the opacity on the color of their materials. Solid, non-transparent objects should have opacity set to 100%. This applies to any solid background colors and skyboxes you may be using as well. They may look fine normally with less opacity, but the rendertexture picks up their alpha and does funky things with it.

Any GUIElements (GUITexts and GUITextures) will overbrighten during the transition, since the two cameras are rendering them on top of each other, if both cameras have a GUILayer. To avoid this, make another camera that only renders GUIElements on top of the other cameras, and nothing else. If you're using OnGUI, then the GUI won't be affected.

CrossFadePro.zip (150KB, MIME TYPE: application/zip)

JavaScript - ScreenCrossFadePro.js

<javascript> var cam1 : Camera; var cam2 : Camera; var fadeTime : float = 2; private var inProgress = false; private var mat : Material;

function Start () {

   mat = new Material (
       "Shader \"Transparent/Unlit\" {" +
       "Properties {" +
       "    _Color (\"Main Color\", Color) = (1,1,1,1)" +
       "}" +
       "SubShader {" +
       "    Pass {" +
       "        ZTest Always ZWrite Off Cull Off Alphatest Greater 0 ColorMask RGB Lighting Off" +
       "        Blend SrcAlpha OneMinusSrcAlpha" +
       "        SetTexture [_RenderTex] {" +
       "            constantColor [_Color]" +
       "            Combine texture * constant, texture * constant" +
       "        }" +
       "    }" +
       "}" +
       "}"
   );
   if (GetComponent(AudioListener)) {
       DestroyImmediate(GetComponent(AudioListener));
   }
   camera.enabled = false;

}

function Update () {

   if (Input.GetKeyDown("space")) {ScreenCrossFade(fadeTime);}

}

function ScreenCrossFade(fadeTime : float) {

   if (inProgress) {return;}
   inProgress = true;
   
   // Make this render camera duplicate the currently active camera
   if (cam1.gameObject.active) {
       var cam = cam1;
       var otherCam = cam2;
   }
   else {
       cam = cam2;
       otherCam = cam1;
   }
   transform.position = cam.transform.position;
   transform.rotation = cam.transform.rotation;
   camera.CopyFrom(cam);
   camera.depth = otherCam.depth+1;
   
   // Switch cameras
   cam1.gameObject.active = !cam1.gameObject.active;
   cam2.gameObject.active = !cam2.gameObject.active;
   
   // Do the cross-fade
   camera.enabled = true;
   for (i = 1.0; i > 0.0; i -= Time.deltaTime / fadeTime) {
       mat.color.a = i;
       yield;
   }
   
   // Clean up
   camera.enabled = false;
   inProgress = false;

}

// Draw a quad with a rendertexture function OnRenderImage (source : RenderTexture, dest : RenderTexture) {

   RenderTexture.active = dest;
   source.SetGlobalShaderProperty ("_RenderTex");
   
   GL.PushMatrix ();
   GL.LoadOrtho ();
   
   mat.SetPass (0);
   GL.Begin (GL.QUADS);
   GL.TexCoord2 (0, 0); GL.Vertex3 (0, 0, 0.1);
   GL.TexCoord2 (1, 0); GL.Vertex3 (1, 0, 0.1);
   GL.TexCoord2 (1, 1); GL.Vertex3 (1, 1, 0.1);
   GL.TexCoord2 (0, 1); GL.Vertex3 (0, 1, 0.1);
   GL.End ();
   
   GL.PopMatrix ();

}

@script RequireComponent(Camera) </javascript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox