forked from: Force Field
高速化に挑戦してみた
// forked from Saqoosha's Force Field
// 高速化に挑戦してみた
package {
import __AS3__.vec.Vector;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.BlendMode;
import flash.display.Graphics;
import flash.display.PixelSnapping;
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.StageQuality;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.filters.BlurFilter;
import flash.filters.ColorMatrixFilter;
import flash.filters.GlowFilter;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.utils.Timer;
import flash.utils.getTimer;
import frocessing.color.ColorHSV;
import net.hires.debug.Stats;
[SWF(backgroundColor=0x0, frameRate=60)]
public class FlashTest extends Sprite {
private static const ZERO_POINT:Point = new Point();
private static const MAP_SCALE:Number = 0.25;
private static const TRAIL_SCALE:Number =4;
private static const DRAW_SCALE:Number = 0.5;
private var _timer:Timer;
private var _seed:Number = new Date().getTime();
private var _offsets:Array = [new Point(), new Point()];
private var _forcemap:BitmapData;
private var _count:int = 0;
private var _particles:Vector.<Particle>;
private var _canvas:Shape;
private var _fade:BitmapData;
private var _darken:ColorMatrixFilter = new ColorMatrixFilter([
1, 0, 0, 0, -2,
0, 1, 0, 0, -2,
0, 0, 1, 0, -2,
0, 0, 0, 1, 0
]);
private var _blur:BlurFilter = new BlurFilter(2, 2, 1);
private var _blurTimer:Timer = new Timer(60);
private var _drawMatrix:Matrix = new Matrix(DRAW_SCALE, 0, 0, DRAW_SCALE, 0, 0);
private var _drawColor:ColorTransform = new ColorTransform(0.1, 0.1, 0.1);
public function FlashTest() {
this.stage.quality = StageQuality.LOW;
this._timer = new Timer(1000, 0);
this._timer.addEventListener(TimerEvent.TIMER, this._onTimer);
this._timer.start();
this._forcemap = new BitmapData(475 * MAP_SCALE, 475 * MAP_SCALE, false, 0x0);
this._blurTimer.addEventListener( TimerEvent.TIMER, onBlurTimer );
this._blurTimer.start();
this._particles = new Vector.<Particle>();
this._fade = new BitmapData( 475 * DRAW_SCALE, 475 * DRAW_SCALE, false, 0x0 );
var bm:Bitmap = this.addChild(new Bitmap(this._fade, PixelSnapping.AUTO, true)) as Bitmap;
bm.scaleX = bm.scaleY = 1 / DRAW_SCALE;
this._canvas = new Shape();
this.addChild( this._canvas );
this._canvas.blendMode = BlendMode.ADD;
this.addEventListener(Event.ENTER_FRAME, this._onEnterFrame);
this.stage.addChild(new Stats());
}
private function _onTimer(e:TimerEvent = null):void {
var t:int = getTimer();
this._offsets[0].x = t / 20;
this._offsets[1].y = t / 32;
this._forcemap.perlinNoise(150, 150, 2, this._seed, true, true, 3, false, this._offsets);
}
private function onBlurTimer(e:TimerEvent):void{
if (this._count & 1) {
this._fade.lock();
this._fade.draw(this._canvas, this._drawMatrix, this._drawColor, BlendMode.ADD);
this._fade.applyFilter(this._fade, this._fade.rect, ZERO_POINT, this._blur);
this._fade.unlock();
}
if (this._count & 0x4) {
this._onTimer();
}
this._count++;
}
private var color:ColorHSV = new ColorHSV(0, 0.5);
private static const colorCycle:uint = 20000;
private static const angleCycle:uint = 1000;
private static const sx:int = 237;
private static const sy:int = 237;
private static const PI:Number = Math.PI;
private function _onEnterFrame(e:Event):void {
var n:uint = 2;
var plist:Vector.<Particle>= this._particles;
var a:Number; var p:Particle;
var t:uint = getTimer();
const angle:Number= t / angleCycle;
color.h = t / colorCycle * 360;
while (n--) {
a = angle + Math.random()* PI;
p = new Particle( sx, sy, Math.cos(a), Math.sin(a), color.value );
plist.push(p);
a = angle + Math.random()* PI;
p = new Particle( sx, sy, Math.cos(a), Math.sin(a), color.value );
plist.push(p);
a = angle + Math.random()* PI;
p = new Particle( sx, sy, Math.cos(a), Math.sin(a), color.value );
plist.push(p);
}
var g:Graphics = this._canvas.graphics;
g.clear();
var map:BitmapData = this._forcemap;
n = plist.length;
var c:uint;
const mapScale:Number =MAP_SCALE;
const trailScale:Number =TRAIL_SCALE;
const ac:uint = 0xFF; const bc:uint = 0x80;const dlife:uint= 5;
const top:int =-10; const bottom:int = 485;
const left:int =-10; const right:int = 485;const lifeK:Number = 0.0006;
const vFriction:Number = 0.16;
while (n--) {
p =plist[n];
c = map.getPixel(p.x * mapScale, p.y * mapScale );
p.vx += (((c >> 16) & ac) - bc) / bc*vFriction;
p.vy += (((c >> 8) & ac) - bc) / bc*vFriction;
p.x += p.vx;
p.y += p.vy;
p.life -= dlife;
if ( p.life < 0 || p.x < left || p.x > right || p.y < top || p.y > bottom) {
plist.splice(n, 1);
} else {
g.lineStyle(0, p.color, p.life * lifeK);
g.moveTo(p.x, p.y);
g.lineTo(p.x - (p.x - p.px) *trailScale, p.y - (p.y - p.py) * trailScale);
p.px = p.x;
p.py = p.y;
}
if( n == 0) break;
n--;
p =plist[n];
c = map.getPixel(p.x * mapScale, p.y * mapScale );
p.vx += (((c >> 16) & ac) - bc) / bc*vFriction;
p.vy += (((c >> 8) & ac) - bc) / bc*vFriction;
p.x += p.vx;
p.y += p.vy;
p.life -= dlife;
if ( p.life < 0 || p.x < left || p.x > right || p.y < top || p.y > bottom) {
plist.splice(n, 1);
} else {
g.lineStyle(0, p.color, p.life * lifeK);
g.moveTo(p.x, p.y);
g.lineTo(p.x - (p.x - p.px) *trailScale, p.y - (p.y - p.py) * trailScale);
p.px = p.x;
p.py = p.y;
}
if( n == 0) break;
n--;
p =plist[n];
c = map.getPixel(p.x * mapScale, p.y * mapScale );
p.vx += (((c >> 16) & ac) - bc) / bc*vFriction;
p.vy += (((c >> 8) & ac) - bc) / bc*vFriction;
p.x += p.vx;
p.y += p.vy;
p.life -= dlife;
if ( p.life < 0 || p.x < left || p.x > right || p.y < top || p.y > bottom) {
plist.splice(n, 1);
} else {
g.lineStyle(0, p.color, p.life * lifeK);
g.moveTo(p.x, p.y);
g.lineTo(p.x - (p.x - p.px) *trailScale, p.y - (p.y - p.py) * trailScale);
p.px = p.x;
p.py = p.y;
}
if( n == 0) break;
n--;
p =plist[n];
c = map.getPixel(p.x * mapScale, p.y * mapScale );
p.vx += (((c >> 16) & ac) - bc) / bc*vFriction;
p.vy += (((c >> 8) & ac) - bc) / bc*vFriction;
p.x += p.vx;
p.y += p.vy;
p.life -= dlife;
if ( p.life < 0 || p.x < left || p.x > right || p.y < top || p.y > bottom) {
plist.splice(n, 1);
} else {
g.lineStyle(0, p.color, p.life * lifeK);
g.moveTo(p.x, p.y);
g.lineTo(p.x - (p.x - p.px) *trailScale, p.y - (p.y - p.py) * trailScale);
p.px = p.x;
p.py = p.y;
}
}
}
}
}
class Particle {
public var x:Number = 0;
public var y:Number = 0;
public var px:Number = 0;
public var py:Number = 0;
public var vx:Number = 0;
public var vy:Number = 0;
public var life:int= 900;
public var color:uint;
public function Particle(x:Number = 0, y:Number = 0, vx:Number = 0, vy:Number = 0, color:uint = 0xffffff) {
this.x = this.px = x;
this.y = this.py = y;
this.vx = vx;
this.vy = vy;
this.color = color;
}
}