-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
using UnityEngine; | ||
using UnityEditor; | ||
using System; | ||
using System.Collections; | ||
|
||
|
||
namespace Prime31.TransitionKit | ||
{ | ||
public class TransitionKit : MonoBehaviour | ||
{ | ||
private static bool _isInitialized = false; | ||
private const int _transitionKitLayer = 31; | ||
|
||
private Texture2D _screenSnapshot; | ||
private TransitionKitDelegate _transitionKitDelegate; | ||
|
||
public Camera transitionKitCamera; | ||
public Material material; | ||
|
||
|
||
/// <summary> | ||
/// holds the instance while we are transitioning | ||
/// </summary> | ||
private static TransitionKit _instance; | ||
public static TransitionKit instance | ||
{ | ||
get | ||
{ | ||
if( !_instance ) | ||
{ | ||
// check if there is a TransitionKit instance already available in the scene graph before creating one | ||
_instance = FindObjectOfType( typeof( TransitionKit ) ) as TransitionKit; | ||
|
||
if( !_instance ) | ||
{ | ||
var obj = new GameObject( "TransitionKit" ); | ||
obj.layer = _transitionKitLayer; | ||
obj.transform.position = new Vector3( 99999f, 99999f, 99999f ); | ||
_instance = obj.AddComponent<TransitionKit>(); | ||
DontDestroyOnLoad( obj ); | ||
} | ||
} | ||
return _instance; | ||
} | ||
} | ||
|
||
|
||
void initialize() | ||
{ | ||
if( _isInitialized ) | ||
return; | ||
|
||
// create the MeshFilter | ||
gameObject.AddComponent<MeshFilter>().mesh = _transitionKitDelegate.meshForDisplay() ?? generateQuadMesh(); | ||
|
||
// create the Material | ||
material = gameObject.AddComponent<MeshRenderer>().material; | ||
material.shader = _transitionKitDelegate.shaderForTransition() ?? Shader.Find( "Unlit/Texture" ); | ||
|
||
// snapshot the main camera before proceeding | ||
_instance.StartCoroutine( _instance.captureScreenshotAndSetupCamera() ); | ||
|
||
_isInitialized = true; | ||
} | ||
|
||
|
||
Mesh generateQuadMesh() | ||
{ | ||
var halfHeight = 0.5f * 2f * 5f; | ||
var halfWidth = halfHeight * ( (float)Screen.width / (float)Screen.height ); | ||
|
||
var mesh = new Mesh(); | ||
mesh.vertices = new Vector3[] | ||
{ | ||
new Vector3( -halfWidth, -halfHeight, 0 ), | ||
new Vector3( -halfWidth, halfHeight, 0 ), | ||
new Vector3( halfWidth, -halfHeight, 0 ), | ||
new Vector3( halfWidth, halfHeight, 0 ) | ||
}; | ||
mesh.uv = new Vector2[] | ||
{ | ||
new Vector2( 0, 0 ), | ||
new Vector2( 0, 1 ), | ||
new Vector2( 1, 0 ), | ||
new Vector2( 1, 1 ) | ||
}; | ||
mesh.triangles = new int[] { 0, 1, 2, 3, 2, 1 }; | ||
|
||
|
||
return mesh; | ||
} | ||
|
||
|
||
IEnumerator captureScreenshotAndSetupCamera() | ||
{ | ||
yield return new WaitForEndOfFrame(); | ||
|
||
_screenSnapshot = new Texture2D( Screen.width, Screen.height, TextureFormat.RGB24, false, false ); | ||
_screenSnapshot.ReadPixels( new Rect( 0, 0, Screen.width, Screen.height ), 0, 0, false ); | ||
_screenSnapshot.Apply(); | ||
|
||
// load up the texture | ||
material.mainTexture = _screenSnapshot; | ||
|
||
// create our camera to cover the screen | ||
transitionKitCamera = gameObject.AddComponent<Camera>(); | ||
transitionKitCamera.isOrthoGraphic = true; | ||
transitionKitCamera.nearClipPlane = -1f; | ||
transitionKitCamera.farClipPlane = 1f; | ||
transitionKitCamera.depth = float.MaxValue; | ||
transitionKitCamera.cullingMask = 1 << _transitionKitLayer; | ||
|
||
if( _transitionKitDelegate != null ) | ||
StartCoroutine( _transitionKitDelegate.onScreenObscured( this ) ); | ||
} | ||
|
||
|
||
void OnLevelWasLoaded( int level ) | ||
{ | ||
if( _transitionKitDelegate != null ) | ||
_transitionKitDelegate.onLevelWasLoaded( this, level ); | ||
} | ||
|
||
|
||
#region Public | ||
|
||
public void transitionWithDelegate( TransitionKitDelegate transitionKitDelegate ) | ||
{ | ||
_transitionKitDelegate = transitionKitDelegate; | ||
initialize(); | ||
} | ||
|
||
|
||
public void cleanup() | ||
{ | ||
if( _instance == null ) | ||
return; | ||
|
||
Destroy( _screenSnapshot ); | ||
_instance._screenSnapshot = null; | ||
|
||
Destroy( gameObject ); | ||
_instance = null; | ||
_isInitialized = false; | ||
} | ||
|
||
|
||
public IEnumerator waitForLevelToLoad( int level ) | ||
{ | ||
while( Application.loadedLevel != level ) | ||
yield return null; | ||
} | ||
|
||
#endregion | ||
|
||
|
||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using UnityEngine; | ||
using System.Collections; | ||
|
||
|
||
namespace Prime31.TransitionKit | ||
{ | ||
public interface TransitionKitDelegate | ||
{ | ||
/// <summary> | ||
/// if the transition needs a custom shader return it here otherwise return null which will use the Unlit/Texture shader | ||
/// </summary> | ||
/// <returns>The for transition.</returns> | ||
Shader shaderForTransition(); | ||
|
||
|
||
/// <summary> | ||
/// if the transition needs a custom Mesh return it here otherwise return null which will use a full screen quad | ||
/// </summary> | ||
/// <returns>The for.</returns> | ||
Mesh meshForDisplay(); | ||
|
||
|
||
/// <summary> | ||
/// called when the screen is fully obscured. You can now load a new scene or modify the current one and it will be fully obscured from view | ||
/// </summary> | ||
IEnumerator onScreenObscured( TransitionKit transitionKit ); | ||
|
||
|
||
/// <summary> | ||
/// called when Unity's MonoBehaviourOnLevelWasLoaded is called. When doing a level load this is where you would want to start your transition | ||
/// </summary> | ||
/// <param name="transitionKit">Transition kit.</param> | ||
void onLevelWasLoaded( TransitionKit transitionKit, int level ); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
Shader "prime[31]/Transitions/Blur" | ||
{ | ||
Properties | ||
{ | ||
_MainTex ( "Base (RGB)", 2D ) = "white" {} | ||
_BlurAmount ( "Blur Amount", Range( 0.0, 1.0 ) ) = 0.0005 | ||
} | ||
|
||
SubShader | ||
{ | ||
//Tags { "RenderType" = "Transparent" "Queue" = "Transparent" } | ||
Tags { "RenderType" = "Opaque" } | ||
|
||
Pass | ||
{ | ||
CGPROGRAM | ||
#pragma exclude_renderers ps3 xbox360 | ||
#pragma fragmentoption ARB_precision_hint_fastest | ||
#pragma vertex vert_img | ||
#pragma fragment frag | ||
|
||
#include "UnityCG.cginc" | ||
|
||
|
||
// uniforms | ||
sampler2D _MainTex; | ||
uniform half _BlurAmount; | ||
|
||
|
||
|
||
fixed4 frag( v2f_img i ) : COLOR | ||
{ | ||
half4 texcol = half4( 0.0 ); | ||
float remaining = 1.0f; | ||
float coef = 1.0; | ||
float fI = 0; | ||
|
||
for( int j = 0; j < 3; j++ ) | ||
{ | ||
fI++; | ||
coef *= 0.32; | ||
texcol += tex2D( _MainTex, float2( i.uv.x, i.uv.y - fI * _BlurAmount ) ) * coef; | ||
texcol += tex2D( _MainTex, float2( i.uv.x - fI * _BlurAmount, i.uv.y ) ) * coef; | ||
texcol += tex2D( _MainTex, float2( i.uv.x + fI * _BlurAmount, i.uv.y ) ) * coef; | ||
texcol += tex2D( _MainTex, float2( i.uv.x, i.uv.y + fI * _BlurAmount ) ) * coef; | ||
|
||
remaining -= 4 * coef; | ||
} | ||
texcol += tex2D( _MainTex, float2( i.uv.x, i.uv.y ) ) * remaining; | ||
|
||
return texcol; | ||
} | ||
|
||
ENDCG | ||
} // end Pass | ||
} // end SubShader | ||
|
||
FallBack "Diffuse" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
Shader "prime[31]/Transitions/Fader" | ||
{ | ||
Properties | ||
{ | ||
_MainTex( "Base (RGB)", 2D ) = "white" {} | ||
_Fade( "Fade Amount", float ) = 0.0 | ||
_Color( "Fade to Color", Color ) = ( 0, 0, 0, 1 ) | ||
} | ||
|
||
SubShader | ||
{ | ||
Tags { "RenderType"="Transparent" "Queue"="Transparent" } | ||
Blend SrcAlpha OneMinusSrcAlpha | ||
Lighting Off | ||
|
||
Pass | ||
{ | ||
ZTest Always Cull Off ZWrite Off | ||
Fog { Mode off } | ||
|
||
|
||
CGPROGRAM | ||
#pragma vertex vert_img | ||
#pragma fragment frag | ||
#pragma fragmentoption ARB_precision_hint_fastest | ||
#include "UnityCG.cginc" | ||
|
||
|
||
uniform sampler2D _MainTex; | ||
uniform float _Fade; | ||
uniform fixed4 _Color; | ||
|
||
|
||
fixed4 frag( v2f_img i ):COLOR | ||
{ | ||
fixed4 texColor = tex2D( _MainTex, i.uv ); | ||
return lerp( texColor, _Color, _Fade ); | ||
} | ||
|
||
ENDCG | ||
} | ||
} | ||
|
||
FallBack off | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.