[away3D] Away3D 4 Basics - The Camera(s) ,The View and the Scene

3 篇文章 0 订阅

Original Site 1: Away3D 4 Basics - The Camera(s)

Original Site 2: Away3D 4 Basics - The View and the Scene

------------------------------------------------------------------------------------------------

Away3D 4 Basics - The Camera(s)

The camera is what you view the 3D world through. Just as a real camera, the virtual 3D camera applies perspective to your models by adjusting properties such as zoom, focus and position. In this tutorial you'll learn how to use the 3 cameras available in Away3D.

No matter what you want to do in Away3D, there's some things that you'll always need to set up. The "Basic" tutorials will explain the Scene, View, Camera, Primitives, Textures and explore some of the possibilities. Each example in the series will be organized as Actionscript Classes so they can be used in both Flash and Flex.

If you are new to 3D on computers, you might want to read our introduction that explains the core 3D concepts. This tutorial also contains the source for 6 other Away3D projects that may be worth checking out. To run the example files, you will first need to set up Away3D on your computer. When you have the Away3D source files and your software is set up, you can open, explore and export the examples in this tutorial by just placing them in your project directory.

To see your 3D content, we need two things. A Viewport (View) and a Scene. This tutorial explains how to use these.

The View

The name of the View class in Away3D is View3D. To create a new View, we use this code:

var cam:Camera3D = new Camera3D();
myView.camera = cam;
Camera3D is a very basic, free moving camera. You can move and point it in any direction.

movie: Camera3D_v4.as

Use the keys A, D, W, S and arrow up/down to navigate the 3D space. In this movie, we use a set of standard movement methods available to all objects in the engine such as:

camera.moveUp(10);
camera.moveDown(10);
camera.moveLeft(10);
camera.moveRight(10);
camera.moveForward(10);
camera.moveBackward(10);

These functions are pretty self explanatory. Note that a new Camera3D is by default positioned in the centre of the 3D world (0,0,0), meaning that if you add a Sphere you will not see anything since the camera is actually inside it. By default, a sphere is only viewable from the outside, so to see the sphere, we either have to invert the sphere ( sphere.invertFaces(); ) or move the camera outside the sphere by setting the cameras Z property like this:

camera.z = -1000;
Here we pull the camera back one thousand units, towards us. Setting the x/y/z properties of the camera will make it move, but remember that any camera must also point to a certain location. By default, the camera will look at the center coordinates of our 3D Scene. To point the camera somewhere else, we use the "lookAt" command:
camera.lookAt( new Vector3D(x,y,z) );
This tells the camera to look at the coordinate x/y/z in our virtual 3D world. You can also make the camera Pan, Tilt and rotate along any of the three axes.

A word of warning about setting the x/y/z properties - make sure you always set the camera position before asking it to "lookAt" something. If you first "lookAt" something and then change the position, the camera will still be looking in the direction set initially. To solve this, always update your "lookAt" reference after positioning the camera or use one of the other cameras that make targeting objects much easier.

The camera will only look at the object when the command is executed. To make the camera continously follow a target, use the LookAtController (explained later).

You can also rotate the camera around it's different axes using pitch, yaw and roll.  If you hold your hand in front of you, pointing straight forward and pretend it's the camera, you can visualize what the following properties do:

 cam.pitch(15); // lift hand up/down<br>
	 cam.yaw(10); // point hand to left/right<br>
	 cam.roll(5); // rotate hand along the axis of your arm (called "banking" in aviation)
While workable, these are usually better controlled using a Camera controller. 

Camera controllers

By itself, a Camera3D instance is quite clumsy to control. You can move it around and roll it along all 3 axes, but most often you'll want to add one of the available Camera controllers. The controller will take your camera as a parameter and then you'll use this class to control your camera instance.

LookAtController

LookAtController has all the properties of Camera3D, but it has a special ability to "target" other objects or positions in the 3D world:


movie: LookAtController_v4.as

Use the navigation keys as for the above movie. Using a LookAtController, the methods for moving the camera now get a new meaning. Here the camera is always looking at the target object, so camera.moveLeft actually rotates the camera around the targeted object.

By default, this camera is looking at the center of the coordinate system, but we can change this to any other position by changing the target:

camera.target = sphere;
To see how the targeting works, just click any of the primitives in the example above to toggle and then use the ASDW keys to rotate. Note that to listen for clicks on any 3D object, you now have to specifically turn ON the mouseEnabled-property. There are also different precision levels available for those that need to tweak the speed:
coneMesh.mouseHitMethod = MouseHitMethod.BOUNDS_ONLY;
coneMesh.mouseEnabled = true;
HoverController

HoverController has all the properties that LookAtController has, but adds methods that are useful for circling around an object by setting the desired pan and tilt. It also hovers the camera, so that it moves softly from one position to another. You are not limited to circle around objects though, so this is maybe the most versatile camera of them all.


movie: HoverController_v4.as

Instead of using the move-methods, we now use two custom properties in the HoverCamera3D:

camController.panAngle = 0;
camController.tiltAngle = 0;

These are the angles from 0 to 360 that the camera will Pan and Tilt to. Note that tiltAngle has a bug in the current Beta version. This will be fixed before the official release, but instead of setting this as a property, you should pass it in the contructor when you create the HoverController like this:

camController = new HoverController( cam, sphereMesh, 180,0 );

You can then change it in your main render-method as long as that isn't called until after a frame has played (EnterFrame).When you set the panAngle and tiltAngle values, the camera won't move there right away. What is special with the HoverCam is that it will do a tween of the position over a predefined number of "steps":

camController.steps = 16;
By increasing this from the default 8 steps, we'll get a slower and smoother transition from one angle to another. You can also reduce this number, all the way down to zero steps. This will move the camera instantly to it's new position. Note in Away3D version 4, you do no longer have to tell the HoverCamera3D to actually "hover". It will do so automatically, so you don't use camController.update() unless you've specifically turned off the autoupdate property.

With the Hovercontroller, you can also use the distance-property as a way to zoom:

cam.distance = 1000;
The above line will move the object towards or away from it's current position, along the line it's currently facing. In other words, there's no zoom in Away3D v4, but you set the distance to the target manually. 

Lenses

By default, this camera will get a perspective lens, but you can also pass in a different kind of lens like this:

var lens:Camera3D = new OrthographicLens();
var cam:Camera3D = new Camera3D( lens );
myView.camera = cam;
So what do these lenses do then? Have a look at the image below to see how three of the lenses render a rectangular cube.

The PerspectiveLens is the default as it is what the average user will use the most often. The OrtographicLens is a special kind of projection that removes the perspective information. This visual effect is most often used for games. The third lens, the FreeMatrixLens, exposes a full projection matrix so that those that know their math can work directly with the matrix.

If you have a SLR camera or even a simple camera that has a proper lens, there's a few things that you may expect in a camera such as zoom. In former versions of the Away3D engine, there was a zoom-property on the camera. In Away3D version 4 the dedicated zoom property is gone but you can achieve the same effect by just changing the distance from the object along the Z axis. Combine this with the fieldOfView (FOV) setting and you have a very flexible camera setup.


movie: Camera_v4.as

All these properties are all considered when rendering the View. The process of figuring out what triangles are visible though the View/Camera is called Culling. Calculating how something should look in 3D requires a lot of CPU and culling make sure only what is visible to the Camera is calculated.

Orbiting your scene using the Mouse

One of the most classic ways to navigate a 3D scene is to just move the camera back from the center of the scene and let the user rotate around it using the mouse. The example below is much more elaborate than what we have done this far in these tutorials so consider it as a taste of what's to come.
movie: basic_cube_v4.as

I'll won't explain explain everything in this code as I'll cover that in the upcoming tutorials. However - feel free to download and tweak it as much as you like. This is just one of many possible solutions to rotating around an object and there's many ways you can improve on this. How about adding scrollwheel support for zooming (camera.zoom)? We'll get back to camera movement in many of the remaining tutorials, so just keep reading to learn more.

Note: You can also find another camera in the "away3d.cameras" package/folder called SpringCam. This is temporary and it will later be converted to a camera controller. It's made for chase-scenarios where your camera will chase after an object.

--------------------------------------------------------------------------------------------------------------------------

Away3D 4 Basics - The View and the Scene

When you look out through a window in your house, you only see what is visible from your point of view, not what is occluded by the walls. The viewable area is decided by the size and shape of the window, so you'll see a "cropped" view of what's outside. This is exactly how the View works in Away3D as well. It is the "window" that we view our 3D world through.

No matter what you want to do in Away3D, there's some things that you'll always need to set up. The "Basic" tutorials will explain the Scene, View, Camera, Primitives, Textures and explore some of the possibilities. Each example in the series will be organized as Actionscript Classes so they can be used in both Flash and Flex.

If you are new to 3D on computers, you might want to read our introduction that explains the core 3D concepts. This tutorial also contains the source for 6 other Away3D projects that may be worth checking out. To run the example files, you will first need to set up Away3D on your computer. When you have the Away3D source files and your software is set up, you can open, explore and export the examples in this tutorial by just placing them in your project directory.

To see your 3D content, we need two things. A Viewport (View) and a Scene. This tutorial explains how to use these.

The View

The name of the View class in Away3D is View3D. To create a new View, we use this code:

var view:View3D = new View3D();
addChild(view)
Note that the view is added to the stage like any other display object in Flash. This does however not mean that it acts as a DisplayObject. Since Away3D from version 4 and up are using Stage3D, all DisplayObjects in Flash will layer on top of it. In other words - you cannot place any graphics behind your 3D scene. This is usually not a problem though and if it's critical, you can just add that object to the 3D scene and stack it there.

By default, the View is not cropped in any way. It fills the entire Flash movie, so the size of your Flash movie is effectively the size of the View. You can adjust the size of the view by setting the x, y, width and height properties like this:

var view:View3D = new View3D();
view.x = 100;
view.y = 100;
view.width = 200;
view.height = 200;
view.backgroundColor = 0x336600;
You can also set the background color (as seen above) as well as a background image (view.background) and the alpha (view.backgroundAlpha).

Caution: setting the z position or rotation or scale along X/Y/Z has no effect even though modern tools like Flash Builder, FlashDevelop and IntelliJ will tell you that these exist. The same goes for many other methods inherited from the Sprite object such as addChild (that will actually crash your movie is you use it), so stick to the properties mentioned above.

By using the position and size properties on multiple Views, we can view the same scene from multiple angles. In the example below, we have four View3D's looking at the same Scene:


movie: MultipleViews_v4.as MultipleViewsView_v4.as

Every view comes with a default Scene and Camera. You can change / remove the defaults by setting the view.camera or view.scene properties, just as we do in the example above.

Note that nothing will be displayed in the View until you tell it to update using the render() method:

view.render();
You will typically call this command from within an EnterFrame or Timer handler. Be careful though. If you set your movie to update too often (too short Timer interval or too high Framerate) the result will actually be reduced performance/speed.  With the transition to Stage3D, there is less of a limit on how often you can call the render-command, especially for simple things like these tutorials. However, if you are building complex scenes with many thousand polygons, you should look carefully at how many frames per second your movie is able to achieve - especially on older machines.

To save some typing and follow general Object Oriented practice, we've created the View as a separate class in the example above. The Scene is created in the main class so we can pass it as a parameter to the four View's. This way, they can all show the same scene, but from different angles just as a 3D modeling software would. To get more View's, we just create a new  MultipleViewsView_v4.as instance.

Antialiasing

A brand new setting in version 4 is antiAliasing:

view.antiAlias = 4;
If you don't specify an antiAlias setting, the default setting of Zero will be used. You have probably already seen examples of Stage3D that looked "blocky", so do remember to set this. Values of 0, 2, 4 and 16 are valid settings. You can see the difference between 0 and 4 in the picture below (2x zoom on a rotated cube).

In general, a higher setting will require more from the graphics card but from what we've tested, it's little to none difference for normal scenes when using a setting of 4. For Stage3D on mobile, antaliasing is always off no matter what you set the property to.

The Scene

The Scene works just like the Stage in Flash. Anything on the Scene can be displayed to the user and manipulated. Before you can use the scene, you must create it. You can do this two ways. The simplest is just to use the Default scene that comes with every view.

var view:View3D = new View3D();
This will create a new View with a Scene. You can access the scene using "view.scene". You can also create a scene and then pass that Scene to the view.
var myScene:Scene3D = new Scene3D;
var view:View3D = new View3D();
view.scene =  myScene;
This is what we did in the example above showing multiple views of the same Scene3D, allowing multiple views to use the same Scene. This also makes for little less typing since you can reference the scene using the "myScene" variable directly rather than "view.scene".

Once you have your Scene in place, you'll want to add content to it. 3D objects in Away3D are either attached to the Scene or an ObjectContainer3D that is already/later put on the Scene. ObjectContainer3D allows you to group several objects together so you can move them together as well as separately.

To place an object on the Scene, we must first create it. Let's create a Sphere with a radius of 100:

var mySphere:SphereGeometry = new SphereGeometry( 100 );

Just as with DisplayObject's in Flash, 3D objects can be created without being added to the DisplayList. To make our Sphere visible, we have to add it to the stage using the addChild method. If we used the Default view, we'll do it this way:

view.scene.addChild( mySphere );

If we want to add this to the custom scene we created earlier, we do it this way:

myScene.addChild( mySphere );
If you want to remove an Object from the Scene, you use the removeChild method as you would for MovieClips or other DisplayObjects in Flash:
myScene.removeChild(mySphere);
While the Scene3D object has the addChild and removeChild methods, it's not a DisplayObject. This means that you cannot move, rotate or scale it as you could in Away3D 3. You can also listen for changes to the scene and mouse clicks using EventListeners. 

This is just about all you need to know about the Scene since it's so simple in use. Just treat the Scene as you would with the Stage in Flash and you are ready to go. Next up - Cameras in Away3D.


磁共振成像(MRI)是一种非侵入性的医学影像技术,通过使用强磁场和无害的无线电波,生成高分辨率的人体内部图像。MRI的基本原理包括磁共振现象和信号处理。 首先,MRI利用强磁场产生静态磁场。这个强磁场使得人体内的原子(通常是氢)的原子核在磁场中定向,使其自旋沿磁场方向预先排列。 其次,MRI利用无线电频率的脉冲来激发人体内的原子核,使其从平衡状态中倾斜。 然后,MRI探测激发后原子核的归位,这个过程称为回波信号。原子核的归位过程会产生微弱的无线电信号。 接下来,MRI系统采集这些回波信号并进行信号处理。信号处理包括一个复杂的计算过程,其基本目标是确定回波信号的来源和特征。通过这种方式,系统可以构建数百个不同方向上的切片图像。 最后,计算机将切片图像组合在一起,形成三维结构,可以帮助医生观察和分析人体内部组织和器官的详细结构。 MRI是一种非常有用的影像技术,因为它能够提供清晰的高对比度图像,并且不涉及使用有害的X射线。它在医学诊断中广泛应用,特别是用于观察脑部、骨骼、关节、脊椎和内脏器官等结构。同时,MRI还可以用来检测肿瘤、损伤、疾病和其他异常,并且具有较高的准确性和灵敏度。 总之,MRI是一种基于磁共振原理的医学影像技术,可生成高分辨率、非侵入性的人体内部图像,对于医学诊断和研究具有重要意义。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值