Skip to content

Commit

Permalink
glb2: Implement compatibility mode
Browse files Browse the repository at this point in the history
This is the other half of go/tbcl/109834, which renames attributes
from (for example) TEXCOORD_n -> _TB_UNITY_TEXCOORD_n.

At import time, rename them back to TEXCOORD_n because that what the
rest of the importer expects. If there happens to already be a
TEXCOORD_n, that accessor is ignored (which is the desired behavior;
we don't want or need to bring in the "compatible" texcoord data)

Change-Id: Id000b015a0ee4b1f702b2f1e6354934ba376cd46
  • Loading branch information
dubois committed Sep 28, 2019
1 parent a36497f commit cfcfd60
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions UnitySDK/Assets/TiltBrush/Scripts/Gltf/Gltf1Schema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ public override IEnumerable<GltfMaterialBase> Materials {
}
}

public override IEnumerable<GltfMeshBase> Meshes {
get {
if (meshes == null) { return new GltfMeshBase[0]; }
return meshes.Values.Cast<GltfMeshBase>();
}
}

// Disposable pattern, with Dispose(void) and Dispose(bool), as recommended by:
// https://docs.microsoft.com/en-us/dotnet/api/system.idisposable
protected override void Dispose(bool disposing) {
Expand Down
7 changes: 7 additions & 0 deletions UnitySDK/Assets/TiltBrush/Scripts/Gltf/Gltf2Schema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ public override IEnumerable<GltfMaterialBase> Materials {
}
}

public override IEnumerable<GltfMeshBase> Meshes {
get {
if (meshes == null) { return new GltfMeshBase[0]; }
return meshes.Cast<GltfMeshBase>();
}
}

// Disposable pattern, with Dispose(void) and Dispose(bool), as recommended by:
// https://docs.microsoft.com/en-us/dotnet/api/system.idisposable
protected override void Dispose(bool disposing) {
Expand Down
1 change: 1 addition & 0 deletions UnitySDK/Assets/TiltBrush/Scripts/Gltf/GltfSchemaCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ public abstract class GltfRootBase : IDisposable {
public abstract IEnumerable<GltfImageBase> Images { get; }
public abstract IEnumerable<GltfTextureBase> Textures { get; }
public abstract IEnumerable<GltfMaterialBase> Materials { get; }
public abstract IEnumerable<GltfMeshBase> Meshes { get; }

public abstract void Dereference(IUriLoader uriLoader = null, PolyFormat gltfFormat = null);

Expand Down
17 changes: 17 additions & 0 deletions UnitySDK/Assets/TiltBrush/Scripts/Gltf/ImportGltf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public static class ImportGltf {
/// </summary>
private const float MIN_VALID_NORMAL_SQRMAGNITUDE = float.Epsilon; // Only fix normals that are exactly the 0 vector.

// Attributes intended only for the Tilt Brush Toolkit are prefixed with this, eg
// "_TB_UNITY_TEXCOORD_0". This distinguishes them a true TEXCOORD_0 that matches gltf's semantics
private const string kTookitAttributePrefix = "_TB_UNITY_";

private static readonly JsonSerializer kSerializer = new JsonSerializer {
ContractResolver = new GltfJsonContractResolver()
};
Expand Down Expand Up @@ -166,6 +170,19 @@ public static ImportState BeginImport(
var root = DeserializeGltfRoot(gltfVersion, reader);
root.Dereference(uriLoader);

// Convert attribute names to the ones we expect.
// This may eg overwrite TEXCOORD_0 with _TB_UNITY_TEXCOORD_0 (which will contain more data)
foreach (var mesh in root.Meshes) {
foreach (var prim in mesh.Primitives) {
string[] attrs = prim.GetAttributeNames()
.Where(n => n.StartsWith(kTookitAttributePrefix)).ToArray();
foreach (var tbAttr in attrs) {
string renamed = tbAttr.Substring(kTookitAttributePrefix.Length);
prim.ReplaceAttribute(tbAttr, renamed);
}
}
}

// Extract Google-specific information.
if (root.asset != null) {
// Get the generator information so we can tell which app and version generated this file.
Expand Down

0 comments on commit cfcfd60

Please sign in to comment.