Sindney
Update: Glidias has created an animation supported version of MS3DParser, get it now: http://wonderfl.net/c/fzxq.
Download the animation supported version here.
This article shows some basic knowledge about parsing a ms3d model.
If you couldn’t get it clear, that’s ok. Because today i’ll show you a working demo on alternativa3d engine.
Note: this demo doesn’t support animation, and that’s what i’m working on recently.
You know, a3d’s animation part doesn’t have any detailed examples, so it would take some time for me to understand how it works.
The Parser Class:
/**
* Copyright (c) 2011 - 2100 Sindney
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package
{
import flash.geom.Vector3D;
import flash.geom.Matrix3D;
import flash.utils.ByteArray;
import flash.utils.Endian;
import alternativa.engine3d.core.Object3D;
import alternativa.engine3d.core.Vertex;
import alternativa.engine3d.objects.Mesh;
/**
* ParserMS3D for alternativa3d v7.8.0
* <p>Note: no animation currently.</p>
*
* @version 0.2
* @date 2011/7/14 21:21
* @author sindney
*/
public class ParserMS3D {
public var objects:Vector.<Object3D>;
public function ParserMS3D() {}
public function getObjectByName(name:String):Object3D {
if (objects != null) {
var i:int, l:int = objects.length;
var target:Object3D;
for (i = 0; i < l; i++) {
target = objects[i];
if (target.name == name) return target;
}
}
return null;
}
/**
* Parse a given model.
* @param data Model's source.
* @return Model's information.<p>NumVertices, NumTriangles, NumGroups</p>
*/
public function parse(data:ByteArray):String {
var i:int, j:int, char:int;
var int0:int, int1:int, int2:int;
var num0:Number, num1:Number;
var id:String = "";
var name:String = "";
var parent_name:String = "";
// read header
data.endian = Endian.LITTLE_ENDIAN;
data.position = 0;
for (i = 0; i < 10; i++) {
char = data.readUnsignedByte();
id += String.fromCharCode(char);
}
if (id != "MS3D000000" ) throw new Error("Error loading MS3D file: Not a valid MS3D file");
if (data.readInt() != 4) throw new Error("Error loading MS3D file: Bad version");
// read vertices
var num_vertices:int = data.readUnsignedShort();
var vertices:Vector.<Vertex> = new Vector.<Vertex>(num_vertices, true);
var verts_joint:Vector.<int> = new Vector.<int>(num_vertices, true);
for (i = 0; i < num_vertices; i++) {
// flag
if (data.readUnsignedByte() != 2) {
var tmp:Vertex = new Vertex();
// position
tmp.x = data.readFloat();
tmp.y = data.readFloat();
tmp.z = data.readFloat();
// bone -1 no bone
data.readByte();
// refCount
data.readUnsignedByte();
vertices[i] = tmp;
}
}
// read triangles
var num_triangles:int = data.readUnsignedShort();
var faceIndices:Vector.<int> = new Vector.<int>();
var uvs:Vector.<Vector.<Number>> = new Vector.<Vector.<Number>>();
var v1:Vertex, v2:Vertex, v3:Vertex;
for (i = 0, j = 0; i < num_triangles; i++, j += 3) {
// flag
if (data.readUnsignedShort() != 2) {
faceIndices[j] = data.readUnsignedShort();
faceIndices[j + 1] = data.readUnsignedShort();
faceIndices[j + 2] = data.readUnsignedShort();
v1 = vertices[faceIndices[j]];
v2 = vertices[faceIndices[j + 1]];
v3 = vertices[faceIndices[j + 2]];
v1.normalX = data.readFloat();
v1.normalY = data.readFloat();
v1.normalZ = data.readFloat();
v2.normalX = data.readFloat();
v2.normalY = data.readFloat();
v2.normalZ = data.readFloat();
v3.normalX = data.readFloat();
v3.normalY = data.readFloat();
v3.normalZ = data.readFloat();
uvs[j] = new Vector.<Number>(2, true);
uvs[j + 1] = new Vector.<Number>(2, true);
uvs[j + 2] = new Vector.<Number>(2, true);
uvs[j][0] = data.readFloat();
uvs[j + 1][0] = data.readFloat();
uvs[j + 2][0] = data.readFloat();
uvs[j][1] = data.readFloat();
uvs[j + 1][1] = data.readFloat();
uvs[j + 2][1] = data.readFloat();
// smoothGroup
data.readUnsignedByte();
// groupIndex
data.readUnsignedByte();
}
}
// read groups
var num_groups:int = data.readUnsignedShort();
objects = new Vector.<Object3D>(num_groups, true);
for (i = 0; i < num_groups; i++) {
// flag
if (data.readUnsignedByte() != 2) {
var mesh:Mesh = new Mesh();
mesh.name = "";
for (j = 0; j < 32; j++) {
char = data.readUnsignedByte();
mesh.name += String.fromCharCode(char);
}
// numtriangles
int0 = data.readUnsignedShort();
// triangles
var v4:Vertex, v5:Vertex, v6:Vertex;
for (j = 0; j < int0; j++) {
int1 = data.readUnsignedShort() * 3;
v1 = vertices[faceIndices[int1]];
v2 = vertices[faceIndices[int1 + 1]];
v3 = vertices[faceIndices[int1 + 2]];
v4 = mesh.addVertex(v1.x, v1.y, v1.z, uvs[int1][0], uvs[int1][1]);
v5 = mesh.addVertex(v2.x, v2.y, v2.z, uvs[int1 + 1][0], uvs[int1 + 1][1]);
v6 = mesh.addVertex(v3.x, v3.y, v3.z, uvs[int1 + 2][0], uvs[int1 + 2][1]);
v4.normalX = v1.normalX;
v4.normalY = v1.normalY;
v4.normalZ = v1.normalZ;
v5.normalX = v2.normalX;
v5.normalY = v2.normalY;
v5.normalZ = v2.normalZ;
v6.normalX = v3.normalX;
v6.normalY = v3.normalY;
v6.normalZ = v3.normalZ;
v1 = v4;
v2 = v5;
v3 = v6;
mesh.addTriFace(v4, v5, v6);
}
// material index
data.readUnsignedByte();
objects[i] = mesh;
mesh.calculateFacesNormals();
mesh.calculateBounds();
}
}
// just ignored material&animation
// cause i've messed up the animation part
(i'll finished when i'm clear.)
return "Vertices: " + num_vertices + "\nTriangles: " + num_triangles + "\nGroups: " + num_groups;
}
}
}
And at last the all one pack.





One of the things about MS3d though is that it only works with triangulated faces. So, one might wish to weld faces in Alternativa3d in such cases. I hope you can get animation up though, since MS3d doesn’t really support any good animation export for Collada.
Well, it seems that animation part in MS3D is out of date. MS3D binds groups and joints together to get thing work. And i couldn’t find the right way to do that in a3d. Just got some strange errors(wrong translation, black screen etc). I hope a3d will have an example shows how it’s animation works. Then i’ll finish this