From aa984be2038f6cf2f6b654e139c58c0e2696e996 Mon Sep 17 00:00:00 2001 From: Paul Du Bois Date: Wed, 12 Jul 2017 09:51:19 -0700 Subject: [PATCH] Import 12.x particles properly. Bump version to 12 12.x fixes an issue with some fbx data not being exported in the fbx coordinate system; but this requires that the Unity importer convert that data back to the Unity coordinate system. Version bump is to allow TB12 exports to say they require the bugfixed Toolkit. Change-Id: I6c44d06c26d9d4bfa114963767e500ef6cb1ecfe --- .../Scripts/Editor/ModelImportSettings.cs | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/UnitySDK/Assets/TiltBrush/Scripts/Editor/ModelImportSettings.cs b/UnitySDK/Assets/TiltBrush/Scripts/Editor/ModelImportSettings.cs index da5b09a4..9f9041f4 100644 --- a/UnitySDK/Assets/TiltBrush/Scripts/Editor/ModelImportSettings.cs +++ b/UnitySDK/Assets/TiltBrush/Scripts/Editor/ModelImportSettings.cs @@ -22,7 +22,7 @@ namespace TiltBrushToolkit { public class ModelImportSettings : AssetPostprocessor { - readonly Version kToolkitVersion = new Version { major=11 }; + readonly Version kToolkitVersion = new Version { major=12 }; readonly Version kRequiredFbxExportVersion = new Version { major=10 }; public static bool sm_forceOldMeshNamingConvention = false; @@ -142,6 +142,16 @@ Material OnAssignMaterialModel(Material material, Renderer renderer) { // Old versions of TB use linear lighting ConvertSrgbToLinear(mesh); } + + if (m_Info.tiltBrushVersion.Value.major >= 12) { + // Pre-TB12, Tilt Brush did unit conversion but not coord system conversion + // when exporting texcoords with Position semantics. TB12 fixes this, so now + // texcoord Position data are in fbx coordinates rather than Unity coordinates. + // However, this means we need to convert from fbx -> Unity coords on import, + // since Unity only takes care of mesh.vertices, tangents, normals, binormals. + FixDataWithPositionSemantic(desc, mesh); + } + if (desc.m_IsParticle) { // Would like to do this in OnPreprocessModel, but we don't yet // know whether it's a particle mesh. @@ -162,6 +172,24 @@ Material OnAssignMaterialModel(Material material, Renderer renderer) { } } + // Convert from fbx coordinate conventions to Unity coordinate conventions + static Vector3 UnityFromFbx(Vector3 v) { + return new Vector3(-v.x, v.y, v.z); + } + + void FixDataWithPositionSemantic(BrushDescriptor desc, Mesh mesh) { + // We don't have full VertexLayout data in BrushDescriptor; currently, particles + // are the only brushes that use Semantic.Position in texcoords + if (desc.m_IsParticle) { + List uv1 = new List(); + mesh.GetUVs(1, uv1); + for (int i = 0; i < uv1.Count; ++i) { + uv1[i] = UnityFromFbx(uv1[i]); + } + mesh.SetUVs(1, uv1); + } + } + void OnPostprocessModel(GameObject g) { // For backwards compatibility, if people have projects that use the old naming if (sm_forceOldMeshNamingConvention) {