现在的位置: 首页 > 综合 > 正文

《Essentiall Guide》读书笔记 【2】【第3章】

2012年10月23日 ⁄ 综合 ⁄ 共 4861字 ⁄ 字号 评论关闭

1,留心flash原生对象的z属性

  书上的话“From Flash 10 onward, native display objects have positions based in a 3D coordinate system but are missing automatic Z sorting. This means display objects placed far away on the Z axis won’t necessarily be drawn behind those nearby, occasionally
resulting in unnatural overlapping. Away3D automatically executes Z sorting on objects in a scene, ensuring that all 3D objects are rendered to the view correctly.”

  flash渲染画面时,不会根据displayObject的z属性决定谁覆盖谁,而是依靠具备flash特色的“深度索引”。fp10新增的z属性,大概是向第三方3D引擎示好。

2,HoverCamera3D

  “Hover”这个词很生动,这确实是一个盘旋视角的相机。我做了个demo,创建一架HoverCamera3D相机,让他以10度俯角绕着绿球转。由于相机本身是不可见的,我偷了个巧:让一个立方体实时刷新到相机的位置。真正用于观察的,仍是处于(0,,0,-1000)处的_view.camera(默认相机)。看代码:

package  
{
	import away3d.cameras.HoverCamera3D;
	import away3d.cameras.TargetCamera3D;
	import away3d.containers.View3D;
	import away3d.core.base.Object3D;
	import away3d.core.base.Segment;
	import away3d.core.base.Vertex;
	import away3d.core.math.Number3D;
	import away3d.materials.WireColorMaterial;
	import away3d.materials.WireframeMaterial;
	import away3d.primitives.Cube;
	import away3d.primitives.LineSegment;
	import away3d.primitives.Sphere;
	import flash.display.Sprite;
	import flash.events.Event;
	
	/**
	 * ...
	 * @author wws
	 */
	[SWF(width='550',height='400',backgroundColor='0x000000')]
	public class TestCamera extends Sprite 
	{
		private var _view:View3D;
		private var _camera:HoverCamera3D;
		private var _cube:Cube;
		private var _sphere:Sphere;
		private var _segment1:LineSegment;
		public function TestCamera() 
		{
			//在(0,,0,1000)处创建一个绿色球
			_sphere = new Sphere( { x:0, y:0, z:1000, radius:20, segmentsH:5, segmentsW:10, material:new WireColorMaterial(0x00cc00) } );
			//新建小立方体,用来实时跟随相机位置
			_cube = new Cube( { width:20, height:20, depth:20, segmentsH:2, segmentsW:2, segmentsD:2, material:new WireColorMaterial(0xcccc00) } );
                        _camera = new HoverCamera3D( { distance:100, target:_sphere, panAngle:0, tiltAngle:10, steps:0 } );
			_segment1 = new LineSegment();
			_segment1.start = new Vertex(_sphere.x, _sphere.y, _sphere.z);
			_segment1.material = new WireframeMaterial(0xffffff);
			
			_view = new View3D( { x:275, y:200 } );
			_view.camera.z = 0;
			_view.camera.y = 600;//观察点位于(0,600,0)处
			_view.camera.lookAt(_sphere.position);
			
			stage.addChild(_view);
			_view.scene.addChildren(_cube, _sphere,_segment1);
			
			stage.addEventListener(Event.ENTER_FRAME, render);
			
		}
		
		private function render(e:Event = null):void {
			_camera.panAngle += 1;
			_camera.hover();
			//_segment1的末端始终连着立方体。
			_segment1.end.setValue(_camera.position.x, _camera.position.y, _camera.position.z);
			//设置立方体处于_camera位置。
			_cube.position = _camera.position;
			_cube.lookAt(_sphere.position);
			_view.render();
		}
		
	}

}

效果图:

HoverCamera3D相机就这样以一定俯角(或仰角),绕着物体旋转拍摄。

3,旋转相机

  原文说的极生动:“the visual effect of rotating a camera appears different to rotating an object, because the camera is acting as your point of view. This means that rotating around the X axis results in a movement similar to nodding your head, rotating around
the Y axis is similar to shaking your head to say “no”, and rotating around the Z axis is similar to leaning your head left or right, as you might do if you had water in your ear!”。

4,camera.zoom

看一个逐渐调整zoom大小的例子

TestCameraZoom.as[Set Document Class]

package  
{
	import away3d.containers.View3D;
	import away3d.materials.Material;
	import away3d.materials.WireColorMaterial;
	import away3d.primitives.Cube;
	import flash.display.Sprite;
	import flash.events.Event;
	
	/**
	 * ...
	 * @author wws
	 */
	[SWF(width='550',height='400',backgroundColor='0')]
	public class TestCameraZoom extends Sprite 
	{
		private var _cube1:Cube, _cube2:Cube, _cube3:Cube;
		private var _view:View3D;
		public function TestCameraZoom() 
		{
			var mat:Material = new WireColorMaterial(0xcc0000);
			//在z轴+1000单位附近放置3个红色立方体
			_cube1 = new Cube({material:mat,x:0,y:0,z:1000,width:100,height:100,depth:100});
			_cube2 = new Cube({material:mat,x:-100,y:50,z:1000,width:50,height:50,depth:50});
			_cube3 = new Cube( { material:mat,x:100, y: -50, z:1000, width:30, height:30, depth:30 } );
			_view = new View3D( { x:275, y:200 } );
			//调整摄像机位置,从斜上方观察这几个立方体,
			with (_view.camera) {
				x = 400;
				y = 800;
				z = 800;
			}
			_view.camera.lookAt(_cube1.position);
			_view.scene.addChildren(_cube1, _cube2, _cube3);
			stage.addChild(_view);
			stage.addEventListener(Event.ENTER_FRAME, render);
			trace(_view.camera.zoom);//默认是10
		}
		
		private function render(e:Event = null):void {
			_view.render();
			_view.camera.zoom -= 0.1//微调焦
		}
		
	}

}

效果图:

  zoom应当翻译成“变焦”,还是翻译成“缩放”,还是都可以....官方文档上解释为“Provides an overall scale value to the view ”。单从上面的效果图看起来,zoom减小,似对应着摄像机逐渐远离场景。

5,camera.focus

  文档解释:A divisor value for the perspective depth of the view.

  书上说的很清楚:the value of focus does not relate to the focus setting in a real life camera but represents the distance between the camera position and the viewing plane. In 3D graphics, a viewing plane is an invisible surface in space that is used as the basis
for projecting the scene to the view. If you imagine the computer screen as your viewing plane, the focus property adjusts the theoretical distance between the surface of this plane and your camera’s position in front of the plane. A small focus value results
in a camera very close to the viewing plane, with an extremely wide angle of view.
  我想起来在《3D游戏编程大师技巧》上看到的一幅图:

  这幅图的其它部分不用管,单看红线圈出来的部分,focus应该就是控制View Distance。

  focus越小,视野越开阔,物体显得小些;focus越大,视野越狭窄,物体显得大些。

6,view默认的camera并不在默认位置

  view默认的camera在z轴-1000处。

  我们new出来的camera默认在z轴0处。

  别忽视了这一点。有时候,我们使用自定义camera能看到的东西,换成view默认的camera就看不到了,这是因为观察点后退了1000,对象变得很小了。

抱歉!评论已关闭.