上篇文章讲到了Papervision3D v2.0有一个更加方便的方法来创建一个3D场景,就是使用BasicView 类。让你的DocumentClass去继承BasicView 类,BasicView 继承自Sprite类,所以可以省掉很多麻烦。
Example002.as
下面的例子使用了和第一篇同样的代码,只不过关键的地方替换成使用BasicView来做了。
package { import flash.display.StageAlign; import flash.display.StageScaleMode; import org.papervision3d.core.geom.Lines3D; import org.papervision3d.core.geom.renderables.Line3D; import org.papervision3d.core.geom.renderables.Vertex3D; import org.papervision3d.core.proto.MaterialObject3D; import org.papervision3d.materials.WireframeMaterial; import org.papervision3d.materials.special.LineMaterial; import org.papervision3d.objects.primitives.Sphere; import org.papervision3d.view.BasicView; public class Example002 extends BasicView { private var sphere:Sphere; public function Example002() { super(0, 0, true, false); // set up the stage stage.align = StageAlign.TOP_LEFT; stage.scaleMode = StageScaleMode.NO_SCALE; // Initialise Papervision3D init3D(); // Create the 3D objects createScene(); // Start rendering the scene startRendering(); } private function init3D():void { // position the camera camera.x = -100; camera.y = -100; camera.z = -500; } private function createScene():void { // First object : a sphere // Create a new material for the sphere : simple white wireframe var sphereMaterial:MaterialObject3D = new WireframeMaterial(0xFFFFFF); // Create a new sphere object using wireframe material, radius 50 with // 10 horizontal and vertical segments var sphere:Sphere = new Sphere(sphereMaterial, 50, 10, 10); // Position the sphere (default = [0, 0, 0]) sphere.x = -100; // Second object : x-, y- and z-axis // Create a default line material and a Lines3D object (container for Line3D objects) var defaultMaterial:LineMaterial = new LineMaterial(0xFFFFFF); var axes:Lines3D = new Lines3D(defaultMaterial); // Create a different colour line material for each axis var xAxisMaterial:LineMaterial = new LineMaterial(0xFF0000); var yAxisMaterial:LineMaterial = new LineMaterial(0x00FF00); var zAxisMaterial:LineMaterial = new LineMaterial(0x0000FF); // Create a origin vertex var origin:Vertex3D = new Vertex3D(0, 0, 0); // Create a new line (length 100) for each axis using the different materials and a width of 2. var xAxis:Line3D = new Line3D(axes, xAxisMaterial, 2, origin, new Vertex3D(100, 0, 0)); var yAxis:Line3D = new Line3D(axes, yAxisMaterial, 2, origin, new Vertex3D(0, 100, 0)); var zAxis:Line3D = new Line3D(axes, zAxisMaterial, 2, origin, new Vertex3D(0, 0, 100)); // Add lines to the Lines3D container axes.addLine(xAxis); axes.addLine(yAxis); axes.addLine(zAxis); // Add the sphere and the lines to the scene scene.addChild(sphere); scene.addChild(axes); } } }
输出以后会发现,他产生的效果和第一篇中的例子是一样的,但是代码少了不少。(点击图片可以看到这个例子)
初始化并渲染
和第一篇的例子做比较,会发现初始的过程变简单了。在构造函数里就已经自己把几个PV3D必须的类实例化了,你可以直接通过camera scene 等访问到。
public function Example002() { super(0, 0, true, false);
如果你查看BasicView的源代码,会发现这4个参数是直接传到Viewport3D 的。
下一步,做一个初始化函数来设定camera的位置。
private function init3D():void { // position the camera camera.x = -100; camera.y = -100; camera.z = -500; }
接下来的代码和第一篇基本一样了,所以这里就不再重复了,唯一不同的是在构造函数中的最后一行。
// Start rendering the scene startRendering();
这里使用了一个很简单的指令来渲染场景,如果你查看startRendering在BasicView的源代码,会发现也是通过enterframe事件调用一个方法onRenderTick。这个方法是可以在我们继承的这个类中重载的,以实现一些更复杂的表现效果。
BasicView类的用法到这里就结束了结束。下一篇开始会介绍如何给你一个3D Object 添加一些简单的运动效果