ドロンとひよこ (3)
////////////////////////////////////////////////////////////////////////////////
// ドロンとひよこ (3)
//
// abakane's Smoke 煙の表現
// [http://wonderfl.net/c/zuO7]
//
// [AS3.0] PerlinNoiseクラスに挑戦!
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1114
////////////////////////////////////////////////////////////////////////////////
/**
* Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/iPiE
*/
////////////////////////////////////////////////////////////////////////////////
// ドロンとひよこ (3)
//
// abakane's Smoke 煙の表現
// [http://wonderfl.net/c/zuO7]
//
// [AS3.0] PerlinNoiseクラスに挑戦!
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1114
////////////////////////////////////////////////////////////////////////////////
package {
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.display.Loader;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.system.Security;
import flash.system.LoaderContext;
import flash.display.Shape;
import flash.geom.Matrix;
import flash.geom.Rectangle;
import flash.display.GradientType;
import flash.utils.Timer;
import flash.events.TimerEvent;
[SWF(backgroundColor="#FFFFFF", width="465", height="465", frameRate="30")]
public class Main extends Sprite {
private static var basePath:String = "http://assets.wonderfl.net/images/related_images/";
private static var sunshinePath:String = "9/9b/9bbe/9bbec77d53bddc5e7a5f2c00abbade6bd641c549";
private var _loader:Loader;
private static var pandaPath:String = "http://www.project-nya.jp/images/flash/panda2d.swf";
private var Panda:Class;
private var panda:MovieClip;
private var loader:Loader;
private static var piyoPath:String = "http://www.project-nya.jp/images/flash/piyo2d.swf";
private var Piyo:Class;
private var piyo:MovieClip;
private var loaded:uint = 0;
private var fogs:Fogs;
public function Main() {
//Wonderfl.capture_delay(4);
init();
}
private function init():void {
draw();
Security.allowDomain("www.project-nya.jp");
Security.loadPolicyFile("http://www.project-nya.jp/crossdomain.xml");
_loader = new Loader();
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, _initialize, false, 0, true);
_loader.load(new URLRequest(pandaPath), new LoaderContext(true));
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, initialize, false, 0, true);
loader.load(new URLRequest(piyoPath), new LoaderContext(true));
//
var rect:Rectangle = new Rectangle(0, 0, 400, 400);
fogs = new Fogs(rect);
fogs.x = 32;
fogs.y = 32;
}
private function _initialize(evt:Event):void {
_loader.removeEventListener(Event.COMPLETE, _initialize);
var content:MovieClip = MovieClip(evt.target.content);
//Pandaクラス
Panda = MovieClip(content.panda).constructor;
loaded ++;
setup();
_loader = null;
}
private function initialize(evt:Event):void {
loader.removeEventListener(Event.COMPLETE, initialize);
var content:MovieClip = MovieClip(evt.target.content);
//Piyoクラス
Piyo = MovieClip(content.piyo).constructor;
loaded ++;
setup();
loader = null;
}
private function setup():void {
if (loaded < 2) return;
//Pandaインスタンス
panda = new Panda();
addChild(panda);
panda.x = 232;
panda.y = 380;
panda.scale = 4;
panda.visible = true;
//Piyoインスタンス
piyo = new Piyo();
addChild(piyo);
piyo.x = 232;
piyo.y = 380;
piyo.scale = 4;
piyo.visible = false;
//
addChild(fogs);
var timer:Timer = new Timer(2500, 1);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, start, false, 0, true);
timer.start();
}
private function start(evt:TimerEvent):void {
evt.target.removeEventListener(TimerEvent.TIMER_COMPLETE, start);
//
fogs.start();
//
var timer:Timer = new Timer(1000, 1);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, next, false, 0, true);
timer.start();
}
private function next(evt:TimerEvent):void {
evt.target.removeEventListener(TimerEvent.TIMER_COMPLETE, next);
//
panda.visible = false;
piyo.visible = true;
//
var timer:Timer = new Timer(1000, 1);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, finish, false, 0, true);
timer.start();
}
private function finish(evt:TimerEvent):void {
evt.target.removeEventListener(TimerEvent.TIMER_COMPLETE, finish);
//
fogs.stop();
}
/////////////////////////////////////////////
//背景
/////////////////////////////////////////////
private function draw():void {
drawSky();
drawGround();
drawSun();
}
private function drawSky():void {
var matrix:Matrix = new Matrix();
matrix.createGradientBox(465, 350, 0.5*Math.PI, 0, 0);
graphics.beginGradientFill(GradientType.LINEAR, [0x3F68AB, 0x77B2EE], [1, 1], [0, 255], matrix);
graphics.drawRect(0, 0, 465, 350);
graphics.endFill();
}
private function drawGround():void {
var matrix:Matrix = new Matrix();
matrix.createGradientBox(465, 115, 0.5*Math.PI, 0, 350);
graphics.beginGradientFill(GradientType.LINEAR, [0x99CC33, 0x7EB133], [1, 1], [0, 255], matrix);
graphics.drawRect(0, 350, 465, 115);
graphics.endFill();
}
private function drawSun():void {
var matrix:Matrix = new Matrix();
matrix.createGradientBox(200, 200, 0, -90, -90);
graphics.beginGradientFill(GradientType.RADIAL, [0xFFFFFF, 0xFFFFFF, 0xFFFFFF], [1, 0.3, 0], [25, 102, 231], matrix);
graphics.drawCircle(10, 10, 100);
graphics.endFill();
var shine:Loader = new Loader();
addChild(shine);
shine.alpha = 0.5;
shine.load(new URLRequest(basePath + sunshinePath), new LoaderContext(true));
}
}
}
//////////////////////////////////////////////////
// Fogsクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.events.Event;
import flash.display.BlendMode;
import flash.geom.Matrix;
import flash.geom.ColorTransform;
import flash.utils.Timer;
import flash.events.TimerEvent;
import org.libspark.betweenas3.BetweenAS3;
import org.libspark.betweenas3.tweens.ITween;
import org.libspark.betweenas3.events.TweenEvent;
import org.libspark.betweenas3.easing.*;
class Fogs extends Sprite {
private var rect:Rectangle;
private var area:Rectangle;
private var bitmapData:BitmapData;
private var bitmap:Bitmap;
private var particles:Array;
private static var radian:Number = Math.PI/180;
private var timer:Timer;
private static var interval:uint = 20;
private static var point:Point = new Point();
public function Fogs(r:Rectangle) {
rect = r;
var bw:uint = rect.width;
var bh:uint = rect.height;
area = new Rectangle(bw*0.3, bh*0.75, bw*0.4, bh*0.25 - 32);
init();
blendMode = BlendMode.SCREEN;
}
private function init():void {
bitmapData = new BitmapData(rect.width, rect.height, true, 0x00000000);
bitmap = new Bitmap(bitmapData);
addChild(bitmap);
particles = new Array();
}
public function start():void {
timer = new Timer(interval);
timer.addEventListener(TimerEvent.TIMER, create, false, 0, true);
timer.start();
addEventListener(Event.ENTER_FRAME, update, false, 0, true);
}
public function stop():void {
if (timer) {
timer.stop();
timer = null;
}
}
private function create(evt:TimerEvent):void {
for (var n:uint = 0; n < 8; n++) {
var fog:Fog = new Fog();
fog.vx = 0;
fog.vy = -10;
var px:Number = area.x + Math.random()*area.width;
var py:Number = area.y + Math.random()*area.height;
var r:Number = Math.atan2(fog.vy, fog.vx);
r += 20*(0.5 - Math.random())*radian;
var tx:Number = Math.cos(r)*(Math.random() + 0.5)*area.width*1.5;
var ty:Number = Math.sin(r)*(Math.random() + 0.5)*area.height*2;
var nx:Number = tx*0.3*Math.random();
var ny:Number = ty*0.3*Math.random();
var itween:ITween = BetweenAS3.tween(fog,
{x: px + tx, y: py + ty, scaleX: 2.5, scaleY: 2.5, alpha: 0, _blurFilter: {blurX: 16, blurY: 16}},
{x: px + nx, y: py + ny, scaleX: 0.8, scaleY: 0.8, alpha: 0.8, _blurFilter: {blurX: 4, blurY: 4, quality: 1}},
1, Cubic.easeOut
);
itween.addEventListener(TweenEvent.COMPLETE, complete, false, 0, true);
itween.play();
particles.push(fog);
}
}
public function update(evt:Event = null):void {
bitmapData.lock();
bitmapData.fillRect(rect, 0x00000000);
for (var n:uint = 0; n < particles.length; n++) {
var fog:Fog = particles[n];
if (fog) {
var matrix:Matrix = new Matrix();
matrix.scale(fog.scaleX, fog.scaleY);
matrix.translate(fog.x, fog.y);
var colorTrans:ColorTransform = new ColorTransform();
colorTrans.alphaMultiplier = fog.alpha;
bitmapData.draw(fog, matrix, colorTrans);
}
}
bitmapData.unlock();
}
private function complete(evt:TweenEvent):void {
evt.target.removeEventListener(TweenEvent.COMPLETE, complete);
//
var fog:Fog = Fog(evt.target.target);
particles.shift();
fog = null;
if (particles.length < 1) {
update();
removeEventListener(Event.ENTER_FRAME, update);
}
}
}
//////////////////////////////////////////////////
// Fogクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.Matrix;
import flash.geom.Rectangle;
import flash.display.GradientType;
import flash.display.SpreadMethod;
import flash.display.InterpolationMethod;
import flash.display.BlendMode;
class Fog extends Sprite {
private static var radius:uint = 40;
private var noise:PerlinNoise;
private static var multiplier:Object = {r: 1, g: 1, b: 1};
private static var offset:Object = {r: 0xFF, g: 0xFF, b: 0xFF};
private var bitmapData:BitmapData;
private var bitmap:Bitmap;
public var px:Number = 0;
public var py:Number = 0;
public var vx:Number = 0;
public var vy:Number = 0;
public function Fog() {
draw();
}
private function draw():void {
var rect:Rectangle = new Rectangle(0, 0, radius*2, radius*2);
noise = new PerlinNoise(rect, radius*0.5, 2, 0);
bitmap = new Bitmap(noise);
bitmap.x = bitmap.y = - radius;
addChild(bitmap);
noise.colorize(multiplier, offset);
//
var circle:Sprite = new Sprite();
var matrix:Matrix = new Matrix();
matrix.createGradientBox(radius*2, radius*2, 0, -radius, -radius);
var colors:Array = [0xFFFFFF, 0xFFFFFF, 0xFFFFFF];
var alphas:Array = [1, 0.7, 0];
var ratios:Array = [0, 161, 255];
circle.graphics.beginGradientFill(GradientType.RADIAL, colors, alphas, ratios, matrix, SpreadMethod.PAD, InterpolationMethod.RGB, 0);
circle.graphics.drawCircle(0, 0, radius);
circle.graphics.endFill();
addChild(circle);
//
circle.cacheAsBitmap = true;
bitmap.cacheAsBitmap = true;
bitmap.mask = circle;
}
}
//////////////////////////////////////////////////
// PerlinNoiseクラス
//////////////////////////////////////////////////
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.geom.ColorTransform;
class PerlinNoise extends BitmapData {
private var source:BitmapData;
private var base:uint;
private var octaves:uint;
private var seed:uint;
private static var point:Point = new Point();
private var offsets:Array = [point, point];
private var color:ColorTransform;
private static var multiplier:Object = {r: 1, g: 1, b: 1, a: 1};
private static var offset:Object = {r: 0x00, g: 0x00, b: 0x00, a: 0x00};
public function PerlinNoise(rect:Rectangle, b:uint = 20, o:uint = 2, s:uint = 1) {
super(rect.width, rect.height, true, 0x00FFFFFF);
source = new BitmapData(rect.width, rect.height, true, 0x00FFFFFF);
create(b, o, s);
}
public function create(b:uint, o:uint, s:uint):void {
base = b;
octaves = o;
seed = s;
if (seed == 0) seed = Math.floor(Math.random()*1000);
lock();
source.perlinNoise(base, base, octaves, seed, false, true, 0x00FFFFFF, true, offsets);
draw(source);
unlock();
}
public function colorize(m:Object, o:Object):void {
multiplier = m;
offset = o;
color = new ColorTransform(multiplier.r, multiplier.g, multiplier.b, 1, offset.r, offset.g, offset.b, 0);
lock();
draw(source, null, color);
unlock();
}
}