Add OBJ mesh support to Unity plugin

This commit is contained in:
Balint-H
2025-01-21 15:11:27 +00:00
parent 36cbef50b7
commit a58f8d0033
4 changed files with 96 additions and 76 deletions
+12 -4
View File
@@ -174,7 +174,14 @@ public class MjImporterWithAssets : MjcfImporter {
throw new Exception( throw new Exception(
$"Trying to import mesh {unsanitizedAssetReferenceName} but {assetPath} already exists."); $"Trying to import mesh {unsanitizedAssetReferenceName} but {assetPath} already exists.");
} }
AssetDatabase.ImportAsset(assetPath); AssetDatabase.ImportAsset(assetPath);
ModelImporter importer = AssetImporter.GetAtPath(assetPath) as ModelImporter;
if (importer != null && !importer.isReadable) {
importer.isReadable = true;
importer.SaveAndReimport();
}
var copiedMesh = AssetDatabase.LoadAssetAtPath<Mesh>(assetPath); var copiedMesh = AssetDatabase.LoadAssetAtPath<Mesh>(assetPath);
if (copiedMesh == null) { if (copiedMesh == null) {
throw new Exception($"Mesh {assetPath} was not imported."); throw new Exception($"Mesh {assetPath} was not imported.");
@@ -283,12 +290,12 @@ public class MjImporterWithAssets : MjcfImporter {
// We use the geom's name, guaranteed to be unique, as the asset name. // We use the geom's name, guaranteed to be unique, as the asset name.
// If geom is nameless, use a random number. // If geom is nameless, use a random number.
var name = var name =
MjEngineTool.Sanitize(parentNode.GetStringAttribute( MjEngineTool.Sanitize(parentNode.GetStringAttribute(
"name", defaultValue: $"{UnityEngine.Random.Range(0, 1000000)}")); "name", defaultValue: $"{UnityEngine.Random.Range(0, 1000000)}"));
var assetPath = Path.Combine(_targetAssetDir, name + ".mat"); var assetPath = Path.Combine(_targetAssetDir, name+".mat");
if (AssetDatabase.LoadMainAssetAtPath(assetPath) != null) { if (AssetDatabase.LoadMainAssetAtPath(assetPath) != null) {
throw new Exception( throw new Exception(
$"Creating a material asset for the geom {name}, but {assetPath} already exists."); $"Creating a material asset for the geom {name}, but {assetPath} already exists.");
} }
AssetDatabase.CreateAsset(material, assetPath); AssetDatabase.CreateAsset(material, assetPath);
AssetDatabase.SaveAssets(); AssetDatabase.SaveAssets();
@@ -297,6 +304,7 @@ public class MjImporterWithAssets : MjcfImporter {
material = DefaultMujocoMaterial; material = DefaultMujocoMaterial;
} }
} }
if (parentNode.GetFloatAttribute("group") > 2) renderer.enabled = false;
renderer.sharedMaterial = material; renderer.sharedMaterial = material;
} }
} }
+66 -65
View File
@@ -11,84 +11,85 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
using System; using System;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using UnityEditor;
using UnityEngine; using UnityEngine;
namespace Mujoco { namespace Mujoco {
/// <summary> /// <summary>
/// Scale vertex data manually line by line. We skip normals. Parameter vertex points /// Scale vertex data manually line by line. We skip normals. Warning: Parameter vertex points
/// (`vp`) was unclear to me how to properly scale them, if you use them and notice an /// (`vp`) were unclear to me how to handle with scaling, if you use them and notice an issue
/// issue please report it. /// please report it.
/// </summary> /// </summary>
public static class ObjMeshImportUtility { public static class ObjMeshImportUtility {
private static Vector3 ToXZY(float x, float y, float z) => new Vector3(x, z, y); private static Vector3 ToXZY(float x, float y, float z) => new Vector3(x, z, y);
public static void CopyAndScaleOBJFile(string sourceFilePath, string targetFilePath, public static void CopyAndScaleOBJFile(string sourceFilePath, string targetFilePath,
Vector3 scale, bool flipFaces=false) { Vector3 scale) {
// OBJ files are human readable // OBJ files are human readable
string[] lines = File.ReadAllLines(sourceFilePath); string[] lines = File.ReadAllLines(sourceFilePath);
StringBuilder outputBuilder = new StringBuilder(); StringBuilder outputBuilder = new StringBuilder();
// Culture info for consistent decimal point handling // Culture info for consistent decimal point handling
CultureInfo invariantCulture = CultureInfo.InvariantCulture; CultureInfo invariantCulture = CultureInfo.InvariantCulture;
scale = ToXZY(scale.x, scale.y, scale.z);
foreach (string line in lines) {
if (line.StartsWith("v ")) // Vertex line
{
// Split the line into components
string[] parts = line.Split(' ');
if (parts.Length >= 4) {
// Scale the vertex. It is unclear to me why flipping along x axis was necessary,
// but without it meshes were incorrectly oriented.
float x = -float.Parse(parts[1], invariantCulture) * scale.x;
float y = float.Parse(parts[2], invariantCulture) * scale.y;
float z = float.Parse(parts[3], invariantCulture) * scale.z;
scale = ToXZY(scale.x, scale.y, scale.z); var swizzled = ToXZY(x, y, z);
foreach (string line in lines) { outputBuilder.AppendLine(
if (line.StartsWith("v ")) // Vertex line $"v {swizzled.x.ToString(invariantCulture)} "+
{ $"{swizzled.y.ToString(invariantCulture)} "+
// Split the line into components $"{swizzled.z.ToString(invariantCulture)}");
string[] parts = line.Split(' ');
if (parts.Length >= 4) {
// Scale the vertex
float x = -float.Parse(parts[1], invariantCulture) * scale.x;
float y = float.Parse(parts[2], invariantCulture) * scale.y;
float z = float.Parse(parts[3], invariantCulture) * scale.z;
var swizzled = ToXZY(x, y, z);
outputBuilder.AppendLine(
$"v {swizzled.x.ToString(invariantCulture)} {swizzled.y.ToString(invariantCulture)} {swizzled.z.ToString(invariantCulture)}");
}
} else if (line.StartsWith("vn ")) {
string[] parts = line.Split(' ');
if (parts.Length >= 4) {
float x = -float.Parse(parts[1], invariantCulture);
float y = float.Parse(parts[2], invariantCulture);
float z = float.Parse(parts[3], invariantCulture);
var swizzled = ToXZY(x, y, z);
outputBuilder.AppendLine(
$"vn {swizzled.x.ToString(invariantCulture)} {swizzled.y.ToString(invariantCulture)} {swizzled.z.ToString(invariantCulture)}");
}
} else if (line.StartsWith("f ") && flipFaces) {
string[] parts = line.Split(' ');
if (parts.Length >= 4) {
outputBuilder.Append(parts[0] + " ");
// Apply same vertex order as STL parser: [0,2,1]
var face = parts.Skip(1).ToArray();
if (face.Length >= 3) {
outputBuilder.Append(face[0] + " "); // vertex 0
outputBuilder.Append(face[2] + " "); // vertex 2
outputBuilder.Append(face[1]); // vertex 1
// Append any remaining vertices in original order
for (int i = 3; i < face.Length; i++) {
outputBuilder.Append(" " + face[i]);
}
}
outputBuilder.AppendLine();
}
} else {
// Copy non-vertex lines as-is
outputBuilder.AppendLine(line);
} }
} else if (line.StartsWith("vn ")) {
// We swizzle the normals too
string[] parts = line.Split(' ');
if (parts.Length >= 4) {
float x = -float.Parse(parts[1], invariantCulture);
float y = float.Parse(parts[2], invariantCulture);
float z = float.Parse(parts[3], invariantCulture);
var swizzled = ToXZY(x, y, z);
outputBuilder.AppendLine(
$"vn {swizzled.x.ToString(invariantCulture)} "+
$"{swizzled.y.ToString(invariantCulture)} "+
$"{swizzled.z.ToString(invariantCulture)}");
}
} else if (line.StartsWith("f ") && scale.x*scale.y*scale.z < 0) {
// Faces definition, flip face by reordering vertices
string[] parts = line.Split(' ');
if (parts.Length >= 4) {
outputBuilder.Append(parts[0]+" ");
var face = parts.Skip(1).ToArray();
if (face.Length >= 3) {
outputBuilder.Append(face[0]+" ");
outputBuilder.Append(face[2]+" ");
outputBuilder.Append(face[1]);
}
outputBuilder.AppendLine();
}
} else {
// Copy non-vertex lines as-is
outputBuilder.AppendLine(line);
} }
// Write the scaled OBJ to the target file
File.WriteAllText(targetFilePath, outputBuilder.ToString());
} }
// Write the scaled OBJ to the target file
File.WriteAllText(targetFilePath, outputBuilder.ToString());
} }
}
} }
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: adec9978c00bb934eb8bf974c26193e1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -41,19 +41,19 @@ public class MjMeshFilter : MonoBehaviour {
return; return;
} }
_shapeChangeStamp = currentChangeStamp;
Tuple<Vector3[], int[]> meshData = _geom.BuildMesh();
if (meshData == null) {
throw new ArgumentException("Unsupported geom shape detected");
}
if(_geom.ShapeType == MjShapeComponent.ShapeTypes.Mesh) { if(_geom.ShapeType == MjShapeComponent.ShapeTypes.Mesh) {
MjMeshShape meshShape = _geom.Shape as MjMeshShape; MjMeshShape meshShape = _geom.Shape as MjMeshShape;
_meshFilter.sharedMesh = meshShape.Mesh; _meshFilter.sharedMesh = meshShape.Mesh;
return; return;
} }
_shapeChangeStamp = currentChangeStamp;
Tuple<Vector3[], int[]> meshData = _geom.BuildMesh();
if (meshData == null)
{
throw new ArgumentException("Unsupported geom shape detected");
}
DisposeCurrentMesh(); DisposeCurrentMesh();
var mesh = new Mesh(); var mesh = new Mesh();