fluid simulation using texture vector processing for Seguchi san
a task for "Seguchi san" find tag "icu-cg2010". http://shirai.la/
// forked from o_ob's fluid simulation using texture vector processing
// a task for "Seguchi san" find tag "icu-cg2010". http://shirai.la/
// forked from ryota_'s forked from: forked from: BitmapDataSample11
// forked from hacker_xaotp5zq's forked from: BitmapDataSample11
// forked from nutsu's BitmapDataSample11
// forked from nulldesign's Liquid10000
package {
import flash.display.Sprite;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.ColorTransform;
import flash.display.BitmapDataChannel;
import flash.filters.BlurFilter;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.text.TextFieldAutoSize;
import flash.text.TextField;
import flash.text.TextFormat;
[SWF(width=1920,height=1080,backgroundColor=0,frameRate=60)]
import flash.accessibility.Accessibility;
public class BitmapDataSample11 extends Sprite {
private var bmpdata:BitmapData;
private var colortrans:ColorTransform;
private var filter:BlurFilter;
private var vectormap:BitmapData;
private var particles:Array;
private var particle_number:uint = 45000;
private var size:Number = 465;
private var fx:int;
private var textBmd:BitmapData;
public function BitmapDataSample11() {
//BitmapDataを作成して表示リストに追加
bmpdata = new BitmapData( size, size, false, 0 );
//addChild( new Bitmap( bmpdata ) );
//エフェクト初期化
colortrans = new ColorTransform( 0.95, 0.99, 0.99 );
filter = new BlurFilter( 2, 2, 1 );
//ベクトルマップとパーティクルの初期化
vectormap = new BitmapData( size, size, false, 0 );
addChild(new Bitmap(vectormap));
init();
addChild( new Bitmap( bmpdata ) );
//イベント
addEventListener( Event.ENTER_FRAME, enterframe );
stage.addEventListener( MouseEvent.CLICK, reset );
}
private function init( e:MouseEvent = null ):void {
//ベクトルマップの初期化
var randomSeed:int = Math.random()*0xFFFFFFFF;
var colors:uint = BitmapDataChannel.RED | BitmapDataChannel.GREEN;
vectormap.perlinNoise( size/2, size/2, 4, randomSeed, false, true, colors );
//パーティクルの初期化
particles = new Array(particle_number);
for (var i:int = 0; i < particle_number; i++) {
particles[i] = new Particle( Math.random()*size, Math.random()*size );
}
}
private function reset( e:MouseEvent = null ):void {
//random effects
fx = Math.random()*3;
//ベクトルマップの初期化
var randomSeed:int = Math.random()*0xFFFFFFFF;
var colors:uint = BitmapDataChannel.RED | BitmapDataChannel.GREEN;
switch (fx) {
case 1:
vectormap.perlinNoise( size/2, size/2, 4, randomSeed, false, true, colors );
vectormap.threshold(
vectormap,
new Rectangle(0,0,size,size),
new Point(0,0),
">",
0x00800000,0x80FF0000,0x00FF0000,
true
);
break;
//texteffect
case 2:
vectormap.threshold(
createText(),
new Rectangle(0,0,size,size),
new Point(0,0),
">",
0x00000000,0xCCCCCCCC,0x00000000,
true
);
break;
default:
vectormap.perlinNoise( size/2, size/2, 4, randomSeed, false, true, colors );
}
//パーティクルの初期化
// particles = new Array(particle_number);
// for (var i:int = 0; i < particle_number; i++) {
// particles[i] = new Particle( Math.random()*size, Math.random()*size );
// }
}
private function createText():BitmapData{
var tfld:TextField = new TextField();
tfld.autoSize = TextFieldAutoSize.LEFT;
tfld.defaultTextFormat = new TextFormat("Verdana", 60, 0xFFFFFF, true);
tfld.text = "Seguchi";
tfld.x = size/4;
tfld.y = size/2;
tfld.backgroundColor = 0xffffff;
// addChild(tfld);
// tfldをBitmapDataに変換するよ
textBmd = new BitmapData(size, size, false, 0x0);
textBmd.applyFilter(textBmd, textBmd.rect, new Point(0, 0), new BlurFilter(50,50));
textBmd.draw(tfld, tfld.transform.matrix);
/* For Debug*/
// addChild(tfld);
return textBmd;
}
private function enterframe( e:Event ):void {
//エフェクトの適用
bmpdata.applyFilter( bmpdata, bmpdata.rect, bmpdata.rect.topLeft, filter );
bmpdata.colorTransform( bmpdata.rect, colortrans );
//パーティクルの描画
bmpdata.lock();
for (var i:int = 0; i <particle_number; i++) {
var p:Particle = particles[i];
//ベクトルマップのPixel値から加速度を算出
var col:uint = vectormap.getPixel( p.x, p.y );
p.ax += ( (col >> 16 & 0xff) - 128 )*0.0005;
p.ay += ( (col >> 8 & 0xff) - 128 )*0.0005;
//加速度から速度と位置を算出
p.x += p.vx += p.ax;
p.y += p.vy += p.ay;
if ( p.x > size ) { p.x -= size; }
else if ( p.x < 0 ){ p.x += size; }
if ( p.y > size ) { p.y -= size; }
else if ( p.y < 0 ){ p.y += size; }
//Pixelへ描画
//fxによって処理を変える(o_ob)
switch (fx) {
case 1:
bmpdata.setPixel( p.x, p.y, 0xffffff );
break;
case 2:
bmpdata.setPixel( p.x, p.y, col );
break;
default:
bmpdata.setPixel( p.x, p.y, col+0x0000ff );
}
//加速度と速度の減衰
p.ax *= 0.96; p.ay *= 0.96;
p.vx *= 0.92; p.vy *= 0.92;
}
bmpdata.unlock();
}
}
}
//パーティクルクラス
class Particle {
//位置
public var x:Number;
public var y:Number;
//加速度
public var ax:Number = 0;
public var ay:Number = 0;
//速度
public var vx:Number = 0;
public var vy:Number = 0;
function Particle( px:Number, py:Number ) {
x = px;
y = py;
}
}