Working on a small project in Papervision 3D I found myself in need of a way to have a 3d object that always faces the camera. That’s when I came across Paul Tondeur’s Sprite3D class ( which works great ).
After playing with that class I realized I dont need some of it’s features like loading lots of images and calculating the angle and showing the right image so it gives a 3d impression.
So I’ve made a slight change to this class to have only one image ( also saving some size )
Here’s the code:
package {
/** papervision classes **/
import org.papervision3d.core.math.Matrix3D;
import org.papervision3d.core.math.Number3D;
import org.papervision3d.core.render.data.RenderSessionData;
import org.papervision3d.materials.BitmapMaterial;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.objects.primitives.Plane;
/**
* creates a plane which is always in front of the camera.
*
* @based on Paul Tondeur's Sprite3D
*/
public class Sprite3D extends DisplayObject3D
{
private var _plane:Plane = null; // plane object
private var _material: BitmapMaterial = null; // texture
private var _firstProject:Boolean = true; // first time projected
private var toDEGREES :Number = 180/Math.PI; // degrees
/**
* Create a new Sprite3D object
*
* @param material A BitmapMaterial
* @param width Optional width of Sprite3D
* @param height Optional height of Sprite3D
*/
public function Sprite3D(material:BitmapMaterial, width:Number = 250, height:Number = 250)
{
super();
_material = material;
_plane = new Plane(_material, width, height );
addChild(_plane);
}
public override function project( parent : DisplayObject3D, renderSessionData:RenderSessionData ):Number
{
/**
* First time the sprite3d is projected, it needs to project _plane as well.
* If not, we won't see anything happen as soon as we adjust it's matrix.
* This only accurs the first time and seems like a pv3d bug */
if( this._firstProject )
{
_plane.project(this, renderSessionData);
this._firstProject = false;
}
/** Update transformation matrix, when dirty transformations are used */
if( this._transformDirty ) updateTransform();
/** Get rotation matrix of this object and calculate rotation values*/
var m:Matrix3D = this.transform;
var rotation:Number3D = new Number3D();
rotation.x = Math.atan2(-m.n23 , m.n22) * toDEGREES;
rotation.y = Math.atan2(-m.n31 , m.n33) * toDEGREES;
rotation.z = Math.atan2(-m.n12 , m.n22) * toDEGREES;
/** Correct _plane and keep it always in front of the camera */
m = new Matrix3D();
m = m.copy3x3( this.transform );
_plane.transform.calculateInverse( m );
_plane.transform = Matrix3D.multiply3x3( _plane.transform , renderSessionData.camera.transform );
return super.project(parent,renderSessionData);
}
/**
* Returns a DispayObject3D
*/
public function get object3D():DisplayObject3D
{
return this._plane;
}
/**
* Returns the material of the object
*/
public function get mat():BitmapMaterial
{
return this._material;
}
}}
Like this:
Like Loading...