Skip to content

Commit

Permalink
Fixed Mesh.boneWeights is out of bounds error
Browse files Browse the repository at this point in the history
  • Loading branch information
Adri authored and Adri committed Apr 15, 2022
1 parent 17ac155 commit f38fb56
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Editor/MdxScriptedImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ public override void OnImportAsset( AssetImportContext context )
Material material = model.materials[i];
if( addMaterialsToAsset )
{
context.AddObjectToAsset(i.ToString(), material);
context.AddObjectToAsset(material.name, material);
}
else
{
string directory = directoryPath + "/Materials/";
Directory.CreateDirectory(directory);

AssetDatabase.CreateAsset(material, directory + i.ToString() + ".mat");
AssetDatabase.CreateAsset(material, directory + material.name + ".mat");
AssetDatabase.SaveAssets();
}
}
Expand Down
24 changes: 22 additions & 2 deletions Runtime/MdxModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ public void Import( string path, MdxImportSettings settings )

ImportMesh();
gameObject = new GameObject();

MeshFilter filter = gameObject.AddComponent<MeshFilter>();
filter.sharedMesh = mesh;

SkinnedMeshRenderer renderer = gameObject.AddComponent<SkinnedMeshRenderer>();
renderer.sharedMesh = mesh;

Expand Down Expand Up @@ -108,7 +110,7 @@ private void ReadFile()
private void ImportMesh()
{
mesh = new Mesh();
mesh.name = cmodel.Name;
mesh.name = Path.GetFileNameWithoutExtension(this.path);

// Set the bounding box.
Bounds bounds = new Bounds();
Expand Down Expand Up @@ -185,6 +187,7 @@ private void ImportMesh()
}

// Combine the submeshes.
// This operation removes vertices that don't belong to any triangle.
mesh.CombineMeshes(combines.ToArray(), false);
}

Expand All @@ -200,7 +203,7 @@ private void ImportMaterials( SkinnedMeshRenderer renderer )
}

Material material = new Material(Shader.Find("MDX/Standard"));
material.name = i.ToString();
material.name = Path.GetFileNameWithoutExtension(this.path) + i.ToString();

// For each layer.
int blendMode = 1; // Cutout.
Expand Down Expand Up @@ -362,6 +365,23 @@ private void ImportWeights()
for( int j = 0; j < cvertices.Count; j++ )
{
CGeosetVertex cvertex = cvertices.Get(j);

// Check if the vertex belongs to a triangle.
// Mesh combines discard vertices that don't belong to any triangle. To avoid the error "Mesh.boneWeights is out of bounds" (more weights than vertices), these weights are ignored.
bool hasTriangle = false;
foreach( CGeosetFace cface in cgeoset.Faces )
{
if( cvertex.ObjectId == cface.Vertex1.ObjectId || cvertex.ObjectId == cface.Vertex2.ObjectId || cvertex.ObjectId == cface.Vertex3.ObjectId )
{
hasTriangle = true;
break;
}
}
if( !hasTriangle )
{
continue;
}

BoneWeight weight = new BoneWeight();

// Group.
Expand Down
21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "com.adcimon.mdx-importer",
"displayName": "MDX Importer",
"version": "1.0.1",
"type": "library",
"unity": "2019.1",
"description": "MDX importer.",
"keywords": [
"mdx",
"importer"
]
"name": "com.adcimon.mdx-importer",
"displayName": "MDX Importer",
"version": "1.0.2",
"type": "library",
"unity": "2019.1",
"description": "MDX importer.",
"keywords":
[
"mdx",
"importer"
]
}

0 comments on commit f38fb56

Please sign in to comment.