forked from: Sea and Clouds
/**
* Copyright kataribe ( http://wonderfl.net/user/kataribe )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/zFcV
*/
// forked from yonatan's Sea and Clouds
// forked from yonatan's Clouds and snow (cloud painter from tencho's Sea of Clouds - http://wonderfl.net/c/h9dR)
// forked from cjcat2266's Super Express Desert Sunset + Rain (Stardust ver.)
// forked from yonatan's Super Express Desert Sunset
// forked from k0rin's Super Express
package {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import net.hires.debug.Stats;
[SWF(width = "465", height = "465", frameRate = "40")]
public class Main extends Sprite
{
public static const WIDTH:Number = 465;
public static const HEIGHT:Number = 465;
private var renderedSky:BitmapData = new BitmapData(WIDTH, (HEIGHT+1)/2);
private var sea:Sea;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
start();
}
public function start():void
{
var matrix:Matrix = new Matrix();
matrix.createGradientBox(WIDTH, HEIGHT, Math.PI / 2);
graphics.beginGradientFill(GradientType.LINEAR, [0x35415B, 0xA08E74], null, [0, 128], matrix);
graphics.drawRect(0, 0, WIDTH, HEIGHT);
graphics.endFill();
graphics.beginFill(0xfafaff);
graphics.drawCircle(200, 150, 16);
graphics.endFill();
sea = new Sea(this);
sea.y = HEIGHT/2;
addChild(sea);
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
addChild(new Stats);
}
private function enterFrameHandler(e:Event):void
{
renderedSky.fillRect(renderedSky.rect, 0);
renderedSky.draw(this);
sea.update();
}
}
}
import flash.display.*;
import flash.filters.*;
import flash.geom.*;
import flash.utils.*;
class Sea extends Sprite{
private const nearScale:Number = 0.25;
private var vertices:Vector.<Number> = new Vector.<Number>;
private var uvt:Vector.<Number> = new Vector.<Number>;
private var indices:Vector.<int> = new Vector.<int>;
private const octaves:int = 3;
private var offsets:Array = [];
private var output:BitmapData = new BitmapData(Main.WIDTH, (Main.HEIGHT+1)/2, false);
private var noise:BitmapData = new BitmapData(Main.WIDTH/2, (Main.HEIGHT+3)/4, true);
private var upsideDown:BitmapData = new BitmapData(Main.WIDTH, (Main.HEIGHT+1)/2, true);
private var seaShape:Shape = new Shape;
private var seaBmd:BitmapData = new BitmapData(Main.WIDTH, (Main.HEIGHT+1)/2, true);
private var dispFilter:DisplacementMapFilter = new DisplacementMapFilter(seaBmd, null, 0, 8, 0, Main.HEIGHT/-2, "clamp");
//dispFilter = new DisplacementMapFilter(seaBmd, null, 0, 8, 0, Main.HEIGHT/2, "clamp");
private var backdrop:BitmapData;
private var cachedOctaves:Array = []; // precalculated perlin noise
private var sprite:Sprite;
public function Sea(backdrop:Sprite) {
vertices.push(0, 0, Main.WIDTH-1, 0, 0, Main.HEIGHT/2-1, Main.WIDTH-1, Main.HEIGHT/2-1);
uvt.push(
0, 0, nearScale,
1, 0, nearScale,
(1-nearScale)/2, 1, 1,
(1+nearScale)/2, 1, 1);
indices.push(0, 1, 2, 2, 1, 3);
sprite = backdrop;
this.backdrop = new BitmapData(Main.WIDTH, (Main.HEIGHT+1)/2);
this.backdrop.draw(backdrop);
initNoise();
addChild(new Bitmap(output));
}
private function initNoise():void {
for(var i:int = 0; i < octaves; i++) {
var bmd:BitmapData = new BitmapData(Main.WIDTH/2, (Main.HEIGHT+3)/4, true, 0);
var pr:Number = 1/Math.pow(2, i);
bmd.perlinNoise(24*pr, 2*pr, 1, 0, true, true, 0xF, false);
pr /= 2;
bmd.colorTransform(bmd.rect, new ColorTransform(pr, pr, pr, pr));
cachedOctaves.push(bmd);
}
}
private function fastPerlin(dst:BitmapData, xOffsets:Array):void {
dst.fillRect(dst.rect, 0);
for(var i:int = 0; i < octaves; i++) {
var offset:int = xOffsets[i];
if(offset >= 0) {
offset %= dst.width;
} else {
offset = dst.width - (-offset % dst.width);
}
var mtx:Matrix = new Matrix;
mtx.tx = offset;
dst.draw(cachedOctaves[i], mtx, null, "add");
mtx.tx = offset - dst.width;
dst.draw(cachedOctaves[i], mtx, null, "add");
}
}
public function update():void {
backdrop.draw(sprite);
for(var i:int = 0; i < octaves; i++) offsets[i] = ((i&1)*2-1) * (1+i)*getTimer()/150;
fastPerlin(noise, offsets);
seaShape.graphics.clear();
seaShape.graphics.beginBitmapFill(noise, null, false, true);
seaShape.graphics.drawTriangles(vertices, indices, uvt);
seaShape.graphics.endFill();
seaBmd.fillRect(seaBmd.rect, 0);
seaBmd.draw(seaShape);
// reflection
upsideDown.fillRect(upsideDown.rect, 0);
upsideDown.draw(backdrop, new Matrix(1, 0, 0, -1, 0, Main.HEIGHT/2));
output.applyFilter(upsideDown, upsideDown.rect, upsideDown.rect.topLeft, dispFilter);
}
}