CrossFade

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Reverted edits by Joseph05408 (Talk); changed back to last version by Eric5h5)
(Description)
 
(4 intermediate revisions by 3 users not shown)
Line 6: Line 6:
  
 
==Description==
 
==Description==
Makes a smooth cross-fade from one camera view to another.
+
Makes a smooth cross-fade from one camera view to another. Requires Unity 2.6 or Unity 3.x.  (Doesn't work on Unity 4.)
 +
 
 +
[[Image:crossfade.jpg]]
  
 
==Usage==
 
==Usage==
See the attached file for an example scene.  The two cameras must be in the same scene, of course...drag these onto the appropriate slots in the inspector after you've attached the script to an object.  The script has a fadeTime public variable; this is the time, in seconds, that it will take for the wipe to completeIt uses a fake "render texture" technique, which means it works in Unity Indie, but the downside is that the image in the first camera will become static during the fade, although the second camera can have motionAlso, it only works properly in the editor if the game window aspect is set to none so there are no black bars, or else the view will shift during the fade.  This is just cosmetic, and doesn't affect an actual game build.
+
See [[ScreenWipes]] for an example scene and the actual script that performs the wipe.  The script below is an example of usage. It should be attached to an object, such as a manager object, and the [[ScreenWipes]] script should also be attached to the manager objectAlso needed are two cameras, which must be in the same scene, naturallyIn this example, you can press space to swap between the two cameras.
  
In actual use, you'd want to modify the script slightly: get rid of the Update function, and call ScreenCrossFade() appropriately instead.
+
Drag the two cameras onto the appropriate slots in the inspector after you've attached the script to an object.  The script has a '''FadeTime''' public variable; this is the time it will take for the wipe to complete.  It uses a fake "render texture" technique involving Texture2D.ReadPixels(), which means it doesn't need Unity Pro, but the downside is that the image in the first camera will become static during the fade, although the second camera can have motion.  Also, it doesn't really work right in the editor.  This is just cosmetic, and doesn't affect an actual game build.
  
<span class="dangerousLink">[[Media:CrossFade.unityPackage.zip|CrossFade.unityPackage.zip]]</span>‎
+
The function is a coroutine:
<span class="fileInfo"> (159KB, MIME TYPE: <code>application/zip</code>)</span>
+
  
==JavaScript - ScreenCrossFade.js==
+
function '''CrossFade (camera1 : '''Camera''', camera2 : '''Camera''', fadeTime : '''float''') :''' IEnumerator
<javascript>
+
// This only works properly in the editor if the game window aspect is set to none
+
// so there are no black bars.
+
var cam1 : GameObject;
+
var cam2 : GameObject;
+
var fadeTime : float = 2;
+
private var inProgress = false;
+
private var tex : Texture2D;
+
  
function Start() {
+
'''camera1''' is the camera that you are fading from, '''camera2''' is the camera you are fading to, and '''fadeTime''' is the length of time, in seconds, it takes to complete the fade.
    transform.position = transform.localScale = Vector3.zero;
+
 
    // Set up GUITexture size
+
==JavaScript - CrossFadeExample.js==
    guiTexture.pixelInset = Rect(0, 0, Screen.width, Screen.height);
+
<syntaxhighlight lang="javascript">var camera1 : Camera;
    tex = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
+
var camera2 : Camera;
}
+
var fadeTime = 2.0;
 +
var curve : AnimationCurve;
 +
private var inProgress = false;
 +
private var swap = false;
  
function Update() {
+
function Update () {
    if (Input.GetKeyDown("space")) {ScreenCrossFade(fadeTime);}
+
if (Input.GetKeyDown("space")) {
 +
DoFade();
 +
}
 
}
 
}
  
function ScreenCrossFade(fadeTime : float) {
+
function DoFade () {
    if (inProgress) {return;}
+
if (inProgress) return;
    inProgress = true;
+
inProgress = true;
   
+
    // Get texture from screen
+
swap = !swap;
    tex.ReadPixels(Rect(0, 0, Screen.width, Screen.height), 0, 0, false);
+
yield ScreenWipe.use.CrossFade (swap? camera1 : camera2, swap? camera2 : camera1, fadeTime);
    tex.Apply();
+
//yield ScreenWipe.use.CrossFade (swap? camera1 : camera2, swap? camera2 : camera1, fadeTime, curve);
    guiTexture.texture = tex;
+
   
+
inProgress = false;
    // Switch cameras
+
    yield;  // So the first Time.deltaTime in the loop below isn't huge because of the tex.Apply() hit
+
    cam1.active = !cam1.active;
+
    cam2.active = !cam2.active;
+
   
+
    // Do the cross-fade
+
    guiTexture.enabled = true;
+
    for (i = 0.51; i > 0.0; i -= Time.deltaTime * 1/(fadeTime*2)) {
+
        guiTexture.color.a = i;
+
        yield;
+
    }
+
   
+
    // Clean up
+
    guiTexture.enabled = false;
+
    inProgress = false;
+
 
}
 
}
 +
</syntaxhighlight>
  
@script RequireComponent(GUITexture)
+
To use this you also need the [[ScreenWipes]] script.
</javascript>
+

Latest revision as of 05:07, 31 October 2013


Author: Eric Haines (Eric5h5)

[edit] Description

Makes a smooth cross-fade from one camera view to another. Requires Unity 2.6 or Unity 3.x. (Doesn't work on Unity 4.)

Crossfade.jpg

[edit] Usage

See ScreenWipes for an example scene and the actual script that performs the wipe. The script below is an example of usage. It should be attached to an object, such as a manager object, and the ScreenWipes script should also be attached to the manager object. Also needed are two cameras, which must be in the same scene, naturally. In this example, you can press space to swap between the two cameras.

Drag the two cameras onto the appropriate slots in the inspector after you've attached the script to an object. The script has a FadeTime public variable; this is the time it will take for the wipe to complete. It uses a fake "render texture" technique involving Texture2D.ReadPixels(), which means it doesn't need Unity Pro, but the downside is that the image in the first camera will become static during the fade, although the second camera can have motion. Also, it doesn't really work right in the editor. This is just cosmetic, and doesn't affect an actual game build.

The function is a coroutine:

function CrossFade (camera1 : Camera, camera2 : Camera, fadeTime : float) : IEnumerator

camera1 is the camera that you are fading from, camera2 is the camera you are fading to, and fadeTime is the length of time, in seconds, it takes to complete the fade.

[edit] JavaScript - CrossFadeExample.js

var camera1 : Camera;
var camera2 : Camera;
var fadeTime = 2.0;
var curve : AnimationCurve;
private var inProgress = false;
private var swap = false;
 
function Update () {
	if (Input.GetKeyDown("space")) {
		DoFade();
	}
}
 
function DoFade () {
	if (inProgress) return;
	inProgress = true;
 
	swap = !swap;
	yield ScreenWipe.use.CrossFade (swap? camera1 : camera2, swap? camera2 : camera1, fadeTime);
	//yield ScreenWipe.use.CrossFade (swap? camera1 : camera2, swap? camera2 : camera1, fadeTime, curve);
 
	inProgress = false;
}

To use this you also need the ScreenWipes script.

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox