forked from: Tunnel Effect
Tunnel Effect
by rect (http://blog.r3c7.net/)
// forked from rect's Tunnel Effect
//
// Tunnel Effect
// by rect (http://blog.r3c7.net/)
//
package
{
import flash.events.Event;
import flash.display.*;
import flash.geom.Point;
import flash.utils.getTimer;
import flash.utils.ByteArray;
[SWF(backgroundColor="#000000")]
public class main extends Sprite
{
private var w:uint = 200;
private var h:uint = 200;
private var tw:uint = 256;
private var th:uint = 256;
private var cont:Bitmap;
private var buffer:BitmapData;
private var zero:Point = new Point(0,0);
private var texture:Vector.<Vector.<uint>>;
private var distTable:Vector.<Vector.<int>>;
private var angleTable:Vector.<Vector.<uint>>;
private var byte:ByteArray;
public function main():void
{
stage.frameRate = 40;
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.quality = StageQuality.LOW;
var bmp:BitmapData = new BitmapData(tw, th, false, 0x000000);
bmp.perlinNoise(32,32,3,180,true,false,0,true);
byte = new ByteArray();
buffer = new BitmapData(w, h, false, 0x000000);
var scBmp:BitmapData = new BitmapData(w, h, false, 0x000000);
cont = new Bitmap(scBmp);
addChild(cont);
cont.width = stage.stageWidth;
cont.height = stage.stageHeight;
cont.x = (stage.stageWidth - cont.width) / 2;
cont.y = (stage.stageHeight - cont.height) / 2;
texture = new Vector.<Vector.<uint>>(tw);
for (var x:int = 0; x < tw; x++)
{
texture[x] = new Vector.<uint>(th);
for (var y:int = 0; y < th; y++)
{
texture[x][y] = bmp.getPixel(x, y);
}
}
distTable = new Vector.<Vector.<int>>(w*2);
angleTable = new Vector.<Vector.<uint>>(w*2);
for (var i:int = 0; i < w*2; i++)
{
distTable[i] = new Vector.<int>(h*2);
angleTable[i] = new Vector.<uint>(h*2);
for (var j:int = 0; j < h*2; j++)
{
var ratio:Number = 24.0;
var distance:int = int(ratio * th / Math.sqrt((i - w) * (i - w) + (j - h) * (j - h))) % th;
var angle:Number = (0.5 * tw * Math.atan2(j - h, i - w) / Math.PI);
distTable[i][j] = distance;
angleTable[i][j] = angle;
}
}
addEventListener(Event.ENTER_FRAME, update);
}
private function update(e:Event):void
{
var targetBmd:BitmapData = cont.bitmapData;
var bytes:ByteArray = byte;
bytes.position = 0;
var pixels:uint = w * h;
var tick:Number = (getTimer() *0.005);
var shX:int = (tw * 0.9 * tick);
var shY:int = (th * 0.25 * tick);
var shlX:int = w / 2 + (w / 4 * Math.sin(tick * 0.017453292519943295));
var shlY:int = h / 2 + (h / 4 * Math.sin(tick * 0.017453292519943295));
for (var i:uint = 0; i < pixels; i++)
{
var x:int = Math.floor(i % w);
var y:int = Math.floor(i / w);
var rgb:uint = texture[(distTable[x + shlX][y + shlY] + shX) & (tw - 1)][(angleTable[x + shlX][y + shlY] + shY) & (th - 1)];
bytes.writeUnsignedInt(rgb);
}
bytes.position = 0;
buffer.setPixels(buffer.rect, bytes);
targetBmd.copyPixels(buffer, buffer.rect, zero);
}
}
}