<?xml version="1.0" encoding="UTF-8" standalone="yes"?><oembed><version><![CDATA[1.0]]></version><provider_name><![CDATA[Ondrej Paska Blog]]></provider_name><provider_url><![CDATA[https://fishtrone.wordpress.com]]></provider_url><author_name><![CDATA[Ondrej Paska]]></author_name><author_url><![CDATA[https://fishtrone.wordpress.com/author/randalfien/]]></author_url><title><![CDATA[Unity Transition Effects]]></title><type><![CDATA[link]]></type><html><![CDATA[<p>In a gamejam game I worked on recently, I wanted to create a nice transition effect between two different &#8220;worlds&#8221;.</p>
<p>This is what it ended up like:</p>
<p><img loading="lazy" data-attachment-id="35" data-permalink="https://fishtrone.wordpress.com/2018/11/30/unity-transition-effects/blog/" data-orig-file="https://fishtrone.files.wordpress.com/2018/11/blog.gif" data-orig-size="550,305" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="blog" data-image-description="" data-image-caption="" data-medium-file="https://fishtrone.files.wordpress.com/2018/11/blog.gif?w=300" data-large-file="https://fishtrone.files.wordpress.com/2018/11/blog.gif?w=550" class="alignnone size-full wp-image-35" src="https://fishtrone.files.wordpress.com/2018/11/blog.gif?w=550&#038;h=305" alt="blog" width="550" height="305" /></p>
<p>In this blog post I want to show you how it works and how to create even better effects.</p>
<p>The two worlds in my case were two different Game Objects each with many children objects. For each world there is a separate camera. To make the camera only render the corresponding object, you need to assign each object to a different layer (check apply to children as well). Then set the camera culling mask to that layer only.</p>
<p><img loading="lazy" data-attachment-id="37" data-permalink="https://fishtrone.wordpress.com/2018/11/30/unity-transition-effects/unityblog-2/" data-orig-file="https://fishtrone.files.wordpress.com/2018/11/unityblog1.jpg" data-orig-size="645,236" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Ondra&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1543615841&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="unityblog" data-image-description="" data-image-caption="" data-medium-file="https://fishtrone.files.wordpress.com/2018/11/unityblog1.jpg?w=300" data-large-file="https://fishtrone.files.wordpress.com/2018/11/unityblog1.jpg?w=645" class="alignnone  wp-image-37" src="https://fishtrone.files.wordpress.com/2018/11/unityblog1.jpg?w=489&#038;h=179" alt="unityblog" width="489" height="179" srcset="https://fishtrone.files.wordpress.com/2018/11/unityblog1.jpg?w=489&amp;h=179 489w, https://fishtrone.files.wordpress.com/2018/11/unityblog1.jpg?w=150&amp;h=55 150w, https://fishtrone.files.wordpress.com/2018/11/unityblog1.jpg?w=300&amp;h=110 300w, https://fishtrone.files.wordpress.com/2018/11/unityblog1.jpg 645w" sizes="(max-width: 489px) 100vw, 489px" /></p>
<p>For a refresher on multiple cameras, check out <a href="https://blog.theknightsofunity.com/using-multiple-unity-cameras-why-this-may-be-important/" target="_blank" rel="noopener">this excellent blog post.</a></p>
<p>In this case we have root2 on top of root1 (the order in which cameras render is determined by their &#8220;depth&#8221;).</p>
<p>We need a way to mask everything in the top layer. We can write a shader that only renders in the depth buffer and blocks everything else from rendering: <!-- HTML generated using hilite.me --></p>
<div style="background:#ffffff;overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin:0;line-height:125%;"> Shader <span style="color:#0000ff;">"Custom/DepthMask"</span>
 {
     SubShader
     {
         Tags {<span style="color:#0000ff;">"Queue"</span> = <span style="color:#0000ff;">"Geometry-1"</span> }
         Lighting Off
         Pass
         {
             ZWrite On
             ZTest LEqual
             ColorMask <span style="color:#0000ff;">0</span>     
         }
     }
 }
</pre>
</div>
<p>For more info on how that works, check out <a href="http://wiki.unity3d.com/index.php/DepthMask" target="_blank" rel="noopener">this wiki page</a>.</p>
<p>Now when we create a quad and assign a material with this shader, we can use it to hide parts of the layer. In the sample code, I added this to root2 and will use it show/hide this layer. You can also have a unique quad like this (I call it a &#8220;wiper&#8221;) in each layer.</p>
<p>The following script does some of the setup for the effect.</p>
<div style="background:#ffffff;overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin:0;line-height:125%;"><span style="color:#000080;font-weight:bold;">        public</span> GameObject Root1;
	<span style="color:#000080;font-weight:bold;">public</span> GameObject Root2;

	<span style="color:#000080;font-weight:bold;">public</span> GameObject Wiper;
	
	<span style="color:#000080;font-weight:bold;">public</span> Camera Camera1;
	<span style="color:#000080;font-weight:bold;">public</span> Camera Camera2;

	<span style="color:#000080;font-weight:bold;">private</span> <span style="color:#000080;font-weight:bold;">void</span> Awake()
	{
		SetupCameras();
		
		Root1.SetActive(<span style="color:#000080;font-weight:bold;">true</span>);
		Root2.SetActive(<span style="color:#000080;font-weight:bold;">false</span>);
	}

	<span style="color:#000080;font-weight:bold;">private</span> <span style="color:#000080;font-weight:bold;">void</span> SetupCameras()
	{
		Camera1.clearFlags = CameraClearFlags.SolidColor;
		Camera1.depth = <span style="color:#0000ff;">0</span>;
		Camera1.cullingMask = <span style="color:#0000ff;">1</span> &lt;&lt; Root1.layer;
		
		Camera2.clearFlags = CameraClearFlags.Nothing;
		Camera2.depth = <span style="color:#0000ff;">1</span>;
		Camera2.cullingMask = <span style="color:#0000ff;">1</span> &lt;&lt; Root2.layer;
	}
</pre>
</div>
<p>&nbsp;</p>
<p>The clearFlags are importing, because without them, Camera2 would render a solid color or skybox underneath our content, which would not be masked by the shader.</p>
<p><!-- HTML generated using hilite.me -->Next let&#8217;s animate things with <a href="http://dotween.demigiant.com" target="_blank" rel="noopener">DOTween</a>, which makes it really easy to move stuff around without messing with unity animation system.</p>
<p>We start with the wiper fully masking the contents of Root2 and then move it aside. On subsequent transitions we always flip this tween.</p>
<div style="background:#ffffff;overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin:0;line-height:125%;">	<span style="color:#000080;font-weight:bold;">private</span> <span style="color:#000080;font-weight:bold;">bool</span> _toggled;
	<span style="color:#000080;font-weight:bold;">private</span> <span style="color:#000080;font-weight:bold;">bool</span> _transitionActive;
	
	<span style="color:#000080;font-weight:bold;">private</span> <span style="color:#000080;font-weight:bold;">void</span> Update()
	{
	  <span style="color:#000080;font-weight:bold;">if</span> (!_transitionActive &amp;&amp; Input.GetKeyDown(KeyCode.Space))
	  {
	    ToggleTransition();
	  }
	}

	<span style="color:#000080;font-weight:bold;">private</span> <span style="color:#000080;font-weight:bold;">void</span> ToggleTransition()
	{
	  _toggled = !_toggled;	
		
	  Root1.SetActive(<span style="color:#000080;font-weight:bold;">true</span>);
	  Root2.SetActive(<span style="color:#000080;font-weight:bold;">true</span>);

	  Wiper.transform.position = <span style="color:#000080;font-weight:bold;">new</span> Vector3(_toggled ? <span style="color:#0000ff;">0</span> : -<span style="color:#0000ff;">31</span>, <span style="color:#0000ff;">0</span>, -<span style="color:#0000ff;">9f</span>);
	  Wiper.transform.DOMoveX(_toggled ? -<span style="color:#0000ff;">31</span> : <span style="color:#0000ff;">0</span>, <span style="color:#0000ff;">1.5f</span>).OnComplete(OnTransitionFinished);
		
	  _transitionActive = <span style="color:#000080;font-weight:bold;">true</span>;
	}

	<span style="color:#000080;font-weight:bold;">private</span> <span style="color:#000080;font-weight:bold;">void</span> OnTransitionFinished()
	{
	  _transitionActive = <span style="color:#000080;font-weight:bold;">false</span>;
	  Root1.SetActive(!_toggled);
	  Root2.SetActive(_toggled);
	}
</pre>
</div>
<p>&nbsp;</p>
<p>You can see that both &#8220;worlds&#8221; are only active during the transition. That&#8217;s useful when you want to stop any simulation or user interaction on the occluded layer.</p>
<p>If, instead of a quad, you use a circle mesh, you get this cool effect:</p>
<p><img loading="lazy" data-attachment-id="38" data-permalink="https://fishtrone.wordpress.com/2018/11/30/unity-transition-effects/bloggif2/" data-orig-file="https://fishtrone.files.wordpress.com/2018/11/bloggif2.gif" data-orig-size="550,313" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="bloggif2" data-image-description="" data-image-caption="" data-medium-file="https://fishtrone.files.wordpress.com/2018/11/bloggif2.gif?w=300" data-large-file="https://fishtrone.files.wordpress.com/2018/11/bloggif2.gif?w=550" class="alignnone size-full wp-image-38" src="https://fishtrone.files.wordpress.com/2018/11/bloggif2.gif?w=550&#038;h=313" alt="bloggif2" width="550" height="313" /></p>
<p>In 2D you can get similar effects with <a href="https://docs.unity3d.com/Manual/class-SpriteMask.html">unity 2D mask</a>, but this method works for any content 2D or 3D. You can also use it for two different scenes, loaded additively.</p>
<p>Full code as usual on <a href="https://github.com/randalfien/unity-transition-effect">github.com/randalfien/unity-transition-effect</a></p>
<p>You can also <a href="https://ondrejtrhon.itch.io/reality-onoff">try the game (in browser!) on itchio</a></p>
]]></html><thumbnail_url><![CDATA[https://fishtrone.files.wordpress.com/2018/11/bloggif2.gif?fit=440%2C330]]></thumbnail_url><thumbnail_width><![CDATA[440]]></thumbnail_width><thumbnail_height><![CDATA[250]]></thumbnail_height></oembed>