Pocket

Intro
This is a condensed version of Appendix A of my Book. It’s a pocket reference for Papervision3D. It’s presently a work in progress. Keep checking this page for weekly updates.

Chapter 1 Code Snippets & Equations

Perspective Projection

T = scale = focal length/(focal length + z)

Sinusoidal Orbit

ball.x = radius*Math.sin(myAngle)*scale; //ball orbit x
ball.y = radius*Math.cos(myAngle)*scale; //ball orbit y

Torus Parametric Equations

x=(c+a*cos(v))cos(u)
y=(c+a*cos(v))sin(u)
z=a*sin(v)

Generating Random Colors

Math.random()*0xffffff

Depth Sorting (from the Stage for CS4)

//Sorting
function sortParticles():void
{
particlesArray.sortOn(“z”, Array.DESCENDING|Array.NUMERIC);
for(var i:int = 0; i < particlesArray.length; i++)
{
addChildAt(particlesArray[i] as Sprite, i);
}
}

Depth Sorting (from a container for CS4)

//Sorting
function sortParticles():void
{
particlesArray.sort(particleZSort);
for(var i:int = 0; i < particlesArray.length; i++)
{
myDisplayObject.addChildAt(particlesArray[i] as Sprite, i);
}
}

//Sorting Function
function particleZSort(particle1:DisplayObject,particle2:DisplayObject):int
{
var zpos1:Vector3D = particle1.transform.matrix3D.position;
zpos1 = myDisplayObject.transform.matrix3D.deltaTransformVector(zpos1);
var zpos2:Vector3D = particle2.transform.matrix3D.position;
zpos2 = myDisplayObject.transform.matrix3D.deltaTransformVector(zpos2);
return zpos2.z – zpos1.z;
}

X, Y, Z Rotation

xnew = xold*cos(angleZ) – yold*sin(angleZ)
ynew = xold*sin(angleZ) + yold*cos(angleZ)
xnew = xold*cos(angleY) – zold*sin(angleY)
znew = xold*sin(angleY) + zold*cos(angleY)
ynew = yold*cos(angleX) – zold*sin(angleX)
znew = yold*sin(angleX) + zold*cos(angleX)

Radian Degree Conversion

Radians = Degrees*PI/180
Degrees = Radians*180/PI

Spherical Parametric Equations (generating a grid)

plane.x = radius*Math.sin(i*pStep)*Math.sin(j*tStep);
plane.z = -radius*Math.sin(i*pStep)*Math.cos(j*tStep);
plane.y =-radius*Math.cos(i*pStep);

Chapter 2 Code Snippets & Equations

Downloading Papervision3D

Here are the steps you need to follow to get the Papervision3D classes:

Chapter 3 Code Snippets & Equations

I. Available Prims in Papervision

Available Prims in Papervision

Available Prims in Papervision

Rezzing pimitive is simple; in almost every case accept for the cube, you only have to replace the bracketed statements in the template code below with the appropriate commands for the prim being rezzed.

Primitive Template Code


package
{
//Flash imports
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
//import Papervision3D
import org.Papervision3D3d.materials.WireframeMaterial;

1. [Your Primitive Import Statement Goes Here]

import org.Papervision3D3d.view.BasicView;

//Create WirePlane class
public class WirePlane extends BasicView
{
//Instantiate plane and wireframe material

2. [Your Primitive Name Type Declaration Goes Here]

private var planeMaterial:WireframeMaterial;

public function WirePlane()
{
//Call super
super(1, 1, true, false);
initPV3D();
startRendering();
}
//Initiate Papaervision
protected function initPV3D():void
{
//Set Stage
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
//Set the background to black
opaqueBackground = 0;
// Create a wireframe material for your primitve
planeMaterial = new WireframeMaterial(0xFFFFFF);
planeMaterial.doubleSided=true;
//Add your primitive method

3. [Your Primitive Method Goes Here]

// Add Your Primitive to the Scene
4. [Your AddChild Statement Goes Here]

}
// override onRenderTick so you can execute code
override protected function onRenderTick(event:Event=null):void
{
//Apply an action to your primitive
5. [Your Action Statement Goes Here]
//Call the super.onRenderTick function
super.onRenderTick(event);
}
The code snippets for rezzing the available primitives in Papervision are as follows;

Plane

Using the template code above you can rez a rotating plane to the stage by replacing the bracketed statements with the appropriately numbered code below:

1. Include the Import the Papervision3D Plane class
import org.papervision3d.objects.primitives.Plane;
2. Declare variable name plane and data type it Plane:
private var plane:Plane;
3. Instantiate a Plane using the variable name plane and add the material, height, width, horizontal and vertical segments to the Plane method.
plane = new Plane( material, width, height, segmentsW, segmentsH)
4. Add your plane to the scene using the addChild command.
scene.addChild(plane)
5. Start your plane rotating using the yaw command.
plane.yaw(2);

Cube

1. Import the Papervision Cube class and the MaterialsList class.
import org.papervision3d.objects.primitives.Cube;
import org.papervision3d.materials.utils.MaterialsList;
2. Declare the cube variable name and data type it Cube, cubeMaterial name and data type it Wireframe, and myMaterialList name and data type it MaterialsList.
private var cube:Cube;
private var cubeMaterial:WireframeMaterial;
private var myMaterialsList:MaterialsList;

Added Step (difference between cube and other primitives)
Apply materials to your cube by placing your materials into a MaterialsList Object. You set up the cube myMaterialsLists by replacing the planeMaterial = new WireframeMaterial(0xFFFFFF) statement in the template code with the following:

cubeMaterial = new WireframeMaterial(0xFFFFFF);

var myMaterialsList:MaterialsList = new MaterialsList();
myMaterialsList.addMaterial( wireframeMaterial, “front” );
myMaterialsList.addMaterial( wireframeMaterial, “back” );
myMaterialsList.addMaterial( wireframeMaterial, “left” );
myMaterialsList.addMaterial( wireframeMaterial, “right” );
myMaterialsList.addMaterial( wireframeMaterial, “top” );
myMaterialsList.addMaterial( wireframeMaterial, “bottom” );

3. Instantiate a Cube using the variable name cube and add the myMaterialList, height, width, horizontal and vertical segments to the Cube method.
cube = new Cube(materials, width, depth, height, segmentsS, segmentsT, segmentsH, insideFaces, excludeFaces)
4. Add your cube to the scene using the addChild command.
scene.addChild(cube);
5. Start your cube rotating using the yaw command.
cube.yaw(2);

Sphere

1. Import the Papervision3D Sphere class.
import org.papervision3d.objects.primitives.Sphere;
2. Declare the sphere variable name and data type it Sphere.
private var sphere:Sphere;
3. Set the sphere parameters and the material. Instantiate the Sphere class as sphere.
sphere = new Sphere(material, radius, wSegments , hSegments);
4. Add your sphere to the scene using the addChild command.
scene.addChild(sphere);
5. Start your sphere rotating using the yaw command.
sphere.yaw(2);

Cylinder

1. Import the Papervision Cylinder class
import org.papervision3d.objects.primitives.Cylinder;
2. Declare the cylinder variable name and data type it Cylinder ;
private var cylinder:Cylinder;
3. Instantiate a Cylinder using the variable name cylinder and add the material, radius, height, horizontal and vertical segments and optional top radius.
cylinder = new Cylinder(wireframeMaterial, radius, height, wSegments, hSegments, topRadius);
4. Then add the cylinder to the scene using the addChild command.
scene.addChild(cylinder);
5. Start your cylinder rotating using the yaw command.
cylinder.yaw(2);

Cone

1. Import the Papervision3D Cone class.
import org.papervision3d.objects.primitives.Cone;
2. Create the cone variable and data type it Cone.
private var cone:Cone;
3. Instantiate a Cone using the variable name cone and add the material, radius, height, horizontal and vertical segments.
cone = new Cone(coneMaterial, 140,400, 10, 10);
4. Then add the cone to the scene using the addChild command.
scene.addChild(cone);
5. Start your cone rotating using the yaw command.
cone.yaw(2);

Switch Case

switch (number) {
case 1:
trace ( ” case 1 tested true ” );
break;
case 2:
trace ( ” case 2 tested true ” );
break;
case 3:
trace ( ” case 3 tested true ” );
break;
default:
trace ( ” no case tested true ” )
}
Timer Class

package {
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.display.Sprite;

public class TimerExample extends Sprite {

public function TimerExample() {
var myTimer:Timer = new Timer(1000, 2);
myTimer.addEventListener(“timer”, timerHandler);
myTimer.start();
}

public function timerHandler(event:TimerEvent):void {
trace(“timerHandler: ” + event);
}
}
}

Switch Case Parametric Equations

switch (number) {
case 0:
//Plane
paraVec =  new Vector3D(-.5*(2-ix), .5*(iy),0);
break;
case 1:
//Cylinder
paraVec = new Vector3D(Math.cos(ix), iy, Math.sin(ix));
break;
case 2:
//Cone
paraVec = new Vector3D((iy)*Math.cos(ix), Math.sin(iy), (iy)*Math.sin(ix));
break;
case 3:
//Sphere
paraVec =  new Vector3D(Math.cos(iy)*Math.cos(ix), Math.sin(iy), Math.cos(iy)*Math.sin(ix));
break;
case 4:
//Torus
paraVec = new Vector3D((1+.4*Math.cos(iy))*Math.cos(ix), .4*Math.sin(iy), (1+.4*Math.cos(iy))*Math.sin(ix));
break;
default:
trace ( ” no case tested true ” )
}

Chapter 4 Code Snippets & Equations


Comments are closed.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: