Skip to content

Commit

Permalink
Bring gltf code in line with Tilt Brush 23
Browse files Browse the repository at this point in the history
- Add ImportMaterialCollector to API (unused in TBT; only used in TB)
- Rename PolyImportOptions -> GltfImportOptions
- Remove unused ability to satisfy URIs with PolyFormat bundles
- Remove unused GltfImportOptions "clientThrottledMainThread"
- Remove #if'd out "gvrss" code
  This part may come back, if I bring over the blocks-specific
  materials and material lookup tables

Change-Id: Id000b015f3c287ef28421f2f426d1330ef31cf17
  • Loading branch information
dubois committed Feb 4, 2020
1 parent e7a37d6 commit 825aeab
Show file tree
Hide file tree
Showing 15 changed files with 128 additions and 180 deletions.
6 changes: 3 additions & 3 deletions UnitySDK/Assets/TiltBrush/Scripts/Editor/Glb1Importer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public class Glb1Importer : ScriptedImporter {
// import at queue offset = 0.
const int kImportQueueOffset = 1;

private static readonly PolyImportOptions kOptions = new PolyImportOptions {
rescalingMode = PolyImportOptions.RescalingMode.CONVERT,
private static readonly GltfImportOptions kOptions = new GltfImportOptions {
rescalingMode = GltfImportOptions.RescalingMode.CONVERT,
scaleFactor = 1,
recenter = false,
};
Expand All @@ -44,7 +44,7 @@ public override void OnImportAsset(AssetImportContext ctx) {
ctx.assetPath, Path.GetDirectoryName(ctx.assetPath));

ImportGltf.GltfImportResult result = ImportGltf.Import(
GltfSchemaVersion.GLTF1, gltfStream, loader, kOptions);
GltfSchemaVersion.GLTF1, gltfStream, loader, null, kOptions);

// The "identifier" param passed here is supposed to be:
// - Unique to this asset
Expand Down
6 changes: 3 additions & 3 deletions UnitySDK/Assets/TiltBrush/Scripts/Editor/Glb2Importer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public class Glb2Importer : ScriptedImporter {
// import at queue offset = 0.
const int kImportQueueOffset = 1;

private static readonly PolyImportOptions kOptions = new PolyImportOptions {
rescalingMode = PolyImportOptions.RescalingMode.CONVERT,
private static readonly GltfImportOptions kOptions = new GltfImportOptions {
rescalingMode = GltfImportOptions.RescalingMode.CONVERT,
scaleFactor = 1,
recenter = false,
};
Expand All @@ -44,7 +44,7 @@ public override void OnImportAsset(AssetImportContext ctx) {
ctx.assetPath, Path.GetDirectoryName(ctx.assetPath));

ImportGltf.GltfImportResult result = ImportGltf.Import(
GltfSchemaVersion.GLTF2, gltfStream, loader, kOptions);
GltfSchemaVersion.GLTF2, gltfStream, loader, null, kOptions);

// The "identifier" param passed here is supposed to be:
// - Unique to this asset
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
namespace TiltBrushToolkit {

public class ModelImportSettings : AssetPostprocessor {
private readonly Version kToolkitVersion = TbtSettings.Version;
private readonly Version kToolkitVersion = TbtSettings.TbtVersion;
readonly Version kRequiredFbxExportVersion = new Version { major=10 };

public static bool sm_forceOldMeshNamingConvention = false;
Expand Down
13 changes: 2 additions & 11 deletions UnitySDK/Assets/TiltBrush/Scripts/Gltf/Gltf1Schema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ protected override void Dispose(bool disposing) {
}

/// Map glTFid values (ie, string names) names to the objects they refer to
public override void Dereference(IUriLoader uriLoader = null, PolyFormat gltfFormat = null) {
public override void Dereference(IUriLoader uriLoader = null) {
// "dereference" all the names
scenePtr = scenes[scene];
foreach (var pair in buffers) {
Expand All @@ -102,17 +102,8 @@ public override void Dereference(IUriLoader uriLoader = null, PolyFormat gltfFor
Debug.Assert(buffer.uri != null);
}
buffer.data = uriLoader.Load(buffer.uri);
} else if (gltfFormat != null) {
// Runtime import case; the uris refer to resource files in the PolyFormat.
Debug.Assert(buffer.type == "arraybuffer");
foreach (PolyFile resource in gltfFormat.resources) {
if (resource.relativePath == buffer.uri) {
buffer.data = new Reader(resource.contents);
break;
}
}
}
}
}
foreach (var pair in accessors) {
pair.Value.gltfId = pair.Key;
pair.Value.bufferViewPtr = bufferViews[pair.Value.bufferView];
Expand Down
10 changes: 1 addition & 9 deletions UnitySDK/Assets/TiltBrush/Scripts/Gltf/Gltf2Schema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ protected override void Dispose(bool disposing) {
}

/// Map gltfIndex values (ie, int indices) names to the objects they refer to
public override void Dereference(IUriLoader uriLoader = null, PolyFormat gltfFormat = null) {
public override void Dereference(IUriLoader uriLoader = null) {
// "dereference" all the indices
scenePtr = scenes[scene];
for (int i = 0; i < buffers.Count; i++) {
Expand All @@ -97,14 +97,6 @@ public override void Dereference(IUriLoader uriLoader = null, PolyFormat gltfFor
// Only id 0 may lack a URI; this indicates that it is the binary chunk.
Debug.Assert(! (i != 0 && buffer.uri == null));
buffer.data = uriLoader.Load(buffer.uri);
} else if (gltfFormat != null) {
// Runtime import case; the uris refer to resource files in the PolyFormat.
foreach (PolyFile resource in gltfFormat.resources) {
if (resource.relativePath == buffer.uri) {
buffer.data = new Reader(resource.contents);
break;
}
}
}
}
for (int i = 0; i < accessors.Count; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,24 @@

using System;

using UnityEngine;

namespace TiltBrushToolkit {
/// <summary>
/// Options that indicate how to import a given asset.
/// </summary>
[Serializable]
[AutoStringifiable]
public struct PolyImportOptions {
public struct GltfImportOptions {
public enum RescalingMode {
// Convert the object's units to scene units and optionally apply a scale as well
// (given by scaleFactor).
// Apply scaleFactor.
CONVERT,
// Scale the object such that it fits a box of a particular size (desiredSize).
// Scale the object such that it fits a box of desiredSize, ignoring scaleFactor.
FIT,
}

/// <summary>
/// If not set, axis conventions default to the glTF 2.0 standard.
/// </summary>
public AxisConvention? axisConventionOverride;

/// <summary>
/// What type of rescaling to perform.
/// </summary>
Expand All @@ -53,30 +54,16 @@ public enum RescalingMode {
/// </summary>
public bool recenter;

/// <summary>
/// If true, do not immediately perform heavy main thread operations (mesh import, texture creation,
/// etc) on import. Rather, an enumerator will be returned (mainThreadThrottler) in PolyImportResult
/// which you can enumerate to gradually create meshes and perform other heavy UI thread operations.
/// This option is useful for performance-sensitive applications that want to be in full control of
/// when Unity objects are created on the main thread.
/// </summary>
[HideInInspector]
public bool clientThrottledMainThread;

/// <summary>
/// Returns a default set of import options.
/// </summary>
public static PolyImportOptions Default() {
PolyImportOptions options = new PolyImportOptions();
public static GltfImportOptions Default() {
GltfImportOptions options = new GltfImportOptions();
options.recenter = true;
options.rescalingMode = RescalingMode.CONVERT;
options.scaleFactor = 1.0f;
options.desiredSize = 1.0f;
return options;
}

public override string ToString() {
return AutoStringify.Stringify(this);
}
}
}
26 changes: 4 additions & 22 deletions UnitySDK/Assets/TiltBrush/Scripts/Gltf/GltfMaterialConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,31 +173,13 @@ private static bool IsGlobalMaterial(GltfMaterialBase gltfMaterial) {
/// <returns>The global material that corresponds to the given GLTF material,
/// if found. If not found, null.</returns>
private static Material LookUpGlobalMaterial(GltfMaterialBase gltfMaterial) {
// Is this a Blocks gvrss material?
#if false
if (gltfMaterial.TechniqueExtras != null) {
string surfaceShader = null;
gltfMaterial.TechniqueExtras.TryGetValue("gvrss", out surfaceShader);

if (surfaceShader != null) {
// Blocks material. Look up the mapping in PtSettings.
Material material = PtSettings.Instance.LookupSurfaceShaderMaterial(surfaceShader);
if (material != null) {
return material;
} else {
Debug.LogWarningFormat("Unknown gvrss surface shader {0}", surfaceShader);
}
}
}
#endif

// Check if it's a Tilt Brush material.
Guid guid = ParseGuidFromMaterial(gltfMaterial);
if (guid != Guid.Empty) {
// Tilt Brush global material. PBR materials will use unrecognized guids;
// these will be handled by the caller.
BrushDescriptor desc;
if (TiltBrushToolkit.TbtSettings.BrushManifest.BrushesByGuid.TryGetValue(guid, out desc)) {
if (TbtSettings.Instance.TryGetBrush(guid, out desc)) {
return desc.Material;
}
}
Expand Down Expand Up @@ -227,7 +209,7 @@ private static Material LookUpGlobalMaterial(GltfMaterialBase gltfMaterial) {
Guid templateGuid = ParseGuidFromShader(gltfMat);

BrushDescriptor desc;
if (!TiltBrushToolkit.TbtSettings.BrushManifest.BrushesByGuid.TryGetValue(templateGuid, out desc)) {
if (!TbtSettings.Instance.TryGetBrush(templateGuid, out desc)) {
// If they are the same, there is no template/instance relationship.
if (instanceGuid != templateGuid) {
Debug.LogErrorFormat("Unexpected: cannot find template material {0} for {1}",
Expand Down Expand Up @@ -445,7 +427,7 @@ public static BrushDescriptor LookupBrushDescriptor(GltfMaterialBase gltfMateria
return null;
} else {
BrushDescriptor desc;
TiltBrushToolkit.TbtSettings.BrushManifest.BrushesByGuid.TryGetValue(
TbtSettings.Instance.TryGetBrush(
guid, out desc);
if (desc == null) {
// Maybe it's templated from a pbr material; the template guid
Expand All @@ -455,7 +437,7 @@ public static BrushDescriptor LookupBrushDescriptor(GltfMaterialBase gltfMateria
return null;
}
Guid templateGuid = ParseGuidFromShader((Gltf1Material)gltfMaterial);
TiltBrushToolkit.TbtSettings.BrushManifest.BrushesByGuid.TryGetValue(
TbtSettings.Instance.TryGetBrush(
templateGuid, out desc);
}
return desc;
Expand Down
4 changes: 3 additions & 1 deletion UnitySDK/Assets/TiltBrush/Scripts/Gltf/GltfSchemaCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public abstract class GltfRootBase : IDisposable {
public abstract IEnumerable<GltfMaterialBase> Materials { get; }
public abstract IEnumerable<GltfMeshBase> Meshes { get; }

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

// Disposable pattern, with Dispose(void) and Dispose(bool), as recommended by:
// https://docs.microsoft.com/en-us/dotnet/api/system.idisposable
Expand Down Expand Up @@ -214,6 +214,8 @@ public abstract class GltfPrimitiveBase {
public abstract GltfMaterialBase MaterialPtr { get; }
public abstract GltfAccessorBase IndicesPtr { get; }
public abstract GltfAccessorBase GetAttributePtr(string attributeName);
// Rename attribute from original -> replacement.
// ie, attrs[replacement] = attrs.pop(original)
public abstract void ReplaceAttribute(string original, string replacement);
public abstract HashSet<string> GetAttributeNames();
}
Expand Down
3 changes: 2 additions & 1 deletion UnitySDK/Assets/TiltBrush/Scripts/Gltf/IUriLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public interface IUriLoader {


// ----------------------------------------------------------------------

public class Reader : IBufferReader {
private byte[] data;

Expand Down Expand Up @@ -113,6 +113,7 @@ public class BufferedStreamLoader : IUriLoader {
private string uriBase;
private int bufferSize;

/// glbPath is the .gltf or .glb file being read; or null.
public BufferedStreamLoader(string glbPath, string uriBase, int bufferSize=4096) {
this.glbPath = glbPath;
this.uriBase = uriBase;
Expand Down
Loading

0 comments on commit 825aeab

Please sign in to comment.