ひよこちゃん 流される
//////////////////////////////////////////////////////////////////////////////
ひよこちゃん 流される
http://www.project-nya.jp/modules/weblog/details.php?blog_id=1108
//////////////////////////////////////////////////////////////////////////////
/**
* Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/ducC
*/
////////////////////////////////////////////////////////////////////////////////
// ひよこちゃん 流される
// http://www.project-nya.jp/modules/weblog/details.php?blog_id=1108
////////////////////////////////////////////////////////////////////////////////
package {
import flash.display.Sprite;
import flash.display.Shape;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.system.Security;
import flash.geom.Rectangle;
[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:PiyoLoader;
private static var piyoPath:String = "http://www.project-nya.jp/images/flash/piyo.swf";
private var piyo:MovieClip;
private static var radius:uint = 10;
private static var radian:Number = Math.PI/180;
private static var speed:uint = 5;
private var river:River;
private var scale:Number = 0.1;
private var perspective:Perspective;
private var cx:int = 232;
private var cy:int = 220;
private var obj:Object3D;
public function Main() {
//Wonderfl.disable_capture();
Wonderfl.capture_delay(6);
init();
}
private function init():void {
var sky:Sky = new Sky(465, 232);
addChild(sky);
var sun:Sun = new Sun(100);
addChild(sun);
sun.x = 10;
sun.y = 10;
var shine:PhotoLoader = new PhotoLoader();
addChild(shine);
shine.alpha = 0.5;
shine.load(basePath + sunshinePath);
var rect:Rectangle = new Rectangle(0, 0, 465, 233);
river = new River(rect);
addChild(river);
river.y = 232;
loader = new PiyoLoader();
loader.addEventListener(PiyoLoader.COMPLETE, complete, false, 0, true);
loader.load(piyoPath);
perspective = new Perspective();
perspective.init(cx, cy);
obj = new Object3D();
addChild(obj);
obj.px = 0;
obj.py = 0;
obj.pz = 2000;
}
private function complete(evt:Event):void {
loader.removeEventListener(PiyoLoader.COMPLETE, complete);
piyo = loader.content;
piyo.scaleX = piyo.scaleY = 2;
obj.addChild(piyo);
perspective.register(obj, 0);
perspective.setView(0, 200, 0);
perspective.transform();
piyo.shade.visible = false;
loader = null;
piyo.velocity = 0;
piyo.angle = 0;
addEventListener(Event.ENTER_FRAME, update, false, 0, true);
river.start();
}
private function update(evt:Event):void {
piyo.piyo.rotation = piyo.velocity;
piyo.velocity = radius*Math.sin(piyo.angle*radian);
piyo.angle += speed;
obj.pz -= 10;
if (obj.pz < -500) {
obj.pz = 2000;
}
perspective.transform();
}
}
}
import flash.display.Sprite;
class Perspective {
private var objList:Array;
private var fl:Number = 250;
private var vp:Object;
private var view:Object;
private var ax:Number = 0;
private var ay:Number = 0;
private var az:Number = 0;
private var brighted:Boolean = false;
private var front:Number = -100;
private var back:Number = 100;
private var depthList:Array;
public function Perspective() {
objList = new Array();
vp = {x: 0, y: 0};
view = {x: 0, y: 0, z: 0};
depthList = new Array();
}
public function init(x:int, y:int):void {
vp = {x: x, y: -y};
}
public function register(obj:Object3D, n:uint):void {
objList.push(obj);
obj.id = n;
}
public function transform():void {
for (var n:uint = 0; n < objList.length; n++) {
var obj:Object3D = objList[n];
setPerspective(obj);
}
manageDepths();
}
private function setPerspective(obj:Object3D):void {
if (obj.pz + view.z > - fl + 50) {
var scale:Number = fl/(fl + obj.pz + view.z);
obj.scale = scale;
obj.x = vp.x + (obj.px - view.x)*scale;
obj.y = - vp.y - (obj.py + view.y)*scale;
if (brighted) obj.brightness = -100*(obj.pz + view.z - front)/(back - front);
obj.visible = true;
} else {
obj.visible = false;
}
}
private function manageDepths():void {
objList.sortOn(["pz", "id"], [Array.DESCENDING | Array.NUMERIC, Array.DESCENDING | Array.NUMERIC]);
for (var n:uint = 0; n < objList.length; n++) {
var obj:Object3D = objList[n];
obj.stage.addChildAt(obj, n+1);
}
}
public function screen(f:Number, b:Number):void {
if (front == back) {
brighted = false;
} else {
brighted = true;
front = f;
back = b;
}
}
public function setView(x:Number, y:Number, z:Number):void {
view.x = x;
view.y = -y;
view.z = -z;
transform();
}
}
import flash.display.Sprite;
import flash.geom.ColorTransform;
class Object3D extends Sprite {
public var id:uint;
public var px:Number;
public var py:Number;
public var pz:Number;
public var angle:Number;
private var _scale:Number = 1;
private var colorTrans:ColorTransform;
private var _brightness:Number = 0;
public function Object3D() {
colorTrans = new ColorTransform(0, 0, 0, 1, 0, 0, 0, 0);
}
public function get scale():Number {
return _scale;
}
public function set scale(param:Number):void {
_scale = param;
scaleX = scaleY = _scale;
}
public function get brightness():Number {
return _brightness;
}
public function set brightness(param:Number):void {
_brightness = param;
var percent:uint = 100 - Math.abs(param);
var offset:uint = (param > 0) ? (255*param*0.01) : 0;
setColorTrans(percent, offset);
}
private function setColorTrans(percent:Number, offset:Number):void {
colorTrans.redMultiplier = percent*0.01;
colorTrans.greenMultiplier = percent*0.01;
colorTrans.blueMultiplier = percent*0.01;
colorTrans.redOffset = offset;
colorTrans.greenOffset = offset;
colorTrans.blueOffset = offset;
colorTrans.alphaMultiplier = alpha;
colorTrans.alphaOffset = 0;
transform.colorTransform = colorTrans;
}
}
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.HTTPStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.system.Security;
import flash.system.LoaderContext;
class PiyoLoader extends Sprite {
private var loader:Loader;
private var info:LoaderInfo;
public var content:MovieClip;
public static const IO_ERROR:String = IOErrorEvent.IO_ERROR;
public static const HTTP_STATUS:String = HTTPStatusEvent.HTTP_STATUS;
public static const SECURITY_ERROR:String = SecurityErrorEvent.SECURITY_ERROR;
public static const INIT:String = Event.INIT;
public static const COMPLETE:String = Event.COMPLETE;
public function PiyoLoader() {
Security.allowDomain("www.project-nya.jp");
loader = new Loader();
info = loader.contentLoaderInfo;
}
public function load(file:String):void {
info.addEventListener(IOErrorEvent.IO_ERROR, ioerror, false, 0, true);
info.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus, false, 0, true);
info.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror, false, 0, true);
info.addEventListener(Event.INIT, initialize, false, 0, true);
info.addEventListener(Event.COMPLETE, complete, false, 0, true);
try {
loader.load(new URLRequest(file), new LoaderContext(true));
} catch (err:Error) {
trace(err.message);
}
}
public function unload():void {
loader.unload();
}
private function ioerror(evt:IOErrorEvent):void {
loader.unload();
dispatchEvent(new Event(PiyoLoader.IO_ERROR));
}
private function httpstatus(evt:HTTPStatusEvent):void {
dispatchEvent(new Event(PiyoLoader.HTTP_STATUS));
}
private function securityerror(evt:SecurityErrorEvent):void {
dispatchEvent(new Event(PiyoLoader.SECURITY_ERROR));
}
private function initialize(evt:Event):void {
content = MovieClip(info.content);
dispatchEvent(new Event(PiyoLoader.INIT));
}
private function complete(evt:Event):void {
info.removeEventListener(IOErrorEvent.IO_ERROR, ioerror);
info.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus);
info.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror);
info.removeEventListener(Event.INIT, initialize);
info.removeEventListener(Event.COMPLETE, complete);
//addChild(loader);
dispatchEvent(new Event(PiyoLoader.COMPLETE));
}
}
import flash.display.Shape;
import flash.geom.Matrix;
import flash.display.GradientType;
class Sky extends Shape {
private static var _width:uint;
private static var _height:uint;
private static var color1:uint = 0x3F68AB;
private static var color2:uint = 0x77B2EE;
public function Sky(w:uint, h:uint) {
_width = w;
_height = h;
draw();
}
private function draw():void {
var colors:Array = [color1, color2];
var alphas:Array = [1, 1];
var ratios:Array = [0, 255];
var matrix:Matrix = new Matrix();
matrix.createGradientBox(_width, _height, 0.5*Math.PI, 0, 0);
graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, matrix);
graphics.drawRect(0, 0, _width, _height);
graphics.endFill();
}
}
import flash.display.Shape;
import flash.geom.Matrix;
import flash.display.GradientType;
class Sun extends Shape {
private static var radius:uint;
private static var color:uint = 0xFFFFFF;
public function Sun(r:uint) {
radius = r;
draw();
}
private function draw():void {
var colors:Array = [color, color, color];
var alphas:Array = [1, 0.3, 0];
var ratios:Array = [25, 102, 231];
var matrix:Matrix = new Matrix();
matrix.createGradientBox(radius*2, radius*2, 0, -radius, -radius);
graphics.beginGradientFill(GradientType.RADIAL, colors, alphas, ratios, matrix);
graphics.drawCircle(0, 0, radius);
graphics.endFill();
}
}
import flash.display.Sprite;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.HTTPStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.display.Bitmap;
import flash.system.LoaderContext;
class PhotoLoader extends Sprite {
private var loader:Loader;
private var info:LoaderInfo;
public var content:Bitmap;
private var smoothing:Boolean;
public static const IO_ERROR:String = IOErrorEvent.IO_ERROR;
public static const HTTP_STATUS:String = HTTPStatusEvent.HTTP_STATUS;
public static const SECURITY_ERROR:String = SecurityErrorEvent.SECURITY_ERROR;
public static const INIT:String = Event.INIT;
public static const COMPLETE:String = Event.COMPLETE;
public function PhotoLoader() {
loader = new Loader();
info = loader.contentLoaderInfo;
}
public function load(file:String, s:Boolean = false):void {
smoothing = s;
info.addEventListener(IOErrorEvent.IO_ERROR, ioerror, false, 0, true);
info.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus, false, 0, true);
info.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror, false, 0, true);
info.addEventListener(Event.INIT, initialize, false, 0, true);
info.addEventListener(Event.COMPLETE, complete, false, 0, true);
try {
loader.load(new URLRequest(file), new LoaderContext(true));
} catch (err:Error) {
trace(err.message);
}
}
public function unload():void {
loader.unload();
}
private function ioerror(evt:IOErrorEvent):void {
loader.unload();
dispatchEvent(new Event(PhotoLoader.IO_ERROR));
}
private function httpstatus(evt:HTTPStatusEvent):void {
dispatchEvent(new Event(PhotoLoader.HTTP_STATUS));
}
private function securityerror(evt:SecurityErrorEvent):void {
dispatchEvent(new Event(PhotoLoader.SECURITY_ERROR));
}
private function initialize(evt:Event):void {
content = Bitmap(info.content);
if (smoothing) content.smoothing = true;
dispatchEvent(new Event(PhotoLoader.INIT));
}
private function complete(evt:Event):void {
info.removeEventListener(IOErrorEvent.IO_ERROR, ioerror);
info.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpstatus);
info.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityerror);
info.removeEventListener(Event.INIT, initialize);
info.removeEventListener(Event.COMPLETE, complete);
addChild(loader);
dispatchEvent(new Event(PhotoLoader.COMPLETE));
}
}
import flash.display.Sprite;
import flash.display.Shape;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.geom.Matrix;
import flash.display.GradientType;
import flash.geom.ColorTransform;
class River extends Sprite {
private var rect:Rectangle;
private var light:Shape;
private var river:BitmapData;
private var perspective:PerspectiveMap;
private var bitmapData:BitmapData;
private static var bColor:uint = 0xFFFFFF;
private var seed:uint;
private var offsets:Array;
private var blue:ColorTransform;
private static var color:Object = {r: 0x66, g: 0x99, b: 0xCC};
public function River(r:Rectangle) {
rect = r;
init();
}
public function start():void {
addEventListener(Event.ENTER_FRAME, drift, false, 0, true);
}
public function stop():void {
removeEventListener(Event.ENTER_FRAME, drift);
}
private function init():void {
river = new BitmapData(rect.width, rect.height, false);
perspective = new PerspectiveMap(rect, 2);
bitmapData = river.clone();
addChild(new Bitmap(bitmapData));
overlay();
seed = Math.floor(Math.random()*1000);
offsets = [new Point(), new Point()];
blue = new ColorTransform(0.5, 0.5, 0.5, 1, color.r, color.g, color.b, 0);
}
private function drift(evt:Event):void {
offsets[0].y -= 2;
offsets[1].y -= 1;
river.lock();
river.perlinNoise(20, 10, 2, seed, false, true, 0, true, offsets);
perspective.transform(river);
river.unlock();
bitmapData.lock();
bitmapData.draw(river, null, blue);
bitmapData.unlock();
}
private function overlay():void {
light = new Shape();
addChild(light);
var colors:Array = [bColor, bColor];
var alphas:Array = [0.8, 0];
var ratios:Array = [0, 255];
var matrix:Matrix = new Matrix();
matrix.createGradientBox(rect.width, rect.height, 0.5*Math.PI, 0, 0);
light.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, matrix);
light.graphics.drawRect(0, 0, rect.width, rect.height);
light.graphics.endFill();
}
}
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.filters.DisplacementMapFilter;
import flash.filters.DisplacementMapFilterMode;
class PerspectiveMap {
private var rect:Rectangle;
private var scale:Number;
private var perspective:DisplacementMapFilter;
public function PerspectiveMap(r:Rectangle, s:Number) {
rect = r;
scale = s;
if (scale > 1) init();
}
private function init():void {
var map:BitmapData = new BitmapData(rect.width, rect.height, false);
var n:Number = 1/(scale - 1);
var t:Number = n + 1;
var mx:Number = 0;
var my:Number = 0;
for (var py:uint = 0; py < rect.height; py++) {
var pr:Number = (py + 1)/rect.height;
var pf:Number = pr/(pr + n);
var pdx:Number = pf/2;
var pdy:Number = pf*t - pr;
mx = Math.max(mx, pdx);
my = Math.max(my, pdy);
}
var xscale:Number = 127/256/mx;
var yscale:Number = 127/256/ my;
for (var y:uint = 0; y < rect.height; y++) {
var r:Number = (y + 1)/rect.height;
var f:Number = r/(r + n);
var dy:Number = f*t - r;
var cy:uint = Math.round(256*dy*yscale + 128) << 8;
for (var x:uint = 0; x < rect.width; x++) {
var dx:Number = (0.5 - x/rect.width)*f;
var cx:uint = Math.round(256*dx*xscale + 128) | cy;
map.setPixel(x, y, cx);
}
}
perspective = new DisplacementMapFilter(map, null, 4, 2, rect.width/xscale, rect.height/yscale, DisplacementMapFilterMode.IGNORE);
}
public function transform(bd:BitmapData):void {
if (scale == 1) return;
bd.applyFilter(bd, rect, new Point(), perspective);
}
}