3D Vector Drawing and Text with Papervision3D: Part 2

by Jeff Winder Paul Tondeur | August 2009 | Open Source

In the previous part of the article by Jeff Winder and Paul Tondeur, we saw how to create a template class for the 3D text examples and add interactivity to 3D vector text and shapes. We also discussed about drawing vector shapes such as lines, circles, and rectangles. In this part, we will see how to draw lines with Lines3D and add interactivity to Lines3D lines.

Drawing lines with Lines3D

Before VectorVision was integrated, Papervision3D already had a Lines3D class for drawing 3D lines. Two differences between drawing lines with VectorShape3D and Lines3D are:

  • Whereas VectorShape3D enables you to easily draw rectangles, circles, and ellipses, Lines3D does not have built-in methods to do such things.
  • Lines3D creates lines with a Vertex3D as the start and end point, resulting in 3D projection of the vertices that make the line. On the other hand, VectorShape3D lets you draw a 2D shape, which you then can rotate in order to achieve a 3D perspective.

Let's take a look at how to create straight as well as curved lines with Lines3D, and how to add interactivity. The following class will serve as a template for the Lines3D examples to come:

package
{
import flash.events.Event;
import org.papervision3d.core.geom.Lines3D;
import org.papervision3d.core.geom.renderables.Line3D;
import org.papervision3d.core.geom.renderables.Vertex3D;
import org.papervision3d.materials.special.LineMaterial;
import org.papervision3d.view.BasicView;
public class Lines3DTemplate extends BasicView
{
private var lines:Lines3D;
private var easeOut:Number = 0.6;
private var reachX:Number = 0.5
private var reachY:Number = 0.5
private var reachZ:Number = 0.5;
public function Lines3DTemplate ()
{
super(stage.stageWidth,stage.stageHeight);
stage.frameRate = 40;
init();
startRendering();
}
private function init():void
{
//code to be added
}
override protected function onRenderTick(e:Event=null):void
{
var xDist:Number = mouseX - stage.stageWidth * 0.5;
var yDist:Number = mouseY - stage.stageHeight * 0.5;
camera.x += (xDist - camera.x * reachX) * easeOut;
camera.y += (yDist - camera.y * reachY) * easeOut;
camera.z += (-mouseY * 2 - camera.z ) * reachZ;
super.onRenderTick();
}
}
}

Let's first examine what happens when we draw a line using the Lines3D class.

How drawing with Lines3D works

Each line is defined by a start and an end point, both being 3D vertices. The vertices are converted into 2D space coordinates. The lineTo() method of the Flash drawing API is then used to render the line.

To create a line with the Lines3D class, we need to take these steps in the following order:

  • Create line material with the LineMaterial class. The material defines the look of the line.
  • Create a Lines3D instance, which is a do3D that will be used to store and render the lines.
  • Use the Line3D class to instantiate a line.
  • Add the line to the Lines3D instance with the addLine() method.

Equivalent to how Particle instances need to be added in Particles using theaddParticle() method, we add Line3D instances to a Lines3D instance in order to render them.

Lines3D has the following three methods to add Line3D instances:

  • addLine()
  • ad dNewLine()
  • ad dNewSegmentedLine()

Let's have a look at what they do and create some lines.

Straight lines

All t he following code should be added inside the init() method. First we create line material.

var blueMaterial:LineMaterial = new LineMaterial(0x0000FF);

You can pass two optional parameters as shown in the next table:

 

Parameter

Data type

Default value

Description

1

color

Number

0xFF0000

Defines the color of the line material using a 24 bit hexadecimal color value.

2

alpha

Number

1

Sets the transparency of the material.

Sign up for a Packt account to see the rest of this article

Now that you've read a few articles, you might want to consider signing up for a Packt account. It takes a matter of seconds, will give you access to all the articles on PacktPub.com, and once you've signed up you'll be returned here to carry on reading your article.

Furthermore, you'll gain access to nine free ebooks, and be offered a free trial of PacktLib, Packt's online library. Simply enter your details here, or log in to your existing account.

Log in

...or register

Post new comment

Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Sort A-Z