forked from: forked from: forked from: forked from: forked from: 空間に絵を描く Graphics3D
// forked from dizgid's forked from: forked from: forked from: forked from: 空間に絵を描く Graphics3D
// forked from dizgid's forked from: forked from: forked from: 空間に絵を描く PerspectiveProjection / curveTo
// forked from dizgid's forked from: forked from: 空間に絵を描く PerspectiveProjection / curveTo
// forked from dizgid's forked from: 空間に絵を描く PerspectiveProjection
// forked from miniapp's 空間に絵を描く
package {
import flash.display.GraphicsPathCommand;
import flash.display.SpreadMethod;
import flash.display.Sprite;
import flash.display.Shape;
import flash.display.Graphics;
import flash.filters.GlowFilter;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.geom.Matrix3D;
import flash.geom.PerspectiveProjection;
import flash.geom.Point;
import flash.geom.Utils3D;
import flash.geom.Vector3D;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.ui.Keyboard;
[SWF(backgroundColor="0x000000", width="465", height="465", frameRate="60")]
public class Projection1 extends Sprite {
//private static var identityMatrix:Matrix3D = new Matrix3D();
private var viewport:Shape;
private var projection:PerspectiveProjection;
private var worldMatrix:Matrix3D;
private var viewMatrix:Matrix3D;
private var g:Graphics;
private var _graphics3D:Graphics3D;
public function Projection1() {
this.graphics.beginFill(0x000000);
this.graphics.drawRect(0,0,stage.stageWidth, stage.stageHeight);
this.graphics.endFill();
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
viewport = new Shape();
viewport.x = centerX;
viewport.y = centerY;
viewport.filters = [new GlowFilter(0x3300CC, 0.7, 32, 32, 2, 2)];
g = viewport.graphics
addChild(viewport);
projection = new PerspectiveProjection();
projection.fieldOfView = 60;
worldMatrix = new Matrix3D();
viewMatrix = new Matrix3D();
viewMatrix.appendTranslation(0, 0, projection.focalLength);
_drawGraphic3D();
var tf:TextField = new TextField();
tf.autoSize = TextFieldAutoSize.LEFT;
tf.textColor = 0x666666;
tf.text = 'shitfで回転';
addChild(tf);
Wonderfl.capture_delay( 60 );
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
private function _drawGraphic3D():void
{
var v:Number = 100;
_graphics3D = new Graphics3D();
_graphics3D.moveTo( v, v, -v);
_graphics3D.lineTo( v,-v, -v);
_graphics3D.lineTo(-v,-v, -v);
_graphics3D.lineTo(-v, v, -v);
_graphics3D.lineTo( v, v, -v);
_graphics3D.moveTo( v, v, -v);
_graphics3D.lineTo( v, v, v);
_graphics3D.moveTo(-v, v, -v);
_graphics3D.lineTo(-v, v, v);
_graphics3D.moveTo( v,-v, -v);
_graphics3D.lineTo( v,-v, v);
_graphics3D.moveTo(-v,-v, -v);
_graphics3D.lineTo(-v,-v, v);
_graphics3D.moveTo( v, v, v);
_graphics3D.lineTo( v,-v, v);
_graphics3D.lineTo(-v,-v, v);
_graphics3D.lineTo(-v, v, v);
_graphics3D.lineTo( v, v, v);
//_graphics3D.moveTo( 0, 0, 0);
drawCircle(_graphics3D, 0,0, 100);
drawCircle(_graphics3D, 0,0, 80);
}
public var uv:Vector.<Number> = new Vector.<Number>();
public var verts:Vector.<Number> = new Vector.<Number>();
public var projectedVerts:Vector.<Number> = new Vector.<Number>();
public var commands:Vector.<int> = new Vector.<int>();
public var matrix:Matrix3D = new Matrix3D();
public var shiftkeyIsDown:Boolean = false;
public var centerX:Number = stage.stageWidth / 2;
public var centerY:Number = stage.stageHeight / 2;
public var pmouseX:Number;
public var pmouseY:Number;
private var _c:Number;
private function keyUpHandler(e:KeyboardEvent):void {
if (e.keyCode == Keyboard.SHIFT) shiftkeyIsDown = false;
}
private function keyDownHandler(e:KeyboardEvent):void {
if (e.keyCode == Keyboard.SHIFT) shiftkeyIsDown = true;
}
private function enterFrameHandler(e:Event):void {
verts = _graphics3D.verts;
commands = _graphics3D.commands;
if (shiftkeyIsDown) {
//一フレーム前のマウス座標との差分を回転角度にする。
var dx:Number = mouseY - pmouseY;
var dy:Number = mouseX - pmouseX;
worldMatrix.identity();
worldMatrix.appendRotation(-dx, Vector3D.X_AXIS);
worldMatrix.appendRotation(dy, Vector3D.Y_AXIS);
// 頂点データを回転させる。
worldMatrix.transformVectors(verts, verts);
}
var matrix:Matrix3D = new Matrix3D();
matrix.append(viewMatrix);
matrix.append(projection.toMatrix3D());
//2次元に投影
Utils3D.projectVectors(matrix, verts, projectedVerts, uv);
render(commands, projectedVerts);
pmouseX = mouseX;
pmouseY = mouseY;
}
private function render(commands:Vector.<int>, projectedVerts:Vector.<Number>):void
{
g.clear();
g.lineStyle(6, 0x3300CC);
g.drawPath(commands, projectedVerts);
g.lineStyle(1, 0xffffff);
g.drawPath(commands, projectedVerts);
}
//test用
/**
http://livedocs.adobe.com/flash/9.0_jp/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00001904.html
*/
private function drawCircle(g:Graphics3D, x:Number, y:Number, r:Number):void
{
var z:Number = 0;
g.moveTo(x+r, y, z);
g.curveTo(r+x, Math.tan(Math.PI/8)*r+y, z, Math.sin(Math.PI/4)*r+x, Math.sin(Math.PI/4)*r+y, z);
g.curveTo(Math.tan(Math.PI/8)*r+x, r+y, z, x, r+y, z);
g.curveTo(-Math.tan(Math.PI/8)*r+x, r+y, z, -Math.sin(Math.PI/4)*r+x, Math.sin(Math.PI/4)*r+y, z );
g.curveTo(-r+x, Math.tan(Math.PI/8)*r+y, z, -r+x, y, z);
g.curveTo(-r+x, -Math.tan(Math.PI/8)*r+y, z, -Math.sin(Math.PI/4)*r+x, -Math.sin(Math.PI/4)*r+y, z);
g.curveTo(-Math.tan(Math.PI/8)*r+x, -r+y, z, x, -r+y, z);
g.curveTo(Math.tan(Math.PI/8)*r+x, -r+y, z, Math.sin(Math.PI/4)*r+x, -Math.sin(Math.PI/4)*r+y, z);
g.curveTo(r+x, -Math.tan(Math.PI/8)*r+y, z, r+x, y, z);
}
}
}
import org.papervision3d.core.math.Quaternion;
import flash.display.GraphicsPathCommand;
import flash.geom.Vector3D;
internal class Graphics3D{
public var commands:Vector.<int>;
public var verts:Vector.<Number>;
private var _thikness:Number = 10;
private var curPos:Vector3D = new Vector3D();
private var prevPos:Vector3D = new Vector3D();
private var _prevX : Vector3D = new Vector3D(); // 道路描画時の直前のX方向
private var _prevY : Vector3D = new Vector3D(); // 道路描画時の直前のY方向(カメラの上)
private var _prevZ : Vector3D = new Vector3D(); // 道路描画時の直前のZ方向(正面)
public function Graphics3D(){
commands = new Vector.<int>();
verts = new Vector.<Number>();
}
public function lineStyle(thickness:Number, color:uint, alpha:Number):void
{
//
}
public function moveTo(x:Number,y:Number,z:Number):void
{
commands.push(GraphicsPathCommand.MOVE_TO);
verts.push(x,y,z);
}
public function curveTo(x:Number,y:Number,z:Number, cx:Number,cy:Number,cz:Number):void
{
commands.push(GraphicsPathCommand.CURVE_TO);
verts.push(x,y,z,cx,cy,cz);
}
public function lineTo(x:Number,y:Number,z:Number):void
{
commands.push(GraphicsPathCommand.LINE_TO);
verts.push(x,y,z);
}
}