Loading 3D Model Data In Flash At Runtime

Technology
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:

Discuss this article with the rest of the community on our Discord server!
Tagged

ADVERTISEMENT

Maker Faire Bay Area 2023 - Mare Island, CA

Escape to an island of imagination + innovation as Maker Faire Bay Area returns for its 15th iteration!

Buy Tickets today! SAVE 15% and lock-in your preferred date(s).

FEEDBACK