PerlinNoiseをDrawTriangleで適当に変形させていく
PerlinNoiseをDrawTriangleで適当に変形させていく
@author Hiiragi
もうちょっときれいになるかと思ったけど、そうでもなかった。
package
{
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import net.hires.debug.Stats;
/**
* PerlinNoiseをDrawTriangleで適当に変形させていく
* @author Hiiragi
*
* もうちょっときれいになるかと思ったけど、そうでもなかった。
*
*/
[SWF(width = 465, height = 465, frameRate = 60, backgroundColor = 0)]
public class DrawTriangleTest extends Sprite
{
private const SW:int = 465;
private const SH:int = 465;
private var _image:BitmapData;
private var _xNum:int;
private var _yNum:int;
private var _xDiff:Number;
private var _yDiff:Number;
private var _particles:Vector.<Particle>;
private var _vertices:Vector.<Number>;
private var _indices:Vector.<int>;
private var _uvtData:Vector.<Number>;
private var _isExistIndices:Boolean = false;
private var _isExistUvtData:Boolean = false;
public function DrawTriangleTest():void
{
_xNum = 10;
_yNum = 10;
_xDiff = SW / (_xNum - 1);
_yDiff = SH / (_yNum - 1);
_particles = new Vector.<Particle>;
_indices = new Vector.<int>;
_uvtData = new Vector.<Number>;
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
_image = new BitmapData(SW, SH, false);
_image.perlinNoise(SW, SH, 3, Math.random() * 1000 >> 0, false, false);
this.addChild(new Stats());
for (var y:int = 0; y < _yNum; y++)
{
for (var x:int = 0; x < _xNum; x++)
{
var p:Particle = new Particle();
p.x = _xDiff * x
p.y = _yDiff * y;
p.vx = Math.floor(Math.random() * 10 - 5);
p.vy = Math.floor(Math.random() * 10 - 5);
//p.ax = Math.floor(Math.random() * 4 - 2);
//p.ay = Math.floor(Math.random() * 4 - 2);
_particles.push(p);
}
}
this.addEventListener(Event.ENTER_FRAME, drawImage);
}
private function drawImage(e:Event):void
{
var xNum:int = _xNum;
var yNum:int = _yNum;
var xDiff:Number = _xDiff;
var yDiff:Number = _yDiff;
var cnt:int = 0;
_vertices = new Vector.<Number>;
for (var y:int = 0; y < yNum; y++)
{
for (var x:int = 0; x < xNum; x++)
{
_vertices.push(_particles[cnt].x, _particles[cnt].y);
cnt++;
if (!_isExistIndices) _uvtData.push(x / (xNum - 1), y / (yNum - 1));
if (x < xNum - 1)
{
var pos:int = x + y * xNum;
if (!_isExistUvtData) _indices.push(pos, pos + 1, pos + xNum, pos + xNum, pos + 1, pos + xNum + 1);
}
}
}
if (!_isExistIndices)
{
_isExistIndices = true;
_isExistUvtData = true;
}
particleUpdate();
var g:Graphics = this.graphics;
g.clear();
//g.lineStyle(1, 0xFFFFFF);
g.beginBitmapFill(_image, null, false, true);
g.drawTriangles(_vertices, _indices, _uvtData, TriangleCulling.NEGATIVE);
g.endFill();
if (_cnt++ > 100000) this.removeEventListener(Event.ENTER_FRAME, drawImage);
}
private var _cnt:int = 0;
private function particleUpdate():void
{
var len:int = _particles.length;
for (var i:int = 0; i < len; i++)
{
//_particles[i].vx += _particles[i].ax;
//_particles[i].vy += _particles[i].ay;
//
//_particles[i].vx *= 0.998;
//_particles[i].vy *= 0.998;
_particles[i].x += _particles[i].vx;
_particles[i].y += _particles[i].vy;
if (_particles[i].x < 0)
{
_particles[i].x = 0;
_particles[i].vx *= -1;
}
else if (_particles[i].x > SW)
{
_particles[i].x = SW;
_particles[i].vx *= -1;
}
if (_particles[i].y < 0)
{
_particles[i].y = 0;
_particles[i].vy *= -1;
}
else if (_particles[i].y > SH)
{
_particles[i].y = SH;
_particles[i].vy *= -1;
}
}
}
}
}
import flash.geom.Rectangle;
class Particle
{
private var _x:Number;
private var _y:Number;
private var _vx:Number;
private var _vy:Number;
private var _ax:Number;
private var _ay:Number;
public function Particle(x:Number = 0, y:Number = 0, vx:Number = 0, vy:Number= 0, ax:Number = 0, ay:Number = 0)
{
_x = x;
_y = y;
_vx = vx;
_vy = vy;
_ax = ax;
_ay = ay;
}
public function get x():Number { return _x; }
public function set x(value:Number):void
{
_x = value;
}
public function get y():Number { return _y; }
public function set y(value:Number):void
{
_y = value;
}
public function get vx():Number { return _vx; }
public function set vx(value:Number):void
{
_vx = value;
}
public function get vy():Number { return _vy; }
public function set vy(value:Number):void
{
_vy = value;
}
public function get ax():Number { return _ax; }
public function set ax(value:Number):void
{
_ax = value;
}
public function get ay():Number { return _ay; }
public function set ay(value:Number):void
{
_ay = value;
}
}