From 1fb0c3db149c17c35553f6d7f207396b0a8d6231 Mon Sep 17 00:00:00 2001 From: Felix Herbst Date: Thu, 16 Jul 2020 19:22:14 +0200 Subject: [PATCH] set subdivision scheme to "none" and explicity set normal interpolation to "vertex" --- .../Runtime/Scripts/IO/Geometry/MeshExporter.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/package/com.unity.formats.usd/Runtime/Scripts/IO/Geometry/MeshExporter.cs b/package/com.unity.formats.usd/Runtime/Scripts/IO/Geometry/MeshExporter.cs index 9904ae59a..5790ba75e 100644 --- a/package/com.unity.formats.usd/Runtime/Scripts/IO/Geometry/MeshExporter.cs +++ b/package/com.unity.formats.usd/Runtime/Scripts/IO/Geometry/MeshExporter.cs @@ -295,16 +295,27 @@ static void ExportMesh(ObjectContext objContext, scene.Write(path, sample); UnityEngine.Profiling.Profiler.EndSample(); + pxr.UsdPrim usdPrim = scene.GetPrimAtPath(path); + // TODO: this is a bit of a half-measure, we need real support for primvar interpolation. // Set interpolation based on color count. if (sample.colors != null && sample.colors.Length == 1) { - pxr.UsdPrim usdPrim = scene.GetPrimAtPath(path); var colorPrimvar = new pxr.UsdGeomPrimvar(usdPrim.GetAttribute(pxr.UsdGeomTokens.primvarsDisplayColor)); colorPrimvar.SetInterpolation(pxr.UsdGeomTokens.constant); var opacityPrimvar = new pxr.UsdGeomPrimvar(usdPrim.GetAttribute(pxr.UsdGeomTokens.primvarsDisplayOpacity)); opacityPrimvar.SetInterpolation(pxr.UsdGeomTokens.constant); } + // for polygonal meshes, we do not want the default subdivision scheme of "catmull-clark" but instead "none" + // so that authored normals keep working. + var usdGeomMesh = new pxr.UsdGeomMesh(usdPrim); + usdGeomMesh.CreateSubdivisionSchemeAttr().Set(new pxr.TfToken("none")); + + // explicitly set the normal interpolation here – + // default should be "vertex" anyways but seems some viewers don't respect the default + var normalInterp = new pxr.UsdGeomPrimvar(usdPrim.GetAttribute(pxr.UsdGeomTokens.normals)); + normalInterp.SetInterpolation(pxr.UsdGeomTokens.vertex); + string usdMaterialPath; if (exportContext.exportMaterials && sharedMaterial != null) { if (!exportContext.matMap.TryGetValue(sharedMaterial, out usdMaterialPath)) { @@ -329,9 +340,6 @@ static void ExportMesh(ObjectContext objContext, } } - var usdPrim = scene.GetPrimAtPath(path); - var usdGeomMesh = new pxr.UsdGeomMesh(usdPrim); - // Process each subMesh and create a UsdGeomSubset of faces this subMesh targets. for (int si = 0; si < mesh.subMeshCount; si++) { int[] indices = mesh.GetTriangles(si);