标题: Alternativa3D教程之一——Hello, Alternativa3D [打印本页] 作者: cdl51 时间: 2013-12-18 09:02 标题: Alternativa3D教程之一——Hello, Alternativa3D In this tutorial we will create simple 3d-scene to understand scene building basics in Alternativa3D, step by step.
在这个向导中,我们通过建立简单的3D立方体来一步步了解基于Alternativa3D的场景构建。
So, let’s create new ActionScript Project in Flex Builder (or in other software you’re using), and name it, say, HelloAlternativa3D. Then connect all SWC-libraries from Alternativa3D package.
那么,我们在Flex Builder中新建一个ActionScript项目(或者你正在使用的其他软件),命名为HelloAlternativa3D,然后把Alternativa3D包中所有的SWC库都建立连接。
Our project will have only one file with following code inside:
我们的项目只包含一个文件,里面的代码如下:
HelloAlternativa3D.as
We’ll look through the code later, now just try to copy it to your project, compile and run it.
稍后我们浏览一下代码,现在试着拷贝代码到你的项目中,编译、运行它。
You’ll see this (it is a link to SWF here):
You can control camera using “arrows” and WASD, Space and Shift. Mousewheel controls camera field of view (FOV).
你可以通过鼠标和按键“WASD、Space和Shift”来控制摄像机,鼠标滚轮控制摄像机的视野。
Creating scene——创建场景
Let’s review code line by line. At first we create scene and put cube primitive to it:
我们一行行的温习一下代码。首先我们建立了场景并且把一个原始的立方体加入其中:
// Create scene object. Scene is a container which have everything else inside.
//建立场景对象,它是里面一切对象的容器。
scene = new Scene3D();
// Set root object for scene hierarchy. Root object coordinate system becomes scene global coordinate system.
//为阶层式场景设置根对象,根对象的坐标系会成为场景全局坐标系。
scene.root = new Object3D();
// Create cube primitive——建立原始立方体。
box = new Box(100, 100, 100, 3, 3, 3);
// Set material which draws primitive edges——利用原始的边缘绘制材质。
Adding camera——添加摄像机
Scene is ready, so we want to see result. We have to put camera on the scene and connect it to view:
场景已经准备好了,我们想看一下结果。我们必须把摄像机放入场景中并且与视窗建立连接:
// Create camera instance and set it's coordinates
//建立摄像机实例并设置坐标系。
camera = new Camera3D();
camera.x = 100;
camera.y = -150;
camera.z = 100;
// Add camera to the scene——向场景中添加摄像机
scene.root.addChild(camera);
// Create a view and connect it to the camera
//建立视窗并与摄像机建立连接
view = new View();
addChild(view);
view.camera = camera;
复制代码
Camera revival——摄像机运动(不知道这样翻译是否恰当)
Let’s use controller from library. It implements basic movement and rotation camera controls, with basic collision detection:
我们使用引擎库里面的控制器,它是一个基于碰撞检测的控制摄像机基本移动、旋转的工具:
// Create controller and connect camera——建立控制器并连接摄像机
cameraController = new CameraController(stage);
cameraController.camera = camera;
// Set default control keys——设置默认控制键
cameraController.setDefaultBindings();
// Turn on camera collision detection——打开摄像机碰撞检测
cameraController.checkCollisions = true;
// Set collision detection radius for camera——设置摄像机碰撞检测弧度
cameraController.collisionRadius = 20;
// Aim camera at the cube primitive——把摄像机定位到原始立方体上
cameraController.lookAt(box.coords);
// Activate camera controls——摄像机激活控制
cameraController.controlsEnabled = true;
复制代码
Latest touch——最新的接触
So, scene is ready, camera is set, we just have to “push the button”.
那么,场景已经就绪了,摄像机也设置好了,我们必须“按下按钮”。
public function HelloAlternativa3D() {
//...
// Initialize FPS display
FPS.init(stage);
// This function is for view size changes during player window changes
addEventListener(Event.RESIZE, onResize);
// This function enables user input processing and scene calculation every frame
// Scene calculating. Scene will be recalculated and all changes will be redrawed in camera.
scene.calculate();
}
复制代码
Conclusion——结论
So, we just got basic understanding about scene creation process, and showing it on screen. We’ll learn different aspects of Alternativa3D workflow in next tutorials.
我们刚才基本了解了场景的建立过程,而且显示在了屏幕上。在下一节向导中,我们会学习到Alternativa3D工作流中其他方面的知识。