Utils3D.projectVectors() & 100,000 pixels

Actionscript:
  1. var matrix:Matrix3D = new Matrix3D();
  2.  
  3. const PARTICLE_NUM:int = 100000;
  4. var verts:Vector.<Number> = new Vector.<Number>();
  5. var pVerts:Vector.<Number> = new Vector.<Number>();
  6. var uvts:Vector.<Number> = new Vector.<Number>();
  7.  
  8. for (var i:int = 0; i<PARTICLE_NUM; i++){
  9.     verts.push(Math.random()*250 - 125);
  10.     verts.push(Math.random()*250 - 125);
  11.     verts.push(Math.random()*250 - 125);
  12.    
  13.     pVerts.push(0), pVerts.push(0);
  14.     uvts.push(0), uvts.push(0), uvts.push(0);
  15. }
  16.  
  17. var canvas:BitmapData = new BitmapData(400,400,false, 0x000000);
  18. addChild(new Bitmap(canvas));
  19. var dx:Number=0;
  20. var dy:Number=0;
  21.  
  22. addEventListener(Event.ENTER_FRAME, onLoop);
  23. function onLoop(evt:Event):void {
  24.  
  25.     dx += (mouseX - dx)/4;
  26.     dy += (mouseY - dy)/4;
  27.    
  28.     matrix.identity();
  29.     matrix.appendRotation(dy,Vector3D.X_AXIS);
  30.     matrix.appendRotation(dx,Vector3D.Y_AXIS);
  31.     matrix.appendTranslation(200, 200, 0);
  32.    
  33.     Utils3D.projectVectors(matrix, verts, pVerts, uvts);
  34.    
  35.     canvas.lock();
  36.     canvas.fillRect(canvas.rect, 0x000000);
  37.         var leng:int = pVerts.length;
  38.     for (var i:int = 0; i<leng; i+=2){
  39.         canvas.setPixel( pVerts[i], pVerts[i + 1], 0xFFFFFF);
  40.     }
  41.     canvas.unlock();
  42. }

The above shows an easy way to use Utils3D.projectVectors() to move some pixels around in 3D. Since the 3D math is done behind the scenes by the flash player it runs quite fast...

This entry was posted in 3D, BitmapData, Vector, matrix, setPixel and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

3 Comments

  1. Posted June 4, 2009 at 7:55 am | Permalink

    thanks a lot!

    line 37 can be:
    for (var i:int = 0; i<PARTICLE_NUM*2; i+=2){
    that way you actually draw all 100.000 dots

    i tested this with matrix.transformVectors, exact same result, but you get a transformed z-coord, which can be used for fun stuff like zbuffering

  2. Posted June 4, 2009 at 8:44 am | Permalink

    nice catch bugshake - that’s a bug/flaw in my logic :) …. there are a few other posts that do that same thing… I’ll have to fix those soon.

    yeah transformVectors is cool.

  3. Posted June 4, 2009 at 9:19 am | Permalink

    I lucked out… I used this same code as the basis for a few other posts… but I didn’t have a const for the number of particles in any of them - instead I used pVerts.length - inadvertently avoiding the issue. Thanks for pointing this out… makes me wonder what other kinds of things like that are lurking about in some of the posts on this site :)

    I checked out your site and I dig the c# style trace post - very nice idea.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*