Allow loading obj meshes

This commit is contained in:
Bálint Hodossy
2025-01-20 11:09:18 +00:00
parent 61973a33c9
commit 36cbef50b7
3 changed files with 112 additions and 10 deletions
+17 -9
View File
@@ -153,20 +153,21 @@ public class MjImporterWithAssets : MjcfImporter {
var assetReferenceName = MjEngineTool.Sanitize(unsanitizedAssetReferenceName);
var sourceFilePath = Path.Combine(_sourceMeshesDir, fileName);
if (Path.GetExtension(sourceFilePath) == ".obj") {
throw new NotImplementedException("OBJ mesh file loading is not yet implemented. " +
"Please convert to binary STL. " +
if (Path.GetExtension(sourceFilePath) != ".obj" && Path.GetExtension(sourceFilePath) != ".stl") {
throw new NotImplementedException("Type of mesh file not yet supported. " +
"Please convert to binary STL or OBJ. " +
$"Attempted to load: {sourceFilePath}");
}
var targetFilePath = Path.Combine(_targetMeshesDir, assetReferenceName + ".stl");
var targetFilePath = Path.Combine(_targetMeshesDir, assetReferenceName
+ Path.GetExtension(sourceFilePath));
if (File.Exists(targetFilePath)) {
File.Delete(targetFilePath);
}
var scale = MjEngineTool.UnityVector3(
parentNode.GetVector3Attribute("scale", defaultValue: Vector3.one));
CopyMeshAndRescale(sourceFilePath, targetFilePath, scale);
var assetPath = Path.Combine(_targetAssetDir, assetReferenceName + ".stl");
var assetPath = Path.Combine(_targetAssetDir, assetReferenceName + Path.GetExtension(sourceFilePath));
// This asset path should be available because the MuJoCo compiler guarantees element names
// are unique, but check for completeness (and in case sanitizing the name broke uniqueness):
if (AssetDatabase.LoadMainAssetAtPath(assetPath) != null) {
@@ -174,7 +175,7 @@ public class MjImporterWithAssets : MjcfImporter {
$"Trying to import mesh {unsanitizedAssetReferenceName} but {assetPath} already exists.");
}
AssetDatabase.ImportAsset(assetPath);
var copiedMesh = AssetDatabase.LoadMainAssetAtPath(assetPath) as Mesh;
var copiedMesh = AssetDatabase.LoadAssetAtPath<Mesh>(assetPath);
if (copiedMesh == null) {
throw new Exception($"Mesh {assetPath} was not imported.");
}
@@ -186,9 +187,16 @@ public class MjImporterWithAssets : MjcfImporter {
private void CopyMeshAndRescale(
string sourceFilePath, string targetFilePath, Vector3 scale) {
var originalMeshBytes = File.ReadAllBytes(sourceFilePath);
var mesh = StlMeshParser.ParseBinary(originalMeshBytes, scale);
var rescaledMeshBytes = StlMeshParser.SerializeBinary(mesh);
File.WriteAllBytes(targetFilePath, rescaledMeshBytes);
if (Path.GetExtension(sourceFilePath) == ".stl") {
var mesh = StlMeshParser.ParseBinary(originalMeshBytes, scale);
var rescaledMeshBytes = StlMeshParser.SerializeBinary(mesh);
File.WriteAllBytes(targetFilePath, rescaledMeshBytes);
} else if (Path.GetExtension(sourceFilePath) == ".obj") {
ObjMeshImportUtility.CopyAndScaleOBJFile(sourceFilePath, targetFilePath, scale);
} else {
throw new NotImplementedException($"Extension {Path.GetExtension(sourceFilePath)} " +
$"not yet supported for MuJoCo mesh asset.");
}
}
private void ParseMaterial(XmlElement parentNode) {