Sphere of Squares

[SWF(width = 600, height = 600, backgroundColor=0x000000)]
var squareNum:int  = 1000;
var hw:Number = stage.stageWidth / 2;
var hh:Number = stage.stageHeight / 2;
// verts defines a single square
var verts:Vector.<Number> = Vector.<Number>([-20, 0, 0, 20, 0, 0, 20, 0, 40, -20, 0, 40, -20, 0, 0]);
var cmds:Vector.<int> = Vector.<int>([1,2,2,2,2]);
var tempVerts:Vector.<Number> = new Vector.<Number>();
var newVerts:Vector.<Number> = new Vector.<Number>();
var pVerts:Vector.<Number> = new Vector.<Number>(10 * squareNum);
var uv:Vector.<Number> = new Vector.<Number>(15 * squareNum);
var vectors:Shape = new Shape();
var canvas:BitmapData = new BitmapData(stage.stageWidth,stage.stageHeight,false, 0x000000);
addChild(new Bitmap(canvas));
var blurred:BitmapData = new BitmapData(stage.stageWidth,stage.stageHeight,false, 0x000000);
var blur:BlurFilter = new BlurFilter(20,20,1);

var m:Matrix3D = new Matrix3D();

var radius:Number = 200;
// duplicate the verts array a bunch of times
// each time moving the square to a random place on the
// circumference of a sphere
for (var i:int = 0; i<squareNum; i++){
	m.identity();
	var s:Number = Math.random()*.5 + .5;
	m.appendScale(s, s, s);
	m.appendRotation(90,Vector3D.X_AXIS);
	m.appendTranslation(0, 0, radius);
	m.appendRotation(Math.random()*360,Vector3D.X_AXIS);
	m.appendRotation(Math.random()*360,Vector3D.Y_AXIS);
	m.appendRotation(Math.random()*360,Vector3D.Z_AXIS);
	m.transformVectors(verts,tempVerts);
	newVerts = newVerts.concat(tempVerts);
	cmds = cmds.concat(Vector.<int>([1,2,2,2,2]));
}
newVerts.fixed = pVerts.fixed = uv.fixed = true;
var dx:Number = 0, dy:Number = 0;
var pnt:Point = new Point();
addEventListener(Event.ENTER_FRAME, onLoop);
function onLoop(evt:Event):void {
	   dx += (mouseX - dx) / 4;
	   dy += (mouseY - dy) / 4;
	   m.identity();
	   m.appendRotation(dx,Vector3D.Z_AXIS);
	   m.appendRotation(dy,Vector3D.X_AXIS);
	   m.appendTranslation(hw,hh, 0);
	   Utils3D.projectVectors(m, newVerts, pVerts, uv);
	   with(vectors.graphics){
		   clear();
		   beginFill(0xFFFFFF);
		   drawCircle(hw, hh, radius+10);
		   beginFill(0x000000);
		   drawPath(cmds, pVerts, GraphicsPathWinding.NON_ZERO);
	   }
	   canvas.fillRect(canvas.rect, 0x000000);
	   canvas.draw(vectors);
	   blurred.copyPixels(canvas, canvas.rect, pnt);
	   blurred.applyFilter(blurred,blurred.rect, pnt, blur);
	   canvas.draw(blurred, null, null, BlendMode.SCREEN);
}

This snippet is similar to yesterdays post but the visual result is rather different. This one does a little more Matrix3D stuff resulting in a sphere made up entirely of squares. This is obscured by the size of the squares and the fact that they overlap and cut each other up. BitmapData a BlurFilter and a BlendMode give the entire thing a slight glow…


Have a look at the swf here…

12 comments

  1. That’s beautiful Zevan! I am amazed by your ability to create totally outstanding and different thing every day, how do you do that? It reminds me BIT-101’s calendar with experiments a few years ago.

  2. Thanks Og2t… to answer your question, whenever I have an idea for something I write it down – the result is that I always have a small list of things to try to create. Also, if I find myself coding something interesting during a day of commercial work, I’ll take a few minutes to turn it into a post.

    I could actually probably talk about bit-101 for a super long time – I’ve enjoyed that site for years… has always been really inspiring.

  3. I’m still a big fan of bit-101 even though it is mainly about iphone dev now… If you just discovered actionsnippet there’s lots of stuff for you to mess with, I think I’m nearing the 300th post ๐Ÿ˜‰

  4. yeah, a few people have suggested it … and may just have to start doing that… I started working on a script that would autopost to wonderful, but never finished it… (not even sure its possible actually – never got far enough along with the script)…

  5. I guess your right though, I should probably start posting there…. It bugs me that they don’t have a list of most recently added things independent of rank…

Leave a Reply

Your email address will not be published. Required fields are marked *