Skip to content

Commit

Permalink
glb2: Add Glb2Importer
Browse files Browse the repository at this point in the history
Mostly copy/paste from Glb1Importer but it returns a different schema
version and uses the standard ".glb" file extension.

Change-Id: Id000b01593e8277a7e9cc5b4209aa7ebf8d7bd99
  • Loading branch information
dubois committed Sep 17, 2019
1 parent 1c8bd40 commit e555daa
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
72 changes: 72 additions & 0 deletions UnitySDK/Assets/TiltBrush/Scripts/Editor/Glb2Importer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


using Object = UnityEngine.Object;
#if UNITY_2017_1_OR_NEWER
using System;
using System.IO;

using UnityEngine;
using UnityEditor;
using UnityEditor.Experimental.AssetImporters;

namespace TiltBrushToolkit {

[ScriptedImporter(kVersion, "glb", kImportQueueOffset)]
public class Glb2Importer : ScriptedImporter {
const int kVersion = 2;
// ImportGltf needs to reference meshes and textures, so the glb import
// must come after them. We're assuming that Unity built-in asset types
// import at queue offset = 0.
const int kImportQueueOffset = 1;

private static readonly PolyImportOptions kOptions = new PolyImportOptions {
rescalingMode = PolyImportOptions.RescalingMode.CONVERT,
scaleFactor = 1,
recenter = false,
};

public override void OnImportAsset(AssetImportContext ctx) {
StringReader gltfStream = new StringReader(GlbParser.GetJsonChunkAsString(ctx.assetPath));
IUriLoader loader = new BufferedStreamLoader(
ctx.assetPath, Path.GetDirectoryName(ctx.assetPath));

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

// The "identifier" param passed here is supposed to be:
// - Unique to this asset
// - Deterministic (if possible)
foreach (var obj in result.textures) {
if (! AssetDatabase.Contains(obj)) {
ctx.AddObjectToAsset("Texture/" + obj.name, obj);
}
}
foreach (var obj in result.materials) {
ctx.AddObjectToAsset("Material/" + obj.name, obj);
}
foreach (var obj in result.meshes) {
ctx.AddObjectToAsset("Mesh/" + obj.name, obj);
}
string objectName = Path.GetFileNameWithoutExtension(ctx.assetPath);
result.root.name = objectName;
ctx.AddObjectToAsset("ROOT", result.root);
ctx.SetMainObject(result.root);
}
}

} // namespace TiltBrushToolkit

#endif
11 changes: 11 additions & 0 deletions UnitySDK/Assets/TiltBrush/Scripts/Editor/Glb2Importer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e555daa

Please sign in to comment.