Li : Dev Actionscript Development Blog

9Jul/09Off

Importing graphics from Flash to Away3D

In a very similar way we used the wumedia package to extract fonts from Flash, we can use it to extract vector graphics in general from Flash. This was possible before in Away3D using VectorSwf.as, as shown on a previous post, but the work flow that I will present now is so much simpler, that we've decided to deprecate VectorSwf.as and replace it with the much more powerful and easier to use Swf.as

Click on the image to see what I'm talking about (right-click view source enabled). The snowman was designed in Flash, and a swf was exported containing this graphic and other vector assets that may want to be used in 3D. After that, the assets swf is loaded using the following code, which is the same way many other external assets are loaded with Away3D, such as Collada, 3ds, Obj, etc:

_loader = Swf.load("assets/vectorassets.swf", {libraryClips:["Snowman"],  scaling:2});

"libraryClips" is an array containing all the linkage names of the clips that want to be extracted. It is important to set these linkages in Flash in order to be able to identify the clips from the main app. The parameter is optional however, if not supplied, Swf.as will attempt to extract the graphics from the stage. For this to be possible however, the instances on the stage must be set as graphics types.

Another thing to take into consideration is the flattening of the clips. As one of our current goals is to be able to extrude these clips, Away3D treats flattened graphics differently from graphics that are grouped, or on layers, and non flattened graphics might produce sorting problems at this stage. In order to correctly flatten a vector graphic in Flash (as for instance a drawing imported from Illustrator), the way I recommend it is:

- Alternate "break apart" with "distribute to layers" until no grouped elements are left.

- Select all the resulting shapes and go to Modify -> Shape -> Convert Lines to Fills.

- Copy the content of all the produced layers and paste it into a new clip.

This simple process has ensured me so far to be able to migrate any graphic from Flash to 3D properly. Even pretty complex ones copy-pasted from Illustrator.

Sometimes its good to keep layers however. Imagine a window component that you wish to take to 3D. The window could have a title, a title bar, a frame and a background. And now imagine that you want this window to remain in layers as opposed to being flat (eg, the title bar above the background, title above the title bar). What you do is skip the flattening and use:

{libraryClips:["Snowman"], scaling:2, perspectiveOffset:-100, perspectiveFocus:2000}

in the init object passed to the constructor. This example would separate the layers by 100 pixels backwards, and if viewed exactly 2000 pixels from the front of the container in which the graphics were imported to by Swf.as, you will see exactly what you saw in 2D! With perspective correction of course. As each element is drawn back, it is scaled up so that the overall image doesn't loose proportion in each of its elements. If you move from this focus point however, you should become aware of the layering and the offsets between the graphics.

I hope someone finds usefulness in all this. And please, do provide feedback if you've got any ideas or requests!

Filed under: 3D, Tutorials Comments Off
Comments (1) Trackbacks (0)
  1. And no-one to congrats ???
    A chance I was Around !!!
    VectorSWF R.I.P (I don’t like you much)…
    Long live SWF.as !!!

No trackbacks yet.