<?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>flashing in public</title>
	<atom:link href="http://blog.bwhiting.co.uk/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://blog.bwhiting.co.uk</link>
	<description>...usually a bad idea</description>
	<lastBuildDate>Fri, 11 Jan 2013 15:49:20 +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>Vector graphics on the GPU with Stage3D</title>
		<link>http://blog.bwhiting.co.uk/?p=423</link>
		<comments>http://blog.bwhiting.co.uk/?p=423#comments</comments>
		<pubDate>Thu, 10 Jan 2013 13:07:13 +0000</pubDate>
		<dc:creator>bwhiting</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.bwhiting.co.uk/?p=423</guid>
		<description><![CDATA[Hey guys and gals, Started playing with vectors on the GPU last year but it sort of ground to a halt really, I didn&#8217;t like the ugly aliasing and let face it vectors should look smooth as a peach. Anyway, skip forward until flash player 11.6 beta was released supporting new shader op codes (namely [...]]]></description>
			<content:encoded><![CDATA[<p>Hey guys and gals,</p>
<p>Started playing with vectors on the GPU last year but it sort of ground to a halt really, I didn&#8217;t like the ugly aliasing and let face it vectors should look smooth as a peach. Anyway, skip forward until flash player 11.6 beta was released supporting new shader op codes (namely ddx and ddy) woo, now I had the ability to add in that sweet sweet anti aliasing by leveraging the screen space derivatives!</p>
<p>Cool so now that I was able to render filled triangles and bezier curves (and soooooo close to cubic curves too &#8211; need help on this one though), I thought it should be easy to tie it in to the new readGraphicsData command as seen here:<br />
<a title="Query Graphics Data – Live demo" href="http://www.bytearray.org/?p=5013" target="_blank">Query Graphics Data</a></p>
<p>Sadly not quite as straight forward as you might think!<br />
The readGraphicsData method returns all the information to serialize the graphics of any object with graphics in it in flash! But there are still a few difficulties with parsing this data for use on the GPU.</p>
<p>Here are some of the issues:</p>
<ul>
<li>Determining if a bezier curve is concave or convex (this depends on what side of the closed region the curve is on)</li>
</ul>
<p><a href="http://blog.bwhiting.co.uk/wp-content/uploads/2013/01/curves.png"><img class="alignnone size-medium wp-image-430" title="curves" src="http://blog.bwhiting.co.uk/wp-content/uploads/2013/01/curves-300x133.png" alt="" width="300" height="133" /></a><br />
(of the two curves on the left shape one is concave and one is convex, blue and red respectively and the shader needs tweaking dependant on which one it is)</p>
<ul>
<li>Determining if two curves overlap (i.e. in long sweeping thin curves&#8230; in this case the curve may need to be broken down into smaller curves)</li>
<li>Gradients fills! A couple of problems here, 1 is reversing the gradient matrix and the other is to replicate it on the GPU. Simple gradient fills are possible but can get complex easily as you start added more than two colours (maybe at this point a 1&#215;256 pixel texture could be used as a lookup) . That said I have not got round to this yet so might not be to bad.</li>
<li><strong>TRIANGULATION!!!!!!</strong> This is the real problem here&#8230; (at least to me it is). So from the output of the readGraphicsData we have extracted all the curves and along the way we have collected a series of points. These points make up the triangles that we use to fill the solid sections of the shape. Things get tricky however because these points do NOT automatically make up a nice sequence of triangles, you will get overlapping issues and a whole host of other problems. So this is where the triangulation comes in. At first I tried Delaunay triangulation but it was too greedy making triangles outside of the actual shape, so no good. Then I tried some ear clipping examples that I found but only 1 of the 3 I tried kind of worked and I say &#8220;kind of&#8221; because  it goes into a number of infinite loops that I had to hardcode exceptions for <img src='http://blog.bwhiting.co.uk/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  and as such it misses a few triangles. (Also every now and again it would reject a complete path for no apparent reason). Not only that but the ear clipping algorithm is SLOW and doesn&#8217;t scale very well.</li>
</ul>
<p>True vector graphics are great, they are something we love about the platform and will miss when the next major release of actionscript comes about. This is the reason why I though it would be great if we could emulate it on the GPU and I am sure it&#8217;s possible. It would however be 100 times easier if Adobe could extend their api to expose the result of the internal triangulation that they have already implemented in the player. That way they are still leaving it up to us to handle the rendering but we won&#8217;t have to spend an AGE trying to do work that they already have done a long time ago.</p>
<p>&nbsp;</p>
<p>Anyway am bored of typing so I will post a small demo video (best viewed @ high resolution + full screen):<br />
<iframe src="http://www.youtube.com/embed/NmgRiMhP4q4?rel=0&amp;showsearch=false&amp;modestbranding=true" frameborder="0" width="640" height="380"></iframe><br />
..Interactive demo coming soon.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>If anyone wants to know more, discuss the topic, contribute, point me at an awesome triangulation library for as3, or anything else.. just drop me a message or reply to this post.</p>
<p>Related link(s) of justice:<br />
<a title="http://www.bytearray.org/?p=5013" href="http://www.bytearray.org/?p=5013" target="_blank">http://www.bytearray.org/?p=5013</a></p>
<p>&nbsp;</p>
<p>TODOs:</p>
<p>Use edge lists to enable anti aliasing on straight edges of standard (non curve) triangles.</p>
<p>Use edges to also determine if curve is convex or concave.</p>
<p>Look into cubic curve  - quadratic bezier curve conversion</p>
<p>&nbsp;</p>
<p>UPDATE:</p>
<p>In an ideal world I would like this to become a small Open Source project. One that is not geared to any specific engine or renderer just a simple tool that can be used to generate the required data from any flash display object. If anyone is interested get in touch, I will be more than happy so share the code once its a bit more optimized and would love to see this become something useful not just for the developers of a particular engine but for all flash devs <img src='http://blog.bwhiting.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bwhiting.co.uk/?feed=rss2&#038;p=423</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>3D Procedural Geometry using revolutions</title>
		<link>http://blog.bwhiting.co.uk/?p=413</link>
		<comments>http://blog.bwhiting.co.uk/?p=413#comments</comments>
		<pubDate>Thu, 09 Aug 2012 10:09:19 +0000</pubDate>
		<dc:creator>bwhiting</dc:creator>
				<category><![CDATA[3d]]></category>

		<guid isPermaLink="false">http://blog.bwhiting.co.uk/?p=413</guid>
		<description><![CDATA[Many of the basic primitives we utilize in 3D can be constructed procedurally. All the way from simple planes to complex shapes. Today I thought I would really quickly share something I use a lot &#8211; a revolution mesh. The idea has been around for ages and can be found most 3D packages i.e. the [...]]]></description>
			<content:encoded><![CDATA[<p>Many of the basic primitives we utilize in 3D can be constructed procedurally. All the way from simple planes to complex shapes.</p>
<p>Today I thought I would really quickly share something I use a lot &#8211; a revolution mesh. The idea has been around for ages and can be found most 3D packages i.e. the lathe modifier in 3DSMax and its in as3 3D libs like Away3D I think. The good thing about this technique is that it can be used to produce a number of different shapes all with one function (which makes for a smaller code base as well as flexibility).</p>
<p>The idea is simple, pass in a list of 2D points and rotate them about an axis for any given number of times and then construct a solid mesh out of the result.</p>
<p>First up, a demo:<br />
<iframe width="640" height="480" src="http://bwhiting.co.uk/b3d/revolution/" frameborder="0" allowfullscreen="1"></iframe><br />
link to standalone version: <a href="http://bwhiting.co.uk/b3d/revolution/" target="_blank">here</a></p>
<p>Okay so not very impressive but all of those objects were created in the same way, just revolving a series of points about and axis (in this instance the y/up axis).</p>
<p>So the code to generate them looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">numRevolutions = <span style="color: #000000; font-weight:bold;">25</span>;
revolutions = <span style="color: #0033ff; font-weight: bold;">new</span> Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>Object3D<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
revolutions.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>generateRevolutionObject<span style="color: #000000;">&#40;</span>SplineBuilder.generateTorus<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">5</span>,<span style="color: #000000; font-weight:bold;">2</span>,<span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
revolutions.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>generateRevolutionObject<span style="color: #000000;">&#40;</span>SplineBuilder.generateCircle<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">5</span>,<span style="color: #000000; font-weight:bold;">15</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
revolutions.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>generateRevolutionObject<span style="color: #000000;">&#40;</span>SplineBuilder.generateCone<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
revolutions.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>generateRevolutionObject<span style="color: #000000;">&#40;</span>SplineBuilder.generateTube<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
revolutions.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>generateRevolutionObject<span style="color: #000000;">&#40;</span>SplineBuilder.generateDisc<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
revolutions.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>generateRevolutionObject<span style="color: #000000;">&#40;</span>SplineBuilder.generateCylinder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
revolutions.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>generateRevolutionObject<span style="color: #000000;">&#40;</span>SplineBuilder.generateArc<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">5</span>, <span style="color: #000000; font-weight:bold;">5</span>, <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #000000; font-weight:bold;">0.25</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
revolutions.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>generateRevolutionObject<span style="color: #000000;">&#40;</span>SplineBuilder.generateWave<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">20</span>,<span style="color: #000000; font-weight:bold;">5</span>,<span style="color: #000000; font-weight:bold;">0.5</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
revolutions.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>generateRevolutionObject<span style="color: #000000;">&#40;</span>SplineBuilder.generateWaveAbs<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">20</span>,<span style="color: #000000; font-weight:bold;">5</span>,<span style="color: #000000; font-weight:bold;">0.5</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
revolutions.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>generateRevolutionObject<span style="color: #000000;">&#40;</span>SplineBuilder.generateSquareWave<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">10</span>,<span style="color: #000000; font-weight:bold;">20</span>,<span style="color: #000000; font-weight:bold;">5</span>,<span style="color: #000000; font-weight:bold;">0.5</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
revolutions.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>generateRevolutionObject<span style="color: #000000;">&#40;</span>SplineBuilder.generateLine<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">5</span>,<span style="color: #000000; font-weight:bold;">5</span>,<span style="color: #000000; font-weight:bold;">5</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
scene.addObjects<span style="color: #000000;">&#40;</span>revolutions<span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> generateRevolutionObject<span style="color: #000000;">&#40;</span>spline<span style="color: #000000; font-weight: bold;">:</span>Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>Number<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>Object3D
<span style="color: #000000;">&#123;</span>
	<span style="color: #009900;">//builds the mesh based on the number of revolutions and the points</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> mesh<span style="color: #000000; font-weight: bold;">:</span>RevolutionMesh = <span style="color: #0033ff; font-weight: bold;">new</span> RevolutionMesh<span style="color: #000000;">&#40;</span>numRevolutions, spline<span style="color: #000000;">&#41;</span>;	
	<span style="color: #6699cc; font-weight: bold;">var</span> object<span style="color: #000000; font-weight: bold;">:</span>Object3D = <span style="color: #0033ff; font-weight: bold;">new</span> Object3D<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.build<span style="color: #000000;">&#40;</span>mesh, material<span style="color: #000000;">&#41;</span>;
	<span style="color: #009900;">//gives the object a parent transform so can be rotated about the scene origin</span>
	object.<span style="color: #004993;">transform</span>.<span style="color: #004993;">parent</span> = transformer;					
	<span style="color: #0033ff; font-weight: bold;">return</span> object;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The magic happens in the SplineBuilder and the RevolutionMesh. The SplineBuilder is just a really simple class with static methods to generate lists of points:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//couple of example functions from SplineBuilder</span>
<span style="color: #0033ff; font-weight: bold;">public</span> static <span style="color: #339966; font-weight: bold;">function</span> generateCone<span style="color: #000000;">&#40;</span><span style="color: #004993;">height</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">10</span>, radius<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>Number<span style="color: #000000; font-weight: bold;">&gt;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> spline<span style="color: #000000; font-weight: bold;">:</span>Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>Number<span style="color: #000000; font-weight: bold;">&gt;</span> = <span style="color: #0033ff; font-weight: bold;">new</span> Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>Number<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	spline.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #004993;">height</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
	spline.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>radius, <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
	spline.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #0033ff; font-weight: bold;">return</span> spline;
<span style="color: #000000;">&#125;</span>
<span style="color: #0033ff; font-weight: bold;">public</span> static <span style="color: #339966; font-weight: bold;">function</span> generateCylinder<span style="color: #000000;">&#40;</span><span style="color: #004993;">height</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">10</span>, radius<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>Number<span style="color: #000000; font-weight: bold;">&gt;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> spline<span style="color: #000000; font-weight: bold;">:</span>Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>Number<span style="color: #000000; font-weight: bold;">&gt;</span> = <span style="color: #0033ff; font-weight: bold;">new</span> Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>Number<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	spline.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,  <span style="color: #004993;">height</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
	spline.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>radius, <span style="color: #004993;">height</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
	spline.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>radius, <span style="color: #000000; font-weight: bold;">-</span><span style="color: #004993;">height</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
	spline.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #000000; font-weight: bold;">-</span><span style="color: #004993;">height</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #0033ff; font-weight: bold;">return</span> spline;
<span style="color: #000000;">&#125;</span>
<span style="color: #0033ff; font-weight: bold;">public</span> static <span style="color: #339966; font-weight: bold;">function</span> generateTube<span style="color: #000000;">&#40;</span><span style="color: #004993;">height</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">10</span>, radius<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span>Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>Number<span style="color: #000000; font-weight: bold;">&gt;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> spline<span style="color: #000000; font-weight: bold;">:</span>Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>Number<span style="color: #000000; font-weight: bold;">&gt;</span> = <span style="color: #0033ff; font-weight: bold;">new</span> Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>Number<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	spline.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>radius<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span>,  <span style="color: #004993;">height</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
	spline.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>radius, <span style="color: #004993;">height</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
	spline.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>radius, <span style="color: #000000; font-weight: bold;">-</span><span style="color: #004993;">height</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
	spline.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>radius<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span>, <span style="color: #000000; font-weight: bold;">-</span><span style="color: #004993;">height</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
	spline.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>radius<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span>,  <span style="color: #004993;">height</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">2</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #0033ff; font-weight: bold;">return</span> spline;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>See, super simple! You can of course come up with these however you like such as drawn user input or curve interpolations.</p>
<p>So the final piece is the RevolutionMesh (or lathe or whatever you want to call it). Its job is to take the input, transform it around an axis and storing the vertices. Then build the uv&#8217;s and indices. (You can then go on to generate normals and tangents etc&#8230;)</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> build<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>vertices<span style="color: #000000;">&#41;</span>		vertices.<span style="color: #004993;">length</span> = <span style="color: #000000; font-weight:bold;">0</span>;
	<span style="color: #0033ff; font-weight: bold;">else</span> 			vertices = <span style="color: #0033ff; font-weight: bold;">new</span> Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>Number<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;			
	<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>uvs<span style="color: #000000;">&#41;</span>		uvs.<span style="color: #004993;">length</span> = <span style="color: #000000; font-weight:bold;">0</span>;
	<span style="color: #0033ff; font-weight: bold;">else</span> 			uvs = <span style="color: #0033ff; font-weight: bold;">new</span> Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>Number<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>ids<span style="color: #000000;">&#41;</span>		ids.<span style="color: #004993;">length</span> = <span style="color: #000000; font-weight:bold;">0</span>;
	<span style="color: #0033ff; font-weight: bold;">else</span> 			ids = <span style="color: #0033ff; font-weight: bold;">new</span> Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>uint<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
	<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>normals<span style="color: #000000;">&#41;</span>		normals.<span style="color: #004993;">length</span> = <span style="color: #000000; font-weight:bold;">0</span>;
	<span style="color: #0033ff; font-weight: bold;">else</span> 			normals = <span style="color: #0033ff; font-weight: bold;">new</span> Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>Number<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;	
&nbsp;
	<span style="color: #009900;">//doesn't have to revolve the whole way round, _start and _end are values from 0-1 so a start of 0 and end of 0.5 would mean a 0 - 180 degrees of revolution</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> totalAngle<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000;">&#40;</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">PI</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000;">&#40;</span>_end<span style="color: #000000; font-weight: bold;">-</span>_start<span style="color: #000000;">&#41;</span>;
	<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">angle</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = totalAngle<span style="color: #000000; font-weight: bold;">/</span>_divisions;
	<span style="color: #6699cc; font-weight: bold;">var</span> startAngle<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000;">&#40;</span><span style="color: #004993;">Math</span>.<span style="color: #004993;">PI</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">*</span>_start;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> _divisions<span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">1</span>; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> vout<span style="color: #000000; font-weight: bold;">:</span>Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>Number<span style="color: #000000; font-weight: bold;">&gt;</span> = <span style="color: #0033ff; font-weight: bold;">new</span> Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>Number<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		transformer.<span style="color: #004993;">identity</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
		transformer.appendRotation<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>startAngle<span style="color: #000000; font-weight: bold;">*</span>Math3D.RADIANS_TO_DEGREES<span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">angle</span><span style="color: #000000; font-weight: bold;">*</span>i<span style="color: #000000; font-weight: bold;">*</span>Math3D.RADIANS_TO_DEGREES<span style="color: #000000;">&#41;</span>, _axix<span style="color: #000000;">&#41;</span>;	
		transformer.transformVectors<span style="color: #000000;">&#40;</span>_spline, vout<span style="color: #000000;">&#41;</span>;
		vertices = vertices.<span style="color: #004993;">concat</span><span style="color: #000000;">&#40;</span>vout<span style="color: #000000;">&#41;</span>;
		<span style="color: #009900;">//could do the same for normals if supplied ;)</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> splineLength<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = _spline.<span style="color: #004993;">length</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">3</span>;
	<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span>i = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> _divisions<span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">1</span>; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> j<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span>; j <span style="color: #000000; font-weight: bold;">&lt;</span> splineLength; j<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			uvs.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">&#40;</span>_divisions<span style="color: #000000;">&#41;</span>, j<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">&#40;</span>splineLength<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span>i = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> _divisions; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span>j = <span style="color: #000000; font-weight:bold;">0</span>; j <span style="color: #000000; font-weight: bold;">&lt;</span> splineLength<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span>; j<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> id0<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">*</span>splineLength<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">+</span>j;
			<span style="color: #6699cc; font-weight: bold;">var</span> id1<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000;">&#40;</span>i<span style="color: #000000; font-weight: bold;">*</span>splineLength<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">+</span>j;
			<span style="color: #6699cc; font-weight: bold;">var</span> id2<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">*</span>splineLength<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">+</span>j<span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">1</span>;
			<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">id3</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #000000;">&#40;</span>i<span style="color: #000000; font-weight: bold;">*</span>splineLength<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">+</span>j<span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">1</span>;
			ids.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span>id0, id2, id1<span style="color: #000000;">&#41;</span>;
			ids.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">id3</span>, id1, id2<span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>				
	<span style="color: #000000;">&#125;</span>			
	<span style="color: #009900;">//go forth and generate normals etc...</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>It works by firstly using a matrix3D to rotate the points and copy them into a vertex list. Next it calculates the uvs based upon vertices&#8217; index in the original point list and the revolution index (u based on the rev index and v based on the position in the point list). Once that is done its a simple case of assigning the indices!</p>
<p>There you have it, some fairly simple code which can be reused to create a crap load of geometry.</p>
<p>Hope that helps someone out, shout if you have and questions or spot any problems etc&#8230;</p>
<p>b  </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bwhiting.co.uk/?feed=rss2&#038;p=413</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Frustum Culling &#8211; on steroids!</title>
		<link>http://blog.bwhiting.co.uk/?p=355</link>
		<comments>http://blog.bwhiting.co.uk/?p=355#comments</comments>
		<pubDate>Mon, 18 Jun 2012 09:53:37 +0000</pubDate>
		<dc:creator>bwhiting</dc:creator>
				<category><![CDATA[3d]]></category>
		<category><![CDATA[speed]]></category>

		<guid isPermaLink="false">http://blog.bwhiting.co.uk/?p=355</guid>
		<description><![CDATA[Am not going to really explain how frustum culling works here or how to set it up, there are already some good articles out there will link to a couple of Actionscript ones at the end. This article will explain how to use caching to very easily boost the speed of your frustum culling. As [...]]]></description>
			<content:encoded><![CDATA[<p>Am not going to really explain how frustum culling works here or how to set it up, there are already some good articles out there will link to a couple of Actionscript ones at the end. This article will explain how to use caching to very easily boost the speed of your frustum culling. As far as testing goes I haven&#8217;t yet seen anything as fast as this so I thought I would share it so anyone else can benefit from it or maybe some bright spark can find a way to improve it&#8230; there is always room for improvement where flash is concerned <img src='http://blog.bwhiting.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p><em>Just a super quick reasoning why frustum culling is a good thing if done properly:</em></p>
<p><em>If the sphere&#8217;s position is the same (i.e. a pointer to) the object that it describes position then NO transformations are needed to move the sphere into world space. Consider trying to check a bounding box, while it might be a tighter fit (woop), it would require that the frustum be transformed into local space or the box into world space&#8230; either way that is gonna be seriously slow for large numbers of objects when compared to the bounding sphere checks. Frustum checking is easy to implement and can save you a vast amount of time, no rendering of off screen objects!</em></p>
<p>This is what the standard frustum culling code looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//loop thorugh your objects or spheres</span>
<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #004993;">length</span>; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> object<span style="color: #000000; font-weight: bold;">:</span>Object3D = objects<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>;
	object.cullFlag = <span style="color: #000000; font-weight:bold;">1</span>;		<span style="color: #009900;">//object.visible = true;</span>
&nbsp;
	<span style="color: #6699cc; font-weight: bold;">var</span> sphere<span style="color: #000000; font-weight: bold;">:</span>BoundingSphere = object.sphere;
	<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">position</span><span style="color: #000000; font-weight: bold;">:</span>Vector3D = sphere.<span style="color: #004993;">position</span>;
	<span style="color: #6699cc; font-weight: bold;">var</span> radius<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = sphere.radius;
&nbsp;
	<span style="color: #6699cc; font-weight: bold;">var</span> plane<span style="color: #000000; font-weight: bold;">:</span>Plane3D;
	<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">distance</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> j <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span>; j <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight:bold;">6</span>; j<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span>	<span style="color: #009900;">//6 planes in the frustum (planes.length)</span>
	<span style="color: #000000;">&#123;</span>
		plane = planes<span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span>;
		<span style="color: #004993;">distance</span> = plane.a <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #004993;">position</span>.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">+</span> plane.<span style="color: #004993;">b</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #004993;">position</span>.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">+</span> plane.<span style="color: #004993;">c</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #004993;">position</span>.z <span style="color: #000000; font-weight: bold;">+</span> plane.<span style="color: #004993;">d</span>;
		<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">distance</span> <span style="color: #000000; font-weight: bold;">+</span> radius <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight:bold;">0.0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
			object.cullFlag = <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span>;		<span style="color: #009900;">//object.visible = false;</span>
			<span style="color: #0033ff; font-weight: bold;">break</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>What the above code is doing is looping through all of you objects or bounding spheres and for each one it projects its position onto each plane that makes up the frustum. If any of the tests show the sphere to be outside of the frustum then it can be marked as not visible and then it will not need to be rendered. As soon as it fails on one plane you can move on to test the next object/sphere but if it passes you will need to go on to check against the next plane until you are sure that it passes all test. (You can see why this is inefficient for objects IN the frustum as 6 checks are required just to confirm that where as the best case is when you can discard an object on the first plane check*)</p>
<p>Now this could be sped up a little bit by unrolling the inner loop but it will only make a very small difference in speed, but it will help.</p>
<p>Introducing the &#8220;cache&#8221; variable into the mix! Okay here&#8217;s where the huge speed up can be gained. In the vast majority of cases, if an object is culled by a frustum plane in one frame and is still culled in the next frame the likelihood that it was culled by the same plane is extremely high! With that in mind if we store the plane that the sphere failed the test on and then test against that one first, for a large number of cases we can eradicate a number of unnecessary plane tests.</p>
<p>Bring on the updated code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//loop thorugh your objects or spheres</span>
<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #004993;">length</span>; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> object<span style="color: #000000; font-weight: bold;">:</span>Object3D = objects<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>;
	object.cullFlag = <span style="color: #000000; font-weight:bold;">1</span>;		<span style="color: #009900;">//object.visible = true;</span>
&nbsp;
	<span style="color: #6699cc; font-weight: bold;">var</span> sphere<span style="color: #000000; font-weight: bold;">:</span>BoundingSphere = object.sphere;
	<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">position</span><span style="color: #000000; font-weight: bold;">:</span>Vector3D = sphere.<span style="color: #004993;">position</span>;
	<span style="color: #6699cc; font-weight: bold;">var</span> radius<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = sphere.radius;
&nbsp;
	<span style="color: #6699cc; font-weight: bold;">var</span> plane<span style="color: #000000; font-weight: bold;">:</span>Plane3D;
	<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">distance</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>;
	<span style="color: #6699cc; font-weight: bold;">var</span> cache<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = sphere.cache;		<span style="color: #009900;">//the id of the last plane failed on (-1 if none)</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>cache <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		plane = planes<span style="color: #000000;">&#91;</span>cache<span style="color: #000000;">&#93;</span>;
		<span style="color: #004993;">distance</span> = plane.a <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #004993;">position</span>.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">+</span> plane.<span style="color: #004993;">b</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #004993;">position</span>.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">+</span> plane.<span style="color: #004993;">c</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #004993;">position</span>.z <span style="color: #000000; font-weight: bold;">+</span> plane.<span style="color: #004993;">d</span>;
		<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">distance</span> <span style="color: #000000; font-weight: bold;">+</span> radius <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight:bold;">0.0</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			object.cullFlag = <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span>;	<span style="color: #009900;">//object.visible = false;</span>
			<span style="color: #0033ff; font-weight: bold;">continue</span>;			<span style="color: #009900;">//objtect still culled so move to next object</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> j <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span>; j <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight:bold;">6</span>; j<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span>	<span style="color: #009900;">//6 planes in the frustum (planes.length)</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>j == cache<span style="color: #000000;">&#41;</span> <span style="color: #0033ff; font-weight: bold;">continue</span>;
		plane = planes<span style="color: #000000;">&#91;</span>j<span style="color: #000000;">&#93;</span>;
		<span style="color: #004993;">distance</span> = plane.a <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #004993;">position</span>.<span style="color: #004993;">x</span> <span style="color: #000000; font-weight: bold;">+</span> plane.<span style="color: #004993;">b</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #004993;">position</span>.<span style="color: #004993;">y</span> <span style="color: #000000; font-weight: bold;">+</span> plane.<span style="color: #004993;">c</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #004993;">position</span>.z <span style="color: #000000; font-weight: bold;">+</span> plane.<span style="color: #004993;">d</span>;
		<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">distance</span> <span style="color: #000000; font-weight: bold;">+</span> radius <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight:bold;">0.0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
			object.cullFlag = <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span>;	<span style="color: #009900;">//object.visible = false;</span>
			sphere.cache = j;	<span style="color: #009900;">//set the cache to match the current plane</span>
			<span style="color: #0033ff; font-weight: bold;">break</span>;
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #009900;">//if it's got this far then the object is not culled so the cache can be reset</span>
	sphere.cache = <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This time round the code checks to see if a plane has been cached and if so then it checks that one first. This means in the best case (a stationary camera with a stationary scene) all the objects, once they have been culled once already will be culled again with only 1 plane test instead of a maximum of 6! Muchos muchos faster!</p>
<p>There are still things that can be optimised such as not testing the cached plane again in the second section if it passes the check in the first section. You could also unroll all the loops to remove any array/vector lookups for a speed up too.</p>
<p>The method can also easily be extend to check for intersections as well a 1/0, true/false or hit/not hit result. Allowing for other checks such as a bounding box or something more tightly fitting.</p>
<p>I have found this approach to be the fastest yet and my implementation can chew through 10,000 objects in under 1 ms without a problem and this is without any bounding volume hierarchies. (Most flash games will probably not even have that many objects in them anyway but still good to know).</p>
<p>Any questions at all then do ask! Or if I have made any booboos then do let me know.</p>
<p>couple o links:</p>
<p><a href="http://blogs.aerys.in/jeanmarc-leroux/2009/12/13/frustum-culling-in-flash-10/">http://blogs.aerys.in/jeanmarc-leroux/2009/12/13/frustum-culling-in-flash-10/</a> &lt;&#8211; nice code (lacking optimisations but I am sure it is just to show the implementation)<br />
<a href="http://jacksondunstan.com/articles/1811">http://jacksondunstan.com/articles/1811</a> &lt;&#8211; includes some camera code to so you can get up and running<br />
<a href="http://www.flipcode.com/archives/Frustum_Culling.shtml">http://www.flipcode.com/archives/Frustum_Culling.shtml</a><br />
<a href="http://www.crownandcutlass.com/features/technicaldetails/frustum.html">http://www.crownandcutlass.com/features/technicaldetails/frustum.html</a></p>
<p>&nbsp;</p>
<p>*With that in mind maybe one should check the plane most likely to fail most objects, probably the near and far planes, but not necessarily. You could easy count the the number of culls against each plane as you navigate around your scene and get an average to work out which one is the best culler and check that one first&#8230; it will depend on your needs though and could be total overkill, but hey this is flash right and flash IS slow, so every little helps!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bwhiting.co.uk/?feed=rss2&#038;p=355</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>SSAO in stage 3D (source added)</title>
		<link>http://blog.bwhiting.co.uk/?p=383</link>
		<comments>http://blog.bwhiting.co.uk/?p=383#comments</comments>
		<pubDate>Fri, 15 Jun 2012 10:40:54 +0000</pubDate>
		<dc:creator>bwhiting</dc:creator>
				<category><![CDATA[3d]]></category>

		<guid isPermaLink="false">http://blog.bwhiting.co.uk/?p=383</guid>
		<description><![CDATA[I had deveoped my own, simple implementation of SSAO (Screen Space Ambient Occlusion)  and thought I would try some other implementations to get better resukts. I&#8217;ll use this post to share video&#8217;s of progress and demos to follow as well. &#160; Video 1: SSAO pre blur with 8 samples per pixel (hits the agal instruction limit [...]]]></description>
			<content:encoded><![CDATA[<p>I had deveoped my own, simple implementation of SSAO (<a href="http://en.wikipedia.org/wiki/Screen_Space_Ambient_Occlusion">Screen Space Ambient Occlusion</a>)  and thought I would try some other implementations to get better resukts.</p>
<p>I&#8217;ll use this post to share video&#8217;s of progress and demos to follow as well.</p>
<p>&nbsp;</p>
<p>Video 1:</p>
<p>SSAO pre blur with 8 samples per pixel (hits the agal instruction limit real fast). Still some kinks to iron out for sure, depth buffer is currently really short to help with the accuracy but should be able to improve this I have been able to encode it across 4 channels now not 1.</p>
<p>Runs pretty quick, 60fps is still achievable.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><iframe src="http://www.youtube.com/embed/CyirdBNRKCc?rel=0&amp;showsearch=false&amp;modestbranding=true" frameborder="0" width="640" height="380"></iframe></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>related/useful links:</p>
<p>SSAO</p>
<p><a href="http://www.gamerendering.com/category/lighting/ssao-lighting/">http://www.gamerendering.com/category/lighting/ssao-lighting/</a><br />
<a href="http://www.gamedev.net/page/resources/_/technical/graphics-programming-and-theory/a-simple-and-practical-approach-to-ssao-r2753">http://www.gamedev.net/page/resources/_/technical/graphics-programming-and-theory/a-simple-and-practical-approach-to-ssao-r2753</a><br />
<a href="http://www.john-chapman.net/content.php?id=8">http://www.john-chapman.net/content.php?id=8</a><br />
<a href="http://blog.nextrevision.com/?p=76">http://blog.nextrevision.com/?p=76</a><br />
<a href="http://www.gamerendering.com/2008/09/28/linear-depth-texture/">http://www.gamerendering.com/2008/09/28/linear-depth-texture/</a><br />
<a href="http://www.theorangeduck.com/page/pure-depth-ssao">http://www.theorangeduck.com/page/pure-depth-ssao</a></p>
<p>DEPTH ENCODING</p>
<p><a href="http://www.gamedev.net/topic/516146-how-to-encode-the-depth-value-in-a-32bit-rgba-texture/">http://www.gamedev.net/topic/516146-how-to-encode-the-depth-value-in-a-32bit-rgba-texture/</a><br />
<a href="http://www.gamedev.net/topic/486847-encoding-16-and-32-bit-floating-point-value-into-rgba-byte-texture/">http://www.gamedev.net/topic/486847-encoding-16-and-32-bit-floating-point-value-into-rgba-byte-texture/</a><br />
<a href="http://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/">http://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/</a><br />
<a href="http://aras-p.info/blog/2007/03/03/a-day-well-spent-encoding-floats-to-rgba/">http://aras-p.info/blog/2007/03/03/a-day-well-spent-encoding-floats-to-rgba/</a><br />
<a href="http://asgl.googlecode.com/svn-history/r490/trunk/ASGL_FP11/src/asgl/shaders/agal/AGALCodec.as">http://asgl.googlecode.com/svn-history/r490/trunk/ASGL_FP11/src/asgl/shaders/agal/AGALCodec.as</a><br />
<a href="http://www.gamerendering.com/2008/09/25/packing-a-float-value-in-rgba/">http://www.gamerendering.com/2008/09/25/packing-a-float-value-in-rgba/</a><br />
<a href="http://www.gamedev.net/topic/585330-packing-a-generic-float-into-a-rgba-texture/">http://www.gamedev.net/topic/585330-packing-a-generic-float-into-a-rgba-texture/</a>     &lt;&#8211; nice function at the end of this one<br />
<a href="http://www.gamedev.net/topic/442138-packing-a-float-into-a-a8r8g8b8-texture-shader/">http://www.gamedev.net/topic/442138-packing-a-float-into-a-a8r8g8b8-texture-shader/</a></p>
<p>SOURCE CODE::</p>
<p>Here it is folks, later than I would have liked but I have been busier than badger in Spring.<br />
I have done my best to comment what is going on.<br />
It took a lot of playing around with to get it working and muchos muchos trial and error.</p>
<p>PLEASE do take this and try and make it better I am sure there is room for improvement!!!</p>
<p>Also feel free to ask any questions at all and I will try and answer them.<br />
Cheers Daniel Holden (orange duck for the code from which this is ported)<br />
<a href="http://www.theorangeduck.com/page/pure-depth-ssao" title="pure depth ssao">pure depth ssao</a><br />
(Although I use a normal texture to save on operations)</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> flags<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #000000;">&#40;</span>smooth<span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">?</span> <span style="color: #990000;">&quot;linear&quot;</span> <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #990000;">&quot;nearest&quot;</span>;
&nbsp;
<span style="color: #009900;">//varying registers</span>
<span style="color: #6699cc; font-weight: bold;">var</span> uv_in<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;v0&quot;</span>;
&nbsp;
<span style="color: #009900;">//samplers</span>
<span style="color: #6699cc; font-weight: bold;">var</span> tex_normal<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;fs1&quot;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> tex_depth<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;fs2&quot;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> tex_noise<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;fs3&quot;</span>;
&nbsp;
<span style="color: #009900;">//float3 - float4</span>
<span style="color: #6699cc; font-weight: bold;">var</span> colour<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;ft0&quot;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> normal<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;ft1&quot;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">position</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;ft2&quot;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">random</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;ft3&quot;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> ray<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;ft4&quot;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> hemi_ray<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;ft5&quot;</span>;
&nbsp;
<span style="color: #009900;">//float1</span>
<span style="color: #6699cc; font-weight: bold;">var</span> depth<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;ft6.x&quot;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> radiusDepth<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;ft6.y&quot;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> occlusion<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;ft6.z&quot;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> occ_depth<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;ft6.w&quot;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> difference<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;ft7.x&quot;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> temp<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;ft7.y&quot;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> temp2<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;ft7.z&quot;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> temp3<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;ft7.w&quot;</span>;			<span style="color: #009900;">//NOT USED</span>
<span style="color: #6699cc; font-weight: bold;">var</span> fallOff<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;ft0.x&quot;</span>;		<span style="color: #009900;">//NOT USED</span>
&nbsp;
<span style="color: #009900;">//float2</span>
<span style="color: #6699cc; font-weight: bold;">var</span> uv<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;ft0&quot;</span>;
&nbsp;
<span style="color: #009900;">//constants</span>
<span style="color: #6699cc; font-weight: bold;">var</span> radius<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;fc0.z&quot;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">scale</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;fc0.w&quot;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> decoder<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;fc2.z&quot;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> zero<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;fc0.x&quot;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> one<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;fc0.y&quot;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> two<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;fc1.z&quot;</span>;			<span style="color: #009900;">//NOT USED</span>
<span style="color: #6699cc; font-weight: bold;">var</span> thresh<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;fc1.w&quot;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> neg_one<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;fc2.y&quot;</span>;		<span style="color: #009900;">//NOT USED</span>
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> depth_decoder<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;fc3.xyzw&quot;</span>;
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> area<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;fc1.x&quot;</span>;			<span style="color: #009900;">//NOT USED</span>
<span style="color: #6699cc; font-weight: bold;">var</span> falloff<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;fc1.y&quot;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> total_strength<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;fc2.x&quot;</span>;
<span style="color: #6699cc; font-weight: bold;">var</span> base<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;fc4.x&quot;</span>;
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> invSamples<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;fc2.w&quot;</span>;
&nbsp;
&nbsp;
<span style="color: #009900;">//SHADER OF DOOOOOOOOOM</span>
AGAL.<span style="color: #004993;">init</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #009900;">//sample normal at current fragment, and decode</span>
AGAL.tex<span style="color: #000000;">&#40;</span>normal, uv_in, tex_normal, <span style="color: #990000;">&quot;2d&quot;</span>, <span style="color: #990000;">&quot;clamp&quot;</span>, flags<span style="color: #000000;">&#41;</span>;<span style="color: #009900;">//ex ft0, v0, fs0 &lt;2d,wrap,linear&gt; \n&quot;+</span>
AGAL.<span style="color: #004993;">decode</span><span style="color: #000000;">&#40;</span>normal, normal, decoder<span style="color: #000000;">&#41;</span>;
<span style="color: #009900;">//sample deopth at current fragment, and decode</span>
AGAL.tex<span style="color: #000000;">&#40;</span>colour, uv_in, tex_depth, <span style="color: #990000;">&quot;2d&quot;</span>, <span style="color: #990000;">&quot;clamp&quot;</span>, flags<span style="color: #000000;">&#41;</span>;<span style="color: #009900;">//ex ft0, v0, fs0 &lt;2d,wrap,linear&gt; \n&quot;+</span>
AGAL.decodeFloatFromRGBA<span style="color: #000000;">&#40;</span>depth, colour, depth_decoder<span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #009900;">//use this instead if depth is not encoded</span>
<span style="color: #009900;">//AGAL.mov(&quot;oc.a&quot;, one);</span>
<span style="color: #009900;">//AGAL.mov(&quot;oc&quot;, depth);//col+&quot;.xyz&quot;);</span>
&nbsp;
<span style="color: #009900;">//sample random vector</span>
AGAL.mov<span style="color: #000000;">&#40;</span>uv, uv_in<span style="color: #000000;">&#41;</span>;
AGAL.mul<span style="color: #000000;">&#40;</span>uv, uv, <span style="color: #004993;">scale</span><span style="color: #000000;">&#41;</span>;					
AGAL.tex<span style="color: #000000;">&#40;</span><span style="color: #004993;">random</span>, uv, tex_noise, <span style="color: #990000;">&quot;2d&quot;</span>, <span style="color: #990000;">&quot;wrap&quot;</span>, flags<span style="color: #000000;">&#41;</span>;
<span style="color: #009900;">//AGAL.mul(random+&quot;.z&quot;, random+&quot;.z&quot;, neg_one);		//not sure if negation needed?</span>
&nbsp;
<span style="color: #009900;">//position</span>
AGAL.mov<span style="color: #000000;">&#40;</span><span style="color: #004993;">position</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #990000;">&quot;.xy&quot;</span>, uv_in<span style="color: #000000; font-weight: bold;">+</span><span style="color: #990000;">&quot;.xy&quot;</span><span style="color: #000000;">&#41;</span>;
AGAL.mov<span style="color: #000000;">&#40;</span><span style="color: #004993;">position</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #990000;">&quot;.z&quot;</span>, depth<span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #009900;">//radiusDepth</span>
AGAL.div<span style="color: #000000;">&#40;</span>radiusDepth, radius, depth<span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #009900;">//occlusion</span>
AGAL.mov<span style="color: #000000;">&#40;</span>occlusion, zero<span style="color: #000000;">&#41;</span>;					
&nbsp;
<span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>=<span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight:bold;">8</span>; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #009900;">//reflect the random normal against the current normal and size accoring to depth, further should be larger</span>
	AGAL.reflect<span style="color: #000000;">&#40;</span>ray,<span style="color: #990000;">&quot;fc&quot;</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>, <span style="color: #004993;">random</span><span style="color: #000000;">&#41;</span>;	<span style="color: #009900;">//could just add but will look crap?</span>
	AGAL.mul<span style="color: #000000;">&#40;</span>ray, ray, radiusDepth<span style="color: #000000;">&#41;</span>;
&nbsp;
	<span style="color: #009900;">//dot the ray against normal</span>
	AGAL.dp3<span style="color: #000000;">&#40;</span>hemi_ray, ray, normal<span style="color: #000000;">&#41;</span>;
	AGAL.sign<span style="color: #000000;">&#40;</span>hemi_ray, hemi_ray, temp<span style="color: #000000;">&#41;</span>;
	AGAL.mul<span style="color: #000000;">&#40;</span>hemi_ray, hemi_ray, ray<span style="color: #000000;">&#41;</span>;
	AGAL.<span style="color: #004993;">add</span><span style="color: #000000;">&#40;</span>hemi_ray, hemi_ray, <span style="color: #004993;">position</span><span style="color: #000000; font-weight: bold;">+</span><span style="color: #990000;">&quot;.xyz&quot;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
	<span style="color: #009900;">//use position to sample from </span>
	AGAL.sat<span style="color: #000000;">&#40;</span>hemi_ray<span style="color: #000000; font-weight: bold;">+</span><span style="color: #990000;">&quot;.xy&quot;</span>, hemi_ray<span style="color: #000000; font-weight: bold;">+</span><span style="color: #990000;">&quot;.xy&quot;</span><span style="color: #000000;">&#41;</span>;
	AGAL.tex<span style="color: #000000;">&#40;</span>colour, hemi_ray<span style="color: #000000; font-weight: bold;">+</span><span style="color: #990000;">&quot;.xy&quot;</span>, tex_depth, <span style="color: #990000;">&quot;2d&quot;</span>, <span style="color: #990000;">&quot;clamp&quot;</span>, flags<span style="color: #000000;">&#41;</span>;
	AGAL.decodeFloatFromRGBA<span style="color: #000000;">&#40;</span>occ_depth, colour, depth_decoder<span style="color: #000000;">&#41;</span>;
&nbsp;
	<span style="color: #009900;">//gets the difference in depth between the current depth and sampled depth</span>
	AGAL.sub<span style="color: #000000;">&#40;</span>difference, depth, occ_depth<span style="color: #000000;">&#41;</span>;				
	AGAL.sge<span style="color: #000000;">&#40;</span>temp, difference, thresh<span style="color: #000000;">&#41;</span>;	<span style="color: #009900;">// 1 if difference is bigger than the threshold, 0 otherwise</span>
	AGAL.slt<span style="color: #000000;">&#40;</span>temp2, difference, falloff<span style="color: #000000;">&#41;</span>;	<span style="color: #009900;">// 1 if difference is less than the falloff, 0 otherwise</span>
&nbsp;
	<span style="color: #009900;">//set difference to range 0 - 1 (and clamp)</span>
	AGAL.div<span style="color: #000000;">&#40;</span>difference, difference, falloff<span style="color: #000000;">&#41;</span>;
	AGAL.mul<span style="color: #000000;">&#40;</span>difference, temp, difference<span style="color: #000000;">&#41;</span>;						
	AGAL.mul<span style="color: #000000;">&#40;</span>difference, temp2, difference<span style="color: #000000;">&#41;</span>;
&nbsp;
	<span style="color: #009900;">//accumulate the occusion</span>
	AGAL.<span style="color: #004993;">add</span><span style="color: #000000;">&#40;</span>occlusion, occlusion, difference<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
<span style="color: #009900;">//bring back into range 0-1</span>
AGAL.mul<span style="color: #000000;">&#40;</span>occlusion, occlusion, invSamples<span style="color: #000000;">&#41;</span>;
<span style="color: #009900;">//apply any multiplier</span>
AGAL.mul<span style="color: #000000;">&#40;</span>occlusion, occlusion, total_strength<span style="color: #000000;">&#41;</span>;
<span style="color: #009900;">//add it to a base value</span>
AGAL.<span style="color: #004993;">add</span><span style="color: #000000;">&#40;</span>occlusion, occlusion, base<span style="color: #000000;">&#41;</span>;
<span style="color: #009900;">//invert and boom headshot</span>
AGAL.sub<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;oc&quot;</span>, one, occlusion<span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> fragmentShader<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = AGAL.<span style="color: #004993;">code</span>;</pre></div></div>

<p>here are the constants used:: (some of them didn&#8217;t end up being used so they can be left out &#8211; too busy/lazy to do that myself yet)</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//uv sample offset</span>
<span style="color: #6699cc; font-weight: bold;">var</span> radius<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">0.002</span>;		<span style="color: #009900;">//you should derive from texture size</span>
<span style="color: #009900;">//noise uv scale</span>
<span style="color: #6699cc; font-weight: bold;">var</span> scaler <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">24</span>;		<span style="color: #009900;">//much smaller and the noise blocks become more apparent</span>
<span style="color: #009900;">//unused at the mo</span>
<span style="color: #6699cc; font-weight: bold;">var</span> falloff <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">0.05</span>;		<span style="color: #009900;">//not using this so ignore it / remove it</span>
<span style="color: #009900;">//unused at the mo</span>
<span style="color: #6699cc; font-weight: bold;">var</span> area <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">5</span>;			<span style="color: #009900;">//not using this so ignore it / remove it</span>
<span style="color: #009900;">//the depth difference threshold</span>
<span style="color: #6699cc; font-weight: bold;">var</span> depthThresh<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">0.0001</span>;
<span style="color: #009900;">//strength of the effect</span>
<span style="color: #6699cc; font-weight: bold;">var</span> total_strength <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">1</span>;
<span style="color: #009900;">//base value for the effect</span>
<span style="color: #6699cc; font-weight: bold;">var</span> base <span style="color: #000000; font-weight: bold;">:</span> <span style="color: #004993;">Number</span> = <span style="color: #000000; font-weight:bold;">0</span>;
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> sample_sphere<span style="color: #000000; font-weight: bold;">:</span>Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>Number<span style="color: #000000; font-weight: bold;">&gt;</span> = <span style="color: #0033ff; font-weight: bold;">new</span> Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>Number<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
sample_sphere.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">0.5381</span>, <span style="color: #000000; font-weight:bold;">0.1856</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">0.4319</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
sample_sphere.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">0.1379</span>, <span style="color: #000000; font-weight:bold;">0.2486</span>, <span style="color: #000000; font-weight:bold;">0.4430</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
sample_sphere.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">0.3371</span>, <span style="color: #000000; font-weight:bold;">0.5679</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">0.0057</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>; 
sample_sphere.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">0.6999</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">0.0451</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">0.0019</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
sample_sphere.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">0.0689</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">0.1598</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">0.8547</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>; 
sample_sphere.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">0.0560</span>, <span style="color: #000000; font-weight:bold;">0.0069</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">0.1843</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
sample_sphere.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">0.0146</span>, <span style="color: #000000; font-weight:bold;">0.1402</span>, <span style="color: #000000; font-weight:bold;">0.0762</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>; 
sample_sphere.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">0.0100</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">0.1924</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">0.0344</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
sample_sphere.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">0.3577</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">0.5301</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">0.4358</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>; 
sample_sphere.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">0.3169</span>, <span style="color: #000000; font-weight:bold;">0.1063</span>, <span style="color: #000000; font-weight:bold;">0.0158</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
sample_sphere.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">0.0103</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">0.5869</span>, <span style="color: #000000; font-weight:bold;">0.0046</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>; 
sample_sphere.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">0.0897</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">0.4940</span>, <span style="color: #000000; font-weight:bold;">0.3287</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
sample_sphere.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">0.7119</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">0.0154</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">0.0918</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>; 
sample_sphere.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">0.0533</span>, <span style="color: #000000; font-weight:bold;">0.0596</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">0.5411</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
sample_sphere.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span> <span style="color: #000000; font-weight:bold;">0.0352</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">0.0631</span>, <span style="color: #000000; font-weight:bold;">0.5460</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>; 
sample_sphere.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">0.4776</span>, <span style="color: #000000; font-weight:bold;">0.2847</span>,<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">0.0271</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #009900;">//...</span>
&nbsp;
context3D.setProgramConstantsFromVector<span style="color: #000000;">&#40;</span>Context3DProgramType.FRAGMENT, <span style="color: #000000; font-weight:bold;">0</span>, Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>Number<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #000000; font-weight:bold;">1</span>, radius, scaler<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
context3D.setProgramConstantsFromVector<span style="color: #000000;">&#40;</span>Context3DProgramType.FRAGMENT, <span style="color: #000000; font-weight:bold;">1</span>, Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>Number<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span>area, falloff, <span style="color: #000000; font-weight:bold;">2</span>, depthThresh<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
context3D.setProgramConstantsFromVector<span style="color: #000000;">&#40;</span>Context3DProgramType.FRAGMENT, <span style="color: #000000; font-weight:bold;">2</span>, Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>Number<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span>total_strength, <span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span>, <span style="color: #000000; font-weight:bold;">0.5</span>, <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">8</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
context3D.setProgramConstantsFromVector<span style="color: #000000;">&#40;</span>Context3DProgramType.FRAGMENT, <span style="color: #000000; font-weight:bold;">3</span>, Vector <span style="color: #000000; font-weight: bold;">&lt;</span>Number<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">255</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">255</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">255</span><span style="color: #000000;">&#41;</span>, <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">255</span><span style="color: #000000; font-weight: bold;">*</span><span style="color: #000000; font-weight:bold;">255</span><span style="color: #000000;">&#41;</span>, <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000; font-weight:bold;">255</span>, <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>;
context3D.setProgramConstantsFromVector<span style="color: #000000;">&#40;</span>Context3DProgramType.FRAGMENT, <span style="color: #000000; font-weight:bold;">4</span>, Vector.<span style="color: #000000; font-weight: bold;">&lt;</span>Number<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span>base, <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
context3D.setProgramConstantsFromVector<span style="color: #000000;">&#40;</span>Context3DProgramType.FRAGMENT, <span style="color: #000000; font-weight:bold;">5</span>, sample_sphere<span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>Enjoy! On-line demo to follow soonish, and please to feedback with any comments or improvements.</p>
<p>IMPORTANT NOTE:<br />
the values in this shader are very VERY important, tiny tweaks/mistakes can throw the whole thing off so do be carefull and don&#8217;t be supprised if the world explodes when you play around with it.</p>
<p> <img src='http://blog.bwhiting.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>b</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bwhiting.co.uk/?feed=rss2&#038;p=383</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>3D turntables using b3d (a stage3d engine)</title>
		<link>http://blog.bwhiting.co.uk/?p=374</link>
		<comments>http://blog.bwhiting.co.uk/?p=374#comments</comments>
		<pubDate>Wed, 30 May 2012 08:39:30 +0000</pubDate>
		<dc:creator>bwhiting</dc:creator>
				<category><![CDATA[3d]]></category>

		<guid isPermaLink="false">http://blog.bwhiting.co.uk/?p=374</guid>
		<description><![CDATA[Hooked up b3d to bdj, a little mp3 wrapper for advanced mp3 playback and this was the result: &#160; Added another video (higher res)&#8230; demo features: (runs at 60 fps on my machine, just down to 25-26 while recording) bdj crossfade volume pitch adjustment addition bend and all the usual stuff like: play pause stop [...]]]></description>
			<content:encoded><![CDATA[<p>Hooked up b3d to bdj, a little mp3 wrapper for advanced mp3 playback and this was the result:</p>
<p>&nbsp;</p>
<p><iframe src="http://www.youtube.com/embed/tUAzuFKkERQ?rel=0&amp;showsearch=false&amp;modestbranding=true" frameborder="0" width="420" height="315"></iframe></p>
<p>Added another video (higher res)&#8230;</p>
<p><iframe src="http://www.youtube.com/embed/-SLUCxr91x4?rel=0&amp;showsearch=false&amp;modestbranding=true" frameborder="0" width="420" height="315"></iframe><br />
demo features:</p>
<p>(runs at 60 fps on my machine, just down to 25-26 while recording)</p>
<p>bdj</p>
<ul>
<li>crossfade</li>
<li>volume</li>
<li>pitch adjustment</li>
<li>addition bend</li>
<li>and all the usual stuff like: play pause stop seek cue&#8230;</li>
</ul>
<p>b3d</p>
<ul>
<li>mouse ray generation</li>
<li>ray plane intersections (for the mouse dragging)</li>
<li>as3 native rendering for the interactive areas</li>
<li>screen space anti aliasing (experimental)</li>
<li>a fast bloom filter</li>
<li>a fast DOF filter</li>
</ul>
<p>If anyone wants more info, or for me to upload a better/longer video or a live playable demo just shout. and I will see what I can do.</p>
<p>&nbsp;</p>
<p>things to note:</p>
<p>in order to play tracks backwards and pitch them smoothly I had to cache the whole track as a bytearray 1st, although you can extract on the fly it takes about twice as long (not an issue on higher end machines but a problem all the same&#8230; oh for a faster as3).</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>Acknowledgements:</p>
<p>Lee Brimelow (gave me the idea when he first posted this&#8230;)</p>
<p><a href="http://www.leebrimelow.com/?p=1129">http://www.leebrimelow.com/?p=1129</a></p>
<p>The code for playing an mp3 in reverse came from a response to lees post.</p>
<p>Andre Michelle (some great code)</p>
<p><a href="http://blog.andre-michelle.com/2009/pitch-mp3/">http://blog.andre-michelle.com/2009/pitch-mp3/</a></p>
<p>DanceDreemer for the model on turbosquid</p>
<p>Music: Usher &#8211; Yeah (Speedbreaker remix), Scatman 2003</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bwhiting.co.uk/?feed=rss2&#038;p=374</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Updated Stress Test (4000+ individually transformed normal mapped meshes at 60FPS)</title>
		<link>http://blog.bwhiting.co.uk/?p=362</link>
		<comments>http://blog.bwhiting.co.uk/?p=362#comments</comments>
		<pubDate>Thu, 24 May 2012 11:36:34 +0000</pubDate>
		<dc:creator>bwhiting</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.bwhiting.co.uk/?p=362</guid>
		<description><![CDATA[Should run a bit faster across all machines. &#160; update includes: faster matrix composition bare minimum uploads to GPU by combining the various techniques used in the material into one on the fly. added another material into the demo (normal mapped) added other meshes to you can try and identify where the limits are.. geometry bound or [...]]]></description>
			<content:encoded><![CDATA[<p>Should run a bit faster across all machines.</p>
<p>&nbsp;</p>
<p>update includes:</p>
<ul>
<li>faster matrix composition</li>
<li>bare minimum uploads to GPU by combining the various techniques used in the material into one on the fly.</li>
<li>added another material into the demo (normal mapped)</li>
<li>added other meshes to you can try and identify where the limits are.. geometry bound or draw call bound</li>
</ul>
<p>&nbsp;</p>
<p>link to updated demo:</p>
<p><a href="http://bwhiting.co.uk/b3d/stress2/">http://bwhiting.co.uk/b3d/stress2/</a></p>
<p>&nbsp;</p>
<p>controls (as before but also):</p>
<p>p &#8211; turns of mouse look</p>
<p>n &#8211; changes the mesh being rendered</p>
<p>&nbsp;</p>
<p>Quick tip, go full screen in your browser to maintain access to all the keyboard short-cuts. (click the title bar and hit F11 is the usual command I think)</p>
<p>As before any feedback on performance would be great, there is still room for gains but fir the time being I am happy. Would love to hear what someone with an i7 and a beast of a graphics card can achieve.</p>
<p>&nbsp;</p>
<p>b</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bwhiting.co.uk/?feed=rss2&#038;p=362</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Stage3D optimisation</title>
		<link>http://blog.bwhiting.co.uk/?p=341</link>
		<comments>http://blog.bwhiting.co.uk/?p=341#comments</comments>
		<pubDate>Mon, 19 Mar 2012 12:11:36 +0000</pubDate>
		<dc:creator>bwhiting</dc:creator>
				<category><![CDATA[3d]]></category>
		<category><![CDATA[speed]]></category>

		<guid isPermaLink="false">http://blog.bwhiting.co.uk/?p=341</guid>
		<description><![CDATA[Having been playing with Stage3D for a while now, I though I would write a small piece on optimisation. With great power comes great responsibility! Stage3D give you GPU access, which can expose some serious rendering horsepower, but if you don&#8217;t treat it with respect your going to find you run into limitations pretty quick! [...]]]></description>
			<content:encoded><![CDATA[<p>Having been playing with Stage3D for a while now, I though I would write a small piece on optimisation.</p>
<p><strong>With great power comes great responsibility!</strong></p>
<p>Stage3D give you GPU access, which can expose some serious rendering horsepower, but if you don&#8217;t treat it with respect your going to find you run into limitations pretty quick!</p>
<p>So what follows is a rough (very) guide on how to squeeze the most out of the new 3D apis.</p>
<p>Rule 1 (of 1)<br />
CPU&#8217;s are fast, GPU&#8217;s are faster, communication between the two however is probably the biggest bottleneck you will face.<br />
Therefore: Reduce this wherever possible.<br />
This means minimise calls to the following Context3D functions;<br />
Context3D.drawTriangles();<br />
Context3D.setProgram();<br />
Context3D.setProgramConstants&#8230;();<br />
(actually most of Context3D&#8217;s functions but the above are the real doozies)</p>
<p><strong>drawTriangles()</strong><br />
GPU&#8217;s can draw triangles fast, and lots of them, millions of them every second without breaking into a sweat.<br />
So you might think, &#8220;I can call drawTriangles() 50,000* times no sweat as long as I am only drawing a few triangles in each call.&#8221;.<br />
WRONG!! This command is a mighty expensive one so use it very wisely!<br />
How: When you call drawTriangles you pass it a vector of ids that, per three, represent one triangle. Given that this call is expensive it then makes sense that you pass it as many triangles as possible in one call. Sadly this doesn&#8217;t quite mean you can just group your geometry into big chunks as you cannot change state (alter the material or any parameters) during this call meaning everything that is sent through will be rendered with the same program and set of constants. It does mean however that static (non moving elements) that share the same program/material, can be combined into one list. Things such as trees, grass and any other repeatable geometry are good candidates for this. You can do this for dynamic geometry also but it gets complex as you have to upload transformation data in a separate buffer, this is one way particle systems can be created. The downside is that each time there is a change to any of the objects the whole buffer will need to be re-uploaded. It is also vital that you only try and draw objects that will be seen on screen, so don&#8217;t draw that which is out of view of the camera -&gt; frustum culling saves the day.</p>
<p>side note:<br />
Even high end games rarely want to be issuing more than 1000-2000 draw calls, but the likes of battlefield 3 can get up-to the 3000 mark in some of the environments. Newer consoles however, can issue over 10,000 draw calls and do it much faster due to a more direct access to the hardware.</p>
<p><strong>setProgram()</strong><br />
Assuming you have now done everything in your power to reduce the number of draw calls you issue, next thing to look at is state changes (changing the current program).<br />
Changing state on the GPU might seem like a trivial thing but it is actually something you want to keep to a minimum to be able to squeeze the most out of your graphics card.<br />
There are a few things you can do here to reduce this problem.<br />
1. Group the objects that require drawing by their material/program! For example suppose you had 100 cubes, 50 of them with one material and 50 of them with another. Now if you had a list containing all of those cubes and blindly sent them to be rendered you could end up having to change state a large number of times. If it so happened that each cube in the list had a different material to the object before it, then the program will have to be updated for every draw call. Not good. If that list however was sorted so that</p>
<p>- even if you are only drawing one triangle with this call there</p>
<p>*there is an actual limit of 32,768 drawTriangles() calls per present() call.</p>
<p><strong>setProgramConstants&#8230;()</strong><br />
This function is what allows us to upload constants to the gpu. It is how we upload our matrices and any float1/2/3/4..s that we want to utilise in our shaders. While it may not be a huge bottleneck it still has a noticeable impact on performance in my experiments.<br />
So how to optimise?<br />
Any constant that is likely to be reused by different materials, then upload only once per frame not once per object rendered. So what are the likely culprits?<br />
The view projection matrix! This is 16 float values that will not change between objects so it makes sense to upload it once! 16 numbers vs 16,000 for a 1000 objects and 999 less calls to setProgramConstants, and that is a good thing!<br />
The same applies to anything else that will not vary between objects, camera and light positions or common numbers used in shaders (0,0.5,1,-1..).<br />
What this shows is that it is important to have some sort of system to manage uploads so you can keep track of what is already uploaded and only upload data that isn&#8217;t already there!</p>
<p>side note:<br />
This also translates into how you write shaders, knowing that each constant requires an upload should make you rethink sometimes about how to achieve something whilst using minimal constants, take unpacking a normal from a texture. Usually you would multiple the value from the texture by 2 then subtract one (2 floats required) but the same result can also be achieved with a subtract by 0.5 then a divide 0.5 (1 float required). Perhaps not the best example but I am sure you get the idea. REUSE is your friend!</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p>While I only focused on 3 methods of the context3d, almost all of them will incur some penalty but those highlighted are the ones I have found to be a more serious problem.</p>
<p>Quick additions:<br />
Drawing to a bitmapdata from the gpu is slow, so if you have to do it, ensure it is at a small a resolution as possible, in theory a 1&#215;1 pixel readback should be big enough for picking!<br />
Resizing the back buffer is slow!<br />
Creating textures is slow, don&#8217;t do it on the fly. Pre allocate if possible then pick from a pool (more relevant for post processing).</p>
<p>At some point in the future, I hope to write some test examples that highlight the cost of the functions mentioned above (have already done quite a few but they have been tied into other things rather than dedicated standalone tests).</p>
<p>I hope all of that makes some form of sense to someone <img src='http://blog.bwhiting.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  If you have any additions, corrections or questions&#8230; fire away. Will probably update this from time to time to add in more that I have missed out, it&#8217;s a broad area with many possible optimisations!</p>
<p>&nbsp;</p>
<p>Super Quick Summary:</p>
<p><strong>REDUCE DRAW CALLS!</strong> Group items where possible and use culling to ensure you are only drawing what is neccessary.</p>
<p><strong>REUSE MATERIALS</strong> and <strong>BATCH RENDERING</strong> by material if you can.</p>
<p><strong>UPLOAD THE MINIMUM NUMBER OF CONSTANTS</strong> you can get away with <img src='http://blog.bwhiting.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bwhiting.co.uk/?feed=rss2&#038;p=341</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Stage3D stress test (b3d engine) 20,000 primitives ~ 15,000,000 triangles (updated with playable demo)</title>
		<link>http://blog.bwhiting.co.uk/?p=314</link>
		<comments>http://blog.bwhiting.co.uk/?p=314#comments</comments>
		<pubDate>Thu, 23 Feb 2012 14:01:39 +0000</pubDate>
		<dc:creator>bwhiting</dc:creator>
				<category><![CDATA[3d]]></category>

		<guid isPermaLink="false">http://blog.bwhiting.co.uk/?p=314</guid>
		<description><![CDATA[key: Click to start. WASD or LEFT/RIGHT/UP/DOWN to fly Mouse move to look Shift to fly faster Space to toggle rotation + to add 500 doughnuts - to remove 500 doughnuts m to change material (4 available) Once started double click to toggle fullscreen (NOTE all keys bar arrow keys will be disabled) demo (requires [...]]]></description>
			<content:encoded><![CDATA[<p>key:<br />
Click to start.<br />
WASD or LEFT/RIGHT/UP/DOWN to fly<br />
Mouse move to look<br />
Shift to fly faster<br />
Space to toggle rotation<br />
+ to add 500 doughnuts<br />
- to remove 500 doughnuts<br />
m to change material (4 available)<br />
Once started double click to toggle fullscreen (NOTE all keys bar arrow keys will be disabled)</p>
<p>demo (requires flash player 11 <a href="http://get.adobe.com/flashplayer/">download</a>):<br />
<iframe width="640" height="480" src="http://bwhiting.co.uk/b3d/stress/" frameborder="0" allowfullscreen="1"></iframe><br />
link to play standalone version (better experience):<br />
<a href="http://bwhiting.co.uk/b3d/stress/" target="_blank">here</a></p>
<p>Let me know how it runs (assuming it does) if you can: frame-rate, number of objects it can handle etcetera.</p>
<p>video 1 (looks like crap so am uploading another one):<br />
<iframe width="640" height="300" src="http://www.youtube.com/embed/whueG8Yhgf4?rel=0&#038;showsearch=false&#038;modestbranding=true" frameborder="0" allowfullscreen></iframe></p>
<p>video 2 (hopfully looks a bit better..still looks balls, ah well)<br />
<iframe width="640" height="300" src="http://www.youtube.com/embed/Aa5JkskbQUI?rel=0&#038;showsearch=false&#038;modestbranding=true" frameborder="0" allowfullscreen></iframe></p>
<p>Starts to slow down with 2000 &#8211; 3000 and above primitives on screen <img src='http://blog.bwhiting.co.uk/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  there is still room for some efficiency improvements but not bad for now</p>
<p>Demonstrates how essential good culling is, despite what the stats say whilst recording I am able to cull a full scene of 10,000 objects in under 1 ms on the release player no problems&#8230;without that it would probably die (with it you can happily navigate a scene with 50,000 objects in it at 60fps on a good machine as the majority are being culled away leaving perhaps only 500-1500 visible at any one time when they are spread out like they are in the demo)</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bwhiting.co.uk/?feed=rss2&#038;p=314</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Utils3D.projectVectors vs Utils3D.projectVector vs matrix.transformVector&#8230; note to self</title>
		<link>http://blog.bwhiting.co.uk/?p=286</link>
		<comments>http://blog.bwhiting.co.uk/?p=286#comments</comments>
		<pubDate>Thu, 05 Jan 2012 14:38:44 +0000</pubDate>
		<dc:creator>bwhiting</dc:creator>
				<category><![CDATA[3d]]></category>

		<guid isPermaLink="false">http://blog.bwhiting.co.uk/?p=286</guid>
		<description><![CDATA[just a note to self really&#8230; given: Utils3D.projectVectors(matrix, vertices, projected, uvt); vector1 = Utils3D.projectVector(vector0); vector2 = matrix.transformVector(vector0); then: projected[0] == vector2.x/vector2.w ≈≈ vector1.x projected[1] == vector2.y/vector2.w ≈≈ vector1.y uvt[2] == 1/vector2.w == 1/vector1.w not sure why projectVector != projectVectors result but its close enough&#8230; wasted too much time trying to find out why they are [...]]]></description>
			<content:encoded><![CDATA[<p>just a note to self really&#8230;</p>
<p>given:<br />
Utils3D.projectVectors(matrix, vertices, projected, uvt);<br />
vector1 = Utils3D.projectVector(vector0);<br />
vector2 = matrix.transformVector(vector0);</p>
<p>then:<br />
projected[0] == vector2.x/vector2.w ≈≈ vector1.x<br />
projected[1] == vector2.y/vector2.w ≈≈ vector1.y<br />
uvt[2] == 1/vector2.w == 1/vector1.w</p>
<p>not sure why projectVector != projectVectors result but its close enough&#8230; wasted too much time trying to find out why they are not the same so any ideas on why that is &#8211; let me know!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bwhiting.co.uk/?feed=rss2&#038;p=286</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>As3 short-cut library :- bwhiting swc</title>
		<link>http://blog.bwhiting.co.uk/?p=266</link>
		<comments>http://blog.bwhiting.co.uk/?p=266#comments</comments>
		<pubDate>Tue, 03 Jan 2012 11:56:12 +0000</pubDate>
		<dc:creator>bwhiting</dc:creator>
				<category><![CDATA[misc]]></category>

		<guid isPermaLink="false">http://blog.bwhiting.co.uk/?p=266</guid>
		<description><![CDATA[updated: 17.01.2012 Quick follow-on from the previous post. Added a few more features/functions that I use so feel free to give it a whirl see how you get on, please feedback with any problems or suggestions. &#160; Quick run down of the functions: addListener &#8211; adds an event listener to a target with some added [...]]]></description>
			<content:encoded><![CDATA[<p>updated: 17.01.2012</p>
<p>Quick follow-on from the previous post.</p>
<p>Added a few more features/functions that I use so feel free to give it a whirl see how you get on, please feedback with any problems or suggestions.</p>
<p>&nbsp;</p>
<p>Quick run down of the functions:</p>
<ul>
<li>addListener &#8211; adds an event listener to a target with some added magical powers</li>
<li>removeListener &#8211; removes a single listener assigned by the &#8220;addListener&#8221; function</li>
<li>removeAllListeners &#8211; removes all listeners assigned by the &#8220;addListener&#8221; function</li>
<li>buttonify &#8211; assigns some button like functionality to an interactive object</li>
<li>unbuttonify &#8211; removes the functionality added by a call to the &#8220;buttonify&#8221; function</li>
<li>grid &#8211; arranges the target&#8217;s children in a grid</li>
<li>hbox &#8211; arranges the target&#8217;s children in a hbox</li>
<li>vbox &#8211; arranges the target&#8217;s children in a vbox</li>
<li>addChildren &#8211; a small short-cut to add a few children to a DisplayObjectContainer at once</li>
<li>removeAllChildren &#8211; short-cut to remove all the children of a DisplayObjectContainer</li>
</ul>
<p>&nbsp;</p>
<p>more details (with code and samples) to follow but more info can be found in the docs below.</p>
<p>download swc:<br />
<a href="http://code.google.com/p/bwhiting/downloads/list" target="_blank">bwhiting v1.2</a></p>
<p>docs:<br />
<a href="http://bwhiting.co.uk/bwhiting/" target="_blank">click com.bwhiting.utilities</a></p>
<p><strong>Code and Samples:</strong></p>
<h3>Layout functions</h3>
<p><a href="http://bwhiting.co.uk/bwhiting_demos/LayoutTest.html">demo here</a></p>
<p>In the demo above there are 3 buttons.<br />
The first button arranges the content in a hbox style with a padding of 5<br />
The second button arranges the content in a vbox style with a padding of 5<br />
The third button arranges the content in a grid style with fixed number of columns (in brackets) and a padding of 5</p>
<p>The code that drives it is very simple&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> updateLayout<span style="color: #000000;">&#40;</span>layout<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>layout == <span style="color: #990000;">&quot;hbox&quot;</span><span style="color: #000000;">&#41;</span> hbox<span style="color: #000000;">&#40;</span>container, padding<span style="color: #000000;">&#41;</span>;
	<span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>layout == <span style="color: #990000;">&quot;vbox&quot;</span><span style="color: #000000;">&#41;</span> vbox<span style="color: #000000;">&#40;</span>container, padding<span style="color: #000000;">&#41;</span>;
	<span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>layout == <span style="color: #990000;">&quot;grid&quot;</span><span style="color: #000000;">&#41;</span> grid<span style="color: #000000;">&#40;</span>container, cols, padding<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>So what happens when you click one of those buttons is the above function is called with a parameter &#8220;layout&#8221; based on which button was clicked.<br />
The sprite &#8220;container&#8221; holds 5 simple display objects and the functions hbox/vbox/grid all operate automatically on the children of the 1st parameter.<br />
To to arrange display objects horizontally or vertically or in a grid only takes one line of code&#8230; <img src='http://blog.bwhiting.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3>Event functions</h3>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//adds a listener to the stage for Event.RESIZE and calls the onResize function when triggered</span>
addListener<span style="color: #000000;">&#40;</span><span style="color: #004993;">stage</span>, <span style="color: #004993;">Event</span>.<span style="color: #004993;">RESIZE</span>, onResize<span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #009900;">//adds a listener to mc1 for MouseEvent.CLICK and calls the handleClick1 function when triggered</span>
addListener<span style="color: #000000;">&#40;</span>mc1, <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span>, handleClick1<span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #009900;">//notice how no event parameter is needed in the function</span>
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> handleClick1<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;handleClick1&quot;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #009900;">//adds a listener to mc2 for MouseEvent.CLICK and calls the handleClick2 function when triggered</span>
<span style="color: #009900;">//but also passes mc2 in as a parameter</span>
addListener<span style="color: #000000;">&#40;</span>mc2, <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span>, handleClick2, mc2<span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> handleClick2<span style="color: #000000;">&#40;</span>mc<span style="color: #000000; font-weight: bold;">:</span>Movieclip<span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;handleClick2&quot;</span>, mc<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #009900;">//adds a listener to mc3 for MouseEvent.CLICK and calls the handleClick3 function when triggered</span>
<span style="color: #009900;">//but also passes mc3 as a parameter and tells it to also return the event as a parameter</span>
<span style="color: #009900;">//thanks to the return event flag (the true after mc3)</span>
addListener<span style="color: #000000;">&#40;</span>mc3, <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span>, handleClick3, mc3, <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> handleClick3<span style="color: #000000;">&#40;</span>mc<span style="color: #000000; font-weight: bold;">:</span>Movieclip, e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;handleClick3&quot;</span>, mc, e.<span style="color: #004993;">ctrlKey</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #009900;">//adds a listener to mc4 for MouseEvent.CLICK and calls the handleClick4 function when triggered</span>
<span style="color: #009900;">//but also passes an array containing mc4 and an alpha value</span>
addListener<span style="color: #000000;">&#40;</span>mc4, <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span>, handleClick4, <span style="color: #000000;">&#91;</span>mc4, <span style="color: #000000; font-weight:bold;">0.5</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> handleClick4<span style="color: #000000;">&#40;</span>array<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	array<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span>.<span style="color: #004993;">alpha</span> = array<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #009900;">//adds a listener to mc5 for MouseEvent.CLICK and calls the handleClick5 function when triggered</span>
<span style="color: #009900;">//but also passes the parameters mc5 and 0.5 in sequence thanks to the apply parameters flag (the true at the end)!</span>
addListener<span style="color: #000000;">&#40;</span>mc5, <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span>, handleClick5, <span style="color: #000000;">&#91;</span>mc5, <span style="color: #000000; font-weight:bold;">0.5</span><span style="color: #000000;">&#93;</span>, <span style="color: #0033ff; font-weight: bold;">false</span>, <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> handleClick5<span style="color: #000000;">&#40;</span>mc<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MovieClip</span>, <span style="color: #004993;">alpha</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	mc.<span style="color: #004993;">alpha</span> = <span style="color: #004993;">alpha</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #009900;">//adds a listener to mc6 for MouseEvent.CLICK and calls the handleClick6 function when triggered</span>
<span style="color: #009900;">//but also passes the parameters mc6 and 0.5 in sequence and also sends the event though</span>
addListener<span style="color: #000000;">&#40;</span>mc5, <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">CLICK</span>, handleClick5, <span style="color: #000000;">&#91;</span>mc5, <span style="color: #000000; font-weight:bold;">0.5</span><span style="color: #000000;">&#93;</span>, <span style="color: #0033ff; font-weight: bold;">true</span>, <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> handleClick6<span style="color: #000000;">&#40;</span>mc<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MovieClip</span>, <span style="color: #004993;">alpha</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Number</span>, e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	mc.<span style="color: #004993;">alpha</span> = <span style="color: #004993;">alpha</span>;
	<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>e.<span style="color: #004993;">ctrlKey</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #009900;">//notice how no event parameter is needed in the function</span>
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> onResize<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #009900;">//aligns this centrally on the stage (using the align function included in bwhiting.swc)</span>
	<span style="color: #004993;">align</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span>, <span style="color: #004993;">stage</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This has real potential to save some typing it is also pretty powerful to if you use your imagination.<br />
i.e.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//quick dragging! - not the best example but shows what it can do</span>
addListener<span style="color: #000000;">&#40;</span>box, <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">MOUSE_DOWN</span>, box.<span style="color: #004993;">startDrag</span>, <span style="color: #000000;">&#91;</span><span style="color: #0033ff; font-weight: bold;">false</span>, <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Rectangle</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #000000; font-weight:bold;">0</span>,<span style="color: #004993;">stage</span>.stageWidth<span style="color: #000000; font-weight: bold;">-</span>box.<span style="color: #004993;">width</span>, <span style="color: #004993;">stage</span>.stageHeight<span style="color: #000000; font-weight: bold;">-</span>box.<span style="color: #004993;">height</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>, <span style="color: #0033ff; font-weight: bold;">false</span>, <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>you can also clear a listener or remove all registered listeners with one call (must have been registered with &#8220;addListener&#8221;)<br />
i.e.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900;">//remove single listener</span>
removeListener<span style="color: #000000;">&#40;</span>box, <span style="color: #004993;">MouseEvent</span>.<span style="color: #004993;">MOUSE_DOWN</span><span style="color: #000000;">&#41;</span>;
<span style="color: #009900;">//remove all listeners attached to box</span>
removeAllListeners<span style="color: #000000;">&#40;</span>box<span style="color: #000000;">&#41;</span>;</pre></div></div>

<p>b</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bwhiting.co.uk/?feed=rss2&#038;p=266</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
