pv3d(三)camera
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
package {
import fl.controls.Slider;
import fl.events.SliderEvent;
import flash.display.Sprite;
import flash.display.Shape;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFormat;
import org.papervision3d.core.clipping.FrustumClipping;
import org.papervision3d.materials.ColorMaterial;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.objects.primitives.Plane;
import org.papervision3d.view.BasicView;
[SWF(width="600",height="800",frameRate="40",backgroundColor="#000000")]
public class CameraSettingsDemo extends BasicView {
private var camTarget:DisplayObject3D;
private var sliderValuesMin:Array;//装载slider最小值
private var sliderValuesMax:Array;//装载slider最大值
private var menuContainer:Sprite;//装载Slider实例的容器
private var textFields:Array;//装载TextField实例
private var initialValues:Array;//装载获取值
private var numberOfSliders:uint;
private var nearPlane:Plane;
private var addCounter:uint;
private var viewportSize:int=480;
public function CameraSettingsDemo() {
super(viewportSize, viewportSize,false,true,"Free");
init();
createSliders();
startRendering();
}
private function sliderChangedListener(event:SliderEvent):void {
switch (event.target.name) {
case "0" :
camera.zoom=event.target.value;
textFields[0].text="camera.zoom: "+camera.zoom;
Slider(menuContainer.getChildByName("2")).value=camera.fov;
textFields[2].text="camera.fov: "+Math.round(camera.fov);
break;
case "1" :
camera.focus=event.target.value;
textFields[1].text="camera.focus: "+Math.round(camera.focus);
Slider(menuContainer.getChildByName("2")).value=camera.fov;
textFields[2].text="camera.fov: "+Math.round(camera.fov);
break;
case "2" :
camera.fov=event.target.value;
textFields[2].text="camera.fov: "+Math.round(camera.fov);
Slider(menuContainer.getChildByName("1")).value=camera.focus;
textFields[1].text="camera.focus: "+Math.round(camera.focus);
break;
case "3" :
camera.x=event.target.value;
textFields[3].text="camera.x: "+camera.x;
break;
case "4" :
camera.y=event.target.value;
textFields[4].text="camera.y: "+camera.y;
break;
case "5" :
camera.z=event.target.value;
textFields[5].text="camera.z: "+camera.z;
Slider(menuContainer.getChildByName("2")).value=camera.fov;
textFields[2].text="camera.fov: "+Math.round(camera.fov);
Slider(menuContainer.getChildByName("1")).value=camera.focus;
textFields[1].text="camera.focus: "+Math.round(camera.focus);
break;
case "6" :
viewport.viewportWidth=viewport.viewportHeight=viewportSize*(event.target.value)/100;
viewport.x=stage.stageWidth/2-viewport.viewportWidth/2;
viewport.y=stage.stageHeight/2-viewport.viewportHeight/2;
textFields[6].text="viewport: "+viewport.viewportWidth;
Slider(menuContainer.getChildByName("2")).value=camera.fov;
textFields[2].text="camera.fov: "+Math.round(camera.fov);
Slider(menuContainer.getChildByName("1")).value=camera.focus;
textFields[1].text="camera.focus: "+Math.round(camera.focus);
viewport.graphics.clear();
viewport.graphics.lineStyle(2, 0xffffff);
viewport.graphics.drawRect(0, 0, viewport.viewportWidth, viewport.viewportHeight);
break;
default :
break;
}
}
public function init():void {
camera.z=-201;
camera.useClipping=false;
camera.useCulling=true;
viewport.graphics.lineStyle(2, 0xffffff);
viewport.graphics.drawRect(0, 0, viewport.viewportWidth, viewport.viewportHeight);
viewport.x=stage.stageWidth/2-viewport.viewportWidth/2;
viewport.y=stage.stageHeight/2-viewport.viewportHeight/2;
renderer.clipping=new FrustumClipping(FrustumClipping.NEAR);
camTarget=new Plane(new ColorMaterial(0x0000ff),200,200);
camTarget.z=2000;
scene.addChild(camTarget);
camera.target = camTarget;
textFields=[];
sliderValuesMin=[1,1,1,-100,-100,-2500,0];
sliderValuesMax=[100,100,100,100,100,2000,100];
initialValues=[camera.zoom,camera.focus,camera.fov,camera.x,camera.y,camera.z,viewport.viewportWidth];
var plane:Plane=null;
var planeMaterial:ColorMaterial=new ColorMaterial(0xff0000);
for (var i:int=0; i<4; i++) {
plane=new Plane(planeMaterial,200,200,4,4);
plane.localRotationY=90;
plane.z=i*300;
plane.x=-300;
scene.addChild( plane);
}
for (i=0; i<4; i++) {
plane=new Plane(planeMaterial,200,200,4,4);
plane.localRotationY=-90;
plane.z=i*300;
plane.x=300;
scene.addChild( plane);
}
}
override protected function onRenderTick(event:Event = null):void {
super.onRenderTick();
}
private function createSliders():void {
menuContainer = new Sprite();
addChild(menuContainer);
var slider:Slider=null;
numberOfSliders=7;
trace(camera.x,camera.y,camera.z);
for (var i:int=0; i<numberOfSliders; i++) {
slider= new Slider();
slider.name=String(i);
//addChild(slider);
slider.x=i*100;
slider.liveDragging=true;
slider.minimum=sliderValuesMin[i];
slider.maximum=sliderValuesMax[i];
slider.value=initialValues[i];
slider.addEventListener(SliderEvent.CHANGE, sliderChangedListener);
menuContainer.addChild(slider);
textFields[i] = new TextField();
textFields[i].defaultTextFormat = new TextFormat(null,null,0xFFFFFF);
textFields[i].selectable=false;
//textFields[i].background=true;
textFields[i].height=20;
textFields[i].x=i*100;
textFields[i].y=20;
menuContainer.addChild(textFields[i]);
if (i==5) {
slider.width=200;
slider.x=0;
slider.y=40;
textFields[i].x=0;
textFields[i].y=60;
}
if (i==6) {
slider.width=100;
slider.x=300;
slider.y=40;
textFields[i].x=300;
textFields[i].y=60;
}
}
menuContainer.x=stage.stageWidth/2-menuContainer.width/2;
menuContainer.y=600;
textFields[0].text="camera.zoom: "+initialValues[0];
textFields[1].text="camera.focus: "+Math.round(initialValues[1]);
textFields[2].text="camera.fov: "+Math.round(initialValues[2]);
textFields[3].text="camera.x: "+Math.round(initialValues[3]);
textFields[4].text="camera.y: "+Math.round(initialValues[4]);
textFields[5].text="camera.z: "+initialValues[5];
textFields[6].text="viewport: "+initialValues[6];
}
}
}
import fl.controls.Slider;
import fl.events.SliderEvent;
import flash.display.Sprite;
import flash.display.Shape;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFormat;
import org.papervision3d.core.clipping.FrustumClipping;
import org.papervision3d.materials.ColorMaterial;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.objects.primitives.Plane;
import org.papervision3d.view.BasicView;
[SWF(width="600",height="800",frameRate="40",backgroundColor="#000000")]
public class CameraSettingsDemo extends BasicView {
private var camTarget:DisplayObject3D;
private var sliderValuesMin:Array;//装载slider最小值
private var sliderValuesMax:Array;//装载slider最大值
private var menuContainer:Sprite;//装载Slider实例的容器
private var textFields:Array;//装载TextField实例
private var initialValues:Array;//装载获取值
private var numberOfSliders:uint;
private var nearPlane:Plane;
private var addCounter:uint;
private var viewportSize:int=480;
public function CameraSettingsDemo() {
super(viewportSize, viewportSize,false,true,"Free");
init();
createSliders();
startRendering();
}
private function sliderChangedListener(event:SliderEvent):void {
switch (event.target.name) {
case "0" :
camera.zoom=event.target.value;
textFields[0].text="camera.zoom: "+camera.zoom;
Slider(menuContainer.getChildByName("2")).value=camera.fov;
textFields[2].text="camera.fov: "+Math.round(camera.fov);
break;
case "1" :
camera.focus=event.target.value;
textFields[1].text="camera.focus: "+Math.round(camera.focus);
Slider(menuContainer.getChildByName("2")).value=camera.fov;
textFields[2].text="camera.fov: "+Math.round(camera.fov);
break;
case "2" :
camera.fov=event.target.value;
textFields[2].text="camera.fov: "+Math.round(camera.fov);
Slider(menuContainer.getChildByName("1")).value=camera.focus;
textFields[1].text="camera.focus: "+Math.round(camera.focus);
break;
case "3" :
camera.x=event.target.value;
textFields[3].text="camera.x: "+camera.x;
break;
case "4" :
camera.y=event.target.value;
textFields[4].text="camera.y: "+camera.y;
break;
case "5" :
camera.z=event.target.value;
textFields[5].text="camera.z: "+camera.z;
Slider(menuContainer.getChildByName("2")).value=camera.fov;
textFields[2].text="camera.fov: "+Math.round(camera.fov);
Slider(menuContainer.getChildByName("1")).value=camera.focus;
textFields[1].text="camera.focus: "+Math.round(camera.focus);
break;
case "6" :
viewport.viewportWidth=viewport.viewportHeight=viewportSize*(event.target.value)/100;
viewport.x=stage.stageWidth/2-viewport.viewportWidth/2;
viewport.y=stage.stageHeight/2-viewport.viewportHeight/2;
textFields[6].text="viewport: "+viewport.viewportWidth;
Slider(menuContainer.getChildByName("2")).value=camera.fov;
textFields[2].text="camera.fov: "+Math.round(camera.fov);
Slider(menuContainer.getChildByName("1")).value=camera.focus;
textFields[1].text="camera.focus: "+Math.round(camera.focus);
viewport.graphics.clear();
viewport.graphics.lineStyle(2, 0xffffff);
viewport.graphics.drawRect(0, 0, viewport.viewportWidth, viewport.viewportHeight);
break;
default :
break;
}
}
public function init():void {
camera.z=-201;
camera.useClipping=false;
camera.useCulling=true;
viewport.graphics.lineStyle(2, 0xffffff);
viewport.graphics.drawRect(0, 0, viewport.viewportWidth, viewport.viewportHeight);
viewport.x=stage.stageWidth/2-viewport.viewportWidth/2;
viewport.y=stage.stageHeight/2-viewport.viewportHeight/2;
renderer.clipping=new FrustumClipping(FrustumClipping.NEAR);
camTarget=new Plane(new ColorMaterial(0x0000ff),200,200);
camTarget.z=2000;
scene.addChild(camTarget);
camera.target = camTarget;
textFields=[];
sliderValuesMin=[1,1,1,-100,-100,-2500,0];
sliderValuesMax=[100,100,100,100,100,2000,100];
initialValues=[camera.zoom,camera.focus,camera.fov,camera.x,camera.y,camera.z,viewport.viewportWidth];
var plane:Plane=null;
var planeMaterial:ColorMaterial=new ColorMaterial(0xff0000);
for (var i:int=0; i<4; i++) {
plane=new Plane(planeMaterial,200,200,4,4);
plane.localRotationY=90;
plane.z=i*300;
plane.x=-300;
scene.addChild( plane);
}
for (i=0; i<4; i++) {
plane=new Plane(planeMaterial,200,200,4,4);
plane.localRotationY=-90;
plane.z=i*300;
plane.x=300;
scene.addChild( plane);
}
}
override protected function onRenderTick(event:Event = null):void {
super.onRenderTick();
}
private function createSliders():void {
menuContainer = new Sprite();
addChild(menuContainer);
var slider:Slider=null;
numberOfSliders=7;
trace(camera.x,camera.y,camera.z);
for (var i:int=0; i<numberOfSliders; i++) {
slider= new Slider();
slider.name=String(i);
//addChild(slider);
slider.x=i*100;
slider.liveDragging=true;
slider.minimum=sliderValuesMin[i];
slider.maximum=sliderValuesMax[i];
slider.value=initialValues[i];
slider.addEventListener(SliderEvent.CHANGE, sliderChangedListener);
menuContainer.addChild(slider);
textFields[i] = new TextField();
textFields[i].defaultTextFormat = new TextFormat(null,null,0xFFFFFF);
textFields[i].selectable=false;
//textFields[i].background=true;
textFields[i].height=20;
textFields[i].x=i*100;
textFields[i].y=20;
menuContainer.addChild(textFields[i]);
if (i==5) {
slider.width=200;
slider.x=0;
slider.y=40;
textFields[i].x=0;
textFields[i].y=60;
}
if (i==6) {
slider.width=100;
slider.x=300;
slider.y=40;
textFields[i].x=300;
textFields[i].y=60;
}
}
menuContainer.x=stage.stageWidth/2-menuContainer.width/2;
menuContainer.y=600;
textFields[0].text="camera.zoom: "+initialValues[0];
textFields[1].text="camera.focus: "+Math.round(initialValues[1]);
textFields[2].text="camera.fov: "+Math.round(initialValues[2]);
textFields[3].text="camera.x: "+Math.round(initialValues[3]);
textFields[4].text="camera.y: "+Math.round(initialValues[4]);
textFields[5].text="camera.z: "+initialValues[5];
textFields[6].text="viewport: "+initialValues[6];
}
}
}