Skip to content

Commit

Permalink
Import 12.x particles properly. Bump version to 12
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dubois committed Jul 12, 2017
1 parent a685515 commit aa984be
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion UnitySDK/Assets/TiltBrush/Scripts/Editor/ModelImportSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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<Vector3> uv1 = new List<Vector3>();
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) {
Expand Down

0 comments on commit aa984be

Please sign in to comment.