<?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>nulldesign // lars gerckens &#187; Actionscript</title>
	<atom:link href="http://www.nulldesign.de/category/actionscript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nulldesign.de</link>
	<description>blog &#38; portfolio</description>
	<lastBuildDate>Mon, 30 Jan 2012 21:39:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>ND2D best practices &#8211; howto</title>
		<link>http://www.nulldesign.de/2012/01/30/nd2d-best-practices-how-to/</link>
		<comments>http://www.nulldesign.de/2012/01/30/nd2d-best-practices-how-to/#comments</comments>
		<pubDate>Mon, 30 Jan 2012 21:39:51 +0000</pubDate>
		<dc:creator>lars</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Molehill / Stage3D]]></category>
		<category><![CDATA[ND2D]]></category>
		<category><![CDATA[Talk]]></category>

		<guid isPermaLink="false">http://www.nulldesign.de/?p=734</guid>
		<description><![CDATA[I just released version 0.9.13 of my Stage3D engine. Meanwhile is ND2D in a very good and stable state. All features that I planned to integrate, are implemented and working. It&#8217;s very close to v 1.0. So it&#8217;s about time to have a little detailed »best practice and how to« post. This post is meant &#8230; <a href="http://www.nulldesign.de/2012/01/30/nd2d-best-practices-how-to/">Read more <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I just released version 0.9.13 of my Stage3D engine. Meanwhile is <a href="https://github.com/nulldesign/nd2d" target="_blank">ND2D</a> in a very good and stable state. All features that I planned to integrate, are implemented and working. It&#8217;s very close to v 1.0. So it&#8217;s about time to have a little detailed »best practice and how to« post. This post is meant for the traditional flash developer who has never touched a GPU (The processor on your graphics card) accelerated environment. There are significant differences in this GPU powered world and you have to think and prepare your assets in a different way, than you used to. Let&#8217;s start:</p>
<p><strong>What is ND2D?<br />
</strong></p>
<p>ND2D is a GPU accelerated 2D game engine, that makes use of the new Stage3D features introduced in Flash Player 11 (Also known as Molehill). It has nothing to do with the traditional flash display list and runs on a different &#8220;layer&#8221;, behind all flash content. If you want to get a little low level knowledge, read <a href="http://www.bytearray.org/?p=2555" target="_blank">Thibault&#8217;s article here</a>. Using the GPU, the flash player is able to render full screen HD content at 60hz&#8230; Finally a dream comes true. Of course Stage3D is mainly focused on 3D, but we can make good use of the hardware for a 2D engine as well and speed things up a lot.</p>
<p><strong>A GPU Environment</strong></p>
<p>First of all, let&#8217;s try to understand a little, how 2D rendering on a GPU works. Actually, the GPU can only deal with 3D data. To render 2D, we just don&#8217;t use the third dimension. So you could call ND2D a &#8220;planes-in-3D-space-engine&#8221; if you like.</p>
<p>Unfortunately, the GPU can only deal with triangles (A triangle is also called a polygon in the 3D world). To render a sprite, we need construct a quad out of two triangles like this:</p>
<p><img class="alignnone size-full wp-image-735" title="texture_uv" src="http://www.nulldesign.de/wp-content/uploads/2012/01/texture_uv.jpg" alt="" width="200" height="200" /></p>
<p>Next we have to specify, which part of our bitmap is mapped to which corner of our quad. This is called UV mapping. As you see in the picture above, the top left corner has a UV coordinate of (0, 0), which is the top left pixel of our bitmap. The lower right corner UV(1,1) is of course the lower rightmost pixel of our image. The GPU interpolates between these coordinates and know&#8217;s which pixel to choose for a UV(0.5, 0.5) coordinate (If our image is 128&#215;128 px, it chooses the pixel 64,64, this is called sampling). One important thing is, that the GPU can only handle textures sizes, that are a power of 2 (32&#215;32, 64&#215;32, 128&#215;128, 256&#215;64, etc.). In the above example, a lot of space and therefor texture memory is wasted, because ND2D has to blow up the 68&#215;68 sized PNG of the little bacteria and create a 128&#215;128 texture. So keep the power of two (2^n) in mind, when exporting your images. Later we&#8217;ll get to know the TextureAtlas and it&#8217;s tools, which will take take of the unused space problem automatically.</p>
<p>So we need to pass all this information to the GPU: A quad/triangle definition, UV coordinates, the bitmap (on the GPU it&#8217;s called a texture). All of this is done internally in ND2D. You only have to deal with these low level details, if you want to create own objects or write your own materials and shaders.</p>
<p><strong>The display hierarchy and it&#8217;s limitations</strong></p>
<p>To mimic the displaylist, ND2D has a similar hierarchy compared to the flash displaylist. It feels very similar, albeit there are significant differences we&#8217;ll get to know now. Everything in ND2D is a Node2D which can have a number of childs, just like in your normal flash display list. The drawing is done from back to front of course. The draw loop starts with the topmost parent and continues with the childs. This is no different to flash&#8217;s displaylist.</p>
<p>One thing that&#8217;s very important to know, basically the most important thing when you&#8217;re dealing with a GPU environment is »how« things are sent to the GPU and being drawn. Keep this in mind, this is the bottleneck and the reason for low speed in your game: We have to try to sent as less data to the GPU and call as less methods as possible! Unfortunately an engine like ND2D or any other engine can&#8217;t automate this process. Let me give you an example:</p>
<p>You&#8217;re building a game where you have hundreds or even thousands of fluffy little bunnies on the screen. If you now would create 1000 Sprite2D&#8217;s, ND2D has to send 2000 triangles and 1000 textures to the GPU and the GPU would have to draw them one by one, which would be just very slow. This might be slower that a traditional <a href="http://www.adobe.com/devnet/flash/articles/blitting_mc.html" target="_blank">blitting</a> approach. But don&#8217;t give up so fast: There is batching. The GPU has methods, that allow ND2D to sent the data for 1000 sprites as one single data package instead of 1000 little one&#8217;s. The downside is, that the texture of all these 1000 sprites has to be the same. That&#8217;s the limitation: Batching is only possible, if the texture of the batched nodes is the same! Good for us, if we want to display 1000 bunnies that all look the same, but what if we have lot&#8217;s of different looking bunnies we want to display? We can&#8217;t get back to rendering them all one by one, this would be slow&#8230;</p>
<p><strong>TextureAtlases / SpriteSheets</strong></p>
<p>Behold! There&#8217;s always a solution and this is called a TextureAtlas. When the limitation is, that all sprites have to have the same texture, then why not just put all graphics we have in one bigger texture:</p>
<p><a href="http://www.nulldesign.de/wp-content/uploads/2012/01/textureatlas.jpg"><img class="alignnone size-full wp-image-736" title="textureatlas" src="http://www.nulldesign.de/wp-content/uploads/2012/01/textureatlas.jpg" alt="" width="128" height="128" /></a></p>
<p>By changing the UV coordinates for each sprite, we can specify which part of the texture should be drawn for our sprite. There are a few good tools, that help you to generate a TextureAtlas (A bitmap that has a size of 2^n). You don&#8217;t have to do this by hand. ND2D currently supports these tools:</p>
<p>- <a href="http://www.texturepacker.com/" target="_blank">TexturePacker</a> (cocos2d + cocos2d-0.99.4 format)<br />
- <a href="http://zwoptexapp.com/" target="_blank">Zwoptex App</a> (zwoptex-default format)</p>
<p>This is the main difference to traditional flash. Instead of getting your assets one by one from a library, you &#8220;bake&#8221; them all in a big PNG. And that&#8217;s the way you should go. If, for some reason, you need a dynamic approach and generate this atlas on the fly, you can check out the &#8220;<a href="http://docs.rsnewmedia.co.uk/display/nd2ddynatlas/Releases" target="_blank">nd2d-dynatlas</a>&#8221; extension built by wjammal (thanks mate!).</p>
<p><strong>Using a batch</strong></p>
<p>ND2D provides two different kind of batches: The Sprite2DCloud and the Sprite2DBatch (I&#8217;ll explain the differences later). You just create a batch, pass it the TextureAtlas and the Texture2D and start to add children&#8217;s:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> atlasTex:Texture2D = Texture2D.<span style="color: #006600;">textureFromBitmapData</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> textureAtlasBitmap<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">bitmapData</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> atlas:TextureAtlas = <span style="color: #000000; font-weight: bold;">new</span> TextureAtlas<span style="color: #66cc66;">&#40;</span>atlasTex.<span style="color: #006600;">bitmapWidth</span>, atlasTex.<span style="color: #006600;">bitmapHeight</span>, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">XML</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> textureAtlasXML<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>, TextureAtlas.<span style="color: #006600;">XML_FORMAT_ZWOPTEX</span>, <span style="color: #cc66cc;">5</span>, <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
batch = <span style="color: #000000; font-weight: bold;">new</span> Sprite2DBatch<span style="color: #66cc66;">&#40;</span>atlasTex<span style="color: #66cc66;">&#41;</span>;
batch.<span style="color: #006600;">setSpriteSheet</span><span style="color: #66cc66;">&#40;</span>atlas<span style="color: #66cc66;">&#41;</span>;
&nbsp;
s = <span style="color: #000000; font-weight: bold;">new</span> Sprite2D<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
batch.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>s<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>As you can see, you have to add an empty Sprite2D to the batch. After adding the child to the batch, the batch passes a copy of the TextureAtlas to the sprite. Then you&#8217;re able to set individual frames or animations on that sprite:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">s.<span style="color: #006600;">spriteSheet</span>.<span style="color: #006600;">playAnimation</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;walkingBunny&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>To stop any confusion: A TextureAtlas sometimes is called a SpriteSheet and vice versa. In ND2D, a TextureAtlas means a bitmap containing packed images like in the screenshot above, plus an XML definition that defines the UV coordinates for each sprite. The simpler version is a SpriteSheet, which just contains images of equal sizes and doesn&#8217;t need an XML. You can create SpriteSheets with tools like <a href="http://www.bit-101.com/blog/?p=2939" target="_blank">SWFSheet</a> by <a href="http://www.bit-101.com/blog/" target="_blank">Keith Peiters</a>.</p>
<p><strong>Performance</strong></p>
<p>In an ideal world, you would place all your graphics in one big TextureAtlas and work with just one batch. In reality it&#8217;s not always possible. The size of a texture is limited (On desktop 4096 x 4096 / 2048 x 2048 on mobile) and you sometimes can&#8217;t squeeze all your graphics and animations into it. You might need a second batch with a second texture. You can&#8217;t nest batches and since we live in a hierarchical world, you have to keep in mind, that one batch and all of it&#8217;s children will be drawn before the other! So one batch could deal with all background and level assets, while the upper batch renders the characters and other foreground graphics.</p>
<p>I said, I&#8217;ll explain the difference between a Sprite2DCloud and a Sprite2DBatch and here we go. I won&#8217;t get into technical details here, but there a basically two different methods for batching data. For those who are interested: <a href="http://www.nulldesign.de/2011/04/07/molehill-nd2d-speeding-up-the-engine/" target="_blank">ND2D &#8211; speeding up the engine</a>.</p>
<p>The Sprite2DCloud does more computation on the CPU and delivers a complete package to the GPU, while the Sprite2DBatch receives &#8220;chunks&#8221; of data and processes it on the GPU:</p>
<p><em><strong>Sprite2DCloud: Higher CPU load, lower GPU usage</strong></em><br />
<em><strong> Sprite2DBatch: Lower CPU load, higher GPU usage</strong></em></p>
<p>On a desktop machine with a decent CPU, the cloud will be faster. On machines with a slower CPU or on mobile systems, the batch could be faster. So, I&#8217;m afraid it&#8217;s up to you to choose which batching method you&#8217;d like to use. One more important thing I have to say about the differences: Due to technical limitations (and speed optimizations) the cloud can just render it&#8217;s own children and won&#8217;t render the children&#8217;s children, while the batch will render the full display list tree. No limitations there. I&#8217;d always vote for the batch, even though it&#8217;s a bit slower on a desktop machine, but still powerful enough for our fluffy bunny horde.</p>
<p>There are other objects in ND2D that are fully calculated on the GPU. For example the ParticleSystem2D. Get into detail <a href="http://www.nulldesign.de/2011/10/05/nd2d-particles-gpu-vs-cpu/" target="_blank">here</a>.</p>
<p><strong>Outlook</strong></p>
<p>I mentioned the word »mobile« quite a few times and you might ask, when Stage3D for mobile will be available. I can&#8217;t say when it&#8217;s public, but as you know, Adobe is working hard on it. All I can say, is that ND2D is already ready for mobile. MultiTouchEvent&#8217;s are integrated and a new compressed texture format (ATF) also, which will be released with Stage3D for mobile as well (hopefully).</p>
<p>I hope this post was somehow useful to you and helps you to get started in this new accelerated world. If you have any questions, don&#8217;t hesitate to ask them. ND2D has also a <a href="http://www.nulldesign.de/nd2d/forum/" target="_blank">forum</a> where a lot of questions have been answered.</p>
<p><strong>Resources</strong></p>
<ul>
<li><a href="https://github.com/nulldesign/nd2d" target="_blank">Sources</a></li>
<li><a href="http://www.nulldesign.de/nd2d/forum/" target="_blank">Forum</a></li>
<li><a href="http://www.nulldesign.de/nd2d/docs/" target="_blank">Documentation</a></li>
<li><a href="https://github.com/nulldesign/nd2d/wiki" target="_blank">Wiki</a></li>
<li><a href="http://blog.tabinda.net/actionscript/wck-vs-nape/" target="_blank">WCK / Nape integration</a></li>
<li>Getting started Videos: <a href="http://www.youtube.com/watch?v=cysug6VNXPM" target="_blank">Part 1</a> | <a href="http://www.youtube.com/watch?v=sSeXdB5wKuU" target="_blank">Part 2</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.nulldesign.de/2012/01/30/nd2d-best-practices-how-to/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Adobe AIR Native Extensions for iOS, Learnings</title>
		<link>http://www.nulldesign.de/2012/01/22/adobe-air-native-extensions-for-ios-learnings/</link>
		<comments>http://www.nulldesign.de/2012/01/22/adobe-air-native-extensions-for-ios-learnings/#comments</comments>
		<pubDate>Sun, 22 Jan 2012 14:08:26 +0000</pubDate>
		<dc:creator>lars</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Talk]]></category>

		<guid isPermaLink="false">http://www.nulldesign.de/?p=726</guid>
		<description><![CDATA[In my current client project, we&#8217;re developing an AIR application targeted for iOS (Android will follow) and we wanted to make use of some iOS SDK features, so I had to write my first NativeExtension. Developing the Objective C part is pretty straight forward (If you know C++ and Objective C) and so is the &#8230; <a href="http://www.nulldesign.de/2012/01/22/adobe-air-native-extensions-for-ios-learnings/">Read more <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In my current client project, we&#8217;re developing an AIR application targeted for iOS (Android will follow) and we wanted to make use of some iOS SDK features, so I had to write my first <a href="http://www.adobe.com/devnet/air/native-extensions-for-air.html" target="_blank">NativeExtension</a>. Developing the Objective C part is pretty straight forward (If you know C++ and Objective C) and so is the Actionscript part. There are some good examples and tutorials on the <a href="http://www.adobe.com/devnet/air/native-extensions-for-air.html" target="_blank">Adobe site</a> about all kind of extensions.</p>
<p>The hard part was to get this thing to work. So I just wanted to share my settings here. This might become useful, if you&#8217;re starting to develop your first ANE. I had strange crashes when I packaged the app with my ANE and I couldn&#8217;t figure out what was wrong. The app just crashed everytime I launched it on the device. The crashlog wasn&#8217;t very helpful. After quite a search, I found out, that I didn&#8217;t set an apparently important compiler flag for the LLVM compiler in my XCode project. So, be sure to set:</p>
<p><strong>Enable Linking With Shared Libraries: No</strong></p>
<p>And if you want to get rid of the warnings:</p>
<p><strong>Warnings: Missing Function Prototypes: No</strong></p>
<p>The second part was packaging the ANE correctly. The working command for my case is:</p>
<p><strong>adt -package -target ane MyExtension.ane extension.xml -swc MyExtension.swc -platform iPhone-ARM library.swf libMyNativeExtensionIOS.a</strong></p>
<p>The annoying thing about packaging the ANE is, that after you have built your swc, you have to extract the library.swf out of it (By renaming it to .zip and extracting the swf). So you need both, the swc AND the swf. I didn&#8217;t write an ANT task to do automate the process until now and I don&#8217;t know the reason for this strange step, since the ADT compiler has everything it needs within the swc. Only Adobe knows ;)</p>
<p>Obviously you can not test on the device everytime, because the deployment process to iOS is more or less manual and just takes too long at the moment. I found out, that I could link the ANE as a regular library (SWC) in my Flash Builder project and launch the app on my desktop machine. When the native extension tries to create the context on the desktop machine, it fails and returns null, because it was just built for the iOS platform:</p>
<p><strong>context = ExtensionContext.createExtensionContext(EXTENSION_ID, null);</strong></p>
<p>So I could implement a fallback for the extention when running on the desktop that mocked the behaviour in AS3. To package the application for iOS, I wrote a small ANT task. This way we can easily test on the device and have a fallback, when testing 0n the desktop without writing desktop extensions as well.</p>
<p>So, maybe someone will find this useful&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nulldesign.de/2012/01/22/adobe-air-native-extensions-for-ios-learnings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ND2D &#8211; Stage3D Masks</title>
		<link>http://www.nulldesign.de/2011/09/02/nd2d-stage3d-masks/</link>
		<comments>http://www.nulldesign.de/2011/09/02/nd2d-stage3d-masks/#comments</comments>
		<pubDate>Fri, 02 Sep 2011 14:39:15 +0000</pubDate>
		<dc:creator>lars</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Molehill / Stage3D]]></category>
		<category><![CDATA[ND2D]]></category>
		<category><![CDATA[Source]]></category>

		<guid isPermaLink="false">http://www.nulldesign.de/?p=633</guid>
		<description><![CDATA[Another feature I really wanted to implement in ND2D were masks. Just like the setMask() method in flash. In Stage3D (OpenGL), there is no such thing as a mask. You can display textured triangles, that&#8217;s it, but you know that nearly everything is possible with a pixel shader. So let&#8217;s start: The idea of masking &#8230; <a href="http://www.nulldesign.de/2011/09/02/nd2d-stage3d-masks/">Read more <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Another feature I really wanted to implement in <a href="https://github.com/nulldesign/nd2d" target="_blank">ND2D</a> were masks. Just like the setMask() method in flash. In Stage3D (OpenGL), there is no such thing as a mask. You can display textured triangles, that&#8217;s it, but you know that nearly everything is possible with a pixel shader. So let&#8217;s start:</p>
<p>The idea of masking in a fragment shader is to grab the pixel color of your texture, then grab the pixel color of your mask, multiply the two colors and display the result. But how do we find the correct pixel in the mask? Our task is to find the right UV coordinates for the mask texture.</p>
<p><img class="alignnone size-full wp-image-636" title="masking_uv" src="http://www.nulldesign.de/wp-content/uploads/2011/09/masking_uv.png" alt="" width="502" height="287" /></p>
<p>If you look at the above image, the mask is rotated and overlaps the sprite we want to mask. How do we find the correct pixel (UV coordinate) of the mask, that overlaps this orange pixel in the sprite? Somehow we have to map the position of the pixel in the sprite to the pixel in the mask and we can do that by transforming it between the different coordinate systems. In a vertex shader we calculate the final pixel positon from local space to world space. The idea is to map this pixel in world space back to the local coordinate system of the mask. This way it&#8217;s pretty easy to find the correct UV coordinates. Let&#8217;s do a simple actionscript test:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// this is the top right corner of our sprite quad.</span>
<span style="color: #000000; font-weight: bold;">var</span> v:Vector3D = <span style="color: #000000; font-weight: bold;">new</span> Vector3D<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span>, -<span style="color: #cc66cc;">128</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// this is the sprites matrix, translated a bit</span>
<span style="color: #000000; font-weight: bold;">var</span> clipSpaceMatrix:Matrix3D = <span style="color: #000000; font-weight: bold;">new</span> Matrix3D<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
clipSpaceMatrix.<span style="color: #006600;">appendTranslation</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// this is the masks matrix, it's in the same position as the sprite</span>
<span style="color: #000000; font-weight: bold;">var</span> maskClipSpaceMatrix:Matrix3D = <span style="color: #000000; font-weight: bold;">new</span> Matrix3D<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
maskClipSpaceMatrix.<span style="color: #006600;">appendTranslation</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">// this is the masks size</span>
<span style="color: #000000; font-weight: bold;">var</span> maskBitmap:Rectangle = <span style="color: #000000; font-weight: bold;">new</span> Rectangle<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">256</span>, <span style="color: #cc66cc;">256</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// invert the matrix, because we want to map back from world space to local mask space</span>
maskClipSpaceMatrix.<span style="color: #006600;">invert</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// transform our vertex from local sprite space to world space</span>
v = clipSpaceMatrix.<span style="color: #006600;">transformVector</span><span style="color: #66cc66;">&#40;</span>v<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#93;</span> moved to clipspace: Vector3D<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">228</span>, -<span style="color: #cc66cc;">128</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// transform world space vertex back to local mask space</span>
<span style="color: #808080; font-style: italic;">// the result is the same vector of course, because the positions of mask and sprite are equal</span>
v = maskClipSpaceMatrix.<span style="color: #006600;">transformVector</span><span style="color: #66cc66;">&#40;</span>v<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#93;</span> moved to local mask <span style="color: #0066CC;">space</span>: Vector3D<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span>, -<span style="color: #cc66cc;">128</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// calculate the uv coordinates from the local pixel position</span>
v = <span style="color: #000000; font-weight: bold;">new</span> Vector3D<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>v.<span style="color: #006600;">x</span> + <span style="color: #66cc66;">&#40;</span>maskBitmap.<span style="color: #0066CC;">width</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">0.5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">/</span> maskBitmap.<span style="color: #0066CC;">width</span>,
                 <span style="color: #66cc66;">&#40;</span>v.<span style="color: #006600;">y</span> + <span style="color: #66cc66;">&#40;</span>maskBitmap.<span style="color: #0066CC;">height</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">0.5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">/</span> maskBitmap.<span style="color: #0066CC;">height</span>,
                  <span style="color: #cc66cc;">0.0</span>, <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// the result is what we expect, the top right uv coordinate:</span>
<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#93;</span> local mask uv: Vector3D<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Porting this idea to a shader is pretty straight forward. Let&#8217;s code a PB3D Material Shader:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">void</span> evaluateVertex<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
     interpolatedUV = float4<span style="color: #66cc66;">&#40;</span>uvCoord.<span style="color: #006600;">x</span> + uvOffset.<span style="color: #006600;">x</span>, uvCoord.<span style="color: #006600;">y</span> + uvOffset.<span style="color: #006600;">y</span>, <span style="color: #cc66cc;">0.0</span>, <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
     float4 worldSpacePos = float4<span style="color: #66cc66;">&#40;</span>vertexPos.<span style="color: #006600;">x</span>, vertexPos.<span style="color: #006600;">y</span>, <span style="color: #cc66cc;">0.0</span>, <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> objectToClipSpaceTransform;
     <span style="color: #808080; font-style: italic;">// maskObjectToClipSpaceTransform is the invertex clipspace matrix of the mask</span>
     float4 localMaskSpacePos = worldSpacePos <span style="color: #66cc66;">*</span> maskObjectToClipSpaceTransform;
&nbsp;
     <span style="color: #808080; font-style: italic;">// halfMaskSize.xy is maskBitmap.width/height * 0.5 passed as a parameter</span>
     <span style="color: #808080; font-style: italic;">// invertedMaskSize.xy = 1.0 / maskBitmap.width/height passed as a parameter, because divisions are not properly working in the current pb3d release</span>
     interpolatedMaskUV = float4<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>localMaskSpacePos.<span style="color: #006600;">x</span> + halfMaskSize.<span style="color: #006600;">x</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> invertedMaskSize.<span style="color: #006600;">x</span>,
                                 <span style="color: #66cc66;">&#40;</span>localMaskSpacePos.<span style="color: #006600;">y</span> + halfMaskSize.<span style="color: #006600;">y</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> invertedMaskSize.<span style="color: #006600;">y</span>,
                                  <span style="color: #cc66cc;">0.0</span>, <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0066CC;">void</span> evaluateFragment<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    float4 texel = sample<span style="color: #66cc66;">&#40;</span>textureImage, float2<span style="color: #66cc66;">&#40;</span>interpolatedUV.<span style="color: #006600;">x</span>, interpolatedUV.<span style="color: #006600;">y</span><span style="color: #66cc66;">&#41;</span>, PB3D_2D <span style="color: #66cc66;">|</span> PB3D_MIPNEAREST <span style="color: #66cc66;">|</span> PB3D_CLAMP<span style="color: #66cc66;">&#41;</span>;
    float4 texel2 = sample<span style="color: #66cc66;">&#40;</span>textureMaskImage, float2<span style="color: #66cc66;">&#40;</span>interpolatedMaskUV.<span style="color: #006600;">x</span>, interpolatedMaskUV.<span style="color: #006600;">y</span><span style="color: #66cc66;">&#41;</span>, PB3D_2D <span style="color: #66cc66;">|</span> PB3D_MIPNEAREST <span style="color: #66cc66;">|</span> PB3D_CLAMP<span style="color: #66cc66;">&#41;</span>;
&nbsp;
    result = float4<span style="color: #66cc66;">&#40;</span>texel.<span style="color: #006600;">r</span> <span style="color: #66cc66;">*</span> <span style="color: #0066CC;">color</span>.<span style="color: #006600;">r</span> <span style="color: #66cc66;">*</span> texel2.<span style="color: #006600;">r</span>,
                    texel.<span style="color: #006600;">g</span> <span style="color: #66cc66;">*</span> <span style="color: #0066CC;">color</span>.<span style="color: #006600;">g</span> <span style="color: #66cc66;">*</span> texel2.<span style="color: #006600;">g</span>,
                    texel.<span style="color: #006600;">b</span> <span style="color: #66cc66;">*</span> <span style="color: #0066CC;">color</span>.<span style="color: #006600;">b</span> <span style="color: #66cc66;">*</span> texel2.<span style="color: #006600;">b</span>,
                    texel.<span style="color: #006600;">a</span> <span style="color: #66cc66;">*</span> <span style="color: #0066CC;">color</span>.<span style="color: #006600;">a</span> <span style="color: #66cc66;">*</span> texel2.<span style="color: #006600;">a</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>If you don&#8217;t want to use PixelBender3D and like to &#8216;torture&#8217; yourself with AGAL, you can write the same shader this way:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/*
vertex shader:
&nbsp;
vc0-vc3 = clipspace matrix of sprite
vc4-vc7 = inverted clipspace matrix of mask
vc8.xy = half mask width / height
vc8.zw = mask width / height
va0 = vertex
va1 = uv
*/</span>
&nbsp;
m44 vt0, va0, vc0           <span style="color: #808080; font-style: italic;">// vertex * clipspace</span>
m44 vt1, vt0, vc4           <span style="color: #808080; font-style: italic;">// clipspace to local pos in mask</span>
<span style="color: #0066CC;">add</span> vt1.<span style="color: #006600;">xy</span>, vt1.<span style="color: #006600;">xy</span>, vc8.<span style="color: #006600;">xy</span>  <span style="color: #808080; font-style: italic;">// add half masksize to local pos</span>
div vt1.<span style="color: #006600;">xy</span>, vt1.<span style="color: #006600;">xy</span>, vc8.<span style="color: #006600;">zw</span>  <span style="color: #808080; font-style: italic;">// local pos / masksize</span>
mov v0, va1                 <span style="color: #808080; font-style: italic;">// copy uv</span>
mov v1, vt1                 <span style="color: #808080; font-style: italic;">// copy mask uv</span>
mov op, vt0                 <span style="color: #808080; font-style: italic;">// output position</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*
fragment shader:
*/</span>
&nbsp;
mov ft0, v0                                <span style="color: #808080; font-style: italic;">// get interpolated uv coords</span>
tex ft1, ft0, fs0 <span style="color: #66cc66;">&lt;</span>2d,clamp,linear,nomip<span style="color: #66cc66;">&gt;</span>  <span style="color: #808080; font-style: italic;">// sample texture</span>
mov ft2, v1                                <span style="color: #808080; font-style: italic;">// get interpolated uv coords for mask</span>
tex ft3, ft2, fs1 <span style="color: #66cc66;">&lt;</span>2d,clamp,linear,nomip<span style="color: #66cc66;">&gt;</span>  <span style="color: #808080; font-style: italic;">// sample mask</span>
mul ft1, ft1, ft3                          <span style="color: #808080; font-style: italic;">// mult mask color with tex color</span>
mov oc, ft1                                <span style="color: #808080; font-style: italic;">// output color</span></pre></div></div>

<p>The result is visible here: <a href="http://www.nulldesign.de/nd2d/tests/mask_alpha.html" target="_blank">ND2D &#8211; alpha masks</a> (Move your mouse over the crates). I added one more feature: You can set the alpha of a mask, that means that you can specify how much the mask affects the sprite. In the demo above the alpha fades from 0.0 to 1.0. Since we&#8217;re using all four color components in our calculations (r,g,b,a), we can not only mask the alpha, but all color channels. I don&#8217;t know if this it&#8217;s a &#8220;nice thing to have&#8221; or if it will get annoying when you use sprites as masks in your game and need to provide an extra image for that. Just let me know :) Here is the example: <a href="http://www.nulldesign.de/nd2d/tests/mask_color.html" target="_blank">ND2D &#8211; disco color masks</a>.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.nulldesign.de/2011/09/02/nd2d-stage3d-masks/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>F10 Astro Blackhole</title>
		<link>http://www.nulldesign.de/2008/09/30/f10-astro-blackhole/</link>
		<comments>http://www.nulldesign.de/2008/09/30/f10-astro-blackhole/#comments</comments>
		<pubDate>Tue, 30 Sep 2008 08:31:36 +0000</pubDate>
		<dc:creator>lars</dc:creator>
				<category><![CDATA[3D]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Experiments]]></category>
		<category><![CDATA[Flash 10]]></category>
		<category><![CDATA[Particles]]></category>
		<category><![CDATA[Source]]></category>

		<guid isPermaLink="false">http://www.nulldesign.de/?p=182</guid>
		<description><![CDATA[The Flash 10 beta player is out for a while and I found a few minutes to try out the new native 3D effects. You can get quite nice and fast results out of the new API if you only want to display flat 2D planes in 3D-space: (Space = fullscreen, Download source, Flash Player &#8230; <a href="http://www.nulldesign.de/2008/09/30/f10-astro-blackhole/">Read more <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://labs.adobe.com/technologies/flashplayer10/">Flash 10 beta player</a> is out for a while and I found a few minutes to try out the new native 3D effects. You can get quite nice and fast results out of the new API if you only want to display flat 2D planes in 3D-space:</p>
<p style="text-align: center;"><a href="http://www.nulldesign.de/exp/expviewer.php?file=f10_planets.swf&amp;width=600&amp;height=450"><img class="aligncenter" src="http://www.nulldesign.de/exp/f10_planets.jpg" alt="" width="480" height="305" /></a><br />
(Space = fullscreen, <a href="http://www.nulldesign.de/exp/f10_planets.zip">Download source</a>, Flash Player 10 needed)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nulldesign.de/2008/09/30/f10-astro-blackhole/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>ND3D engine goes google.code</title>
		<link>http://www.nulldesign.de/2008/08/07/nd3d-engine-goes-googlecode/</link>
		<comments>http://www.nulldesign.de/2008/08/07/nd3d-engine-goes-googlecode/#comments</comments>
		<pubDate>Thu, 07 Aug 2008 10:30:45 +0000</pubDate>
		<dc:creator>lars</dc:creator>
				<category><![CDATA[3D]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Experiments]]></category>
		<category><![CDATA[Flash 9]]></category>
		<category><![CDATA[Source]]></category>

		<guid isPermaLink="false">http://www.nulldesign.de/?p=105</guid>
		<description><![CDATA[Good news everyone! I packed everything together, cleaned up a bit and created a google.code project for my 3D engine. Since it became popular under the name &#8216;nulldesign&#8217;s 3d engine&#8217; I call it ND3D from now on ;). I will post additional infos, more examples and future developments here in my blog and on the &#8230; <a href="http://www.nulldesign.de/2008/08/07/nd3d-engine-goes-googlecode/">Read more <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Good news everyone! I packed everything together, cleaned up a bit and created a <a href="http://code.google.com/p/nd3d/">google.code project</a> for my 3D engine. Since it became popular under the name &#8216;nulldesign&#8217;s 3d engine&#8217; I call it ND3D from now on ;). I will post additional infos, more examples and future developments <a href="http://www.nulldesign.de/nd3d-as3-3d-engine/">here</a> in my blog and on the project page. So long&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nulldesign.de/2008/08/07/nd3d-engine-goes-googlecode/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Flex: Adding a tooltip to truncated Datagrid columns</title>
		<link>http://www.nulldesign.de/2008/06/13/flex-adding-a-tooltip-to-truncated-datagrid-columns/</link>
		<comments>http://www.nulldesign.de/2008/06/13/flex-adding-a-tooltip-to-truncated-datagrid-columns/#comments</comments>
		<pubDate>Fri, 13 Jun 2008 08:22:41 +0000</pubDate>
		<dc:creator>lars</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://www.nulldesign.de/?p=66</guid>
		<description><![CDATA[I just found a nice way to automatically truncate text in datagrid columns and show tooltips for truncated elements, that I wanted to share: package &#123; import mx.controls.dataGridClasses.DataGridItemRenderer; &#160; public class TruncateToolTipRenderer extends DataGridItemRenderer &#123; private var textTruncated:Boolean = false; private var originalText:String; &#160; public function TruncateDataGridItemRenderer&#40;&#41; &#123; super&#40;&#41;; &#125; &#160; override public function set &#8230; <a href="http://www.nulldesign.de/2008/06/13/flex-adding-a-tooltip-to-truncated-datagrid-columns/">Read more <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I just found a nice way to automatically truncate text in datagrid columns and show tooltips for truncated elements, that I wanted to share:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">controls</span>.<span style="color: #006600;">dataGridClasses</span>.<span style="color: #006600;">DataGridItemRenderer</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TruncateToolTipRenderer <span style="color: #0066CC;">extends</span> DataGridItemRenderer
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> textTruncated:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> originalText:<span style="color: #0066CC;">String</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> TruncateDataGridItemRenderer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> <span style="color: #0066CC;">text</span><span style="color: #66cc66;">&#40;</span>value:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">super</span>.<span style="color: #0066CC;">text</span> = value;
			originalText = value;
			textTruncated = truncateToFit<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> validateProperties<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">super</span>.<span style="color: #006600;">validateProperties</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			toolTip = textTruncated ? originalText : <span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Just set the itemRenderer property of your column to the new TruncateToolTipRenderer. Neat!</p>
<p><strong>Update:</strong> &#8230; and it&#8217;s a lot easier if you just set the itemRenderer to mx.controls.Label, because a Label already has truncate and tooltip functionality. As so often in Flex, everything is built in. Lesson learned!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nulldesign.de/2008/06/13/flex-adding-a-tooltip-to-truncated-datagrid-columns/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>3D Engine Sources + Examples</title>
		<link>http://www.nulldesign.de/2008/04/09/3d-engine-sources-examples/</link>
		<comments>http://www.nulldesign.de/2008/04/09/3d-engine-sources-examples/#comments</comments>
		<pubDate>Wed, 09 Apr 2008 12:31:21 +0000</pubDate>
		<dc:creator>lars</dc:creator>
				<category><![CDATA[3D]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Experiments]]></category>
		<category><![CDATA[Flash 9]]></category>
		<category><![CDATA[Source]]></category>

		<guid isPermaLink="false">http://www.nulldesign.de/?p=61</guid>
		<description><![CDATA[About some time ago I started to code my own 3D engine in flash. Derived from a small AS2 project, I challenged myself to built my own flash 3D engine. So I took out my good old Actionscript Animation Book and opened the 3D chapter. Very soon I could move a cube around. A few &#8230; <a href="http://www.nulldesign.de/2008/04/09/3d-engine-sources-examples/">Read more <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>About some time ago I started to code my own 3D engine in flash. Derived from a small AS2 project, I challenged myself to built my own flash 3D engine. So I took out my good old <a href="http://www.friendsofed.com/book.html?isbn=1590595181" target="_blank">Actionscript Animation Book</a> and opened the 3D chapter. Very soon I could move a <a href="http://www.nulldesign.de/2006/11/10/solid3dengine/" target="_blank">cube</a> around. A few pages later I learned how to implement simple <a href="http://www.nulldesign.de/exp/expviewer.php?file=lighting_test.swf&amp;width=800&amp;height=400">dynamic lighting</a>. The next challenge was to get texture mapping to work. Since flash still can&#8217;t distort images, you need a workaround. After I found these great examples: <a href="http://www.sebleedelisle.com/?page_id=7" target="_blank">Seb Lee-Delisle&#8217;s flash texture maps</a> and <a href="http://session.andre-michelle.com/w3d/" target="_blank">Andre Michelle&#8217;s texture examples</a> it was done. Somewhere inbetween I switched to AS3, which was quickly done. Meanwhile <a href="http://blog.papervision3d.org/" target="_blank">Papervision3D</a> became very popular and I thought it didn&#8217;t make sense to continue evolving my engine. But since I came so far, I needed to find out how to implement a few effects like <a href="http://www.nulldesign.de/exp/expviewer.php?file=3d_blur.swf&amp;width=800&amp;height=400">depth of field</a> or <a href="http://www.nulldesign.de/exp/expviewer.php?file=f9_3d_additive_cubes.swf&amp;width=800&amp;height=400">additive rendering</a> ;)</p>
<p style="text-align: center;"><img src="http://www.nulldesign.de/exp/3dengine_samples.jpg" alt="" width="472" height="473" /></p>
<p>My engine shouldn&#8217;t and doesn&#8217;t compete with <a href="http://blog.papervision3d.org/" target="_blank">Papervision3D</a> or <a href="http://www.flashsandy.org/blog/" target="_blank">Sandy3D</a>, nor it has a very user-friendly API, no stunning effects or animation support, but I learned a lot while building it, understanding 3D to 2D rendering, optimizing the code for a few ms of extra speed (AS3 rocks!) or challenge problems with 3D rotations like <a href="http://en.wikipedia.org/wiki/Gimbal_Lock" target="_blank">gimbal lock</a> and their solutions: <a href="http://en.wikipedia.org/wiki/Quaternion" target="_blank">quaternions</a>. It&#8217;s just another 3D flash engine, at least I can say: I made it! ;)</p>
<p>It&#8217;s undocumented, there&#8217;s still a lot of work to do and it doesn&#8217;t have a cool name , but if you want to play around with it or just take a look how I set up this and that, feel free to download the <a onclick="urchinTracker('3dengine_nulldesign.zip')" href="http://www.nulldesign.de/nd3d-as3-3d-engine/">sources</a> (yes, the <a href="http://www.nulldesign.de/exp/expviewer.php?file=3dribbon.swf&amp;width=700&amp;height=400">3D ribbon</a> example is included). And I&#8217;m always interested in what you think about it, so drop a comment or mail.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nulldesign.de/2008/04/09/3d-engine-sources-examples/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>Collision Tests</title>
		<link>http://www.nulldesign.de/2007/12/19/collision-tests/</link>
		<comments>http://www.nulldesign.de/2007/12/19/collision-tests/#comments</comments>
		<pubDate>Wed, 19 Dec 2007 14:41:32 +0000</pubDate>
		<dc:creator>lars</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Experiments]]></category>
		<category><![CDATA[Flash 9]]></category>

		<guid isPermaLink="false">http://www.nulldesign.de/2007/12/19/collision-tests/</guid>
		<description><![CDATA[As you noticed, I&#8217;m building a small arcarde shooter in AS3 besides my daily work. It started as a small test and got me ;). In my previous post I used getObectsUnderPoint for a hittest between the shot and the enemyclips, this works fine if you don&#8217;t need a collision reaction and the clips travel &#8230; <a href="http://www.nulldesign.de/2007/12/19/collision-tests/">Read more <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>As you noticed, I&#8217;m building a small arcarde shooter in AS3 besides my daily work. It started as a small test and got me ;). In my previous post I used getObectsUnderPoint for a hittest between the shot and the enemyclips, this works fine if you don&#8217;t need a collision reaction and the clips travel at a moderade speed. Now I wanted to integrate a collision detection and reaction for the enemies and made use of some nice scripts I found out there:</p>
<p>A shape-based collision detection for bitmaps by <a target="_blank" href="http://www.gskinner.com/blog/archives/2005/10/source_code_sha.html">Grant Skinner</a>, since the third parameter in hitTestPoint(x:Number, y:Number, shapeFlag:Boolean) only works for vectorimages and I&#8217;m using bitmaps as graphics. A <a target="_blank" href="http://www.gskinner.com/blog/archives/2005/02/source_code_gri.html">Proximity Manager</a>. This small class comes really handy and the principle is so simple. The proximity manager splits the stage into grids and stores for every sprite in which grid they are. So you don&#8217;t need to test every sprite against all other sprites, instead you just say: &#8220;Give me all neighbours of that sprite&#8221;, which saves a lot of loops.</p>
<p>So I had the tools for a nice collision detection, for the collision reaction I converted a simple snooker algorithm to AS3: <a target="_blank" href="http://freespace.virgin.net/hugo.elias/models/m_snokr.htm">Snooker Balls</a>. Watch the result:<br />

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_shooter_collision_2061188538"
			class="flashmovie"
			width="480"
			height="240">
	<param name="movie" value="http://www.nulldesign.de/exp/shooter_collision.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://www.nulldesign.de/exp/shooter_collision.swf"
			name="fm_shooter_collision_2061188538"
			width="480"
			height="240">
	<!--<![endif]-->
		
	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p align="center">Move your mouse over me!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nulldesign.de/2007/12/19/collision-tests/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>It all started as a performance test&#8230;</title>
		<link>http://www.nulldesign.de/2007/12/01/it-all-started-as-a-performance-test/</link>
		<comments>http://www.nulldesign.de/2007/12/01/it-all-started-as-a-performance-test/#comments</comments>
		<pubDate>Sat, 01 Dec 2007 12:25:57 +0000</pubDate>
		<dc:creator>lars</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flash 9]]></category>

		<guid isPermaLink="false">http://www.nulldesign.de/2007/12/01/it-all-started-as-a-performance-test/</guid>
		<description><![CDATA[Ok, AS3 can render thousands of particles per frame&#8230; but what if we want more than a few single moving pixels and hittests? So I started to write a small test, instancing bitmaps on random locations. And hey: These objects look like bombs, now I need something to destroy them &#8230; and there was the &#8230; <a href="http://www.nulldesign.de/2007/12/01/it-all-started-as-a-performance-test/">Read more <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Ok, AS3 can render <a href="http://www.nulldesign.de/2006/11/23/f9-particles/" target="_blank">thousands of particles</a> per frame&#8230; but what if we want more than a few single moving pixels and hittests? So I started to write a small test, instancing bitmaps on random locations. And hey: These objects look like bombs, now I need something to destroy them &#8230; and there was the battleship ;). For the hittest I used <a href="http://livedocs.adobe.com/flex/2/langref/flash/display/DisplayObjectContainer.html#getObjectsUnderPoint()" target="_blank">getObjectsUnderPoint</a>. It seems like this method is a bit slower than hitTest or hitTestObject, but you can actually save a lot of loops because you don&#8217;t need to test each bomb with each shot. Instead of:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// pseudo code</span>
<span style="color: #b1b100;">for</span> each<span style="color: #66cc66;">&#40;</span>bombs<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">for</span> each<span style="color: #66cc66;">&#40;</span>shots<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>bomb.<span style="color: #006600;">hitTestObject</span><span style="color: #66cc66;">&#40;</span>shot<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #808080; font-style: italic;">// shot hit bomb...</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>you go:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// pseudo code</span>
<span style="color: #b1b100;">for</span> each<span style="color: #66cc66;">&#40;</span>bombs<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> objects:<span style="color: #0066CC;">Array</span> = shotClip.<span style="color: #006600;">getObjectsUnderPoint</span><span style="color: #66cc66;">&#40;</span>bomb.<span style="color: #006600;">x</span>, bomb.<span style="color: #006600;">y</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>objects.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
          <span style="color: #808080; font-style: italic;">// shot hit bomb...</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>This only works, if you have a seperate clip for the shots. Enough of the talk: <a href="http://www.nulldesign.de/exp/expviewer.php?file=shooter_test.swf&amp;width=500&amp;height=350" target="_blank">Launch!</a></p>
<p style="text-align: center"> <img src="http://www.nulldesign.de/exp/shooter_test.jpg" height="203" width="450" /><br />
(Move using the mouse and rotate the gun with cursor keys)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nulldesign.de/2007/12/01/it-all-started-as-a-performance-test/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>AS3 Preloading continued</title>
		<link>http://www.nulldesign.de/2007/11/30/as3-preloading-continued/</link>
		<comments>http://www.nulldesign.de/2007/11/30/as3-preloading-continued/#comments</comments>
		<pubDate>Fri, 30 Nov 2007 10:28:13 +0000</pubDate>
		<dc:creator>lars</dc:creator>
				<category><![CDATA[Actionscript]]></category>

		<guid isPermaLink="false">http://www.nulldesign.de/2007/11/30/as3-preloading-continued/</guid>
		<description><![CDATA[I wasn&#8217;t really satisfied with the solution I used for my AS3 preloader (see post: Preloader in AS3 projects). Now Sven found THE solution. It&#8217;s in german, so I try to explain how it works: The class &#8220;Test&#8221; acts as the factory class that&#8217;s responsible for the preloading. The Class &#8220;Demo&#8221; is the main application &#8230; <a href="http://www.nulldesign.de/2007/11/30/as3-preloading-continued/">Read more <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I wasn&#8217;t really satisfied with the solution I used for my AS3 preloader (see post: <a href="http://www.nulldesign.de/2007/11/20/preloader-in-as3-projects-factoryclass/">Preloader in AS3 projects</a>). Now <a href="http://www.ghost23.de/blog/" target="_blank">Sven</a> found <a href="http://www.ghost23.de/blogarchive/2007/11/as3-application.html" target="_blank">THE solution</a>. It&#8217;s in german, so I try to explain how it works:</p>
<p>The class &#8220;Test&#8221; acts as the factory class that&#8217;s responsible for the preloading. The Class &#8220;Demo&#8221; is the main application class. Using the compiler option <strong>-frame frameLabel Demo</strong>,<strong> </strong>the class Demo is compiled into frame 2.</p>
<p>Just like in <a href="http://www.bit-101.com/blog/?p=946" target="_blank">Keith Peter&#8217;s Example</a> and my try using an exclude xml, you can preload a pure AS3 application now, but in this case no flex framework classes are compiled into the resulting SWF and there is no need for an exclude xml. Finally!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nulldesign.de/2007/11/30/as3-preloading-continued/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>AS3 Singletons, the other way</title>
		<link>http://www.nulldesign.de/2007/11/20/as3-singletons-the-other-way/</link>
		<comments>http://www.nulldesign.de/2007/11/20/as3-singletons-the-other-way/#comments</comments>
		<pubDate>Tue, 20 Nov 2007 18:07:16 +0000</pubDate>
		<dc:creator>lars</dc:creator>
				<category><![CDATA[Actionscript]]></category>

		<guid isPermaLink="false">http://www.nulldesign.de/2007/11/20/as3-singletons-the-other-way/</guid>
		<description><![CDATA[Since you can&#8217;t declare a constructor private in AS3, there are a few methods out there to enforce a Singleton. A common way is to declare an internal SingletonEnforcer class outside the package: SingletonEnforcer, but the declaration of the Enforcer outside a package looks a bit &#8220;dirty&#8221; to me. Another way is to declare the &#8230; <a href="http://www.nulldesign.de/2007/11/20/as3-singletons-the-other-way/">Read more <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Since you can&#8217;t declare a constructor private in AS3, there are a few methods out there to enforce a Singleton. A common way is to declare an internal SingletonEnforcer class outside the package: <a href="http://blog.pixelbreaker.com/flash/as30-better-singletons/" target="_blank">SingletonEnforcer</a>, but the declaration of the Enforcer outside a package looks a bit &#8220;<span>dirty</span>&#8221; to me. Another way is to declare the singleton as a <a href="http://www.onflex.org/code/" target="_blank">static variable</a>, but this way you can&#8217;t decide when your singleton is instantiated.</p>
<p>So, why don&#8217;t just try nearly same with a local variable:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Singleton <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> instance:Singleton;
        <span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> const checker:<span style="color: #0066CC;">Object</span> = <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>;
&nbsp;
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Singleton<span style="color: #66cc66;">&#40;</span>initObj:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>initObj <span style="color: #66cc66;">!</span>= checker<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                <span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Private constructor!&quot;</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> getInstance<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Singleton <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>instance == <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                instance = <span style="color: #000000; font-weight: bold;">new</span> Singleton<span style="color: #66cc66;">&#40;</span>checker<span style="color: #66cc66;">&#41;</span>;
            <span style="color: #66cc66;">&#125;</span>
            <span style="color: #b1b100;">return</span> instance;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Update: I found another nice solution by GSkinner: <a href="http://www.gskinner.com/blog/archives/2006/07/as3_singletons.html" target="_blank">SingletonDemo</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nulldesign.de/2007/11/20/as3-singletons-the-other-way/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Preloader in AS3 projects (factoryClass)</title>
		<link>http://www.nulldesign.de/2007/11/20/preloader-in-as3-projects-factoryclass/</link>
		<comments>http://www.nulldesign.de/2007/11/20/preloader-in-as3-projects-factoryclass/#comments</comments>
		<pubDate>Tue, 20 Nov 2007 10:52:01 +0000</pubDate>
		<dc:creator>lars</dc:creator>
				<category><![CDATA[Actionscript]]></category>

		<guid isPermaLink="false">http://www.nulldesign.de/2007/11/20/preloader-in-as3-projects-factoryclass/</guid>
		<description><![CDATA[The last days I was playing around with the Flex Metadata Tag &#8220;Frame&#8221; to implement a preloader in an AS3 project. You might have read Keith Peters post concerning this issue. This method is working fine, except of the fact that I can&#8217;t get rid of the flex framework classes in my compiled SWF, which &#8230; <a href="http://www.nulldesign.de/2007/11/20/preloader-in-as3-projects-factoryclass/">Read more <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The last days I was playing around with the Flex Metadata Tag &#8220;Frame&#8221; to implement a preloader in an AS3 project. You might have read <a href="http://www.bit-101.com/blog/?p=946" target="_blank">Keith Peters post</a> concerning this issue. This method is working fine, except of the fact that I can&#8217;t get rid of the flex framework classes in my compiled SWF, which blows up the size of an nearly empty SWF to 120kb.</p>
<p>If you check out the comments in his post, you&#8217;ll find a few solutions, but none of them worked for me. So I generated a XML exclude File, that excludes the whole flex framework. You might want to give it a try: <a href="http://www.nulldesign.de/stuff/flex_framework_exclude.xml" target="_blank">flex_framework_exclude.xml</a> (compile with the option -load-externs+=flex_framework_exclude.xml). It&#8217;s still not satisfying, but it&#8217;s working&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nulldesign.de/2007/11/20/preloader-in-as3-projects-factoryclass/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Custom right click menus in AIR</title>
		<link>http://www.nulldesign.de/2007/10/31/custom-right-click-menus-in-air/</link>
		<comments>http://www.nulldesign.de/2007/10/31/custom-right-click-menus-in-air/#comments</comments>
		<pubDate>Wed, 31 Oct 2007 20:49:38 +0000</pubDate>
		<dc:creator>lars</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[AIR]]></category>

		<guid isPermaLink="false">http://www.nulldesign.de/2007/10/31/custom-right-click-menus-with-air/</guid>
		<description><![CDATA[I was wondering, why DisplayObjects in AIR don&#8217;t support the MouseEvent.RIGHT_CLICK Event. The only way to implement a right-click-menu seemed to be a NativeMenu. But I needed only the event, not the ugly native-os-menu. Here is the workaround: Just create a blank NativeMenu and listen for the display event. var menu:NativeMenu = new NativeMenu&#40;&#41;; menu.addEventListener&#40;Event.DISPLAYING, &#8230; <a href="http://www.nulldesign.de/2007/10/31/custom-right-click-menus-in-air/">Read more <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I was wondering, why DisplayObjects in AIR don&#8217;t support the <em>MouseEvent.RIGHT_CLICK</em> Event. The only way to implement a right-click-menu seemed to be a <em>NativeMenu</em>. But I needed only the event, not the ugly native-os-menu.</p>
<p>Here is the workaround: Just create a blank <em>NativeMenu </em>and listen for the display event.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">menu</span>:NativeMenu = <span style="color: #000000; font-weight: bold;">new</span> NativeMenu<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">menu</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">DISPLAYING</span>, onRightClick, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
someSprite.<span style="color: #0066CC;">contextMenu</span> = <span style="color: #0066CC;">menu</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.nulldesign.de/2007/10/31/custom-right-click-menus-in-air/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New site, new blog</title>
		<link>http://www.nulldesign.de/2007/10/21/new-site-new-blog/</link>
		<comments>http://www.nulldesign.de/2007/10/21/new-site-new-blog/#comments</comments>
		<pubDate>Sun, 21 Oct 2007 13:13:50 +0000</pubDate>
		<dc:creator>lars</dc:creator>
				<category><![CDATA[Actionscript]]></category>

		<guid isPermaLink="false">http://www.nulldesign.de/2007/10/21/new-site-new-blog/</guid>
		<description><![CDATA[Good news everyone! I removed my three year old flash site and managed to set up this blog instead. I think this is a much better place to showcase my experiments. It&#8217;s not finished yet, but I&#8217;m working hard on it to get everything done. I will post news, talk about my flash experiments, actionscript &#8230; <a href="http://www.nulldesign.de/2007/10/21/new-site-new-blog/">Read more <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Good news everyone!<br />
I removed my three year old flash site and managed to set up this blog instead. I think this is a much better place to showcase my experiments. It&#8217;s not finished yet, but I&#8217;m working hard on it to get everything done.</p>
<p>I will post news, talk about my flash experiments, actionscript in general and whatever pops into my mind here.<br />
So, let&#8217;s get started!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nulldesign.de/2007/10/21/new-site-new-blog/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

