Loading 3D Model Data In Flash At Runtime

blender_20070312.jpg
I was working on a small 3D flash program today and wanted it to read in it's model data from a file that I created with another program, such as Blender. I'm not sure if it's the best way to do things, but it was fairly painless to export the model data to a plaintext VRML file and then convert the mesh data to a simple XML format. It's trivial to read in XML data in Flash, so this saves having to parse a more feature-rich 3D file format.

If you export a single model into VRML format, the data you'll need to extract are the vertices and faces. For example, the simple model above is composed of the following vertices (each line represents the x/y/z location of the vertex).


Coordinate3 {
point [
1.000000 1.000000 -1.649835,
4.465784 -1.000000 -1.649835,
-3.238319 -1.000000 -1.649835,
-1.000000 1.000000 -1.649835,
1.000000 0.999999 1.577631,
4.465783 -1.000001 1.577631,
-3.238319 -1.000000 1.577631,
-1.000000 1.000000 1.577631,
4.448558 -1.022888 0.000000,
]
}

The faces of the model are the planes formed by sets of these vertices. In this model, these are all 4 sided polygons. The numbers used below are an index representing one of the vertices above (-1 is the end of a face).

IndexedFaceSet {
coordIndex [
0, 1, 2, 3, -1,
4, 7, 6, 5, -1,
0, 4, 5, 1, -1,
1, 5, 6, 2, -1,
2, 6, 7, 3, -1,
4, 0, 3, 7, -1,
]
}

With this, you can construct a fairly simple XML structure that will be easily readable in flash:

<scene>
<object>
<vertices>
<vertex x="1.0" y="-1.64983463287" z="-0.999999940395" />
<vertex x="4.46578407288" y="-1.64983463287" z="1.0" />
<vertex x="-3.23831892014" y="-1.64983463287" z="0.999999821186" />
<vertex x="-0.999999642372" y="-1.64983463287" z="-1.00000035763" />
<vertex x="1.00000047684" y="1.57763075829" z="-0.999999463558" />
<vertex x="4.4657831192" y="1.57763075829" z="1.00000059605" />
<vertex x="-3.23831939697" y="1.57763075829" z="0.999999582767" />
<vertex x="-0.999999940395" y="1.57763075829" z="-1.0" />
<vertex x="4.44855833054" y="0.0" z="1.02288770676" />
</vertices>
<poly><v>0</v><v>1</v><v>2</v><v>3</v></poly>
<poly><v>4</v><v>7</v><v>6</v><v>5</v></poly>
<poly><v>0</v><v>4</v><v>5</v><v>1</v></poly>
<poly><v>1</v><v>5</v><v>6</v><v>2</v></poly>
<poly><v>2</v><v>6</v><v>7</v><v>3</v></poly>
<poly><v>4</v><v>0</v><v>3</v><v>7</v></poly>
</object>
</scene>

Give it a shot, and let me know what you come up with!

Resources:


Recent Entries

Leave a comment


Subscribe to MAKE!Subscribe to MAKE Magazine!

Subscribe today, save 42% and get web access to MAKE free. MAKE Digital Edition is available only to subscribers.

$34.95 / 1 year
(4 Quarterly Issues)

Subscribe now


Void your warranty, violate a user agreement, fry a circuit, blow a fuse, poke an eye out. Make: The risk-takers, the doers, the makers of things... Welcome to Make: Online!


CRAFT Maker Shed Maker Faire MAKE television
Holiday Gift Guides from MAKE
Gifts for Dads
Science and Chemistry
Gifts Under $20
More guides: Santa Claus Machines, Geek Toys for Grown Up Girls & Boys


Check out all of the episodes of Make: television

Alex Rider Dream Gadget Contest
Make: Science Room

Connect with MAKE

Be a MAKE fan on Facebook MAKE on Facebook
Visit our Facebook page and become a fan of MAKE!
MAKE on Twitter MAKE on Twitter
Follow our MAKE tweets!
MAKE Flickr Pool MAKE on Flickr
Join our MAKE Flickr Pool!
    make_tips on Twitter




    Maker SHED

    Advertise here with FM.

    Why advertise on MAKE?
    Read what folks are saying about us!

    Click here to advertise on MAKE!



    Subscribe to MAKE Magazine!

    Make: Online authors!

    Gareth BranwynGareth Branwyn
    Senior Editor


    Phillip TorronePhillip Torrone
    Senior Editor
    | AIM | Twitter


    Becky SternBecky Stern
    Associate Editor
    | AIM | Twitter


    Marc de VinckMarc de Vinck
    Contributing Writer
    | AIM | Twitter


    John ParkJohn Park
    Contributing Writer
    | Twitter


    Sean RaganSean Ragan
    Contributing Writer
    | Twitter


    Matt MetsMatt Mets
    Contributing Writer
    | AIM | Twitter


    Dale DoughertyDale Dougherty
    Editor & Publisher
    | Twitter


    Shawn ConnallyShawn Connally
    Managing Editor
    | Twitter


    Goli MohammadiGoli Mohammadi
    Associate Managing Editor

    Kip KayKip Kay
    Weekend Projects
    | AIM | Twitter


    Collin CunninghamCollin Cunningham
    Contributing Writer
    | AIM | Twitter

    Adam FlahertyAdam Flaherty
    Contributing Writer
    | AIM | Twitter



    More contributors: Mark Frauenfelder (Editor-in-Chief, MAKE magazine), Kipp Bradford (Technical Consultant/Writer), Chris Connors (Education), Diana Eng (Guest Author), Peter Horvath (Intern), Brian Jepson (O'Reilly Media), Robert Bruce Thompson (Science Room)

    Suggest a Site!

    Current Podcast

    itunesdl.gif Weekend Project: Beetlebot Simple robot from your parts bin that avoids obstacles. Thanks go to Jerome Demers for the original article in MAKE, Volume 12. To download the Beetlebot video, click here or subscribe in iTunes. Check out the complete Beetlebot article... More...

    Get the Make: Online sent via email
    Enter your email to receive Make: Online each day:



    MAKE Fascination video series brought to you by Dow

    Make: Education
    MAKE: en EspaƱol MAKE: Japan
    Important please read


    Subscribe to MAKE Magazine!

    Recent Posts from the Craft: Blog