<?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>webr3.org &#187; haXe</title>
	<atom:link href="http://webr3.org/blog/category/haxe/feed/" rel="self" type="application/rss+xml" />
	<link>http://webr3.org/blog</link>
	<description>brain&#039;s on fire!</description>
	<lastBuildDate>Mon, 30 Aug 2010 00:11:38 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using Sound.extract to create visualizations.</title>
		<link>http://webr3.org/blog/haxe/using-sound-extract-to-create-visualizations/</link>
		<comments>http://webr3.org/blog/haxe/using-sound-extract-to-create-visualizations/#comments</comments>
		<pubDate>Sat, 18 Jul 2009 20:19:35 +0000</pubDate>
		<dc:creator>nathan</dc:creator>
				<category><![CDATA[Flash 10]]></category>
		<category><![CDATA[experiments]]></category>
		<category><![CDATA[haXe]]></category>
		<category><![CDATA[3D computer graphics]]></category>
		<category><![CDATA[Computer memory]]></category>
		<category><![CDATA[Data buffer]]></category>
		<category><![CDATA[Frame rate]]></category>
		<category><![CDATA[Sampling]]></category>
		<category><![CDATA[Technology/Internet]]></category>

		<guid isPermaLink="false">http://webr3.org/blog/?p=101</guid>
		<description><![CDATA[
Technically, this is a really stupid thing to do, and move over it isn't the cleverest thing to do, regardless though I've done it just for the hell of it.
The aim of this post is to prevent anybody else from making the same stupid mistakes! end product isn't too bad though.
To give a quick over [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://webr3.org/experiments/sound/sound-extract-visualize/"><img class="alignnone size-full wp-image-102" title="sound" src="http://webr3.org/blog/wp-content/uploads/2009/07/sound.jpg" alt="sound" width="600" height="250" /></a></p>
<p>Technically, this is a really stupid thing to do, and move over it isn't the cleverest thing to do, regardless though I've done it just for the hell of it.</p>
<p>The aim of this post is to prevent anybody else from making the same stupid mistakes! end product isn't too bad though.</p>
<p>To give a quick over view, Sound.extract() gives access to raw sound data, as a ByteArray of floats; typically you setup two sound objects, the first loads the sound data from source, the second requests sound data from the first at regular intervals, extracting 2048-8192 samples per request and then playing it - idea is that in the method that requests the samples you can quickly pre-process it before its played and make some nice effects.</p>
<p>So my illogical train of thought was to visualize the raw sound data in real time as it plays to create some nice effects.</p>
<h3>The Numbers Game</h3>
<p>Music in flash is sampled at 44100 Hz Stereo - 44100 samples per second,  per channel - we're getting it at intervals of either 2048, 4096 or 8192 samples - meaning our visualization can run at either 21.53fps,  10.76fps or 5.45fps - which lets be honest is hardly going to make a nice smooth effect is it.</p>
<p>So our only option is to somehow synchronize the "timeline" (the screen updating) with the raw sound being played.</p>
<h3>The Approach</h3>
<p>I decided the only real way to do this was to make a continual buffer of sound that could be read independently by both the SampleDataEvent and by my own code to do the visualizations - after some playing around with numbers I finally gathered that if one was to run at a framerate of 63fps and render the 700 samples per channel at a time then I'd be consuming (63*700 = 41000 Hz / samples per second) = the data in realtime.</p>
<p>To make this possible I needed some kind of fast buffer to store the data in, obvious choice was fast memory which is a ByteArray and can be read and written to directly.</p>
<p>More number came in to play when the size of the buffer needed calculated, had to be a multiple of both the render speed (700) and the data feed rate (2048); which is a buffer of 358400 samples, or sample requests. (358400/175 = 2048).</p>
<p>To get the data in to the buffer I was finding the next offset at which i should write the data to my buffer, sound.extract'ing the data in to the buffer and using a modulo to create wrap around so after 175 samples data was written back at the start.</p>
<p>This covers getting the sound in to a fast memory buffer without consuming too many resources, next up to get it out and render it!</p>
<h3>The Rendering</h3>
<p>Rendering uses the same idea of requesting samples from the buffer, however this time the magic number is (358400/700 =) 512.</p>
<p>I implemented a simple framecounter that increments each frame and resets to 0 every 512 frames (giving the loop on the buffer), then quickly read 700 pairs of samples from the buffer each frame.</p>
<p>The left channel float value is then scaled by 300 used as an X position, whilst the right channel creates the Y position - both x and y are then fed to setPixel() on BitmapData whilst a simple calculation of 0xFFFFFF*leftChannel / 2 sets the color value.</p>
<p>Each frame the 700 dual channel samples are rendered creating 700 realtime 2D particles (hopefully) in time with the sound, and a simple blur filter is used to clear the previous frame in a nicer way.</p>
<h3>The Point</h3>
<p>It's all stupid, pointless and just stick to computeSpectrum - you only get 256 samples rather than 700 but it's perfectly in time and allows you do something other than exactly 63 fps; further it'll always be in time with the music.</p>
<h3>The Result</h3>
<p>Well, it's here - I've got it tuned in to my other halfs <a href="http://citybeatradio.org/" target="_blank">radio station citybeat radio</a> at the minute (which i wholeheartedly recommend for any electronic / trance music fans) and well.. <a href="http://webr3.org/experiments/sound/sound-extract-visualize/">here it is with source</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://webr3.org/blog/haxe/using-sound-extract-to-create-visualizations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Something about practical usage of flash.Memory in HaXe</title>
		<link>http://webr3.org/blog/haxe/something-about-practical-usage-of-flash-memory-in-haxe/</link>
		<comments>http://webr3.org/blog/haxe/something-about-practical-usage-of-flash-memory-in-haxe/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 17:17:18 +0000</pubDate>
		<dc:creator>nathan</dc:creator>
				<category><![CDATA[Flash 10]]></category>
		<category><![CDATA[haXe]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[3D computer graphics]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[Arrays]]></category>
		<category><![CDATA[ByteArray]]></category>
		<category><![CDATA[Class]]></category>
		<category><![CDATA[Data types]]></category>
		<category><![CDATA[Fast Memory]]></category>
		<category><![CDATA[flash player]]></category>
		<category><![CDATA[Integer]]></category>
		<category><![CDATA[Lookup table]]></category>
		<category><![CDATA[ram]]></category>
		<category><![CDATA[Technology/Internet]]></category>

		<guid isPermaLink="false">http://webr3.org/blog/?p=79</guid>
		<description><![CDATA[It's dawned on me that I've completely negated to mention why the haXe implementation of flash.Memory (flash player 10's new opcodes / fastmemory support) is so good.
Other than the really obvious bit that its direct access to a block of ram/fast memory which works extremely quickly, there is one other small but vital detail that's [...]]]></description>
			<content:encoded><![CDATA[<p>It's dawned on me that I've completely negated to mention why the haXe implementation of flash.Memory (flash player 10's new opcodes / fastmemory support) is so good.</p>
<p>Other than the really obvious bit that its direct access to a block of ram/fast memory which works extremely quickly, there is one other small but vital detail that's haXe specific and makes a vast difference between alchemy and haxe.</p>
<h2>The Big Difference</h2>
<p>You can think of flash.Memory as a static ByteArray, thus it is accessible from <em>both</em> static class and instance methods.</p>
<p>Now, another feature which offers huge speed increases in haXe is inlining.</p>
<p>Spelling it out - You can only inline static private methods, flash.Memory is all static access. So stick all your data (or big chunks of it) in flash.Memory and then inline  most of your heavy number crunching class.</p>
<p><strong>All inlined, all using fast memory = very very quick swfs!</strong></p>
<h2>Ideas and Tricks</h2>
<p>First of all, get the thought of only being able to have one chunk or type of data in fast memory at a time, you can partition it up and load everything in there.</p>
<p>A common setup I use is to have the first 1024 bytes as fast storage for float, int, byte, uint variables, the next xxx-thousand bytes as raw bitmap data, then all the other data I may want - like a block of 500k x,y,z floats for 3d data, followed by a few lookup charts.</p>
<p>the first 1024 bytes as fast storage for float, int, byte, uint variables - <em>because</em> you can then access the most commonly used vars using small int offsets (0-255) - and since its a small int your using it avoids all stacks completely, the bytecode will only contain the opcode to get the value from fast memory followed by the offset actually in the bytecode - for anything 256 and over its going to hit the int stack to get the offset first (which is barely noticable, but you know every fraction counts)</p>
<p>the next xxx-thousand bytes as raw bitmap data - <em>because </em>you can then work on your bitmap data ultra fast and write it back to the BitmapData instance using setPixels; very fast indeed, faster than a fixed sized vector even.</p>
<p>then all the other data - this is partially obvious, what may not be is the use of lookup charts, but I'll cover that in a moment.</p>
<h3>Accessing..</h3>
<p>Remember flash.Memory differs from a byte array in that you have to add in the offset for each value you want to grab</p>
<ul>
<li>Byte = 1 Byte</li>
<li>Int = 4 Bytes</li>
<li>Float = 4 Bytes</li>
<li>Double = 8 Bytes</li>
</ul>
<p>To use flash.Memory with functions which require a ByteArray.. [ like BitmapData.getPixels() -  Sound.extract() ] .. you can access it directly from flash.system.ApplicationDomain.currentDomain.domainMemory for example:</p>
<pre>// set position to start of the data we need
flash.system.ApplicationDomain.currentDomain.domainMemory.position = SOME_OFFSET;
// use the fast memory
bitmapData.setPixels( bitmapData.rect , flash.system.ApplicationDomain.currentDomain.domainMemory );</pre>
<p><strong>note:</strong> don't be fooled though, you can't simply call the functions on the bytearray in currentDomain, although <em>it is the data</em> that is in fast memory, it does not use the opcodes, which are what speed it up!</p>
<p><strong>Lookup Charts</strong></p>
<p>Often you'll find yourself calling the same code over and over and over; perhaps without realising just how much; consider we make an animation which uses perlin noise to generate 2d or 3d data - like this example - the example reads about 40k uint values from perlin noise, then converts each pixel to individual r,g,b values, then turns it in to a float -1 to 1 using a simple calculation. Thing is.. that means the same calculation runs 3x40000 times per frame at 50 frames a second.. 6 million times a second!</p>
<p>In this scenario, there are only 256 possible inputs to the calculation and 256 results - so rather than working it out 6 million times we can load it all in to a lookup chart, stick the lookup chart in fast memory and then lookup values rather than calculating - often you can really speed things up by doing this. <a href="http://webr3.org/experiments/perlin-particles/light-cloud/PerlinParticleEffects.hx">Practical example with comments here</a>.</p>
<p>Hope this is of use to somebody and clears things up a bit; I use the methods often so you'll see them in the source of my experiments.</p>
<p><img class="alignnone size-full wp-image-80" title="nopic" src="http://webr3.org/blog/wp-content/uploads/2009/07/nopic.jpg" alt="nopic" width="600" height="250" /></p>
]]></content:encoded>
			<wfw:commentRss>http://webr3.org/blog/haxe/something-about-practical-usage-of-flash-memory-in-haxe/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Haxe Optimization with Inline (Elliots Questions)</title>
		<link>http://webr3.org/blog/haxe/haxe-optimization-with-inline-elliots-questions/</link>
		<comments>http://webr3.org/blog/haxe/haxe-optimization-with-inline-elliots-questions/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 01:54:31 +0000</pubDate>
		<dc:creator>nathan</dc:creator>
				<category><![CDATA[haXe]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[Elliot Rock]]></category>

		<guid isPermaLink="false">http://webr3.org/blog/?p=76</guid>
		<description><![CDATA[
Elliot Rock asked me 3 fantastic questions about my previous post, it's good information so best to share for all I think!
So the inline methods and variables is the main optimisation in the final version?
it's the only optimisation made, I simply added the "inline" keyword to 2 methods and all static vars.
Can you explain where [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-77" title="inliner" src="http://webr3.org/blog/wp-content/uploads/2009/06/inliner.jpg" alt="inliner" width="600" height="250" /></p>
<p><a href="http://www.elliotrock.com/">Elliot Rock</a> asked me 3 fantastic questions about my <a href="http://webr3.org/blog/haxe/massive-amount-of-3d-particles-take-two/">previous post</a>, it's good information so best to share for all I think!</p>
<h2>So the inline methods and variables is the main optimisation in the final version?</h2>
<p><strong>it's the only optimisation made</strong>, I simply added the "inline" keyword to 2 methods and all static vars.</p>
<h2>Can you explain where and why the HaXe methods greatly improve performance please :)</h2>
<p>haxe offers 3 main forms of performance optimisation (we're being swf specific here);</p>
<ol>
<li>haXe produces bytecode/opcodes which are optimized better than the adobe compilers / mxmlc. This provides a small but significant increases in speed which all mount up. See the links at the foot of the page for more information.</li>
<li>haXe provides the ability to inline, which I'll explain further in a moment, this often gives a fantastic increase in speed.</li>
<li>haXe provides access to the new opcodes for fast memory access; the only other place to get them is through Alchemy.</li>
</ol>
<p>I must stress that these are 3 minor benefits of haxe compared to the hundreds of others, but they are very good ones none the less!</p>
<h2>I know inlines are so great but haven't found an explanation on why :)</h2>
<p>The simple answer is it produces less and better optimized opcodes + reduces calls to the stack(s).</p>
<p>I'm going to jump straight in to full on detail here! from the examples in question, here is snippet of code from the non-optimised version:</p>
<h3>Non-Optimized</h3>
<pre>private static var WIDTH : Int = 550;
// ...
po = pointToOffset( Std.int(realVector[a*3]) , Std.int(realVector[(a*3)+1]) , WIDTH );
// ...
private static function pointToOffset( vx : Int , vy : Int , vw : Int ) : UInt {</pre>
<p>taking the single functional line only (the middle one) here is are the opcodes produced for it:</p>
<pre>OLabel
OGetLex(Idx(62))
OFindProp(Idx(83))
OGetProp(Idx(83))
OReg(6)
OSmallInt(3)
OOp(OpIMul)
OGetProp(Idx(17))
OToNumber
OToInt
OFindProp(Idx(83))
OGetProp(Idx(83))
OReg(6)
OSmallInt(3)
OOp(OpIMul)
OSmallInt(1)
OOp(OpIAdd)
OGetProp(Idx(17))
OToNumber
OToInt
OGetLex(Idx(62))         // get name Identical
OGetProp(Idx(47))        // get property WIDTH
OCallProperty(Idx(67),3) // call pointToOffset with arg count: 3 (from previous uncommented opcodes)
OToUInt                  // set result as UInt
OToInt                   // set above as Int
OSetReg(5)               // store it in variable po</pre>
<p>and here's the pointToOffset opcodes</p>
<pre>OReg(1)
OReg(2)
OReg(3)
OOp(OpIMul)  // multiply argument 1 and 2
OOp(OpIAdd)  // add argument 3 to result of previous op
ORet         // return result</pre>
<p>so.. it's going to get the static var WIDTH from the class, then pass it and the other 2 arguments from our vector through to pointToOffset, run all the pointToOffset code, get the result, convert it and store in variable po. *phew* quite a lot really.</p>
<h3>Optimised</h3>
<p>now let's look at the optimised version, remember the only thing to change here is the addition of the keyword "inline":</p>
<pre>private static inline var WIDTH : Int = 550;
// ...
po = pointToOffset( Std.int(realVector[a*3]) , Std.int(realVector[(a*3)+1]) , WIDTH );
// ...
static inline function pointToOffset( vx : Int , vy : Int , vw : Int ) : UInt {</pre>
<p>and the corresponding op codes (snipped as the rest if the same)</p>
<pre>OToNumber
OToInt
OIntRef(Idx(4)) // value 550 on the int stack
OOp(OpIMul)     // multiply first two arguments (vector values)
OOp(OpIAdd)     // add the third (width)
OToInt          // set to int
OSetReg(5)      // store in po</pre>
<p><strong>the static var width and the function point to offset are removed and never called.</strong></p>
<p>back to an english explanation, we've removed some normal but heavy operations and replaced them with 3 simple and light operations, giving us a big speed increase.</p>
<p>All credit to <a href="http://ncannasse.fr/">Nicolas</a> the creator of <a href="http://haxe.org/">haxe</a> for this, it's amazing what he's done.</p>
<h3>Reading</h3>
<ul>
<li><a href="http://ncannasse.fr/blog/flash_9_optimizations ">http://ncannasse.fr/blog/flash_9_optimizations </a></li>
<li><a href="http://ncannasse.fr/blog/haxe_swc">http://ncannasse.fr/blog/haxe_swc</a> (specifically "More Optimizations")</li>
<li><a href="http://haxe.org/ref/inline">http://haxe.org/ref/inline</a></li>
</ul>
<p>Hope that answers the questions, and thanks Elliot.</p>
]]></content:encoded>
			<wfw:commentRss>http://webr3.org/blog/haxe/haxe-optimization-with-inline-elliots-questions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Massive amount of 3D Particles Take Two</title>
		<link>http://webr3.org/blog/haxe/massive-amount-of-3d-particles-take-two/</link>
		<comments>http://webr3.org/blog/haxe/massive-amount-of-3d-particles-take-two/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 15:45:16 +0000</pubDate>
		<dc:creator>nathan</dc:creator>
				<category><![CDATA[Flash 10]]></category>
		<category><![CDATA[experiments]]></category>
		<category><![CDATA[haXe]]></category>
		<category><![CDATA[optimization]]></category>

		<guid isPermaLink="false">http://webr3.org/blog/?p=71</guid>
		<description><![CDATA[
Ralph Hauwert created a great demo of the power of Flash 10 / PixelBender / Alchemy  that you're probably familiar with, then Joa Ebert made a fantastic Flash 9 pure AS3 version that's very impressive, then I gave it a go with haXe and flash.Memory which turned out rather well too.
It's been bugging me [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-72" title="take2" src="http://webr3.org/blog/wp-content/uploads/2009/06/take2.jpg" alt="take2" width="600" height="250" /></p>
<p>Ralph Hauwert created a great <a href="http://www.unitzeroone.com/blog/2009/03/18/flash-10-massive-amounts-of-3d-particles-with-alchemy-source-included/" target="_blank">demo of the power of Flash 10 / PixelBender / Alchemy </a> that you're probably familiar with, then Joa Ebert made a fantastic <a href="http://blog.joa-ebert.com/2009/04/03/massive-amounts-of-3d-particles-without-alchemy-and-pixelbender/">Flash 9 pure AS3 version</a> that's very impressive, then I gave it <a href="http://webr3.org/blog/haxe/flash-10-massive-amounts-of-3d-particles-with-haxe/">a go with haXe and flash.Memory</a> which turned out rather well too.</p>
<p>It's been bugging me for some time that the code just wasn't real world enough though - so I thought I'd give it a go using AS3 and flash 10, using only native methods and no fancy 3D calculations that us commoners don't quite get. In short I've stripped it down and turned it into a simple class which uses flash 10 Vectors and Matrix3D.</p>
<p>I've taken a different approach with these demo's, rather than all in realtime, you simply click and it runs the enterFrame code once and displays the time in ms that it took. There is however a <a href="http://webr3.org/experiments/particle-pusher-take2/realtime/">final realtime version</a> using this code at the end.</p>
<p>Four examples are provided, Pure AS3, then the same code converted to haxe, then the haxe "inline" keyword added and finally the whole thing working in realtime.</p>
<h2>AS3 Flash 10 Version with Vector / Matrix3D</h2>
<p>Here's the main source used:</p>
<pre class="brush: as3;">private function enterFrameHandler( event : Event ): void {
 var d : Date = new Date();
 var t : Number = d.getTime();

 updateMatrix( mouseX , mouseY );
 _matrix.transformVectors( particleVector , realVector );

 var pxs : Vector.&amp;lt;uint&amp;gt; = new Vector.&amp;lt;uint&amp;gt;( PIXELS , true );
 var po : int = 0;
 var a : int = 0;

 while( a &amp;lt; PARTICLES ) {
   po = pointToOffset( int(realVector[a*3]) , int(realVector[(a*3)+1]) , WIDTH );
   if( 0 &amp;lt; po &amp;amp;&amp;amp; po &amp;lt; PIXELS) {
     pxs[po] = increaseColor( pxs[po] );
   }
   a++;
 }
 // update display
 this.bitmapData.lock();
 this.bitmapData.setVector( this.bitmapData.rect , pxs );
 this.bitmapData.unlock( this.bitmapData.rect );

 d = new Date();
 textField.text = &quot;single frame time: &quot; + ( d.getTime() - t );
}

private function updateMatrix( mx : Number , my : Number ) : void {
 tx = tx + ((mx - tx)/10);
 ty = ty + ((my - ty)/10);
 _matrix.identity();
 _matrix.appendRotation( tx , Vector3D.Y_AXIS );
 _matrix.appendRotation( ty , Vector3D.X_AXIS );
 _matrix.appendTranslation( CX, CY, 10 );
}

private static function increaseColor( c : uint ) : uint {
 return c &amp;lt; MAXCOLOR ? c + SHADE : 0xFFFFFF;
}

private static function pointToOffset( vx : int , vy : int , vw : int ) : uint {
 return vx + ( vy * vw );
}
</pre>
<p><a href="http://webr3.org/experiments/particle-pusher-take2/pure-as3/" target="_blank">And here's the AS3 result (with full source)</a></p>
<h2>The same code in haXe</h2>
<p>next up I spent literally 2 minutes converting this to haXe syntax, changed no functionality - just slight syntax changes between the languages.</p>
<pre class="brush: plain;">private function enterFrameHandler( event : Event ) : Void {
 var d : Date = Date.now();
 var t : Float = d.getTime();

 updateMatrix( mouseX , mouseY );
 _matrix.transformVectors( particleVector , realVector );

 var pxs : Vector&amp;lt;UInt&amp;gt; = new Vector&amp;lt;UInt&amp;gt;( PIXELS , true );
 var po : Int = 0;
 var a : Int = 0;

 while( a &amp;lt; PARTICLES ) {
   po = pointToOffset( Std.int(realVector[a*3]) , Std.int(realVector[(a*3)+1]) , WIDTH );
   if( 0 &amp;lt; po &amp;amp;&amp;amp; po &amp;lt; PIXELS) {
     pxs[po] = increaseColor( pxs[po] );
   }
   a++;
 }
 // update display
 this.bitmapData.lock();
 this.bitmapData.setVector( this.bitmapData.rect , pxs );
 this.bitmapData.unlock( this.bitmapData.rect );

 d = Date.now();
 textField.text = &quot;single frame time: &quot; + ( d.getTime() - t );
}

private function updateMatrix( mx : Float , my : Float ) : Void {
 tx = tx + ((mx - tx)/10);
 ty = ty + ((my - ty)/10);
 _matrix.identity();
 _matrix.appendRotation( tx , Vector3D.Y_AXIS );
 _matrix.appendRotation( ty , Vector3D.X_AXIS );
 _matrix.appendTranslation( CX, CY, 10 );
}

private static function increaseColor( c : UInt ) : UInt {
 return c &amp;lt; MAXCOLOR ? c + SHADE : 0xFFFFFF;
}

private static function pointToOffset( vx : Int , vy : Int , vw : Int ) : UInt {
 return vx + ( vy * vw );
}</pre>
<p><a href="http://webr3.org/experiments/particle-pusher-take2/identical/" target="_blank">And here's the identical haXe result (with full source)</a></p>
<h2>Optimized haXe Version</h2>
<p>Next up I spent another minute optimizing (literally, one minute) - haXe supports inlining of variables and methods, so I added the inline keyword to the static vars and two of the static methods (increaseColor and pointToOffset)</p>
<pre class="brush: plain;">private function enterFrameHandler( event : Event ) : Void {
 var d : Date = Date.now();
 var t : Float = d.getTime();

 updateMatrix( mouseX , mouseY );
 _matrix.transformVectors( particleVector , realVector );

 var pxs : Vector&amp;lt;UInt&amp;gt; = new Vector&amp;lt;UInt&amp;gt;( PIXELS , true );
 var po : Int = 0;
 var a : Int = 0;

 while( a &amp;lt; PARTICLES ) {
   po = pointToOffset( Std.int(realVector[a*3]) , Std.int(realVector[(a*3)+1]) , WIDTH );
   if( 0 &amp;lt; po &amp;amp;&amp;amp; po &amp;lt; PIXELS) {
     pxs[po] = increaseColor( pxs[po] );
   }
   a++;
 }
 // update display
 this.bitmapData.lock();
 this.bitmapData.setVector( this.bitmapData.rect , pxs );
 this.bitmapData.unlock( this.bitmapData.rect );

 d = Date.now();
 textField.text = &quot;single frame time: &quot; + ( d.getTime() - t );
}

private function updateMatrix( mx : Float , my : Float ) : Void {
  tx = tx + ((mx - tx)/10);
  ty = ty + ((my - ty)/10);
  _matrix.identity();
  _matrix.appendRotation( tx , Vector3D.Y_AXIS );
  _matrix.appendRotation( ty , Vector3D.X_AXIS );
  _matrix.appendTranslation( CX, CY, 10 );
}

static inline function increaseColor( c : UInt ) : UInt {
  return c &amp;lt; MAXCOLOR ? c + SHADE : 0xFFFFFF;
}

static inline function pointToOffset( vx : Int , vy : Int , vw : Int ) : UInt {
 return vx + ( vy * vw );
}
</pre>
<p><a href="http://webr3.org/experiments/particle-pusher-take2/optimized/" target="_blank">And here's the inlined haXe result (with full source)</a></p>
<h2>Summary</h2>
<p>In under five minutes and with tiny code changes we've gained well over 100% speed increase; using nothing fancy and the speed rivals all the heavily optimized versions using every trick in the book.</p>
<p>The final result (simply with the event listener changed to Event.ENTER_FRAME and fps added) <a href="http://webr3.org/experiments/particle-pusher-take2/realtime/">is available here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://webr3.org/blog/haxe/massive-amount-of-3d-particles-take-two/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>BitmapData, Vectors, ByteArrays and Optimization</title>
		<link>http://webr3.org/blog/haxe/bitmapdata-vectors-bytearrays-and-optimization/</link>
		<comments>http://webr3.org/blog/haxe/bitmapdata-vectors-bytearrays-and-optimization/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 10:13:31 +0000</pubDate>
		<dc:creator>nathan</dc:creator>
				<category><![CDATA[Flash 10]]></category>
		<category><![CDATA[haXe]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[BitmapData]]></category>
		<category><![CDATA[ByteArray]]></category>
		<category><![CDATA[Flash Optimization]]></category>
		<category><![CDATA[Vector]]></category>

		<guid isPermaLink="false">http://webr3.org/blog/?p=53</guid>
		<description><![CDATA[
In this post I'm looking at the layout of BitmapData and the fastest way to access / manipulate the raw data. The syntax used is haXe, however the information is good for as3/flash10 right up until the last section which deals with using fast memory and is haXe/Alchemy specific.
The Simple Stuff
A BitmapData 5px by 5px [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-58" title="opt" src="http://webr3.org/blog/wp-content/uploads/2009/06/opt.jpg" alt="opt" width="600" height="250" /></p>
<p>In this post I'm looking at the layout of BitmapData and the fastest way to access / manipulate the raw data. The syntax used is haXe, however the information is good for as3/flash10 right up until the last section which deals with using fast memory and is haXe/Alchemy specific.</p>
<h3>The Simple Stuff</h3>
<p>A BitmapData 5px by 5px will comprise of 25 pixels; the flash API is a bit misleading as to the point coordinate system used,<br />
as such here's the layout of the data contained:</p>
<pre>       X
   0,-,-,-,-
   -,1,-,-,-
 Y -,-,2,-,-
   -,-,-,3,-
   -,-,-,-,4</pre>
<p>the upper left corner is point(0,0) - the lower right is point(4,4); [not 1,1 to 5,5 as the manual suggests]</p>
<p>to access the data I'm going to be using three specific methods:</p>
<ul>
<li> BitmapData.getPixel( x, y );</li>
<li> BitmapData.getPixels( rect );</li>
<li> BitmapData.getVector( rect );</li>
</ul>
<p>the data returned by getPixels and getVector is in array format (ByteArray and Vector&lt;UInt&gt; respectively), both are 0 indexed with 25 entries.</p>
<p>Pixel values run from left to right, so the 5px example above converts to</p>
<pre>index[0] = point(0,0)
index[1] = -
index[2] = -
index[3] = -
index[4] = -
index[5] = -
index[6] = point(1,1)
index[7] = -
index[8] = -
index[9] = -
index[10] = -
index[11] = -
index[12] = point(2,2)
index[13] = -
index[14] = -
index[15] = -
index[16] = -
index[17] = -
index[18] = point(3,3)
index[19] = -
index[20] = -
index[21] = -
index[22] = -
index[23] = -
index[24] = point(4,4)</pre>
<h3>Converting between X,Y and Offsets</h3>
<p>to work with the data in its raw format we're going to need some helper functions/code to convert between point(x,y) and offset.</p>
<p><strong>X,Y to Offset</strong></p>
<pre>function pointToOffset( x : Int , y : Int , width : Int ) : Int {
    return (y * width) + x;
}</pre>
<p>thus for point(3,3):</p>
<pre>    (3 x 5) + 3 = 18</pre>
<p><strong>Offset to X,Y</strong></p>
<pre>function offsetToPoint( offset : Int , width : Int ) : Point {
    return new Point( offset%width , Std.int(offset/width) );
}
// or
bitmapData.getPixel( offset%width , Std.int(offset/width) );</pre>
<p>thus for offset 18 :</p>
<pre>    x: 18%5 = 3
    y: 18/5 = 3</pre>
<h2>Speed Tests</h2>
<p>For each of the three methods [ getPixel(x,y); getPixels(rect); getVector(rect); ] I've done a lot of testing to see which is fastest. All tests use a non transparent bitmapData of 1,920,000 pixels (1600x1200) the task of the test itself is to get all pixels from the bitmapData, load them in to an array/bytearray/vector, add 1 to the color for each pixel and then write the new data back to the BitmapData.</p>
<h3>Starting with the Flash 9 methods available:</h3>
<p><strong>Test One, getPixel -&gt; array -&gt; setPixel</strong><br />
This method incurs two full loops because there is no setPixels(array) method, the time of a bare loop of 1920000 is 8ms.</p>
<pre>var o : Array&lt;UInt&gt; = new Array&lt;UInt&gt;();
for( i in 0...1920000 ) {
    o[i] = bitmapData.getPixel( i%WIDTH , Std.int(i/WIDTH) ) + 1;
}
for( i in 0...1920000 ) {
    bitmapData.setPixel( i%WIDTH , Std.int(i/WIDTH) , o[i] );
}</pre>
<p>results [all times in ms]</p>
<pre>555, 569, 574, 573, 575, 570, 570, 570, 568, 576, 575, 573, 569, 563, 570</pre>
<h3>Test Two - getPixels -&gt; ByteArray -&gt; setPixels</h3>
<p>This method needs two ByteArrays to avoid using temporary var's and setting positions multiple times, the time of simply calling getPixels() alone is 90ms!</p>
<pre>var b : ByteArray = bitmapData.getPixels( bitmapData.rect );
var n : ByteArray = new ByteArray();
n.endian = b.endian; // getPixels often returns big endian values and byte arrays are little endian by default
b.position = 0; // other wise we get an error
for( i in 0...1920000 ) {
    n.writeUnsignedInt( b.readUnsignedInt() + 1 );
}
n.position = 0; // otherwise we get an error
bitmapData.setPixels( bitmapData.rect , n );</pre>
<p>results [all times in ms]</p>
<pre>597, 590, 591, 598, 591, 591, 595, 593, 595, 591, 591, 594, 591, 594, 588</pre>
<p>Rather disapointing results really, we'd get a lovely 2fps at that rate (all tests ran on a quad core btw).</p>
<h2>On to the methods available in flash 10, which let's us use Vectors</h2>
<h3>Test Three, getPixel -&gt; Vector -&gt; setVector</h3>
<p>This test is included purely to show the speed difference between Vectors and Arrays (test one)</p>
<pre>var o : Vector&lt;UInt&gt; = new Vector&lt;UInt&gt;( 1920000 , true );
for( i in 0...1920000 ) {
    o[i] = bitmapData.getPixel( i%WIDTH , Std.int(i/WIDTH) ) + 1;
}
for( i in 0...1920000 ) {
    bitmapData.setPixel( i%WIDTH , Std.int(i/WIDTH) , o[i] );
}</pre>
<p>results [all times in ms]</p>
<pre>439, 443, 447, 444, 447, 445, 448, 447, 446, 447, 447, 445, 447, 449, 448</pre>
<h3>Test Four, getVector -&gt; Vector -&gt; setVector (unfixed vector)</h3>
<p>unfixed vector initialization is virtually instant, way too quick for me to measure anyways (or to make a difference)</p>
<pre>var v : Vector&lt;UInt&gt; = bitmapData.getVector( bitmapData.rect );
var o : Vector&lt;UInt&gt; = new Vector&lt;UInt&gt;();
for( i in 0...1920000 ) {
    o[i] = v[i] + 1;
}
bitmapData.setVector( bitmapData.rect , o );</pre>
<p>results [all times in ms]</p>
<pre>96, 66, 80, 71, 75, 65, 74, 72, 76, 68, 77, 73, 72, 77, 68, 70, 72, 72, 81, 68</pre>
<h3>Test Five, getVector -&gt; Vector -&gt; setVector (fixed size vector)</h3>
<p>fixed size vectors take time, in this instance Vector&lt;UInt&gt;(1920000,true) takes circa 5ms</p>
<pre>var v : Vector&lt;UInt&gt; = bitmapData.getVector( bitmapData.rect );
var o : Vector&lt;UInt&gt; = new Vector&lt;UInt&gt;( 1920000 , true );
for( i in 0...1920000 ) {
    o[i] = v[i] + 1;
}
bitmapData.setVector( bitmapData.rect , o );</pre>
<p>results [all times in ms]</p>
<pre>53, 41, 41, 46, 45, 47, 41, 40, 45, 45, 46, 41, 40, 46, 45, 47, 41, 50, 42, 46</pre>
<h3>Test Six, getVector -&gt; setVector</h3>
<p>this time we cut out the second vector, as essentially it's not needed</p>
<pre>var v : Vector&lt;UInt&gt; = bitmapData.getVector( bitmapData.rect );
for( i in 0...1920000 ) {
    v[i] = v[i] + 1;
}
bitmapData.setVector( bitmapData.rect , v );</pre>
<p>results [all times in ms]</p>
<pre>44, 36, 38, 36, 38, 36, 36, 38, 36, 36, 37, 39, 35, 38, 38, 36, 39, 36, 38, 38</pre>
<p>Some fantastic speed gains, and since we're getting to smaller figures we need to start counting the cost of each line:</p>
<pre>bitmapData.getVector( bitmapData.rect );</pre>
<p>10, 8, 9, 9, 8, 10, 8, 9, 8, 11, 8, 10, 8, 10, 8, 9, 8, 9, 8 [ms]</p>
<pre>for( i in 0...1920000 ) {
    v[i] = v[i] + 1;
}</pre>
<p>25, 25, 25, 27, 27, 26, 25, 26, 25, 25, 26, 25, 27, 25, 25, 26, 25, 26, 25, 25</p>
<p>however the loop alone takes 8ms so we're looking at av 19ms for this part</p>
<pre>bitmapData.setVector( bitmapData.rect , v );</pre>
<p>11, 5, 6, 4, 4, 6, 4, 6, 4, 4, 5, 5, 6, 4, 6, 4, 5, 5, 4, 5</p>
<p>and finally to be really count the cost we need to see what the uint+1 would take by itself</p>
<pre>for( i in 0...1920000 ) {
    0xFFFFF9 + 1;
}</pre>
<p>9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 8, 9, 9</p>
<p>so the true cost of using this single vector method in test six is 37-9 = 28ms for getVector, update vector, setVector</p>
<p>the true cost of using a vector inside the loop is 26-9 = 17ms (we need this later)</p>
<h2>Adding haXe in to the equation.</h2>
<p>Finally, as you probably know haXe provides access to fast memory; fast memory is essentially a ByteArray set in flash.system.ApplicationDomain.currentDomain.domainMemory and accessed through the new opcodes.</p>
<p>A little trick we can use with haxe is to write info from this fast memory to BitmapData via setPixels() since it is a ByteArray.</p>
<p>First off we need to set up the fast memory storage somewhere in our app:</p>
<pre>var storage : ByteArray = new ByteArray();
storage.endian = Endian.LITTLE_ENDIAN;
storage.length = BYTES;
flash.Memory.select( storage );</pre>
<p>to calulate the value of BYTES we simply do WIDTH*HEIGHT*4 (because a UInt takes up 4 Bytes of memory)</p>
<p>we know from earlier that getVector only takes circa 9ms, whereas getPixels takes a shocking 90ms; so what I'm going to do is:</p>
<h3>Test Seven : getVector -&gt; flash.Memory -&gt; setPixels</h3>
<pre>var v : Vector&lt;UInt&gt; = bitmapData.getVector( bitmapData.rect );
for( i in 0...1920000 ) {
    flash.Memory.setI32( i*4 , v[i] + 1 );
}
// ensure position of fast memory is 0
flash.system.ApplicationDomain.currentDomain.domainMemory.position = 0;
bitmapData.lock();
bitmapData.setPixels( bitmapData.rect , flash.system.ApplicationDomain.currentDomain.domainMemory );
bitmapData.unlock( bitmapData.rect );</pre>
<p>results [all times in ms]<br />
34, 31, 32, 34, 32, 32, 32, 33, 32, 33, 31, 33, 32, 31, 33, 32, 33, 32, 33, 31,</p>
<p>We've shaved off 5ms! but hold on.. there's more - we need to analyse this</p>
<p>bitmapData.getVector( bitmapData.rect ); as we know from earlier takes 9ms</p>
<p>the loop takes 9ms</p>
<pre>flash.system.ApplicationDomain.currentDomain.domainMemory.position = 0;
bitmapData.lock();
bitmapData.setPixels( bitmapData.rect , flash.system.ApplicationDomain.currentDomain.domainMemory );
bitmapData.unlock( bitmapData.rect );</pre>
<p>together all takes 5ms</p>
<p>which means that the true cost of using flash.Memory is 32-9-9-5 = 9ms</p>
<p>if you remember from test six, the vector cost was 17ms so we've gained some and lost some, but overall came out on top, and certainly a tonne better than flash 9!</p>
<p>now, these tests where all with a big chunk of 1600x1200 pixels, in a real world app we'd have less.. so lets see with 800x450px (360,000 pixels)</p>
<h3>Vector Only:</h3>
<pre>var v : Vector&lt;UInt&gt; = bitmapData.getVector( bitmapData.rect );
for( i in 0...360000 ) {
    v[i] = v[i] + 1;
}
bitmapData.setVector( bitmapData.rect , v );</pre>
<p>results [all times in ms]<br />
13, 6, 6, 6, 6, 7, 6, 7, 6, 6, 7, 6, 6, 7, 7, 7, 7, 7, 7, 7  = 137 / 20 : av 6.85ms = 146fps</p>
<h3>Vector + Memory</h3>
<pre>var v : Vector&lt;UInt&gt; = bitmapData.getVector( bitmapData.rect );
for( i in 0...360000 ) {
flash.Memory.setI32( i*4 , v[i] + 1 );
}

// ensure position of fast memory is 0
flash.system.ApplicationDomain.currentDomain.domainMemory.position = 0;
bitmapData.lock();
bitmapData.setPixels( bitmapData.rect , flash.system.ApplicationDomain.currentDomain.domainMemory );
bitmapData.unlock( bitmapData.rect );</pre>
<p>results [all times in ms]<br />
7, 6, 6, 6, 5, 6, 6, 5, 5, 5, 6, 5, 5, 5, 6, 5, 5, 5, 5, 5 = 109 / 20 : av 5.45ms = 183fps</p>
<h2>Summary</h2>
<p>so, there's your choices 800x450px swf using methods to update raw bitmap data at 3fps for flash 9 vs <strong>146fps</strong> for pure as3 vs <strong>183 fps</strong> for haXe (and probably alchemy).</p>
<p><strong>imho:</strong> vectors are a fantastic choice, my personal preference goes to using fastmemory, but thats because i can avoid the stack using it and know that whilst its a little more coding, you can't get faster.</p>
<p><strong>overall:</strong> I'm just grateful we've got options to be able to deliver this kind of performance, really opens the doors for some stunning work and libraries not least the upcoming version of papervision and no doublt some libs for haXe.</p>
]]></content:encoded>
			<wfw:commentRss>http://webr3.org/blog/haxe/bitmapdata-vectors-bytearrays-and-optimization/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>3D Perlin Particle Light Cloud (and source) - haXe / Flash 10</title>
		<link>http://webr3.org/blog/haxe/3d-perlin-particle-light-cloud-and-source-haxe-flash-10/</link>
		<comments>http://webr3.org/blog/haxe/3d-perlin-particle-light-cloud-and-source-haxe-flash-10/#comments</comments>
		<pubDate>Fri, 26 Jun 2009 17:19:47 +0000</pubDate>
		<dc:creator>nathan</dc:creator>
				<category><![CDATA[Flash 10]]></category>
		<category><![CDATA[Perlin Noise]]></category>
		<category><![CDATA[experiments]]></category>
		<category><![CDATA[haXe]]></category>
		<category><![CDATA[3D computer graphics]]></category>
		<category><![CDATA[Environment]]></category>
		<category><![CDATA[Fractals]]></category>
		<category><![CDATA[Noise]]></category>
		<category><![CDATA[Technology/Internet]]></category>

		<guid isPermaLink="false">http://webr3.org/blog/?p=47</guid>
		<description><![CDATA[
Been doing some more playing with Perlin Particles, this time going for more natural, and indeed more useful!
Full source and comments included with this one: 3D Perlin Particle Light Cloud
]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-51" title="perlincloud" src="http://webr3.org/blog/wp-content/uploads/2009/06/perlincloud1.jpg" alt="perlincloud" width="600" height="250" /></p>
<p>Been doing some more playing with Perlin Particles, this time going for more natural, and indeed more useful!</p>
<p>Full source and comments included with this one: <a href="http://webr3.org/experiments/perlin-particles/light-cloud/">3D Perlin Particle Light Cloud</a></p>
]]></content:encoded>
			<wfw:commentRss>http://webr3.org/blog/haxe/3d-perlin-particle-light-cloud-and-source-haxe-flash-10/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>3D Perlin Noise Particles in Realtime</title>
		<link>http://webr3.org/blog/haxe/3d-perlin-noise-particles-in-realtime/</link>
		<comments>http://webr3.org/blog/haxe/3d-perlin-noise-particles-in-realtime/#comments</comments>
		<pubDate>Fri, 26 Jun 2009 15:55:06 +0000</pubDate>
		<dc:creator>nathan</dc:creator>
				<category><![CDATA[Flash 10]]></category>
		<category><![CDATA[Perlin Noise]]></category>
		<category><![CDATA[experiments]]></category>
		<category><![CDATA[haXe]]></category>
		<category><![CDATA[Computer graphics]]></category>
		<category><![CDATA[Fractals]]></category>
		<category><![CDATA[Joa Ebert]]></category>
		<category><![CDATA[Noise]]></category>
		<category><![CDATA[Perlin]]></category>
		<category><![CDATA[Technology/Internet]]></category>

		<guid isPermaLink="false">http://webr3.org/blog/?p=42</guid>
		<description><![CDATA[
I was flicking through Joa Eberts site the other day and came across a post he did, speed coding 2; the product of which was a load of 2D perlin noise particles that looked.. well great! Kindly Joa gave me permission to dissect, modify and port it to haxe. Ultimately though I ended up recoding [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-43" title="perlincube" src="http://webr3.org/blog/wp-content/uploads/2009/06/perlincube.jpg" alt="perlincube" width="600" height="250" /></p>
<p>I was flicking through Joa Eberts site the other day and came across a post he did, <a href="http://blog.joa-ebert.com/2008/01/20/speedcoding-2/">speed coding 2</a>; the product of which was a load of 2D perlin noise particles that looked.. well great! Kindly Joa gave me permission to dissect, modify and port it to haxe. Ultimately though I ended up recoding it all from scratch, I did however borrow the nice color transform code from the original though, they're damn nice.</p>
<p>Typically you'd call BitmapData.perlinNoise() to generate a 2 color, 2 octave fractal perlin noise image - you then take the 0-255 color values for both of the channels, scale them and use them as your x &amp; y coordinates.</p>
<p>I decided to twist it a bit and try 3D, so in the experiments I'm generating a 3 color, 4 octave fractal perlin noise image then using the   scaling the values to provide x,y,z coordinates. Then mapping each particle into a 3d space, calculating it's color by merging values of particles which land on the same display pixel together.</p>
<p>Enough of the going on anyways, I'll post the full code for the examples once I've cleaned and commented it - all done in haXe of course.</p>
<p>First off, we have a 3D version of <a href="http://blog.joa-ebert.com/2008/01/20/speedcoding-2/">Joa's original</a>; in this one the particles are all mapped into an imaginary cube and some nice effects added : <a href="http://webr3.org/experiments/perlin-particles/cube-tracing-light-particles/">3D perlin noise particles tracing a cube.</a></p>
<p>Next up I thought I'd try some variations:</p>
<ul>
<li><a href="http://webr3.org/experiments/perlin-particles/lazer-tracing-cube/">Lazer tracing a cube</a> (runs v fast)</li>
<li><a href="http://webr3.org/experiments/perlin-particles/noise-in-a-box/">Perlin Noise, In a Box!</a></li>
</ul>
<p>Quite impressed with the base performance here from haxe, I get 100fps on the lazer tracing example, and circa 45-50 fps on the versions with effects or more complex algo's.</p>
<p>liking perlin noise (and haXe).</p>
]]></content:encoded>
			<wfw:commentRss>http://webr3.org/blog/haxe/3d-perlin-noise-particles-in-realtime/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>More Mashed Up Spheres</title>
		<link>http://webr3.org/blog/haxe/more-mashed-up-spheres/</link>
		<comments>http://webr3.org/blog/haxe/more-mashed-up-spheres/#comments</comments>
		<pubDate>Fri, 26 Jun 2009 15:28:57 +0000</pubDate>
		<dc:creator>nathan</dc:creator>
				<category><![CDATA[Flash 10]]></category>
		<category><![CDATA[experiments]]></category>
		<category><![CDATA[haXe]]></category>
		<category><![CDATA[3D computer graphics]]></category>
		<category><![CDATA[Adobe Flash]]></category>
		<category><![CDATA[Particles]]></category>
		<category><![CDATA[Sphere]]></category>

		<guid isPermaLink="false">http://webr3.org/blog/?p=38</guid>
		<description><![CDATA[
Doesn't need much of an introduction, after my previous post "Amazing what happens when you modify a Sphere"; I realised that I'd missed a few combinations, and as such thought it'd be a good idea to finish off the obvious combinations.. you know, to see what they looked like.
One of the best, and the one [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-39" title="morespheres" src="http://webr3.org/blog/wp-content/uploads/2009/06/morespheres.jpg" alt="morespheres" width="600" height="250" /></p>
<p>Doesn't need much of an introduction, after my previous post "Amazing what happens when you modify a Sphere"; I realised that I'd missed a few combinations, and as such thought it'd be a good idea to finish off the obvious combinations.. you know, to see what they looked like.</p>
<p>One of the best, and the one that makes the most sense formula wise is M16.. here's the formula for it</p>
<pre>x = Math.cos(a) * Math.cos(r);
y = Math.cos(a) * Math.sin(r);
z = Math.sin(a) * Math.sin(r);</pre>
<p>makes sense to me! anyway, on with the show - here are the rest:</p>
<ul>
<li><a href="/experiments/basic-3d-particles/mashed-up-8/">M8</a></li>
<li><a href="/experiments/basic-3d-particles/mashed-up-9/">M9</a></li>
<li><a href="/experiments/basic-3d-particles/mashed-up-10/">M10</a></li>
<li><a href="/experiments/basic-3d-particles/mashed-up-11/">M11</a></li>
<li><a href="/experiments/basic-3d-particles/mashed-up-12/">M12</a></li>
<li><a href="/experiments/basic-3d-particles/mashed-up-13/">M13</a></li>
<li><a href="/experiments/basic-3d-particles/mashed-up-14/">M14</a></li>
<li><a href="/experiments/basic-3d-particles/mashed-up-15/">M15</a></li>
<li><a href="/experiments/basic-3d-particles/mashed-up-16/">M16</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://webr3.org/blog/haxe/more-mashed-up-spheres/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Amazing what happens when you modify a Sphere..</title>
		<link>http://webr3.org/blog/haxe/amazing-what-happens-when-you-modify-a-sphere/</link>
		<comments>http://webr3.org/blog/haxe/amazing-what-happens-when-you-modify-a-sphere/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 16:59:35 +0000</pubDate>
		<dc:creator>nathan</dc:creator>
				<category><![CDATA[Flash 10]]></category>
		<category><![CDATA[experiments]]></category>
		<category><![CDATA[haXe]]></category>
		<category><![CDATA[3D computer graphics]]></category>

		<guid isPermaLink="false">http://webr3.org/blog/?p=35</guid>
		<description><![CDATA[
Taking the 3d particle primatives a bit further (and adding color) I've found that some very nice and rather interesting models can be made when you simply change a sphere a little bit.
Normally I'd do these in order, but in this case I'd recommend you checkout M4 first to get a taste (it's certainly my [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-36" title="eyecandyspheres" src="http://webr3.org/blog/wp-content/uploads/2009/06/eyecandyspheres.jpg" alt="eyecandyspheres" width="600" height="250" /></p>
<p>Taking the 3d particle primatives a bit further (and adding color) I've found that some very nice and rather interesting models can be made when you simply change a sphere a little bit.</p>
<p>Normally I'd do these in order, but in this case I'd recommend you checkout <a href="http://webr3.org/experiments/basic-3d-particles/mashed-up-4/">M4</a> first to get a taste (it's certainly my fav).</p>
<p>Before the list, a very brief description; here we have the code to generate a sphere:</p>
<p><code><br />
for( i in 0...PARTICLES ) {<br />
a = (PARTICLES/360)*i;<br />
r = (i/PARTICLES) * Math.PI;<br />
x = Math.cos(a) * Math.cos(r);<br />
y = Math.cos(a) * Math.sin(r);<br />
z = Math.sin(a);<br />
}<br />
</code></p>
<p>now all we are going to be doing is adding in some steps, or changing the odd cos/sin with cos/sin/tan - nothing complex.</p>
<ul>
<li><a href="http://webr3.org/experiments/basic-3d-particles/mashed-up-1/">M1 Variation</a></li>
<li><a href="http://webr3.org/experiments/basic-3d-particles/mashed-up-2/">M2 Variation</a></li>
<li><a href="http://webr3.org/experiments/basic-3d-particles/mashed-up-3/">M3 Variation</a></li>
<li><a href="http://webr3.org/experiments/basic-3d-particles/mashed-up-4/">M4 Variation</a> (my fav)</li>
<li><a href="http://webr3.org/experiments/basic-3d-particles/mashed-up-5/">M5 Variation</a></li>
<li><a href="http://webr3.org/experiments/basic-3d-particles/mashed-up-6/">M6 Variation</a></li>
<li><a href="http://webr3.org/experiments/basic-3d-particles/mashed-up-7/">M7 Variation<br />
</a></li>
</ul>
<p>Now.. M7 is a rather interesting one.. because it has two very interesting variations (imho) primarily the eye which produces a flat, yet 3d shaded circle..?</p>
<ul>
<li><a href="http://webr3.org/experiments/basic-3d-particles/m7-strange-variation-the-eye/">M7 Strange Variation - The Eye</a></li>
<li><a href="http://webr3.org/experiments/basic-3d-particles/m7-strange-variation-the-feather/">M7 Strange Variation - The Feather</a></li>
</ul>
<p>In addition there are a few more which are created by simply stepping colors or values:</p>
<ul>
<li><a href="http://webr3.org/experiments/basic-3d-particles/3d-abstract-rainbow/">3D Abstract Rainbow</a></li>
<li><a href="http://webr3.org/experiments/basic-3d-particles/one-three-twentyfour/">One, Three, TwentyFour</a></li>
<li><a href="http://webr3.org/experiments/basic-3d-particles/three-six-twelve/">Three, Six, Twelve</a></li>
<li><a href="http://webr3.org/experiments/basic-3d-particles/six-six-twelve/">Six, Six, Twelve</a></li>
</ul>
<p>again, thanks to <a href="http://haxe.org/">haXe</a> for allowing this - remember it's all just particles rendered in realtime.</p>
]]></content:encoded>
			<wfw:commentRss>http://webr3.org/blog/haxe/amazing-what-happens-when-you-modify-a-sphere/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Particle Cubes and Spheres</title>
		<link>http://webr3.org/blog/haxe/particle-cubes-and-spheres/</link>
		<comments>http://webr3.org/blog/haxe/particle-cubes-and-spheres/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 16:11:44 +0000</pubDate>
		<dc:creator>nathan</dc:creator>
				<category><![CDATA[Flash 10]]></category>
		<category><![CDATA[experiments]]></category>
		<category><![CDATA[haXe]]></category>
		<category><![CDATA[3D computer graphics]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Particles]]></category>
		<category><![CDATA[Rendering]]></category>

		<guid isPermaLink="false">http://webr3.org/blog/?p=30</guid>
		<description><![CDATA[
Following earlier unpublished work, and my version of 300k particles in flash, I found that I've created a nice little setup for rendering the results of mathematical functions in flash; specifically rendering the results as 300k/500k particles - I'm kind of fascinated with particles and plotting functions, as are many others - my preference is [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-31" title="primatives" src="http://webr3.org/blog/wp-content/uploads/2009/06/primatives.jpg" alt="primatives" width="600" height="250" /></p>
<p>Following earlier unpublished work, and <a href="http://webr3.org/blog/haxe/flash-10-massive-amounts-of-3d-particles-with-haxe/">my version of 300k particles</a> in flash, I found that I've created a nice little setup for rendering the results of mathematical functions in flash; specifically rendering the results as 300k/500k particles - I'm kind of fascinated with particles and plotting functions, as are many others - my preference is to work with strange attractors; however I thought I'd shoot right back to basics and see what can be made with primatives and modifications of.</p>
<p><strong>pre-warning, if you're looking for eye candy, forget it </strong>- these are simple practical examples and a bit boring, but useful if you need to know how</p>
<p>Starting with the simplest we have the <strong><a href="http://webr3.org/experiments/basic-3d-particles/random-particle-cube/">Particle Cube</a></strong> - 500k random particles plotted in 3d space within an invisible cube. Then I modified it a bit..</p>
<ul>
<li><a href="http://webr3.org/experiments/basic-3d-particles/sine-modified-x-particle-cube/">Sine Modified Particle Cube</a> (it's all skewed)</li>
<li><a href="http://webr3.org/experiments/basic-3d-particles/particle-cube-sine-variation/">Sine and Time Modified Particle Cube</a> (double cube with a flow of particles between)</li>
<li><a href="http://webr3.org/experiments/basic-3d-particles/particle-cube-sin-cos-tan/">Sine, Tan and Cos Modified Particle Cube</a> (one function applied to each axis)</li>
<li><a href="http://webr3.org/experiments/basic-3d-particles/particle-cube-sin-cos-tan-v1/">Alternative Flattened Sine, Tan, Cos Modified Particle Cube</a> (patchwork circular flattened cube!)</li>
</ul>
<p>Next up the <strong><a href="http://webr3.org/experiments/basic-3d-particles/3d-particle-sphere/">Particle Sphere</a></strong> - another 500k particles in 3d space, all random, and then some modified versions..</p>
<ul>
<li><a href="http://webr3.org/experiments/basic-3d-particles/3d-particle-spherical-circles/">Spherical Circles</a> (evenly distributed particles, creates a single line wrapped around a sphere)</li>
<li><a href="http://webr3.org/experiments/basic-3d-particles/3d-emphasized-particle-sphere/">Emphasized Particle Sphere</a> (double spaced to create a messy particle sphere - but more like particles)</li>
<li><a href="http://webr3.org/experiments/basic-3d-particles/3d-particle-half-sphere/">Half a Sphere</a> (go on, guess)</li>
<li><a href="http://webr3.org/experiments/basic-3d-particles/3d-white-bubble/">White Particle Bubble</a> (evenly distributed to create a kind of bubble effect)</li>
<li><a href="http://webr3.org/experiments/basic-3d-particles/the-binlid-3d-particles/">Split Sections of a Sphere</a> (two opposing but differing chunks of sphere, again all particles, like a swing bin lid)</li>
</ul>
<p>And finally a couple of simple experiments..</p>
<ul>
<li><a href="http://webr3.org/experiments/basic-3d-particles/atan2-warped-random-plane/">Random Warped Particle Plane</a> (random x, random y, atan2(x,y) for z)</li>
<li><a href="http://webr3.org/experiments/basic-3d-particles/3d-beach-bubble/">Beach Bubble</a> (the white bubble with some colour, like a beach ball but translucent)</li>
</ul>
<p>In summary, it's good to see how easily different effects and primatives can be build, and even better to see how easily they can be modified with basic mathematical functions. (and how good <a href="http://haxe.org/">haXe</a> is :p)</p>
]]></content:encoded>
			<wfw:commentRss>http://webr3.org/blog/haxe/particle-cubes-and-spheres/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Flash 10, Massive amounts of 3D Particles (with haXe)</title>
		<link>http://webr3.org/blog/haxe/flash-10-massive-amounts-of-3d-particles-with-haxe/</link>
		<comments>http://webr3.org/blog/haxe/flash-10-massive-amounts-of-3d-particles-with-haxe/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 20:13:42 +0000</pubDate>
		<dc:creator>nathan</dc:creator>
				<category><![CDATA[Flash 10]]></category>
		<category><![CDATA[haXe]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Adobe Flash]]></category>
		<category><![CDATA[Curly bracket programming languages]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Ralph Hauwert]]></category>

		<guid isPermaLink="false">http://webr3.org/blog/?p=16</guid>
		<description><![CDATA[A couple of months ago I was playing a round with strange attractors in flash, using AS3 and flex, then a post caught my attention.
Ralph Hauwert somehow managed to push 300k+ 3D particles around in realtime using alchemy, pixelbender and flash 10; I played around with the code a bit and made similar in pure [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of months ago I was playing a round with strange attractors in flash, using AS3 and flex, then a post caught my attention.</p>
<p><a href="http://www.unitzeroone.com/blog/2009/03/18/flash-10-massive-amounts-of-3d-particles-with-alchemy-source-included/" target="_blank">Ralph Hauwert</a> somehow managed to <a href="http://www.unitzeroone.com/labs/alchemyPushingPixels/" target="_blank">push 300k+ 3D particles around in realtime using alchemy, pixelbender and flash 10</a>; I played around with the code a bit and made similar in pure as3, nothing magnificent.. a month later in a totally unrelated event (namely me getting thoroughly p'd off with PHP) I discovered <a href="http://haxe.org/" target="_blank">haXe</a> and set to work playing around with it.</p>
<p>Finally in another totally unrelated event I came across <a href="http://blog.joa-ebert.com/2009/04/03/massive-amounts-of-3d-particles-without-alchemy-and-pixelbender/">Joa Eberts AS3 only version</a> of Ralphs earlier work; upon closer inspection I found it wasn't as fast as Ralph's alchemy version but completely awesome code non-the-less, to do this in pure AS3 is simply amazing.</p>
<p>As if you can't guess where this is going.. I thought I'd give it a go in "pure haXe" to see if one could replicate the speed of Ralphs version or maybe even top it. Sparing any further detail..</p>
<p>Here's <a href="http://webr3.org/experiments/haxe-particle-pusher/300k/">haXe pushing 300k+ 3d particles around in realtime</a> (source included)</p>
<p>and...</p>
<p>here's haXe pushing <a href="http://webr3.org/experiments/haxe-particle-pusher/600k/">600k particles</a> (twice as many), and erm <a href="http://webr3.org/experiments/haxe-particle-pusher/OneMillion/">1 Million</a> (a bit stuttery but hell why not)</p>
<p>and.. (just for fun)</p>
<p>here's haXe pushing <a href="http://webr3.org/experiments/haxe-particle-pusher/300k-Color/">300k particles in color</a>, and <a href="http://webr3.org/experiments/haxe-particle-pusher/500k-Color/">500k in color</a> for good measure! - before downplaying the color versions, it's worth noting that each r,g,b value is independantly calculated and weighted as a float then combined to a render color right before updating the bitmap, so in effect it's not just x,y,z stored for each pixel, it's r,g,b as well - so 2x more number crunching + a bit more.</p>
<p>think I can safely say I love haXe and since doign this I've been going nuts doing <a href="http://webr3.org/experiments/" target="_self">lots of experiments</a> using similar code :p</p>
<p>so, any doublts about haXe's perfomance with flash can be considered resolved imho.</p>
<p>regards, nathan</p>
<p><img class="alignleft size-full wp-image-17" title="300k-lol" src="http://webr3.org/blog/wp-content/uploads/2009/06/300k-lol.jpg" alt="300k-lol" width="600" height="250" /></p>
]]></content:encoded>
			<wfw:commentRss>http://webr3.org/blog/haxe/flash-10-massive-amounts-of-3d-particles-with-haxe/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
