Monthly Archives: January 2009

Parallax Displacement Map

Actionscript:
  1. [SWF(width=500, height=500, backgroundColor=0xCCCCCC, frameRate=30)]
  2.  
  3. var canvas:BitmapData = new BitmapData(500, 500, true, 0xFF000000);
  4. addChild(new Bitmap(canvas));
  5.  
  6. // create a radial gradient
  7. var radial:Shape = new Shape();
  8. var m:Matrix = new Matrix()
  9. m.createGradientBox(500,500,0,0,0);
  10. radial.graphics.beginGradientFill(GradientType.RADIAL, [0xFFFFFF, 0x000000], [1, 1], [0, 200], m, SpreadMethod.PAD);
  11.  
  12. radial.graphics.drawRect(0,0,500,500);
  13. radial.x = radial.y = 0;
  14.  
  15. var displace:BitmapData = new BitmapData(500, 500, false,0xFF000000);
  16. displace.perlinNoise(150,150, 3, 30, true, false,0,true);
  17.  
  18. // try different blendmodes here
  19. displace.draw(radial, null ,null, BlendMode.LIGHTEN);
  20.  
  21. var displacementMap:DisplacementMapFilter = new DisplacementMapFilter(displace, new Point(0,0), 1, 1,  0, 0, DisplacementMapFilterMode.WRAP);
  22.  
  23. var scale:Number = 50 / stage.stageWidth;
  24.  
  25. addEventListener(Event.ENTER_FRAME, onLoop);
  26. function onLoop(evt:Event):void {
  27.  
  28.     canvas.copyPixels(displace, canvas.rect, new Point(0,0));
  29.      
  30.     displacementMap.scaleX = 25 - mouseX  * scale ;
  31.     displacementMap.scaleY = 25 - mouseY  * scale ;
  32.    
  33.     canvas.applyFilter(canvas, canvas.rect, new Point(0,0), displacementMap);
  34. }

This one is hard to describe, so take a look at the swf first.

I think displacement maps are underused - they're very powerful. In this snippet I use a DisplacementMapFilter to create a parallax effect on some perlin noise and a radial gradient. The result is an abstract texture that looks 3D.

I got the idea from a demo for the alternative game engine. The first time I saw this demo I had no idea how they were doing it... but after looking at it a few times, I noticed some distortion around the neck area of the model... at that point I recognized it as a displacement map and whipped up a demo (using a drawings as the source image). The alternativa demo also has some color stuff happening to simulate lighting... I'm assuming that's done with a ColorMatrixFilter or two, but I'm not sure.

As in the alternativa demo, this technique could be used with 2 images... one depth map rendered out from your favorite 3D app and one textured render....

Posted in 3D, BitmapData, misc | Tagged , | Leave a comment

Matrix() 3 Point Skew

Actionscript:
  1. [SWF(width=500, height=500, backgroundColor=0xFFFFFF, frameRate=30)]
  2.  
  3. var box:Sprite = createSprite("Rect", 0, 0, 100, 100, 0xFF0000);
  4.  
  5. var d0:Sprite = drag(createSprite("Ellipse",  -5, -5, 10, 10));
  6. d0.x = d0.y = 200;
  7.  
  8. var d1:Sprite = drag(createSprite("Ellipse",  -5, -5, 10, 10));
  9. d1.x = 200
  10. d1.y = 300;
  11.  
  12. var d2:Sprite = drag(createSprite("Ellipse",  -5, -5, 10, 10));
  13. d2.y = d0.y;
  14. d2.x = 300;
  15.  
  16. addEventListener(Event.ENTER_FRAME, onLoop);
  17.  
  18. function onLoop(evt:Event):void {
  19.  
  20.     var m:Matrix = new Matrix();
  21.     m.scale((d2.x - d0.x) / 100,(d1.y - d0.y) / 100);
  22.     m.translate(d0.x, d0.y);
  23.    
  24.     m.c =  (d1.x - d0.x) / 100
  25.     m.b =  (d2.y - d0.y ) / 100
  26.    
  27.     box.transform.matrix = m;
  28. }
  29.  
  30. function createSprite(shape:String, xp:Number, yp:Number, w:Number, h:Number, col:uint=0x444444):Sprite {
  31.     var s:Sprite = new Sprite();
  32.     s.graphics.beginFill(col);
  33.     s.graphics["draw" + shape](xp, yp, w, h);
  34.     addChild(s);
  35.     return s;
  36. }
  37.  
  38. function drag(target:*):*{
  39.     target.addEventListener(MouseEvent.MOUSE_DOWN, function(evt:MouseEvent){ evt.currentTarget.startDrag(); });
  40.     return target;
  41. }
  42.  
  43. stage.addEventListener(MouseEvent.MOUSE_UP, function(){ stopDrag() });

The above creates a red rectangle that can be skewed by dragging 3 points. Why is this so cool you ask?

This is why (move your mouse)

The above was written in flash 7, before the Matrix object existed. So in order to create it I used this technique from Eric Lin.

Posted in DisplayObject, MovieClip, matrix | Tagged , | Leave a comment

nesting(function(calls()))

Actionscript:
  1. [SWF(width=500, height=500, backgroundColor=0x000000, frameRate=30)]
  2.  
  3. for (var i:int = 0; i<10; i++){
  4.        // draggable ellipse
  5.     var dot:Sprite = drag(createSprite("Ellipse",  -10, -10, 20, 20));
  6.     dot.x = Math.random() * stage.stageWidth ;
  7.     dot.y = Math.random() * stage.stageHeight ;
  8. }
  9.  
  10. for (i = 0; i<10; i++){
  11.  
  12.       var box:Sprite = drag(spin(createSprite("Rect", -20, -20, 40, 40, 0xFF0000), Math.random()*5 + 1));
  13.     box.x = Math.random() * stage.stageWidth ;
  14.     box.y = Math.random() * stage.stageHeight ;
  15. }
  16.  
  17.  
  18.  // createSprite can create ellipses or rectangles
  19. function createSprite(shape:String, xp:Number, yp:Number, w:Number, h:Number, col:uint=0x444444):Sprite {
  20.     var s:Sprite = new Sprite();
  21.     s.graphics.beginFill(col);
  22.     // trick from a previous post
  23.     s.graphics["draw" + shape](xp, yp, w, h);
  24.     addChild(s);
  25.     return s;
  26. }
  27.  
  28. // drag and spin add listeners to an untyped target and return that target for easy function nesting
  29. function drag(target:*):*{
  30.     target.addEventListener(MouseEvent.MOUSE_DOWN, function(evt:MouseEvent){ evt.currentTarget.startDrag(); });
  31.     return target;
  32. }
  33.  
  34. function spin(target:*, speed:Number):*{
  35.     target.addEventListener(Event.ENTER_FRAME, function(evt:Event){ evt.currentTarget.rotation+=speed; });
  36.     return target;
  37. }
  38.  
  39. stage.addEventListener(MouseEvent.MOUSE_UP, function(){ stopDrag() });

The above will create some draggable circles and some rotating draggable rects... but that's not really the point....

When prototyping and just playing around I write functions that take an Object as an argument, alter that Object in some way and pass that some Object out as a return value.... this makes it so I can write things like this:

Actionscript:
  1. drag(spin(createSprite("Rect", -20, -20, 40, 40, 0xFF0000), Math.random()*5 + 1));

Readability can be a problem so... consider that before using this for anything...

Posted in dynamic, functions, misc | Tagged , | Leave a comment

Image Airbrush

Actionscript:
  1. [SWF(width=500, height=500, backgroundColor=0x000000, frameRate=30)]
  2.  
  3. // draw an ugly gradient
  4. var size:int = 500;
  5. var pixNum:int = size * size;
  6. var gradient:BitmapData = new BitmapData(size, size, true, 0xFF000000);
  7.  
  8. gradient.lock();
  9. var xp:int, yp:int;
  10. for (var i:int = 0; i<pixNum; i++){
  11.     xp = i % size;
  12.     yp = i / size;
  13.     gradient.setPixel(xp, yp, (yp /= 2) <<16 | (255 - yp) <<8 | (xp / 2) );
  14. }
  15. gradient.unlock();
  16.  
  17. // draw an alphaChannel (radial gradient)
  18. var radius:Number = 50;
  19. var diameter:Number = radius * 2;
  20.  
  21. pixNum = diameter * diameter;
  22.  
  23. var brushAlpha = new BitmapData(diameter, diameter, true, 0x00000000);
  24. var dx:int, dy:int;
  25. var ratio:Number = 255 / radius;
  26. var a:int;
  27.  
  28. brushAlpha.lock();
  29. for (i = 0; i<pixNum; i++){
  30.     xp = i % diameter;
  31.     yp = i / diameter;
  32.     dx = xp - radius;
  33.     dy = yp - radius;
  34.     a = int(255 - Math.min(255,Math.sqrt(dx * dx + dy * dy) * ratio));
  35.     brushAlpha.setPixel32(xp, yp, a <<24);
  36. }
  37. brushAlpha.unlock();
  38.  
  39. // create a black canvas
  40. var canvas:BitmapData = new BitmapData(size, size, true, 0xFF000000);
  41. addChild(new Bitmap(canvas));
  42.  
  43. addEventListener(Event.ENTER_FRAME, onLoop);
  44. function onLoop(evt:Event):void {
  45.     // draw the gradient onto the canvas using the alphaChannel (brushAlpha);
  46.     xp = mouseX - radius;
  47.     yp = mouseY - radius;
  48.     canvas.copyPixels(gradient,
  49.                     new Rectangle(xp, yp, diameter, diameter),
  50.                     new Point(xp, yp), brushAlpha, new Point(0,0), true);
  51. }

This demo creates an airbrush that paints one BitmapData onto another. This is achieved by using the alpha related arguments of the BitmapData.copyPixels() function. If your not familiar with these you should take some time to play around with them... they're very powerful.

copyPixels() is the fastest way to draw in flash, so if you need speed, copyPixels() is the way to go. It's much faster than using draw().

Posted in BitmapData, setPixel | Tagged , | 2 Comments