Posts Tagged ‘Papervision3D’

Introducing ASBlender

Monday, January 25th, 2010

As all 3D modelers probably know: exporting models from 3D applications and use them in Flash (Papervision3D etc.) can be frustrating. Stuff like animations, matrices, etc. sometimes don’t work as expected. So I thought: why not read the native file format of a 3D app instead? That way we get access to the native data as used by a 3D app. Not some ‘twisted’ data as presented by exporters.

Don’t get me wrong: most exporters of course do their job very well, but… exporters ‘interpret’. In other words: exporters think in my place. Often exporter are ‘right’, but sometimes they go wrong (or present data in some awkward form). Then it would be cool to have control over the data yourself.

The workflow becomes very simple. We don’t have to export anymore, Flash could simply read the 3D app’s latest saved file. We can interpret the 3D app’s data as we wish. I like that ;-) . On the bad side: native files contains lots of data we probably don’t need, like the 3D app’s viewport size etc. Then again: how cool is that?

Enter ASBlender, a library I slapped together in a few days to read Blender’s .blend file format and parse it to AS3. Blender is a very cool (open source, free!) 3D application which can compete with MAX, Maya, Cinema4D etc. The Blender UI is kind of weird when you first see it, but after a – admittedly – steep learning curve, it rocks.

Blender 2.5 - Eclipse - FDT

More info, code and a wiki is available at github.

The code is very basic and can be improved for sure, so comments, criticism and suggestions are more then welcome.

Have fun!

Unproject with useProjectionMatrix = true

Wednesday, April 29th, 2009

I just updated Papervision3D to allow the CameraObject3D#unproject method to work when CameraObject3D#useProjectionMatrix = true.

Papervision3D has two methods of ‘projecting’ vectors onto the screen:

  1. useProjectionMatrix = false, this is the default ‘fast’ method
  2. useProjectionMatrix = true, this is the method where projection is done by a matrix

Note that Papervision3D switches to method #2 in ‘ortho mode’.

So what is the difference exactly between these two kinds of projection? First we need to define what projection is. Projection is what adds ‘perspective’ to a scene and makes sure your scene fits to the viewport. Its the last step in the so-called ‘render pipeline’.

We can derive the ‘fore shortening’ effect of perspective in several ways. Method #1 is simple, effective and fast. It uses the camera’s focus and zoom values. Method #2 uses a more classic way of projection : it uses a dedicated matrix. Tech buffs: very much like OpenGL’s gluPerspective.

Andy Zupko has a great post on unproject using method #1.

We will discuss unproject using method #2, which works slightly different.

The difference is that when #useProjectionMatrix is set to true, then CameraObject3D#unproject returns a point in world-space, rather then a ray. Let me explain with some code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// we want to use a projection matrix
camera.useProjectionMatrix = true;
 
// '0' indicates we want a point on the near plane
var pointOnNearPlane : Number3D = camera.unproject( screenX, screenY, 0 );
 
// '1' indicates we want a point on the far plane
var pointOnFarPlane : Number3D = camera.unproject( screenX, screenY, 1 );
 
// Construct the ray's direction
var dir : Number3D = Number3D.sub( pointOnFarPlane, pointOnNearPlane );
 
// Normalize
dir.normalize();
 
// So, now you have a ray with its origin at 'pointOnNearPlane',
// and direction 'dir'

Optimization: could save some cycles by doing (*not* in ortho mode!, see below):

1
2
3
4
5
6
7
var camPosition : Number3D = new Number3D( camera.x, camera.y, camera.z );
// Construct the ray's direction
var dir : Number3D = Number3D.sub( pointOnFarPlane, camPosition );
// Normalize
dir.normalize();
// So, now you have a ray with its origin at the camera's position
// and direction 'dir'

Why can’t we use above optimization in ortho mode?
In perspective mode all ‘rays’ originate from the camera’s position: the ‘view cone’ has shape of a pyramid. Hence we can assume ‘pointOnNearPlane’ to coincide with the camera’s position.
In ortho mode however the ‘view cone’ has the shape of a cube, all rays are parallel. Hence we need to unproject *two* points to construct our ray.

Now simply follow Andy Zupko ’s post to drag objects around.
Or perform ‘picking’, or…

Update:
Created a little demo to visualize what I’m ranting about, check it out here

Papervision3D forum

Tuesday, January 20th, 2009

On request of many: Papervision3D now has a forum : http://forum.papervision3d.org/

Read more at the Papervision3D blog.