Make Wind Grid
Reference - https://kopakc.wordpress.com/2013/10/08/procedural-cirrus-clouds-experiments/
http://wonderfl.net/c/2PFV
♥0 |
Line 160 |
Modified 2015-12-25 11:37:41 |
MIT License
archived:2017-03-20 00:57:38
ActionScript3 source code
/**
* Copyright greentec ( http://wonderfl.net/user/greentec )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/2kr1
*/
package {
import com.bit101.components.Label;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.BlendMode;
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.PixelSnapping;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.BlurFilter;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Point;
public class FlashTest extends Sprite {
public var gridSize:int = 15;
public var windScaleMax:int = 1;
public var windGrid:Vector.<Vector.<Number>> = new Vector.<Vector.<Number>>(gridSize);
public var windScale:Vector.<Vector.<Number>> = new Vector.<Vector.<Number>>(gridSize);
public var tempWindGrid:Vector.<Vector.<Number>> = new Vector.<Vector.<Number>>(gridSize);
public var windBitmapData:BitmapData;
public var windColorTransform:ColorTransform;
public var countMax:int = 20;
public var count:int = 0;
public var label:Label;
public function FlashTest() {
// write as3 code here..
var back:BitmapData = new BitmapData(465, 465, false, 0x292929);
addChild(new Bitmap(back));
windBitmapData = new BitmapData(465, 465, false, 0x0);
addChild(new Bitmap(windBitmapData));
windColorTransform = new ColorTransform(.8, .8, .9, 1.0);
label = new Label(this, 400, 0, "");
label.transform.colorTransform = new ColorTransform(0, 0, 0, 1, 255, 255, 255, 0);
initWind();
addEventListener(Event.ENTER_FRAME, onLoop);
stage.addEventListener(MouseEvent.CLICK, onReset);
}
private function onReset(e:Event):void
{
count = 0;
countMax = Math.random() * 20 + 5;
gridSize = 5 + Math.random() * 11;
resetWind();
windBitmapData.fillRect(windBitmapData.rect, 0x0);
}
private function onLoop(e:Event):void
{
count += 1;
if (count <= countMax)
{
interpolateWind();
drawWind();
label.text = "interpolate:\t" + String(count);
}
}
private function drawWind():void
{
var shape:Shape = new Shape();
windBitmapData.lock();
windBitmapData.applyFilter(windBitmapData, windBitmapData.rect, new Point(), new BlurFilter(1, 1));
windBitmapData.colorTransform(windBitmapData.rect, windColorTransform);
var i:int;
var j:int;
var gridWidth:int = 465 / gridSize;
var _x:Number;
var _y:Number;
shape.graphics.clear();
shape.graphics.lineStyle(0, 0xffffff);
for (i = 0; i < gridSize; i += 1)
{
for (j = 0; j < gridSize; j += 1)
{
_x = i * gridWidth + gridWidth / 2;
_y = j * gridWidth + gridWidth / 2;
shape.graphics.drawCircle(_x, _y, 2);
shape.graphics.moveTo(_x, _y);
shape.graphics.lineTo(_x + Math.cos(windGrid[i][j]) * windScale[i][j] * gridWidth / 2, _y + Math.sin(windGrid[i][j]) * windScale[i][j] * gridWidth / 2);
}
}
windBitmapData.draw(shape);
windBitmapData.unlock();
}
private function interpolateWind():void
{
var i:int;
var j:int;
//interpolate
var interpolateNum:int = 1;
var k:int;
for (k = 0; k < interpolateNum; k += 1)
{
var count:int;
for (i = 0; i < gridSize; i += 1)
{
for (j = 0; j < gridSize; j += 1)
{
tempWindGrid[i][j] = windGrid[i][j];
count = 1;
if (i > 0)
{
tempWindGrid[i][j] += windGrid[i - 1][j];
count += 1;
}
if (i < gridSize - 1)
{
tempWindGrid[i][j] += windGrid[i + 1][j];
count += 1;
}
if (j > 0)
{
tempWindGrid[i][j] += windGrid[i][j - 1];
count += 1;
}
if (j < gridSize - 1)
{
tempWindGrid[i][j] += windGrid[i][j + 1];
count += 1;
}
tempWindGrid[i][j] /= count;
}
}
}
for (i = 0; i < gridSize; i += 1)
{
for (j = 0; j < gridSize; j += 1)
{
windGrid[i][j] = tempWindGrid[i][j];
}
}
}
private function initWind():void
{
var i:int;
var j:int;
for (i = 0; i < gridSize; i += 1)
{
windGrid[i] = new Vector.<Number>(gridSize);
windScale[i] = new Vector.<Number>(gridSize);
tempWindGrid[i] = new Vector.<Number>(gridSize);
for (j = 0; j < gridSize; j += 1)
{
windGrid[i][j] = Math.random() * Math.PI * 2;
windScale[i][j] = Math.random() * windScaleMax;
}
}
}
private function resetWind():void
{
var i:int;
var j:int;
for (i = 0; i < gridSize; i += 1)
{
for (j = 0; j < gridSize; j += 1)
{
windGrid[i][j] = Math.random() * Math.PI * 2;
windScale[i][j] = Math.random() * windScaleMax;
}
}
}
}
}