From 80f1998c9c5c81c8d9023c497eea059f25e702b9 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 8 Mar 2021 13:46:40 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=B3=E3=83=BC=E3=83=89=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ScriptedImporter/ScriptedImporterImpl.cs | 72 +++++++++++++------ 1 file changed, 51 insertions(+), 21 deletions(-) diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs index f261057c9..f354671d0 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs @@ -1,4 +1,4 @@ - +using System.Collections.Generic; using System.Linq; using UnityEditor.Experimental.AssetImporters; using UnityEngine; @@ -7,70 +7,100 @@ namespace UniGLTF { public static class ScriptedImporterImpl { - public static void Import(ScriptedImporter scriptedImporter, AssetImportContext importerContext, Axises reverseAxis) + /// + /// glb をパースして、UnityObject化、さらにAsset化する + /// + /// + /// + /// + public static void Import(ScriptedImporter scriptedImporter, AssetImportContext context, Axises reverseAxis) { #if VRM_DEVELOP - Debug.Log("OnImportAsset to " + importerContext.assetPath); + Debug.Log("OnImportAsset to " + context.assetPath); #endif + var loaded = Load(context.assetPath, + scriptedImporter.GetExternalObjectMap().Select(kv => (kv.Key.name, kv.Value)), + reverseAxis + ); + + AddSubAssets(context, loaded); + } + + /// + /// Parse して、UnityObject 化する。 + /// + /// TODO: すべての UnityObject を ImporterContext が所有する。(Disposeで削除できる) + /// + /// glbのパス + /// ScriptedImporter外部に作成済みのAssetへの参照 + /// gltf から unityへの座標変換オプション + /// + static ImporterContext Load(string assetPath, IEnumerable<(string, UnityEngine.Object)> externalObjectMap, Axises reverseAxis) + { // // Parse(parse glb, parser gltf json) // var parser = new GltfParser(); - parser.ParsePath(importerContext.assetPath); + parser.ParsePath(assetPath); // // Import(create unity objects) // - var context = new ImporterContext(parser, null, - scriptedImporter.GetExternalObjectMap() - .Select(kv => (kv.Key.name, kv.Value)) - ); + var context = new ImporterContext(parser, null, externalObjectMap); context.InvertAxis = reverseAxis; context.Load(); context.ShowMeshes(); - // - // SubAsset - // + return context; + } + /// + /// UnityObjectをSubAsset化する。 + /// + /// TODO: SubAsset化されると、ImporterContext から所有権を除去する + /// + /// + /// + static void AddSubAssets(AssetImportContext context, ImporterContext loaded) + { // Texture - foreach (var info in context.TextureFactory.Textures) + foreach (var info in loaded.TextureFactory.Textures) { if (info.IsSubAsset) { var texture = info.Texture; - importerContext.AddObjectToAsset(texture.name, texture); + context.AddObjectToAsset(texture.name, texture); } } // Material - foreach (var info in context.MaterialFactory.Materials) + foreach (var info in loaded.MaterialFactory.Materials) { if (info.IsSubAsset) { var material = info.Asset; - importerContext.AddObjectToAsset(material.name, material); + context.AddObjectToAsset(material.name, material); } } // Mesh - foreach (var mesh in context.Meshes.Select(x => x.Mesh)) + foreach (var mesh in loaded.Meshes.Select(x => x.Mesh)) { // all mesh is subasset - importerContext.AddObjectToAsset(mesh.name, mesh); + context.AddObjectToAsset(mesh.name, mesh); } // Animation - foreach (var clip in context.AnimationClips) + foreach (var clip in loaded.AnimationClips) { // all animation is subasset - importerContext.AddObjectToAsset(clip.name, clip); + context.AddObjectToAsset(clip.name, clip); } // Root GameObject is main object - importerContext.AddObjectToAsset(context.Root.name, context.Root); - importerContext.SetMainObject(context.Root); + context.AddObjectToAsset(loaded.Root.name, loaded.Root); + context.SetMainObject(loaded.Root); } } }