Skip to content

Commit

Permalink
MD5 fix for unsanitized files (0 weights). Closes #65
Browse files Browse the repository at this point in the history
  • Loading branch information
DerSchmale committed Jun 19, 2012
1 parent a07a5aa commit 66ff060
Showing 1 changed file with 57 additions and 17 deletions.
74 changes: 57 additions & 17 deletions src/away3d/loaders/parsers/MD5MeshParser.as
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ package away3d.loaders.parsers
protected override function proceedParsing() : Boolean
{
var token : String;

if(!_startedParsing) {
_textData = getTextData();
_startedParsing = true;
}

while (hasTime()) {
token = getNextToken();
switch (token) {
Expand Down Expand Up @@ -163,8 +163,9 @@ package away3d.loaders.parsers
}

if (_reachedEOF) {
calculateMaxJointCount();
_animation = new SkeletonAnimation(_skeleton, _maxJointCount);

_mesh = new Mesh();
_geometry = _mesh.geometry;

Expand All @@ -174,7 +175,7 @@ package away3d.loaders.parsers

_geometry.animation = _animation;
// _mesh.animationController = _animationController;

finalizeAsset(_mesh);
finalizeAsset(_skeleton);

Expand All @@ -184,6 +185,40 @@ package away3d.loaders.parsers
return ParserBase.MORE_TO_PARSE;
}

private function calculateMaxJointCount() : void
{
_maxJointCount = 0;

var numMeshData : int = _meshData.length;
for (var i : int = 0; i < numMeshData; ++i) {
var meshData : MeshData = _meshData[i];
var vertexData : Vector.<VertexData> = meshData.vertexData;
var numVerts : int = vertexData.length;

for (var j : int = 0; j < numVerts; ++j) {
var zeroWeights : int = countZeroWeightJoints(vertexData[j], meshData.weightData);
var totalJoints : int = vertexData[j].countWeight - zeroWeights;
if (totalJoints > _maxJointCount)
_maxJointCount = totalJoints;
}
}
}

private function countZeroWeightJoints(vertex : VertexData, weights : Vector.<JointData>) : int
{
var start : int = vertex.startWeight;
var end : int = vertex.startWeight + vertex.countWeight;
var count : int = 0;
var weight : Number;

for (var i : int = start; i < end; ++i) {
weight = weights[i].bias;
if (weight == 0) ++count;
}

return count;
}

/**
* Parses the skeleton's joints.
*/
Expand All @@ -197,9 +232,9 @@ package away3d.loaders.parsers
var token : String = getNextToken();

if (token != "{") sendUnknownKeywordError();

_skeleton = new Skeleton();

do {
if (_reachedEOF) sendEOFError();
joint = new SkeletonJoint();
Expand Down Expand Up @@ -314,6 +349,7 @@ package away3d.loaders.parsers
var jointIndices : Vector.<Number> = new Vector.<Number>(len * _maxJointCount, true);
var jointWeights : Vector.<Number> = new Vector.<Number>(len * _maxJointCount, true);
var l : int;
var nonZeroWeights : int;

for (var i : int = 0; i < len; ++i) {
vertex = vertexData[i];
Expand All @@ -322,20 +358,24 @@ package away3d.loaders.parsers
v3 = v1 + 2;
vertices[v1] = vertices[v2] = vertices[v3] = 0;

nonZeroWeights = 0;
for (var j : int = 0; j < vertex.countWeight; ++j) {
weight = weights[vertex.startWeight + j];
bindPose = _bindPoses[weight.joint];
pos = bindPose.transformVector(weight.pos);
vertices[v1] += pos.x * weight.bias;
vertices[v2] += pos.y * weight.bias;
vertices[v3] += pos.z * weight.bias;

// indices need to be multiplied by 3 (amount of matrix registers)
jointIndices[l] = weight.joint * 3;
jointWeights[l++] = weight.bias;
if (weight.bias > 0) {
bindPose = _bindPoses[weight.joint];
pos = bindPose.transformVector(weight.pos);
vertices[v1] += pos.x * weight.bias;
vertices[v2] += pos.y * weight.bias;
vertices[v3] += pos.z * weight.bias;

// indices need to be multiplied by 3 (amount of matrix registers)
jointIndices[l] = weight.joint * 3;
jointWeights[l++] = weight.bias;
++nonZeroWeights;
}
}

for (j = vertex.countWeight; j < _maxJointCount; ++j) {
for (j = nonZeroWeights; j < _maxJointCount; ++j) {
jointIndices[l] = 0;
jointWeights[l++] = 0;
}
Expand Down Expand Up @@ -391,7 +431,7 @@ package away3d.loaders.parsers
parseUV(vertex);
vertex.startWeight = getNextInt();
vertex.countWeight = getNextInt();
if (vertex.countWeight > _maxJointCount) _maxJointCount = vertex.countWeight;
// if (vertex.countWeight > _maxJointCount) _maxJointCount = vertex.countWeight;
vertexData[vertex.index] = vertex;
}

Expand Down

0 comments on commit 66ff060

Please sign in to comment.