pv3d学习(二)坐标
代码
package {
import fl.controls.Button;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.text.TextField;
import org.papervision3d.core.geom.Lines3D;
import org.papervision3d.core.geom.renderables.Line3D;
import org.papervision3d.core.geom.renderables.Vertex3D;
import org.papervision3d.materials.special.LineMaterial;
import org.papervision3d.materials.ColorMaterial;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.objects.primitives.Cube;
import org.papervision3d.render.QuadrantRenderEngine;
import org.papervision3d.view.BasicView;
[SWF(width="600",height="480",backgroundColor="#000000")]
public class RotationDemo extends BasicView{
private var lastMouseX:Number;
private var lastMouseY:Number;
private var buttonLabels:Array;
private var cubeLeft:Cube;
private var camYaw:Number = 270;
private var easeYaw:Number = 270;
private var cubeRight:Cube;
private var bRotationLeft:Boolean;
private var buttonContainer:Sprite;
private var camPitch:Number = 90;
private var easePitch:Number = 90;
private var isOrbiting:Boolean;
private var bRotationRight:Boolean;
public function RotationDemo(){
buttonLabels = ["localRotationZ++", "rotationZ++"];
super(stage.stageWidth, stage.stageHeight);
stage.frameRate = 40;
createButtons();
createTextField();
init();
startRendering();
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}
private function onMouseUp(event:MouseEvent) : void{
isOrbiting = false;
}
private function onMouseDown(event:MouseEvent) : void{
isOrbiting = true;
lastMouseX = event.stageX;
lastMouseY = event.stageY;
}
public function init() : void{
renderer = new QuadrantRenderEngine();
var materialsList:MaterialsList = new MaterialsList();
materialsList.addMaterial(new ColorMaterial(0x333333, 0.5), "front");
materialsList.addMaterial(new ColorMaterial(0x333333, 0.5), "back");
materialsList.addMaterial(new ColorMaterial(0x666666, 0.5), "left");
materialsList.addMaterial(new ColorMaterial(0x666666, 0.5), "right");
materialsList.addMaterial(new ColorMaterial(0x999999, 0.5), "top");
materialsList.addMaterial(new ColorMaterial(0x999999, 0.5), "bottom");
cubeRight = new Cube(materialsList, 60, 60, 60);
var r_XYZ:Axes3D = new Axes3D();
cubeRight.addChild(r_XYZ);
cubeLeft = new Cube(materialsList, 60, 60, 60);
var l_XYZ:Axes3D = new Axes3D();
cubeLeft.addChild(l_XYZ);
var cubeXYZ:Cube = new Cube(materialsList, 120, 120, 120);
scene.addChild(cubeXYZ);
var XYZ:Axes3D = new Axes3D(300);
var lines:Lines3D = new Lines3D();
var line1:Line3D= new Line3D(lines,new LineMaterial(0xffffff),1,new Vertex3D(0,80,0),new Vertex3D(0,80,300));
var line2:Line3D= new Line3D(lines,new LineMaterial(0xffffff),1,new Vertex3D(60,0,0),new Vertex3D(60,0,60));
var line3:Line3D= new Line3D(lines,new LineMaterial(0xffffff),1,new Vertex3D(0,0,60),new Vertex3D(60,0,60));
lines.addLine(line1);
lines.addLine(line2);
lines.addLine(line3);
cubeXYZ.addChild(lines)
cubeXYZ.addChild(cubeRight);
cubeXYZ.addChild(cubeLeft);
cubeXYZ.addChild(XYZ);
cubeXYZ.x=0;
cubeXYZ.y=-50;
cubeXYZ.z=100;
cubeXYZ.localRotationX=15;
cubeXYZ.localRotationY=-125;
cubeLeft.localRotationX = 90;
cubeLeft.x = 200;
cubeLeft.y = 30;
//cubeRight.localRotationZ = -45;
//cubeRight.localRotationX = -45;
var _lines:Lines3D = new Lines3D();
var _line:Line3D= new Line3D(_lines,new LineMaterial(0xffff00),3,new Vertex3D(30,0,-30),new Vertex3D(-30,0,30));
_lines.addLine(_line);
cubeRight.addChild(_lines)
cubeRight.localRotationY = -45;
cubeRight.z = 200;
cubeRight.y = 80;
camera.z = -400;
camera.y = 100;
camera.x = -30;
}
private function createTextField() : void{
var textNote:TextField = new TextField();
textNote.width = 600;
textNote.height =200;
textNote.selectable=false;
textNote.x = 50;
textNote.y = 30;
textNote.multiline = true;
textNote.htmlText = "<font color = '#ffffff' size='16' ><b>X-AXIS</b>(red)、<b>Y-AXIS</b>(green)、<b>Z-AXIS</b>(blue) <br>Press the buttons to start and stop rotation over the <b>Z-AXIS</b>." + "<br><br>The left cube will rotate over its own (blue) Z-AXIS.<br>The right cube will rotate over the (blue) Z-AXIS of the <b>parent cube</b>."+"<br><br> Drag the mouse to move the camera";
addChild(textNote);
}
override protected function onRenderTick(event:Event = null) : void{
if (bRotationLeft)cubeLeft.localRotationZ= cubeLeft.localRotationZ + 1;
if (bRotationRight)cubeRight.rotationZ= cubeRight.rotationZ + 1;
easePitch = easePitch + (camPitch - easePitch) * 0.1;
easeYaw = easeYaw + (camYaw - easeYaw) * 0.1;
camera.orbit(easePitch, easeYaw);
super.onRenderTick();
}
private function createButtons() : void{
buttonContainer = new Sprite();
addChild(buttonContainer);
buttonContainer.y = 380;
for(var i:int=0;i< buttonLabels.length;i++){
var btn:Button = new Button();
btn.label = buttonLabels[i];
btn.x = i * 200;
buttonContainer.addChild(btn);
btn.addEventListener(MouseEvent.CLICK, buttonClickedListener);
}
buttonContainer.x = stage.stageWidth / 2 - buttonContainer.width / 2;
}
private function buttonClickedListener(event:MouseEvent) : void{
switch(event.target.label){
case buttonLabels[0]:
bRotationLeft = !bRotationLeft;
break;
case buttonLabels[1]:
bRotationRight = !bRotationRight;
break;
default:
break;
}
}
private function onMouseMove(event:MouseEvent) : void{
var _vx:Number = event.stageX - lastMouseX;
var _vy:Number = event.stageY - lastMouseY;
if (isOrbiting){
camPitch = camPitch + _vy * 0.25;
camYaw = camYaw + _vx * 0.25;
camPitch = camPitch > 5 ? (camPitch) : (5.0001);
camPitch = camPitch < 175 ? (camPitch) : (175);
lastMouseX = event.stageX;
lastMouseY = event.stageY;
}
}
}
}
import fl.controls.Button;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.text.TextField;
import org.papervision3d.core.geom.Lines3D;
import org.papervision3d.core.geom.renderables.Line3D;
import org.papervision3d.core.geom.renderables.Vertex3D;
import org.papervision3d.materials.special.LineMaterial;
import org.papervision3d.materials.ColorMaterial;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.objects.primitives.Cube;
import org.papervision3d.render.QuadrantRenderEngine;
import org.papervision3d.view.BasicView;
[SWF(width="600",height="480",backgroundColor="#000000")]
public class RotationDemo extends BasicView{
private var lastMouseX:Number;
private var lastMouseY:Number;
private var buttonLabels:Array;
private var cubeLeft:Cube;
private var camYaw:Number = 270;
private var easeYaw:Number = 270;
private var cubeRight:Cube;
private var bRotationLeft:Boolean;
private var buttonContainer:Sprite;
private var camPitch:Number = 90;
private var easePitch:Number = 90;
private var isOrbiting:Boolean;
private var bRotationRight:Boolean;
public function RotationDemo(){
buttonLabels = ["localRotationZ++", "rotationZ++"];
super(stage.stageWidth, stage.stageHeight);
stage.frameRate = 40;
createButtons();
createTextField();
init();
startRendering();
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}
private function onMouseUp(event:MouseEvent) : void{
isOrbiting = false;
}
private function onMouseDown(event:MouseEvent) : void{
isOrbiting = true;
lastMouseX = event.stageX;
lastMouseY = event.stageY;
}
public function init() : void{
renderer = new QuadrantRenderEngine();
var materialsList:MaterialsList = new MaterialsList();
materialsList.addMaterial(new ColorMaterial(0x333333, 0.5), "front");
materialsList.addMaterial(new ColorMaterial(0x333333, 0.5), "back");
materialsList.addMaterial(new ColorMaterial(0x666666, 0.5), "left");
materialsList.addMaterial(new ColorMaterial(0x666666, 0.5), "right");
materialsList.addMaterial(new ColorMaterial(0x999999, 0.5), "top");
materialsList.addMaterial(new ColorMaterial(0x999999, 0.5), "bottom");
cubeRight = new Cube(materialsList, 60, 60, 60);
var r_XYZ:Axes3D = new Axes3D();
cubeRight.addChild(r_XYZ);
cubeLeft = new Cube(materialsList, 60, 60, 60);
var l_XYZ:Axes3D = new Axes3D();
cubeLeft.addChild(l_XYZ);
var cubeXYZ:Cube = new Cube(materialsList, 120, 120, 120);
scene.addChild(cubeXYZ);
var XYZ:Axes3D = new Axes3D(300);
var lines:Lines3D = new Lines3D();
var line1:Line3D= new Line3D(lines,new LineMaterial(0xffffff),1,new Vertex3D(0,80,0),new Vertex3D(0,80,300));
var line2:Line3D= new Line3D(lines,new LineMaterial(0xffffff),1,new Vertex3D(60,0,0),new Vertex3D(60,0,60));
var line3:Line3D= new Line3D(lines,new LineMaterial(0xffffff),1,new Vertex3D(0,0,60),new Vertex3D(60,0,60));
lines.addLine(line1);
lines.addLine(line2);
lines.addLine(line3);
cubeXYZ.addChild(lines)
cubeXYZ.addChild(cubeRight);
cubeXYZ.addChild(cubeLeft);
cubeXYZ.addChild(XYZ);
cubeXYZ.x=0;
cubeXYZ.y=-50;
cubeXYZ.z=100;
cubeXYZ.localRotationX=15;
cubeXYZ.localRotationY=-125;
cubeLeft.localRotationX = 90;
cubeLeft.x = 200;
cubeLeft.y = 30;
//cubeRight.localRotationZ = -45;
//cubeRight.localRotationX = -45;
var _lines:Lines3D = new Lines3D();
var _line:Line3D= new Line3D(_lines,new LineMaterial(0xffff00),3,new Vertex3D(30,0,-30),new Vertex3D(-30,0,30));
_lines.addLine(_line);
cubeRight.addChild(_lines)
cubeRight.localRotationY = -45;
cubeRight.z = 200;
cubeRight.y = 80;
camera.z = -400;
camera.y = 100;
camera.x = -30;
}
private function createTextField() : void{
var textNote:TextField = new TextField();
textNote.width = 600;
textNote.height =200;
textNote.selectable=false;
textNote.x = 50;
textNote.y = 30;
textNote.multiline = true;
textNote.htmlText = "<font color = '#ffffff' size='16' ><b>X-AXIS</b>(red)、<b>Y-AXIS</b>(green)、<b>Z-AXIS</b>(blue) <br>Press the buttons to start and stop rotation over the <b>Z-AXIS</b>." + "<br><br>The left cube will rotate over its own (blue) Z-AXIS.<br>The right cube will rotate over the (blue) Z-AXIS of the <b>parent cube</b>."+"<br><br> Drag the mouse to move the camera";
addChild(textNote);
}
override protected function onRenderTick(event:Event = null) : void{
if (bRotationLeft)cubeLeft.localRotationZ= cubeLeft.localRotationZ + 1;
if (bRotationRight)cubeRight.rotationZ= cubeRight.rotationZ + 1;
easePitch = easePitch + (camPitch - easePitch) * 0.1;
easeYaw = easeYaw + (camYaw - easeYaw) * 0.1;
camera.orbit(easePitch, easeYaw);
super.onRenderTick();
}
private function createButtons() : void{
buttonContainer = new Sprite();
addChild(buttonContainer);
buttonContainer.y = 380;
for(var i:int=0;i< buttonLabels.length;i++){
var btn:Button = new Button();
btn.label = buttonLabels[i];
btn.x = i * 200;
buttonContainer.addChild(btn);
btn.addEventListener(MouseEvent.CLICK, buttonClickedListener);
}
buttonContainer.x = stage.stageWidth / 2 - buttonContainer.width / 2;
}
private function buttonClickedListener(event:MouseEvent) : void{
switch(event.target.label){
case buttonLabels[0]:
bRotationLeft = !bRotationLeft;
break;
case buttonLabels[1]:
bRotationRight = !bRotationRight;
break;
default:
break;
}
}
private function onMouseMove(event:MouseEvent) : void{
var _vx:Number = event.stageX - lastMouseX;
var _vy:Number = event.stageY - lastMouseY;
if (isOrbiting){
camPitch = camPitch + _vy * 0.25;
camYaw = camYaw + _vx * 0.25;
camPitch = camPitch > 5 ? (camPitch) : (5.0001);
camPitch = camPitch < 175 ? (camPitch) : (175);
lastMouseX = event.stageX;
lastMouseY = event.stageY;
}
}
}
}
代码
package {
import org.papervision3d.core.geom.Lines3D;
import org.papervision3d.core.geom.renderables.Line3D;
import org.papervision3d.core.geom.renderables.Vertex3D;
import org.papervision3d.materials.special.LineMaterial;
import org.papervision3d.materials.ColorMaterial;
import org.papervision3d.materials.WireframeMaterial;
import org.papervision3d.objects.primitives.Cone ;
import org.papervision3d.objects.DisplayObject3D;
public class Axes3D extends DisplayObject3D {
private var _lines:Lines3D;
private var _xAxis:Line3D;
private var _yAxis:Line3D;
private var _zAxis:Line3D;
private var _length:Number;
private var _coneX:Cone;
private var _coneY:Cone;
private var _coneZ:Cone;
private var _R:Number;
private var _H:Number;
public function Axes3D(length:Number=80,R:Number=3,H=12) {
//坐标系
_lines = new Lines3D();
addChild (_lines);
var origin:Vertex3D = new Vertex3D();
_length=length;
_R=R;
_H=H;
_xAxis = new Line3D(_lines,new LineMaterial(0xff0000),2,origin,new Vertex3D(_length,0,0));
_yAxis = new Line3D(_lines,new LineMaterial(0x00ff00),2,origin,new Vertex3D(0,_length,0));
_zAxis = new Line3D(_lines,new LineMaterial(0x0000ff),2,origin,new Vertex3D(0,0,_length));
_lines.addLine(_xAxis);
_lines.addLine(_yAxis);
_lines.addLine(_zAxis);
_coneX=new Cone(new WireframeMaterial(0xff0000,100,1),_R,_H,3,1);//new ColorMaterial(0xff0000,0.5)
addChild(_coneX);
_coneX.x=_length+_H*0.5;
_coneX.roll(-90);
_coneY=new Cone(new WireframeMaterial(0x00ff00,100,1),_R,_H,3,1);//new ColorMaterial(0x00ff00,0.5)
addChild(_coneY);
_coneY.y=_length+_H*0.5;
_coneZ=new Cone(new WireframeMaterial(0x0000ff,100,1),_R,_H,3,1);//new ColorMaterial(0x0000ff,0.5)
addChild(_coneZ);
_coneZ.z=_length+_H*0.5;
_coneZ.pitch(90);
}
}
}
import org.papervision3d.core.geom.Lines3D;
import org.papervision3d.core.geom.renderables.Line3D;
import org.papervision3d.core.geom.renderables.Vertex3D;
import org.papervision3d.materials.special.LineMaterial;
import org.papervision3d.materials.ColorMaterial;
import org.papervision3d.materials.WireframeMaterial;
import org.papervision3d.objects.primitives.Cone ;
import org.papervision3d.objects.DisplayObject3D;
public class Axes3D extends DisplayObject3D {
private var _lines:Lines3D;
private var _xAxis:Line3D;
private var _yAxis:Line3D;
private var _zAxis:Line3D;
private var _length:Number;
private var _coneX:Cone;
private var _coneY:Cone;
private var _coneZ:Cone;
private var _R:Number;
private var _H:Number;
public function Axes3D(length:Number=80,R:Number=3,H=12) {
//坐标系
_lines = new Lines3D();
addChild (_lines);
var origin:Vertex3D = new Vertex3D();
_length=length;
_R=R;
_H=H;
_xAxis = new Line3D(_lines,new LineMaterial(0xff0000),2,origin,new Vertex3D(_length,0,0));
_yAxis = new Line3D(_lines,new LineMaterial(0x00ff00),2,origin,new Vertex3D(0,_length,0));
_zAxis = new Line3D(_lines,new LineMaterial(0x0000ff),2,origin,new Vertex3D(0,0,_length));
_lines.addLine(_xAxis);
_lines.addLine(_yAxis);
_lines.addLine(_zAxis);
_coneX=new Cone(new WireframeMaterial(0xff0000,100,1),_R,_H,3,1);//new ColorMaterial(0xff0000,0.5)
addChild(_coneX);
_coneX.x=_length+_H*0.5;
_coneX.roll(-90);
_coneY=new Cone(new WireframeMaterial(0x00ff00,100,1),_R,_H,3,1);//new ColorMaterial(0x00ff00,0.5)
addChild(_coneY);
_coneY.y=_length+_H*0.5;
_coneZ=new Cone(new WireframeMaterial(0x0000ff,100,1),_R,_H,3,1);//new ColorMaterial(0x0000ff,0.5)
addChild(_coneZ);
_coneZ.z=_length+_H*0.5;
_coneZ.pitch(90);
}
}
}