<?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>lab.joelGillman.com &#187; displacement mapping</title>
	<atom:link href="http://lab.joelgillman.com/archives/tag/displacement-mapping/feed" rel="self" type="application/rss+xml" />
	<link>http://lab.joelgillman.com</link>
	<description>Sketches, Code Bites and Toys</description>
	<lastBuildDate>Thu, 05 Nov 2009 23:19:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>False 3D in Flash: Displacement Maps</title>
		<link>http://lab.joelgillman.com/archives/30_false-3d-in-flash-displacement-maps</link>
		<comments>http://lab.joelgillman.com/archives/30_false-3d-in-flash-displacement-maps#comments</comments>
		<pubDate>Wed, 17 Sep 2008 21:53:48 +0000</pubDate>
		<dc:creator>joel gillman</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[displacement mapping]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[photoshop]]></category>
		<category><![CDATA[source code]]></category>

		<guid isPermaLink="false">http://lab.joelgillman.com/?p=30</guid>
		<description><![CDATA[After seeing a post on the Puny Blog about displacement maps I became interested in how they did it.  Unfortunately there wasn&#8217;t any source code so I&#8217;ve replicated the effect from scratch.
Turns out displacement maps are really hard to wrap your head around, so I&#8217;ll try to explain it very carefully and slowly.

I feel [...]]]></description>
			<content:encoded><![CDATA[<p>After seeing a <a href="http://www.punyblog.com/2008/08/displacement-map-filter.html">post</a> on the Puny Blog about displacement maps I became interested in how they did it.  Unfortunately there wasn&#8217;t any source code so I&#8217;ve replicated the effect from scratch.<br />
Turns out displacement maps are really hard to wrap your head around, so I&#8217;ll try to explain it very carefully and slowly.<br />
<span id="more-30"></span><br />
I feel that Wikipedia is always a good place to start:</p>
<blockquote><p><b>Displacement mapping</b> is an alternative computer graphics technique in contrast to <a href="/wiki/Bump_mapping" title="Bump mapping">bump mapping</a>, <a href="/wiki/Normal_mapping" title="Normal mapping">normal mapping</a>, and <a href="/wiki/Parallax_mapping" title="Parallax mapping">parallax mapping</a>, using a (<a href="/wiki/Procedural_texture" title="Procedural texture">procedural</a>-) <a href="/wiki/Texture_mapping" title="Texture mapping">texture-</a> or <a href="/wiki/Heightmap" title="Heightmap">height map</a> to cause an effect where the actual geometric position of points over the textured surface are <i>displaced</i>, often along the <a href="/wiki/Locally" title="Locally" class="mw-redirect">local</a> <a href="/wiki/Surface_normal" title="Surface normal">surface normal</a>, according to the value the texture function evaluates to at each point on the surface. It gives surfaces a great sense of depth and detail, permitting in particular self-occlusion, self-shadowing and silhouettes; on the other hand, it is the most costly of this class of techniques owing to the large amount of additional geometry.</p></blockquote>
<p>Displacement maps are commonly used in 3D programs to make complicated geometry using simple 2D images.  In those programs it actually morphs objects in a 3D-space (along a z-axis).  But in our (so far) 2D flash world, it&#8217;s easier to think of the displacement maps as <a href="http://www.edwardsamuels.com/illustratedstory/chapter%204/goo%20-%20mona%20lisa.jpg">distortion</a>.  In plain English, you distort an image using a separate gray scale image.</p>
<p>I&#8217;m a visual thinker so I drew it out in my sketch book first:<br />
<img src="http://lab.joelgillman.com/thepress/wp-content/uploads/2008/09/displacement_breakthrough.png" alt="" title="displacement_breakthrough" width="450" height="275" class="alignnone size-full wp-image-35" /><br />
This is the distortion I wanted to achieve.  If you can&#8217;t read my chicken scratch it says, &#8220;x: -30 to 0 to +30&#8243;.  Meaning that if every square were for 5 pixels, at the grid&#8217;s most distorted point the bottom-left point would be offset by -30 pixels along it&#8217;s &#8216;x&#8217; axis while the bottom-right point is offset by +30 pixels.  The bottom-middle is not offset along the &#8216;x&#8217; axis.</p>
<p><img src="http://lab.joelgillman.com/thepress/wp-content/uploads/2008/09/photoshop_channles.png" alt="" title="photoshop_channels" width="222" height="180" class="alignright size-full wp-image-48" />After drawing out and writing down what would happen to each pixel, translating it into a gradient was pretty easy.  A gray scale image consists of 255 levels, 0 being black and 255 being white.  If you&#8217;re reading this you probably already know that.  You also know that any digital image is made up of three channels: Red, green and blue.  In Photoshop, you can see these channels individually by opening the channels window.  This will be important, or at least handy to know later.  SO, converting those levels into hex you get 00 (black, -30px offset), 80 (middle gray, 0 offset) and FF (white, +30 offset).</p>
<p>I didn&#8217;t want the whole image to be distorted, so part of the image needed to remain unchanged.  This is where the middle-gray comes in.  The gray fades in from the bottom to about a third of the way up the image and from there it is the same even color all the way up.<br />
<img src="http://lab.joelgillman.com/thepress/wp-content/uploads/2008/09/displacement02.png" alt="" title="My second, more successful attempt" width="450" height="327" class="alignnone size-full wp-image-35" /></p>
<p>If you break it out into channels you get this:<br />
<img src="http://lab.joelgillman.com/thepress/wp-content/uploads/2008/09/displacement02_breakout.png" alt="" title="Channel breakout of the displacement map" width="450" height="327" class="alignnone size-full wp-image-35" /><br />
For this example I am only using the RED and BLUE channels (though since the BLUE and GREEN are the same in this case, they are interchangeable).  The BLUE channel goes from black to gray meaning that pixels will be moved in a negative direction, this is the first part of the skewed illusion.  The second, and most important part, comes from the RED channel.  Starting with the most extreme black (00) and fading to the most extreme red (FF), we get our most extreme distortion. Cowabunga!<br />
From there, as we move up, the image fades more and more to middle-gray creating less and less distortion until there is no distortion at all.</p>
<p>At first I used a picture of some pretty flowers to test the distortion, but I realized that with a real photo, it wasn&#8217;t very obvious what had actually been morphed.  I ditched it for a good ol&#8217; grid.  The grid makes it ridiculously easy to tell what&#8217;s been morphed, plus it looks like a cool Tron-esque world.<br />
<img src="http://lab.joelgillman.com/thepress/wp-content/uploads/2008/09/grid.jpg" alt="" title="Grid to be displaced" width="450" height="327" class="alignnone size-full wp-image-35" /></p>
<hr class="space"/>
<p>Check out the <a href="http://lab.joelgillman.com/thepress/wp-content/uploads/2008/09/false3d_demo.html">working demo</a>. (click to turn the distortion on and off)</p>
<hr class="space"/>
<p><a href="http://lab.joelgillman.com/thepress/wp-content/uploads/2008/09/false3d_demo.zip">Download the source</a>.  All code is EXPLICITLY commented.</p>
<hr class="space"/>
<p>A few sites that I found helpful:
<ul>
<li><a href="http://objectpainters.com/blog/2007/01/09/getting-the-displacement-you-need/">Getting the Displacement Your Need</a></li>
<li><a href="http://www.terrypaton.com/as3-bitmap-displacement/">AS3 Bitmap Displacement</a></li>
<li><a href="http://www.emanueleferonato.com/2007/12/03/understanding-flash-displacement-map-filter/">Understanding Flash Displacement Map Filter</a></li>
</ul>
<hr class="space"/>
<p>After all of that the one thing that I still don&#8217;t get is <em>why</em> when the image is distorted why it looks so crappy.  I&#8217;ve tried using different image formats (jpeg vs png) and I&#8217;ve even dynamically drawn in a vector grid, but it still give me that pixelly crap.  Anyone have an answer? (<a href="#comment-2087">Philipp does!</a>)</p>
<p>So it&#8217;s cleaner looking now, but there&#8217;s still some monster pixelation going on.  Anyone know how to .smoothing=true for a displacement map?  Displacement maps need bitmapData and I don&#8217;t think you can do too much to that in the way of smoothing&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://lab.joelgillman.com/archives/30_false-3d-in-flash-displacement-maps/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
