Posts Tagged ‘3d’

Working in 3D with Flash Player 10/ActionScript 3 101

December 30, 2008

1. The coordinate system in Flash Player is right handed. For angles in ActionScript, when dealing with DisplayObject properties such as rotationX, use degrees; when dealing with maths such as Math.atan, Math.cos etc., use radians.

2. Gumbo SDK and Flex SDK 3.2 have slightly different implementation on coordinates and layout. A UIComponent with the z depth appears much closer in Flash Player if complied with sdk 3.2 than if complied with Gumbo.

3. When Flash Player applies 3D, “the DisplayObject is drawn into a bitmap, then a vector rectangle (1 quad, not 2 triangles) is created with the dimensions of that bitmap, using that bitmap as a fill. The quad edges are then transformed into 3d world space and projected into 2d stage/global space.” (here, see comment #11.)

4. The Matrix3D and PerspectiveProjection class are used to manipulate visaul classes.

DisplayObject.perspectiveProjection.projectionCenter, which is the type of flash.geom.Point, determines the displayObject’s projection center. If you are setting root (which is  [_Main_mx_managers_SystemManager]) object’s projection center, be aware that  (a) root may be be available when the display list of the component is validated; (b) DisplayObject.transform.perspectiveProjection returns null unless you explicitly instantiate and assign a new PerspectiveProjection object to it, like so:

var pp:PerspectiveProjection = new PerspectiveProjection();
pp.projectionCenter = new Point(width/2, height/2);
contentPane.transform.perspectiveProjection = pp;//this prevents the mouse events from emitting from the contentPane

UICompnent(contentPane).parent.transform.perspectiveProjection = pp;//this doesn’t have the mouse event masking problem.

UICompnent(contentPane).root.transform.perspectiveProjection = pp;//this doesn’t have the mouse event masking problem.

Setting the projection center to the root object will affect the 3D projection of all the displayObjects on the stage, while setting the projection center for individual displayObjects will only affect those objects themselves and their children.

AppendRotation/PrependRotation/AppendTranslation/PrependTranslation apply transformation incrementally to a DisplayObject. To apply transformation in absolute values, use .transform.Matrix3D.recompose() instead:

import flash.geom.Vector3D;
import __AS3__.vec.Vector;

var v3:Vector.<Vector3D> = new Vector.<Vector3D>(3);
//The following line is required to avoid error in Gumbo but not sdk 3.2:
//items[id].transform.matrix3D = new Matrix3D();
v3 = item.transform.matrix3D.decompose();
var ang:Number = (-v + 180 +(centralAngle/numItems)*id)*Math.PI/180;
v3[0] = new Vector3D(0, radius*Math.sin(ang), radius*Math.cos(ang));

To quickly manipulate an object in 3D, you can also use a Matrix3D object as follows:

var matrix:Matrix3D  = new Matrix3D();
matrix.appendRotation(1, Vector3D.YAXIS);
//instantiate .transform.matrix3D property if it doesn’t have one yet:
myObject.z = 0;
myObject.transform.matrix3D = matrix;

“To make something not be 3d anymore, set its .transform.matrix to a non-null value (or set .transform.matrix3D to null)”.

5. Some quirks of Flex 3 (“Moxie”):

If you apply an effect (say mx.effects.Blur) on a UIComponent that has its transform.perspectiveProjection.projectionCenter set explicitly already, FP will first remove the .tranform.Matrix3D on the component before applying the effect. This makes the target look like it’s jumping back to the origin. (Tested on SDK 3.2).

If you try to set projectionCenter (of its parent) and z properties on a UIComponent at the same time, your component won’t be able to receive any mouse event at all. This seems to be fixed on Gumbo.

mx.effects.Move seems to have its own projection center set and ignores your setting for its (parent’s) projectionCenter.