日本万華鏡会
...
@author @kndys
package
{
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.media.Camera;
import flash.media.Video;
/**
* ...
* @author @kndys
*/
[SWF(width="465",height="465",backgroundColor="0x0",frameRate="30")]
public class Main extends Sprite
{
private var camera:Camera;
private var video:Video;
private var vertices:Vector.<Number>;
private var indices:Vector.<int>;
private var uvtData:Vector.<Number>;
private var bmd:BitmapData;
private var triUV:Vector.<TriPt>;
private var COS15D:Number = Math.cos(Math.PI / 12);
private var SIN15D:Number = Math.sin(Math.PI / 12);
private var TAN15D:Number = Math.tan(Math.PI / 12);
private var N:uint = 6;
private var unitX:TriPt;
private var unitY:TriPt;
private var pts:Vector.<Vector.<TriPt>>;
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);
// entry point
camera = Camera.getCamera();
camera.setMode(180, 180, 30);
video = new Video(180, 180);
video.attachCamera(camera);
bmd = new BitmapData(180, 180, true, 0);
triUV = new Vector.<TriPt>();
triUV[0] = new TriPt(0, 0, 0);
triUV[1] = new TriPt(1, TAN15D, 1);
triUV[2] = new TriPt(TAN15D, 1, 2);
vertices = new Vector.<Number>();
indices = new Vector.<int>();
uvtData = new Vector.<Number>();
var a:Number = 465 * Math.SQRT2 / N;
unitX = new TriPt(a * COS15D, - a * SIN15D);
unitY = new TriPt( - a * SIN15D, a * COS15D);
pts = new Vector.<Vector.<TriPt>>();
var $x:int, $y:int, $i:int = 0;
for ($x = 0; $x<= N; $x+=1)
{
pts[$x] = new Vector.<TriPt>();
for ($y = 0; $y<= N; $y+=1)
{
pts[$x][$y] = new TriPt(
unitX.x * $x + unitY.x * $y ,
unitX.y * $x + unitY.y * $y,
($x + $y) % 3, $i++);
}
}
var pt:TriPt, uv:TriPt;
for ($x = 0; $x<= N; $x+=1)
{
for ($y = 0; $y<= N; $y+=1)
{
pt = pts[$x][$y];
vertices.push(pt.x, pt.y);
uv = triUV[pt.iUV];
uvtData.push(uv.x, uv.y);
}
}
var p_o:TriPt;
var p_a:TriPt;
var p_b:TriPt;
var p_c:TriPt;
for ($x = 0; $x< N; $x+=1)
{
for ($y = 0; $y< N; $y+=1)
{
p_o = pts[$x][$y];
p_a = pts[$x + 1][$y];
p_b = pts[$x + 1][$y + 1];
p_c = pts[$x][$y + 1];
indices.push(p_o.iVer, p_a.iVer, p_b.iVer, p_o.iVer, p_b.iVer, p_c.iVer);
}
}
trace(vertices.length, indices.length, uvtData.length);
addEventListener(Event.ENTER_FRAME, loop);
}
private function loop(e:Event):void
{
var mx:Number = mouseX;
var my:Number = mouseY;
var $x:int, $y:int, pt:TriPt, x0:Number, y0:Number, uv:TriPt;
for ($x = 0; $x<= N; $x+=1)
{
for ($y = 0; $y<= N; $y+=1)
{
pt = pts[$x][$y];
x0 = pt.x - mx;
y0 = pt.y - my;
var r:Number = 20 / (1 + Math.sqrt(x0 * x0 + y0 * y0));
vertices[pt.iVer<<1] = pt.x + x0 * r;
vertices[(pt.iVer<<1) | 1] = pt.y + y0 * r;
}
}
bmd.draw(video);
graphics.clear();
graphics.beginBitmapFill(bmd);
graphics.lineStyle(0, 0xffffff, 0.5);
graphics.drawTriangles(vertices, indices, uvtData);
graphics.endFill();
}
}
}
class TriPt
{
public var x:Number;
public var y:Number;
public var iUV:int;
public var iVer:int;
public function TriPt(x:Number = 0, y:Number = 0, iUV:int = 0, iVer:int = 0)
{
this.x = x;
this.y = y;
this.iUV = iUV;
this.iVer = iVer;
}
}