PerlinNoise (3)
//////////////////////////////////////////////////////////////////////////////
PerlinNoise (3)
BitmapDataでノイズ生成 (2)
http://www.project-nya.jp/modules/weblog/details.php?blog_id=478
[AS3.0] PerlinNoiseクラスに挑戦!
http://www.project-nya.jp/modules/weblog/details.php?blog_id=1114
//////////////////////////////////////////////////////////////////////////////
♥0 |
Line 132 |
Modified 2010-07-10 19:19:46 |
MIT License
archived:2017-03-30 01:34:40
ActionScript3 source code
/**
* Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/eXKe
*/
////////////////////////////////////////////////////////////////////////////////
// PerlinNoise (3)
//
// BitmapDataでノイズ生成 (2)
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=478
// [AS3.0] PerlinNoiseクラスに挑戦!
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1114
////////////////////////////////////////////////////////////////////////////////
package {
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.display.StageAlign;
import flash.display.Bitmap;
import flash.geom.Rectangle;
import flash.events.Event;
import flash.geom.Point;
import flash.filters.BlurFilter;
[SWF(backgroundColor="#000000", width="465", height="465", frameRate="30")]
public class Main extends Sprite {
private var noise:PerlinNoise;
private var offsets:Array = new Array();
private var label:Label;
public function Main() {
//Wonderfl.capture_delay(1);
init();
}
private function init():void {
graphics.beginFill(0x000000);
graphics.drawRect(0, 0, 465, 465);
graphics.endFill();
var rect:Rectangle = new Rectangle(0, 0, 445, 445);
//ノイズ生成
noise = new PerlinNoise(rect, 80, 80, 3, false, 1 | 2 | 4, 1);
for (var n:uint = 0; n < 3; n++) {
var point:Point = new Point();
offsets.push(point);
}
var bitmap:Bitmap = new Bitmap(noise);
addChild(bitmap);
bitmap.x = 10;
bitmap.y = 10;
//ラベル生成
label = new Label(100, 60);
addChild(label);
label.text = "PerlinNoise";
label.textColor = 0xFFFFFF;
label.x = 232;
label.y = 200;
addEventListener(Event.ENTER_FRAME, update, false, 0, true);
}
private function update(evt:Event):void {
//ノイズ生成
for (var n:uint = 0; n < 3; n++) {
var point:Point = offsets[n];
point.x += Math.random()*16 - 8;
point.y += Math.random()*16 - 8;
}
noise.create(1, offsets);
//ラベル動き
var px:Number = Math.random()*32 - 16;
var py:Number = Math.random()*32 - 16;
label.x = 232 + px;
label.y = 200 + py;
label.filters = [new BlurFilter(uint(px) >> 1, uint(py) >> 1, 3)];
}
}
}
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Point;
class PerlinNoise extends BitmapData {
private var bx:uint;
private var by:uint;
private var octaves:uint;
private var seed:uint;
private var stitch:Boolean = true;
private var fractalNoise:Boolean = true;
private var channel:uint = 0;
private var grayScale:Boolean = true;
private static var point:Point = new Point();
private var offsets:Array = new Array();
public function PerlinNoise(rect:Rectangle, x:uint, y:uint, o:uint = 1, g:Boolean = true, c:uint = 0, s:uint = 1, ox: Number = 0, oy:Number = 0, st:Boolean = false, f:Boolean = true) {
super(rect.width, rect.height, false, 0xFF000000);
bx = x;
by = y;
octaves = o;
grayScale = g;
channel = c;
if (grayScale) channel = 0;
for (var n:uint = 0; n < octaves; n++) {
var p:Point = new Point(ox, oy);
offsets.push(point);
}
stitch = st;
fractalNoise = f;
create(s, offsets);
}
public function create(s:uint, o:Array = null):void {
seed = s;
offsets = o;
if (offsets == null) offsets = [point];
lock();
perlinNoise(bx, by, octaves, seed, stitch, fractalNoise, channel, grayScale, offsets);
draw(this);
unlock();
}
}
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.TextFieldAutoSize;
import flash.text.AntiAliasType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
class Label extends Sprite {
private var txt:TextField;
private static var fontType:String = "Arial";
private var txtWidth:uint;
private var fontSize:uint;
public function Label(w:uint, s:uint) {
txtWidth = w;
fontSize = s;
draw();
}
private function draw():void {
txt = new TextField();
txt.x = -uint(txtWidth/2);
txt.width = txtWidth;
addChild(txt);
txt.autoSize = TextFieldAutoSize.CENTER;
txt.type = TextFieldType.DYNAMIC;
txt.selectable = false;
//txt.embedFonts = true;
//txt.antiAliasType = AntiAliasType.ADVANCED;
var tf:TextFormat = new TextFormat();
tf.font = fontType;
tf.size = fontSize;
tf.align = TextFormatAlign.CENTER;
txt.defaultTextFormat = tf;
}
public function set text(param:String):void {
txt.text = param;
}
public function set textColor(param:uint):void {
txt.textColor = param;
}
}