/**
* Copyright bradsedito ( http://wonderfl.net/user/bradsedito )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/cLpa
*/
// BradSedito
// A 3D Parallax effect, dynamically generated via the PerlinNoise method.
//
// I feel like there's a lot of applications for this. Think shiny,
// reflective surfaces, some sort of faux ray-trace effect. Get creative with it ;)
// Next i'd like to add some dynamic color / blur to it.
package {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.filters.*;
[SWF(width=500, height=500, backgroundColor=0xCCCCCC, frameRate=30)]
public class PerlinDisplace extends MovieClip {
private var canvas:BitmapData;
private var radial:Shape;
private var m:Matrix;
private var displace:BitmapData;
private var displacementMap:DisplacementMapFilter;
private var scale:Number;
public function PerlinDisplace(){
// init
canvas = new BitmapData(500, 500, true, 0xFF000000);
addChild(new Bitmap(canvas));
// create a radial gradient
radial = new Shape();
m = new Matrix()
m.createGradientBox(500,500,0,0,0);
radial.graphics.beginGradientFill(GradientType.RADIAL, [0xFFFFFF, 0x000000], [1, 1], [0, 200], m, SpreadMethod.PAD);
radial.graphics.drawRect(0,0,500,500);
radial.x = radial.y = 0;
displace = new BitmapData(500, 500, false,0xFF000000);
displace.perlinNoise(100,100, 2, 2, true, true, 0, true);
// try different blendmodes here
displace.draw(radial, null ,null, BlendMode.ADD);
displacementMap = new DisplacementMapFilter(displace, new Point(0,0), 1, 1, 0, 0, DisplacementMapFilterMode.WRAP);
scale = 1000 / stage.stageWidth;
addEventListener(Event.ENTER_FRAME, onLoop);
}
// private methods
private function onLoop(evt:Event):void {
canvas.copyPixels(displace, canvas.rect, new Point(0,0));
displacementMap.scaleX = 25 - mouseX * scale ;
displacementMap.scaleY = 25 - mouseY * scale ;
canvas.applyFilter(canvas, canvas.rect, new Point(0,0), displacementMap);
}
}
}