Skip to content

Commit

Permalink
Fix broken brushes when importing from glb
Browse files Browse the repository at this point in the history
Fix XyIsUvZIsDistance trashing UVs. It was falling through and
doing a basis-change on the UV+Distance. We didn't notice because
of an early-out if the target units were meters.

Change-Id: Id000b0155a3dc616edbaafd6d8f25d5a7876008d
  • Loading branch information
dubois committed Feb 7, 2020
1 parent 725bebe commit e7c28d3
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions UnitySDK/Assets/TiltBrush/Scripts/Gltf/ImportGltf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -741,8 +741,11 @@ static void ChangeBasisAndApplyScale(
basisChange *= Matrix4x4.Scale(Vector3.one * scaleFactor);
} else if (semantic == Semantic.UnitlessVector) {
// use basisChange as-is
} else if (semantic == Semantic.XyIsUvZIsDistance && scaleFactor != 1) {
if (data is Vector3[]) {
} else if (semantic == Semantic.XyIsUvZIsDistance) {
// Do unit change
if (scaleFactor == 1) {
// Don't bother
} else if (data is Vector3[]) {
Vector3[] vData = (Vector3[])data;
for (int i = 0; i < vData.Length; ++i) {
var tmp = vData[i]; tmp.z *= scaleFactor; vData[i] = tmp;
Expand All @@ -755,6 +758,8 @@ static void ChangeBasisAndApplyScale(
} else {
Debug.LogWarningFormat("Cannot change basis of type {0}", data.GetType());
}
// no basis-change needed
return;
} else {
// no basis-change or unit-change needed
return;
Expand Down Expand Up @@ -883,8 +888,13 @@ static List<MeshPrecursor> CreateMeshPrecursorsFromPrimitive(

meshes.Add(mesh);
}
foreach (MeshPrecursor mesh in meshes) {
FixInvalidNormals(mesh);

// This was added in PT to address bad data in Poly, but it can force the data
// to be unit-length; don't do it unless we know that's a valid fix.
if (GetNormalSemantic(state, prim.MaterialPtr) == Semantic.UnitlessVector) {
foreach (MeshPrecursor mesh in meshes) {
FixInvalidNormals(mesh);
}
}
return meshes;
}
Expand All @@ -905,7 +915,7 @@ private static void StoreDataInMesh(
break;
case "NORMAL":
ChangeBasisAndApplyScale(
data, Semantic.UnitlessVector, state.scaleFactor, state.unityFromGltf);
data, GetNormalSemantic(state, material), state.scaleFactor, state.unityFromGltf);
mesh.normals = (Vector3[]) data;
break;
case "COLOR":
Expand Down Expand Up @@ -1056,6 +1066,24 @@ private static void FixInvalidNormals(MeshPrecursor mesh) {
}
}

// Within TB, TB brush materials will return Semantic.Unspecified or Semantic.Position.
// This method is useful for skipping overzealous code that wants to "fix" TB's normals.
static Semantic GetNormalSemantic(ImportState state, GltfMaterialBase material) {
#if TILT_BRUSH
// "normalSemantic" doesn't exist in the stripped-down BrushDescriptor used by TBT.
// Only particles use a non-standard semantic for Normals.
// TBT puts vert.normal -> vert.txc1 at import time (see CreateMeshPrecursorsFromPrimitive).
// So BD.normalSemantic in TB becomes BD.txc1Semantic in TBT.
if (state.root.tiltBrushVersion != null) {
BrushDescriptor desc = GltfMaterialConverter.LookupBrushDescriptor(material);
if (desc != null) {
return desc.VertexLayout.normalSemantic;
}
}
#endif
return Semantic.UnitlessVector;
}

// Returns a Semantic which tells us how to manipulate the uv to convert it
// from glTF conventions to Unity conventions.
static Semantic GetTexcoordSemantic(
Expand Down

0 comments on commit e7c28d3

Please sign in to comment.