| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | using System.Globalization; |
| | using System.IO; |
| | using System.Linq; |
| | using System.Text; |
| | using UnityEngine; |
| |
|
| | namespace Mujoco { |
| |
|
| | |
| | |
| | |
| | |
| | |
| | public static class ObjMeshImportUtility { |
| | private static Vector3 ToXZY(float x, float y, float z) => new Vector3(x, z, y); |
| |
|
| | public static void CopyAndScaleOBJFile(string sourceFilePath, string targetFilePath, |
| | Vector3 scale) { |
| | |
| | string[] lines = File.ReadAllLines(sourceFilePath); |
| | StringBuilder outputBuilder = new StringBuilder(); |
| | |
| | CultureInfo invariantCulture = CultureInfo.InvariantCulture; |
| | scale = ToXZY(scale.x, scale.y, scale.z); |
| | foreach (string line in lines) { |
| | if (line.StartsWith("v ")) |
| | { |
| | |
| | string[] parts = line.Split(' '); |
| | if (parts.Length >= 4) { |
| | |
| | |
| | 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 ") && scale.x*scale.y*scale.z < 0) { |
| | |
| | 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 { |
| | |
| | outputBuilder.AppendLine(line); |
| | } |
| | } |
| | |
| | File.WriteAllText(targetFilePath, outputBuilder.ToString()); |
| | } |
| | } |
| | } |
| |
|