Multi Camera

2つのView3DにScene3Dを共有して表示してみました。下画面のオレンジ色の円錐が、上画面のカメラの位置と向きになっています。Away3Dのバージョンは4.0 Betaを使用しました。

Get Adobe Flash Player
package
{
	import flash.display.*;
	import flash.events.Event;
	import flash.geom.Vector3D;
	import flash.utils.getTimer;
	import away3d.cameras.lenses.PerspectiveLens;
	import away3d.containers.ObjectContainer3D;
	import away3d.containers.View3D;
	import away3d.debug.AwayStats;
	import away3d.entities.Mesh;
	import away3d.lights.DirectionalLight;
	import away3d.materials.ColorMaterial;
	import away3d.materials.lightpickers.StaticLightPicker;
	import away3d.materials.methods.FresnelEnvMapMethod;
	import away3d.primitives.CubeGeometry;
	import away3d.primitives.CylinderGeometry;
	import away3d.primitives.SkyBox;
	import away3d.textures.BitmapCubeTexture;

	[SWF(width = "480", height = "550", frameRate = "60")]
	public class MultiCamera extends Sprite
	{
		[Embed(source = "MultiCameraPX.png")]
		private var CubeMapPX:Class;
		[Embed(source = "MultiCameraPY.png")]
		private var CubeMapPY:Class;
		[Embed(source = "MultiCameraPZ.png")]
		private var CubeMapPZ:Class;
		[Embed(source = "MultiCameraNX.png")]
		private var CubeMapNX:Class;
		[Embed(source = "MultiCameraNY.png")]
		private var CubeMapNY:Class;
		[Embed(source = "MultiCameraNZ.png")]
		private var CubeMapNZ:Class;

		private var view1:View3D;
		private var view2:View3D;
		private var cube:Mesh;
		private var cameraPosition:Mesh;

		public function MultiCamera()
		{
			if (stage) addedToStageHanlder();
			else addEventListener(Event.ADDED_TO_STAGE, addedToStageHanlder);
		}

		private function addedToStageHanlder(event:Event = null):void
		{
			view1 = new View3D();
			view1.antiAlias = 4;
			view1.camera.lens = new PerspectiveLens(90);
			view1.height = 270;

			view2 = new View3D(view1.scene);
			view2.antiAlias = 4;
			view2.camera.lens = new PerspectiveLens(90);
			view2.height = 270;
			view2.y = 280;
			view2.camera.lens.far= 10000;
			view2.camera.z = -1500;
			view2.camera.y = 1000;
			view2.camera.lookAt(new Vector3D());

			addChild(view1);
			addChild(view2);

			var cubeMap:BitmapCubeTexture = new BitmapCubeTexture(
				new CubeMapPX().bitmapData, new CubeMapNX().bitmapData,
				new CubeMapPY().bitmapData, new CubeMapNY().bitmapData,
				new CubeMapPZ().bitmapData, new CubeMapNZ().bitmapData);
			view1.scene.addChild(new SkyBox(cubeMap));

			var light:DirectionalLight = new DirectionalLight(0, -1, 0);
			light.specular = 0.25;
			light.ambient = 0.5;
			view1.scene.addChild(light);

			var lightPicker:StaticLightPicker = new StaticLightPicker([light]);

			var cubeMaterial:ColorMaterial = new ColorMaterial(0xFFFFFF);
			cubeMaterial.lightPicker = lightPicker;
			cubeMaterial.addMethod(new FresnelEnvMapMethod(cubeMap));

			cube = new Mesh(new CubeGeometry(1000, 1000, 50), cubeMaterial);
			view1.scene.addChild(cube);

			var cylinderGeometry:CylinderGeometry = new CylinderGeometry(1, 100, 300);
			cylinderGeometry.yUp = false;
			cameraPosition = new Mesh(cylinderGeometry, new ColorMaterial(0xFF7F00));
			view2.scene.addChild(cameraPosition);

			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			addChild(new AwayStats(view1));

			addEventListener(Event.ENTER_FRAME, enterFrameHandler);
		}

		private function enterFrameHandler(event:Event):void
		{
			var duration:int = getTimer();
			view1.camera.x = 1000 * Math.sin(-duration / 10000);
			view1.camera.z = 1000 * Math.cos(-duration / 10000);
			view1.camera.y = 500 * Math.sin(duration / 3000);
			view1.camera.lookAt(new Vector3D());
			cameraPosition.position = view1.camera.position;
			cameraPosition.lookAt(new Vector3D());
			cube.rotationX += 0.25;
			cube.rotationY += 0.25;
			view1.render();
			view2.render();
		}
	}
}
[トップページに戻る]