forked from: forked from: flash on 2009-12-3
...
@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="20")]
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 const PI2:Number = Math.PI * 2;
private const PI2_3:Number = Math.PI * 2 / 3;
private const PI4_3:Number = Math.PI * 4 / 3;
private var N:uint = 7;
private var unitX:TriPt;
private var unitY:TriPt;
private var pts:Vector.<Vector.<TriPt>>;
private var theta:Number;
private var phi:Number;
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(240, 240, 20);
video = new Video(240, 240);
video.attachCamera(camera);
bmd = new BitmapData(180, 180, true, 0);
triUV = new Vector.<TriPt>();
theta = 0;
phi = 0;
triUV[0] = new TriPt();
triUV[1] = new TriPt();
triUV[2] = new TriPt();
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
{
theta += 0.05;
if (theta > PI2) theta -= PI2;
phi += 0.007;
if (phi > PI2) phi -= PI2;
triUV[0].x = 0.5 + 0.15 * Math.cos(phi) + 0.35 * Math.cos(theta);
triUV[0].y = 0.5 + 0.15 * Math.sin(phi) + 0.35 * Math.sin(theta);
triUV[1].x = 0.5 + 0.15 * Math.cos(phi) + 0.35 * Math.cos(theta - PI2_3);
triUV[1].y = 0.5 + 0.15 * Math.sin(phi) + 0.35 * Math.sin(theta - PI2_3);
triUV[2].x = 0.5 + 0.15 * Math.cos(phi) + 0.35 * Math.cos(theta - PI4_3);
triUV[2].y = 0.5 + 0.15 * Math.sin(phi) + 0.35 * Math.sin(theta - PI4_3);
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 = 40 / (1 + Math.sqrt(x0 * x0 + y0 * y0));
vertices[pt.iVer<<1] = pt.x + x0 * r;
vertices[(pt.iVer<<1) | 1] = pt.y + y0 * r;
uv = triUV[pt.iUV];
uvtData[pt.iVer << 1] = uv.x;
uvtData[(pt.iVer << 1) | 1] = uv.y;
}
}
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;
}
}