<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Will&#039;s Blog</title>
	<atom:link href="http://www.willrmiller.com/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.willrmiller.com</link>
	<description>Grab your pixels and start your compilers!</description>
	<lastBuildDate>Wed, 02 May 2012 19:48:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>A Type-Safe Event System for Unity3D</title>
		<link>http://www.willrmiller.com/?p=87</link>
		<comments>http://www.willrmiller.com/?p=87#comments</comments>
		<pubDate>Wed, 02 May 2012 19:40:27 +0000</pubDate>
		<dc:creator>Will</dc:creator>
				<category><![CDATA[Game Development]]></category>

		<guid isPermaLink="false">http://www.willrmiller.com/?p=87</guid>
		<description><![CDATA[The Event Listener pattern is an extremely common design pattern. Using Events instead of method calls let an object communicate with another object (or many objects) without explicit knowledge of the other object. With events acting as an implicit interface between objects, we can write much more loosely coupled (thus more reusable) code. Unity&#8217;s own [...]]]></description>
			<content:encoded><![CDATA[<p>The Event Listener pattern is an extremely common design pattern.  Using Events instead of method calls let an object communicate with another object (or many objects) without explicit knowledge of the other object.  With events acting as an implicit interface between objects, we can write much more loosely coupled (thus more reusable) code.</p>
<p>Unity&#8217;s own message passing system can be leveraged to achieve this effect, but there are a few problems with it.  First, sending messages is hierarchy-dependent.  You either need a reference to the object you wish to send the message (event) to, or you need a reference to that object&#8217;s parent object.  This is not loosely coupled.  Secondly, it&#8217;s not statically type-safe.</p>
<p>There have been several solutions to this problem (for example, <a href="http://technology.blurst.com/unityscript-messaging-system/">FlashBang&#8217;s messaging system</a> or <a href="http://www.unifycommunity.com/wiki/index.php?title=CSharpEventManager">this one</a> on the UnifyCommunity wiki).  These still lack type-safety, and won&#8217;t quite do.</p>
<p>Here is my event system implementation.  It looks quite a bit like the event system in AS3:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">UnityEngine</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> GameEvent
<span style="color: #000000;">&#123;</span>
&nbsp;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Events
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> Events eventsInstance <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> Events instance
    <span style="color: #000000;">&#123;</span>
        get
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>eventsInstance <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                eventsInstance <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Events<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
            <span style="color: #0600FF;">return</span> eventsInstance<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">delegate</span> <span style="color: #0600FF;">void</span> EventDelegate<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> <span style="color: #000000;">&#40;</span>T e<span style="color: #000000;">&#41;</span> where T <span style="color: #008000;">:</span> GameEvent<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">private</span> Dictionary<span style="color: #008000;">&lt;</span><span style="color: #000000;">System</span>.<span style="color: #0000FF;">Type</span>, <span style="color: #000000;">System</span>.<span style="color: #FF0000;">Delegate</span><span style="color: #008000;">&gt;</span> delegates <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Dictionary<span style="color: #008000;">&lt;</span><span style="color: #000000;">System</span>.<span style="color: #0000FF;">Type</span>, <span style="color: #000000;">System</span>.<span style="color: #FF0000;">Delegate</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> AddListener<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> <span style="color: #000000;">&#40;</span>EventDelegate<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> del<span style="color: #000000;">&#41;</span> where T <span style="color: #008000;">:</span> GameEvent
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>delegates.<span style="color: #0000FF;">ContainsKey</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>T<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #000000;">System</span>.<span style="color: #FF0000;">Delegate</span> tempDel <span style="color: #008000;">=</span> delegates<span style="color: #000000;">&#91;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>T<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
            delegates<span style="color: #000000;">&#91;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>T<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #000000;">System</span>.<span style="color: #FF0000;">Delegate</span>.<span style="color: #0000FF;">Combine</span><span style="color: #000000;">&#40;</span>tempDel, del<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">else</span>
        <span style="color: #000000;">&#123;</span>
            delegates<span style="color: #000000;">&#91;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>T<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> del<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> AddListener<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> <span style="color: #000000;">&#40;</span>EventDelegate<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> del<span style="color: #000000;">&#41;</span> where T <span style="color: #008000;">:</span> GameEvent
    <span style="color: #000000;">&#123;</span>
        AddListener<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>del, <span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> RemoveListener<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> <span style="color: #000000;">&#40;</span>EventDelegate<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> del<span style="color: #000000;">&#41;</span> where T <span style="color: #008000;">:</span> GameEvent
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>delegates.<span style="color: #0000FF;">ContainsKey</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>T<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            var currentDel <span style="color: #008000;">=</span> <span style="color: #000000;">System</span>.<span style="color: #FF0000;">Delegate</span>.<span style="color: #0000FF;">Remove</span><span style="color: #000000;">&#40;</span>delegates<span style="color: #000000;">&#91;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>T<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>, del<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>currentDel <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                delegates.<span style="color: #0000FF;">Remove</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>T<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">else</span>
            <span style="color: #000000;">&#123;</span>
                delegates<span style="color: #000000;">&#91;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>T<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> currentDel<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Raise <span style="color: #000000;">&#40;</span>GameEvent e<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>e <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            Debug.<span style="color: #0000FF;">Log</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Invalid event argument: &quot;</span> <span style="color: #008000;">+</span> e.<span style="color: #0000FF;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            return<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>delegates.<span style="color: #0000FF;">ContainsKey</span><span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            delegates<span style="color: #000000;">&#91;</span>e.<span style="color: #0000FF;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">DynamicInvoke</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>To use this thing, first we declare a GameEvent subclass.  This event can carry with it all of the parameters needed by the objects listening for the event.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> SomethingHappenedEvent <span style="color: #008000;">:</span> GameEvent
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// Add event parameters here</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Registering to listen for the event looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> SomeObject <span style="color: #008000;">:</span> MonoBehaviour
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">void</span> OnEnable <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		Events.<span style="color: #0000FF;">instance</span>.<span style="color: #0000FF;">AddListener</span><span style="color: #008000;">&lt;</span>SomethingHappenedEvent<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>OnSomethingHappened<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">void</span> OnDisable <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		Events.<span style="color: #0000FF;">instance</span>.<span style="color: #0000FF;">RemoveListener</span><span style="color: #008000;">&lt;</span>SomethingHappenedEvent<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span>OnSomethingHappened<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">void</span> OnSomethingHappened <span style="color: #000000;">&#40;</span>SomethingHappenedEvent e<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008080; font-style: italic;">// Handle event here</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>And finally, to raise the event, do this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Events.<span style="color: #0000FF;">instance</span>.<span style="color: #0000FF;">Raise</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> SomethingHappenedEvent<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>The cool thing about this implementation is that it&#8217;s type-safe (listener registration errors will be caught at compile time, and no casting of events or event arguments) and it doesn&#8217;t require listening objects to implement a special interface or use Unity&#8217;s built-in message passing system.</p>
<p>The interface for this system is almost identical to that presented by Mike Mittleman at Unite 08, and I&#8217;d wager our implementations are similar.  If you really want a rundown of the benefits and pitfalls of event-driven Unity development, I suggest watching his presentation on Unity&#8217;s website.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willrmiller.com/?feed=rss2&#038;p=87</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C Coroutines for Game Entity State Management</title>
		<link>http://www.willrmiller.com/?p=73</link>
		<comments>http://www.willrmiller.com/?p=73#comments</comments>
		<pubDate>Thu, 08 Jul 2010 19:07:08 +0000</pubDate>
		<dc:creator>Will</dc:creator>
				<category><![CDATA[Game Development]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.willrmiller.com/?p=73</guid>
		<description><![CDATA[State management is one of the more difficult aspects of game programming. Entities in your game must behave differently depending on their internal state, and may depend on the state of other objects as well. There are several approaches to solving this problem &#8211; the naive approach being lots and lots of conditionals. Finite state [...]]]></description>
			<content:encoded><![CDATA[<p>State management is one of the more difficult aspects of game programming.  Entities in your game must behave differently depending on their internal state, and may depend on the state of other objects as well.  </p>
<p>There are several approaches to solving this problem &#8211; the naive approach being lots and lots of conditionals.  Finite state machines approach the problem by objectifying state and state transitions.  More recently, with the growing popularity of higher level languages like Lua, Python, and C#, coroutines have become a popular choice for mitigating the complexities of managing state.</p>
<p>I have implemented coroutines in C, and it&#8217;s turning out great.  Here&#8217;s how to do it!<br />
<span id="more-73"></span><br />
First, let&#8217;s look at a more traditional approach to state management &#8211; the &#8216;finite state machine.&#8217;  Each game entity might keep a state object that implements an interface like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> IState
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> Enter<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> Update<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> Exit<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This rather simple approach should be familiar.  It&#8217;s been my experience that managing state this way has a tendency toward &#8216;state explosion.&#8217;  Consider the following pseudocode:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// WalkTo state</span>
<span style="color: #0000ff;">void</span> WalkTo<span style="color: #008080;">::</span><span style="color: #007788;">Update</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>IsAtDoor<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		WalkToDoor<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	OpenDoor<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	actor<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>ChangeState<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">new</span> WaitForDoor<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// WaitForDoor</span>
<span style="color: #0000ff;">void</span> WaitForDoor<span style="color: #008080;">::</span><span style="color: #007788;">Update</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>IsDoorOpen<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		Idle<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	actor<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">;</span>ChangeState<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">new</span> EnterDoor<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// EnterDoor</span>
<span style="color: #0000ff;">void</span> EnterDoor<span style="color: #008080;">::</span><span style="color: #007788;">Update</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	EnterDoor<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Here, we want an actor to walk to a door, open it, wait for it to open, then walk through.  To accomplish this, we have to write three states for the actor plus two (not shown) for the door.  You can see how easily this can get more complicated.</p>
<p>Ideally, we want to be able to write something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> Actor<span style="color: #008080;">::</span><span style="color: #007788;">WalkThroughDoor</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>IsAtDoor<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		WalkToDoor<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		Sleep<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	OpenDoor<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>IsDoorOpen<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		Idle<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		Sleep<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	EnterDoor<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>If you have been working in Unity3D or UnrealScript, this might look pretty familiar.  Both of these environments provide some kind of coroutine system, as does Lua, Python, and other higher-level languages.</p>
<p>If, however, we are writing our game in C or C++, we&#8217;re out of luck.  Neither of these languages comes with built in support for coroutines.  We could use threads, but synchronization would be a serious problem, and the number of coroutines you could have in your game (which potentially could be quite a lot) would be limited to the number of threads the OS supports.  You could also use the dark magic that lurks in setjmp.h, but it is notoriously difficult to use across different platforms.</p>
<p>The solution I will present takes advantage of a somewhat controversial feature of the C switch statement: if a block in a switch is not terminated with a break statement, the flow of control will simply continue executing code in the next block.  This anomaly, and the fact that it&#8217;s legal to jump into the middle of a loop in C were the basis of Tom Duff&#8217;s famous serial copy optimization, known as &#8220;<a href="http://en.wikipedia.org/wiki/Duff%27s_device">Duff&#8217;s Device</a>.&#8221;</p>
<p>An extremely detailed breakdown of this coroutine implementation by Simon Tatham can be found <a href="http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html">here</a>.  The implementation I arrived at is almost exactly the same as Simon&#8217;s, and looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include </span>
&nbsp;
<span style="color: #339900;">#define COROUTINE_STATIC_BEGIN static int srcLine = 0; switch(srcLine) { case 0:;</span>
<span style="color: #339900;">#define COROUTINE_STATIC_END(value) } return (value)</span>
<span style="color: #339900;">#define COROUTINE_STATIC_END_VOID } return</span>
<span style="color: #339900;">#define COROUTINE_STATIC_YIELD(value) \
	do {\
		srcLine = __LINE__;\
		return (value); case __LINE__:;\
	} while (value)</span>
&nbsp;
<span style="color: #339900;">#define COROUTINE_CONTEXT_PARAM void** contextParam</span>
<span style="color: #339900;">#define COROUTINE_CONTEXT_BEGIN struct ContextTag { int line; int sleepTimer; int sleepTime</span>
<span style="color: #339900;">#define COROUTINE_CONTEXT_END(x) }* x = *contextParam</span>
<span style="color: #339900;">#define COROUTINE_BEGIN(x) if (!x) { x = *contextParam = malloc(sizeof(*x)); x-&gt;line = 0;} \
								if (x) switch (x-&gt;line) { case 0:;</span>
<span style="color: #339900;">#define COROUTINE_END(value) } free(*contextParam); *contextParam = 0; return(value)</span>
<span style="color: #339900;">#define COROUTINE_END_VOID } free(*contextParam); *contextParam = 0; return</span>
<span style="color: #339900;">#define COROUTINE_YIELD(value) \
	do {\
		((struct ContextTag*) *contextParam)-&gt;line = __LINE__;\
		return(value); case __LINE__:;\
	} while (0)</span>
<span style="color: #339900;">#define COROUTINE_YIELD_VOID \
	do {\
		((struct ContextTag*) *contextParam)-&gt;line = __LINE__;\
		return; case __LINE__:;\
	} while (0)</span>
<span style="color: #339900;">#define COROUTINE_SLEEP(time) \
	((struct ContextTag*) *contextParam)-&gt;sleepTimer = 0; \
	((struct ContextTag*) *contextParam)-&gt;sleepTime = time;\
	do {\
		((struct ContextTag*) *contextParam)-&gt;sleepTimer += game.deltaTime;\
		COROUTINE_YIELD_VOID;\
	} while (((struct ContextTag*) *contextParam)-&gt;sleepTimer &lt; ((struct ContextTag*) *contextParam)-&amp;gt;sleepTime)</span>
<span style="color: #339900;">#define COROUTINE_STOP(value) do { free(*contextParam); *contextParam = 0; return (value); } while (0)</span>
<span style="color: #339900;">#define COROUTINE_STOP_VOID do { free(*contextParam); *contextParam = 0; return; } while (0)</span>
<span style="color: #339900;">#define COROUTINE_CONTEXT void*</span>
&nbsp;
<span style="color: #339900;">#define COROUTINE_ABORT(context) do { free (context); context = 0; } while (0)</span></pre></div></div>

<p>These macros set up the switch statement and the labels which act as the reentrance points of the coroutine.  The macros come in two versions: regular and static.  Static functions are those which don&#8217;t need any values saved between calls or that the data is function static.  The regular version assumes that you have some data that needs to persist between calls.  The context macors create a small heap-allocated object that stores your data.</p>
<p>One caveat with is code is that while it&#8217;s perfectly valid C and C++, you have to turn off Edit and Compile when compiling in Visual Studio.  Studio does something strange with the __LINE__ macro when Edit and Compile is turned on.</p>
<p>With these macros, you can write code like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> myCoroutine<span style="color: #008000;">&#40;</span>COROUTINE_CONTEXT_PARAM<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	COROUTINE_CONTEXT_BEGIN<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> i<span style="color: #008080;">;</span>
	COROUTINE_CONTEXT_END<span style="color: #008000;">&#40;</span>ctx<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	COROUTINE_BEGIN<span style="color: #008000;">&#40;</span>ctx<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>ctx<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> ctx<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>i <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span> ctx<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, ctx<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>i<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
 		COROUTINE_YIELD_VOID<span style="color: #008080;">;</span>
 	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #666666;">// Sleep for 30 ticks</span>
	COROUTINE_SLEEP<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">30</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;finished sleeping<span style="color: #000099; font-weight: bold;">\n</span>0&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	COROUTINE_END_VOID<span style="color: #008080;">;</span>
 <span style="color: #008000;">&#125;</span>
&nbsp;
 <span style="color: #0000ff;">int</span> callingFunc<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
 <span style="color: #008000;">&#123;</span>
	COROUTINE_CONTEXT context<span style="color: #008080;">;</span>
	context <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
 	<span style="color: #0000ff;">do</span>
 	<span style="color: #008000;">&#123;</span>
 		myCoroutine<span style="color: #008000;">&#40;</span>context<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>context<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
 <span style="color: #008000;">&#125;</span></pre></div></div>

<p>Now, this might not be as pretty as, say, Unity3D&#8217;s C# coroutines or UnrealScript&#8217;s state blocks, but functionally it&#8217;s about the same.  Using this coroutine implementation has sweeping implications on your C or C++ game code.  Every actor in your game can behave as if it has its own thread, without the headaches of synchronization.</p>
<p>Try it out!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willrmiller.com/?feed=rss2&#038;p=73</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Victory at the Rosetta Stone Game Jam!</title>
		<link>http://www.willrmiller.com/?p=65</link>
		<comments>http://www.willrmiller.com/?p=65#comments</comments>
		<pubDate>Tue, 12 Jan 2010 02:35:30 +0000</pubDate>
		<dc:creator>Will</dc:creator>
				<category><![CDATA[Game Development]]></category>
		<category><![CDATA[Game Design]]></category>
		<category><![CDATA[game jam]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[rosetta stone]]></category>

		<guid isPermaLink="false">http://blog.reversednormal.com/?p=65</guid>
		<description><![CDATA[David McDonough, Jack Cooke, and I polished off a 1st place win at Rosetta Stone&#8217;s first annual 36 hour game jam in Harrisonburg Virginia on Saturday. The requirements for the game were given out beforehand, and were very open-ended: make a game that &#8220;teaches.&#8221;  Coming from Firaxis, we decided to make a strategy game, and [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_69" class="wp-caption alignright" style="width: 160px"><a href="http://www.willrmiller.com/wp-content/uploads/2010/01/IMG_0389-e1263263009305.jpg"><img class="size-thumbnail wp-image-69" title="Working" src="http://blog.reversednormal.com/wp-content/uploads/2010/01/IMG_0389-e1263263009305-150x150.jpg" alt="" width="150" height="150" /></a><p class="wp-caption-text">Working</p></div>
<p>David McDonough, Jack Cooke, and I polished off a 1st place win at Rosetta Stone&#8217;s first annual 36 hour game jam in Harrisonburg Virginia on Saturday.</p>
<p>The requirements for the game were given out beforehand, and were very open-ended: make a game that &#8220;teaches.&#8221;  Coming from Firaxis, we decided to make a strategy game, and settled on a &#8220;virus&#8221; theme.  We liked the idea of a virus strategy game because we could abstract things visually (simple shapes with cool shaders &#8211; no characters to animate).  The simulation aspect of the game play could also be very simple, and amounted to little more than three numbers driving a physics-based steering system (like <a href="http://en.wikipedia.org/wiki/Flocking_(behavior)">this</a>).</p>
<p>Each of us had some basic knowledge about how vira spread in the body, and we really didn&#8217;t go out of our way to do any additional research.  One of Sid Meier&#8217;s 10 Rules of Game Design (we&#8217;re not really sure which one this is &#8211; he changes the order all the time) is to &#8220;make the game first, then do the research.&#8221;   This may sound a bit strange at first, but it makes sense.  If the game isn&#8217;t fun, it doesn&#8217;t engage the player and loses the ability to teach.  If the simulation is to detailed or esoteric, the player can&#8217;t easily grok it, and  you lose them.  Civilization is a great example of this.  It is built from the assumed historical knowledge of the average gamer (unless you fire up the Civilpedia).  The game doesn&#8217;t teach by relaying historical facts, rather it does so by allowing the player to engage in a simulation that, while not historically accurate, illustrates very clearly the dynamics of industry, diplomacy, war, and culture.</p>
<p>The game we ended up with at the end of the day is called Pathogen.  It features game play that is very similar to Phil Hassey&#8217;s Galcon with a 3D play surface.  You move your swarm of vira from cell to cell, expanding your population and avoiding autoimmune cells as you go.   Before each level, you load out with three &#8220;mutation cards&#8221; which give you power-ups as you hit certain population milestones.  The game is very simple, but illustrates well the balancing act a virus must perform when successfully infecting it&#8217;s host.<span id="more-65"></span></p>
<p><strong>What Went Right:</strong></p>
<p><em>Planning </em></p>
<p>We knew the prompt for the game jam ahead of time, so we had quite a bit of time to plan for what we were going to make.  When we got there, we had a very clear development schedule and asset list, so there weren&#8217;t any surprises.  David, who is a producer at Firaxis, was very on top of things as per usual.  He broke down the game into manageable hunks &#8211; usually punctuated by a meal, which served as an excellent motivator.  The only thing missing was Hansoft.</p>
<p><em> </em></p>
<div id="attachment_70" class="wp-caption alignleft" style="width: 160px"><a href="http://www.willrmiller.com/wp-content/uploads/2010/01/Brain.png"><img class="size-thumbnail wp-image-70" title="Brain" src="http://blog.reversednormal.com/wp-content/uploads/2010/01/Brain-150x150.png" alt="" width="150" height="150" /></a><p class="wp-caption-text">Pathogen</p></div>
<p>Scope</p>
<p>You never really <em>finish </em>a game at a game jam &#8211; there&#8217;s always something more you want to do, but we scoped our game very well and stayed true to it.  Again, good planning ahead of time and real world game production experience helped us stay true to our original goals.</p>
<p><em>Unity</em></p>
<p>The Unity Engine played a huge part in our success.  The engine made it very easy to prototype and iterate quickly.  Unity virtually eliminated the need for an asset conditioning pipeline &#8211; regardless of what we threw at it, Unity just worked.  With Unity, there was also never a &#8220;make a build&#8221; step to our design process.  The game was always live, and we could see changes we made instantly.  While we probably would have created an awesome game without it, it certainly could not have been the game we made with it.</p>
<p><em>Sleep</em></p>
<p>For a 36 hour competition, it was tempting to go in thinking we would stay up for the whole thing.  We had the foresight, however, to work sleep into our schedule.  Even short naps did wonders for productivity.  We knew we were going to have to pull a very long night to get the game done, and we opted to do it later in the project rather than burn out with a long stretch at the very beginning.  We worked four hours the first night, slept for six, then worked until the end of the jam, taking naps occasionally.  We all got extremely tired at times, but it wasn&#8217;t as bad as it could have been.  We <em>really</em> should have brought pillows though.  Sleeping on the floor was not fun.</p>
<p><strong>What Went Wrong</strong></p>
<div id="attachment_71" class="wp-caption alignright" style="width: 160px"><a href="http://www.willrmiller.com/wp-content/uploads/2010/01/IMG_0388.jpg"><img class="size-thumbnail wp-image-71" title="IMG_0388" src="http://blog.reversednormal.com/wp-content/uploads/2010/01/IMG_0388-150x150.jpg" alt="" width="150" height="150" /></a><p class="wp-caption-text">The white board</p></div>
<p><em>Source Control</em></p>
<p>One of the down sides of using Unity, and there aren&#8217;t many, is that the indie version does not support source control &#8211; no Perforce or Subversion.  With two people coding at the same time, the project would inevitably start to branch, and we would periodically have to manually merge all of the changes.  Unity projects don&#8217;t like to be moved around &#8211; especially from PC to Mac, so often a merge wasn&#8217;t just merging source files, it was reconstructing whole levels and prefabs.  In the future, it might make sense to at the very least source control the code (I think you can do this) to make merging prefabs and levels in Unity easier, and perform the merges more often.  OR Unity could include source control support in the indie version.  Preferably the latter.</p>
<p><em>Iteration Time</em></p>
<p>The game shipped with three complete levels, which was great, but we didn&#8217;t really have time to balance and test them.  As a result, the game is almost impossible to lose.  This is probably better than a game that&#8217;s impossible to win, but because the levels were so easy, some of the cool systems we put in to progress and upgrade the player (like virus mutation) weren&#8217;t really needed to win.</p>
<p><em>Jack&#8217;s Laptop</em></p>
<div id="attachment_72" class="wp-caption alignleft" style="width: 160px"><a href="http://www.willrmiller.com/wp-content/uploads/2010/01/Virus_SteeringTests.png"><img class="size-thumbnail wp-image-72" title="Virus_SteeringTests" src="http://blog.reversednormal.com/wp-content/uploads/2010/01/Virus_SteeringTests-150x150.png" alt="" width="150" height="150" /></a><p class="wp-caption-text">Developing the steering behavior</p></div>
<p>It was loud.  Really really loud.  Lucky for him, he won a new one!</p>
<p>Overall, there was very little negative to say about our experience.  Rosetta Stone did an awesome job putting this game jam on. The accommodations were great.  There were volunteers to answer questions or get something for you if you needed it, and there was food and drink available pretty much all the time.  Everyone we met was really cool, too, and we hope to see them again next year!</p>
<p>Also, we were on TV!</p>
<p><a href="http://www.whsv.com/home/headlines/81078462.html">http://www.whsv.com/home/headlines/81078462.html</a><br />
<a href="http://www.whsv.com/home/headlines/81078462.html">http://www.whsv.com/home/headlines/81098512.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.willrmiller.com/?feed=rss2&#038;p=65</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Cell (Worley) noise in Unity3D</title>
		<link>http://www.willrmiller.com/?p=52</link>
		<comments>http://www.willrmiller.com/?p=52#comments</comments>
		<pubDate>Tue, 15 Dec 2009 15:50:36 +0000</pubDate>
		<dc:creator>Will</dc:creator>
				<category><![CDATA[Game Development]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Shader]]></category>
		<category><![CDATA[Unity]]></category>

		<guid isPermaLink="false">http://blog.reversednormal.com/?p=52</guid>
		<description><![CDATA[Noise algorithms are a fantastic thing to have in your 3D graphics bag of tricks.  I can&#8217;t tell you how many times Perlin noise has saved my life.  Unfortunately, most good noise algorithms don&#8217;t lend themselves particularly well to computation on the GPU.  Enter Cell noise. Cell noise, otherwise known as Worley noise,  is actually [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.willrmiller.com/wp-content/uploads/2009/12/cellNoise.jpg"><img class="alignleft size-full wp-image-53" style="margin: 5px;" title="Cell Noise" src="http://www.willrmiller.com/wp-content/uploads/2009/12/cellNoise.jpg" alt="Cell Noise" width="200" height="341" /></a>Noise algorithms are a fantastic thing to have in your 3D graphics bag of tricks.  I can&#8217;t tell you how many times Perlin noise has saved my life.  Unfortunately, most good noise algorithms don&#8217;t lend themselves particularly well to computation on the GPU.  Enter Cell noise.</p>
<p>Cell noise, otherwise known as Worley noise,  is actually a visualization of a Voronoi diagram: given a random scattering of points, color each pixel on the surface relative to the distance from the closest randomly scattered point.  Cell noise appears all over nature, it looks great, and is easy to compute!  A cell noise function can give us a volume of noise, which we can sample in a fragment shader, giving us seamless patterns on any object &#8211; no UV mapping required.  Cool!</p>
<p><span id="more-52"></span></p>
<p>I&#8217;m going to base this implementation off of a very nice one I found <a href="http://www.infinity-universe.com/Infinity/index.php?option=com_content&amp;task=view&amp;id=98&amp;Itemid=26">here</a>.  To get started, we need a way to generate our randomly scattered points.  Normally, we would accelerate this sort of thing with a look-up table of some kind, which in GPU land means a texture.  Hop into your favorite image editing program and make a texture that looks something like this:</p>
<p><a href="http://www.willrmiller.com/wp-content/uploads/2009/12/Random1.png"><img class="alignnone size-full wp-image-55" title="Random" src="http://www.willrmiller.com/wp-content/uploads/2009/12/Random1.png" alt="Random" width="179" height="179" /></a></p>
<p>And here&#8217;s the shader:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">Shader <span style="color: #ff0000;">&quot;Miller/CellNoise&quot;</span> <span style="color: #009900;">&#123;</span>
	Properties <span style="color: #009900;">&#123;</span>
		_Color <span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Main Color&quot;</span><span style="color: #339933;">,</span> Color<span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span>
		_SecondaryColor <span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Secondary Color&quot;</span><span style="color: #339933;">,</span> Color<span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span>
		_Random <span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Random Noise&quot;</span><span style="color: #339933;">,</span> 2D<span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;white&quot;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
		_Intensity <span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Intensity&quot;</span><span style="color: #339933;">,</span> Range<span style="color: #009900;">&#40;</span><span style="color:#800080;">0.001</span><span style="color: #339933;">,</span> <span style="color:#800080;">10.0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color:#800080;">1.0</span>
		_Size <span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Size&quot;</span><span style="color: #339933;">,</span> Range<span style="color: #009900;">&#40;</span><span style="color:#800080;">0.000</span><span style="color: #339933;">,</span> <span style="color:#800080;">10.0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color:#800080;">1.0</span>
		_Offset <span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Time&quot;</span><span style="color: #339933;">,</span> <span style="color: #993333;">float</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color:#800080;">0.0</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	SubShader <span style="color: #009900;">&#123;</span>
		pass <span style="color: #009900;">&#123;</span>
			Name <span style="color: #ff0000;">&quot;Noise&quot;</span>
&nbsp;
			<span style="color: #808080; font-style: italic;">/* // Enable this if you want addative blending
			ZWrite Off
			Tags { &quot;Queue&quot; = &quot;Transparent&quot; }
			Blend One One
			*/</span>
&nbsp;
			CGPROGRAM
			<span style="color: #339933;">#pragma target 3.0</span>
			<span style="color: #339933;">#pragma vertex vert</span>
			<span style="color: #339933;">#pragma fragment frag</span>
			<span style="color: #339933;">#pragma fragmentoption ARB_fog_exp2</span>
			<span style="color: #339933;">#include &quot;UnityCG.cginc&quot;</span>
&nbsp;
			uniform float4 _Color<span style="color: #339933;">;</span>
			uniform float4 _SecondaryColor<span style="color: #339933;">;</span>
			uniform <span style="color: #993333;">float</span> _Intensity<span style="color: #339933;">;</span>
			uniform sampler2D _Random<span style="color: #339933;">;</span>
			uniform <span style="color: #993333;">float</span> _Size<span style="color: #339933;">;</span>
			uniform <span style="color: #993333;">float</span> _Offset<span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #993333;">struct</span> v2f <span style="color: #009900;">&#123;</span>
				V2F_POS_FOG<span style="color: #339933;">;</span>
				float3 noisepos<span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
			v2f vert<span style="color: #009900;">&#40;</span>appdata_base v<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				v2f o<span style="color: #339933;">;</span>
&nbsp;
				PositionFog<span style="color: #009900;">&#40;</span>v.<span style="color: #202020;">vertex</span><span style="color: #339933;">,</span> o.<span style="color: #202020;">pos</span><span style="color: #339933;">,</span> o.<span style="color: #202020;">fog</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				o.<span style="color: #202020;">noisepos</span> <span style="color: #339933;">=</span> v.<span style="color: #202020;">vertex</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #b1b100;">return</span> o<span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			float4 getCell3D<span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> in <span style="color: #993333;">int</span> x<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> in <span style="color: #993333;">int</span> y<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> in <span style="color: #993333;">int</span> z<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #993333;">float</span> u <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>x <span style="color: #339933;">+</span> y <span style="color: #339933;">*</span> <span style="color: #0000dd;">31</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color:#800080;">20.0</span><span style="color: #339933;">;</span>
				<span style="color: #993333;">float</span> v <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>z <span style="color: #339933;">-</span> x <span style="color: #339933;">*</span> <span style="color: #0000dd;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color:#800080;">30.0</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">return</span> tex2D<span style="color: #009900;">&#40;</span>_Random<span style="color: #339933;">,</span> float2<span style="color: #009900;">&#40;</span>u<span style="color: #339933;">,</span> v<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			float2 cellNoise3D<span style="color: #009900;">&#40;</span>float3 xyz<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #993333;">int</span> xi <span style="color: #339933;">=</span> <span style="color: #993333;">int</span><span style="color: #009900;">&#40;</span>floor<span style="color: #009900;">&#40;</span>xyz.<span style="color: #202020;">x</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #993333;">int</span> yi <span style="color: #339933;">=</span> <span style="color: #993333;">int</span><span style="color: #009900;">&#40;</span>floor<span style="color: #009900;">&#40;</span>xyz.<span style="color: #202020;">y</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #993333;">int</span> zi <span style="color: #339933;">=</span> <span style="color: #993333;">int</span><span style="color: #009900;">&#40;</span>floor<span style="color: #009900;">&#40;</span>xyz.<span style="color: #202020;">z</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #993333;">float</span> xf <span style="color: #339933;">=</span> xyz.<span style="color: #202020;">x</span> <span style="color: #339933;">-</span> <span style="color: #993333;">float</span><span style="color: #009900;">&#40;</span>xi<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #993333;">float</span> yf <span style="color: #339933;">=</span> xyz.<span style="color: #202020;">y</span> <span style="color: #339933;">-</span> <span style="color: #993333;">float</span><span style="color: #009900;">&#40;</span>yi<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #993333;">float</span> zf <span style="color: #339933;">=</span> xyz.<span style="color: #202020;">z</span> <span style="color: #339933;">-</span> <span style="color: #993333;">float</span><span style="color: #009900;">&#40;</span>zi<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #993333;">float</span> dist1 <span style="color: #339933;">=</span> <span style="color:#800080;">9999999.0</span><span style="color: #339933;">;</span>
				<span style="color: #993333;">float</span> dist2 <span style="color: #339933;">=</span> <span style="color:#800080;">9999999.0</span><span style="color: #339933;">;</span>
				float3 cell<span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> z <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> z <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> z<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> y <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> y <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> y<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
						<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> x <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> x <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> x<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
							cell <span style="color: #339933;">=</span> getCell3D<span style="color: #009900;">&#40;</span>xi <span style="color: #339933;">+</span> x<span style="color: #339933;">,</span> yi <span style="color: #339933;">+</span> y<span style="color: #339933;">,</span> zi <span style="color: #339933;">+</span> z<span style="color: #009900;">&#41;</span>.<span style="color: #202020;">xyz</span><span style="color: #339933;">;</span>
							cell.<span style="color: #202020;">x</span> <span style="color: #339933;">+=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">float</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> xf<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
							cell.<span style="color: #202020;">y</span> <span style="color: #339933;">+=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">float</span><span style="color: #009900;">&#40;</span>y<span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> yf<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
							cell.<span style="color: #202020;">z</span> <span style="color: #339933;">+=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">float</span><span style="color: #009900;">&#40;</span>z<span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> zf<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
							<span style="color: #993333;">float</span> dist <span style="color: #339933;">=</span> dot<span style="color: #009900;">&#40;</span>cell<span style="color: #339933;">,</span> cell<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
							<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>dist <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> dist1<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
								dist2 <span style="color: #339933;">=</span> dist1<span style="color: #339933;">;</span>
								dist1 <span style="color: #339933;">=</span> dist<span style="color: #339933;">;</span>
							<span style="color: #009900;">&#125;</span>
							<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>dist <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> dist2<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
								dist2 <span style="color: #339933;">=</span> dist<span style="color: #339933;">;</span>
							<span style="color: #009900;">&#125;</span>
						<span style="color: #009900;">&#125;</span>
					<span style="color: #009900;">&#125;</span>
				<span style="color: #009900;">&#125;</span>
&nbsp;
				<span style="color: #b1b100;">return</span> float2<span style="color: #009900;">&#40;</span>sqrt<span style="color: #009900;">&#40;</span>dist1<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> sqrt<span style="color: #009900;">&#40;</span>dist2<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			float4 frag<span style="color: #009900;">&#40;</span>v2f i<span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> COLOR <span style="color: #009900;">&#123;</span>
				float2 dists <span style="color: #339933;">=</span> cellNoise3D<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>i.<span style="color: #202020;">noisepos</span> <span style="color: #339933;">+</span> _Offset<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> _Size<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #666666; font-style: italic;">// float4 c = ((_Color * dists.x) + (_SecondaryColor * dists.y)) * _Intensity; // Add the terms for a different look</span>
				float4 c <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>_Color <span style="color: #339933;">*</span> dists.<span style="color: #202020;">x</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>_SecondaryColor <span style="color: #339933;">*</span> dists.<span style="color: #202020;">y</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> _Intensity<span style="color: #339933;">;</span>
				<span style="color: #b1b100;">return</span> c<span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			ENDCG
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	FallBack <span style="color: #ff0000;">&quot;Diffuse&quot;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You can animate the texture by changing the _Offset parameter over time.  It looks great!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willrmiller.com/?feed=rss2&#038;p=52</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

