Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring GltfParser #1076

Merged
merged 5 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace UniGLTF
public class GlbScriptedImporterEditorGUI : RemapScriptedImporterEditorBase
{
GlbScriptedImporter m_importer;
GltfParser m_parser;
GltfData m_data;

RemapEditorMaterial m_materialEditor;
RemapEditorAnimation m_animationEditor;
Expand All @@ -27,14 +27,13 @@ public override void OnEnable()
base.OnEnable();

m_importer = target as GlbScriptedImporter;
m_parser = new GltfParser();
m_parser.ParsePath(m_importer.assetPath);
m_data = new GlbFileParser(m_importer.assetPath).Parse();

var materialGenerator = new GltfMaterialDescriptorGenerator();
var materialKeys = m_parser.GLTF.materials.Select((_, i) => materialGenerator.Get(m_parser, i).SubAssetKey);
var textureKeys = new GltfTextureDescriptorGenerator(m_parser).Get().GetEnumerable().Select(x => x.SubAssetKey);
var materialKeys = m_data.GLTF.materials.Select((_, i) => materialGenerator.Get(m_data, i).SubAssetKey);
var textureKeys = new GltfTextureDescriptorGenerator(m_data).Get().GetEnumerable().Select(x => x.SubAssetKey);
m_materialEditor = new RemapEditorMaterial(materialKeys.Concat(textureKeys), GetEditorMap, SetEditorMap);
m_animationEditor = new RemapEditorAnimation(AnimationImporterUtil.EnumerateSubAssetKeys(m_parser.GLTF), GetEditorMap, SetEditorMap);
m_animationEditor = new RemapEditorAnimation(AnimationImporterUtil.EnumerateSubAssetKeys(m_data.GLTF), GetEditorMap, SetEditorMap);
}

enum Tabs
Expand All @@ -57,13 +56,13 @@ public override void OnInspectorGUI()
break;

case Tabs.Animation:
m_animationEditor.OnGUI(m_importer, m_parser);
m_animationEditor.OnGUI(m_importer, m_data);
RevertApplyRemapGUI(m_importer);
break;

case Tabs.Materials:
m_materialEditor.OnGUI(m_importer, m_parser,
new GltfTextureDescriptorGenerator(m_parser),
m_materialEditor.OnGUI(m_importer, m_data,
new GltfTextureDescriptorGenerator(m_data),
assetPath => $"{Path.GetFileNameWithoutExtension(assetPath)}.Textures",
assetPath => $"{Path.GetFileNameWithoutExtension(assetPath)}.Materials");
RevertApplyRemapGUI(m_importer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace UniGLTF
public class GltfScriptedImporterEditorGUI : RemapScriptedImporterEditorBase
{
GltfScriptedImporter m_importer;
GltfParser m_parser;
GltfData m_data;

RemapEditorMaterial m_materialEditor;
RemapEditorAnimation m_animationEditor;
Expand All @@ -27,14 +27,13 @@ public override void OnEnable()
base.OnEnable();

m_importer = target as GltfScriptedImporter;
m_parser = new GltfParser();
m_parser.ParsePath(m_importer.assetPath);
m_data = new AmbiguousGltfFileParser(m_importer.assetPath).Parse();

var materialGenerator = new GltfMaterialDescriptorGenerator();
var materialKeys = m_parser.GLTF.materials.Select((_, i) => materialGenerator.Get(m_parser, i).SubAssetKey);
var textureKeys = new GltfTextureDescriptorGenerator(m_parser).Get().GetEnumerable().Select(x => x.SubAssetKey);
var materialKeys = m_data.GLTF.materials.Select((_, i) => materialGenerator.Get(m_data, i).SubAssetKey);
var textureKeys = new GltfTextureDescriptorGenerator(m_data).Get().GetEnumerable().Select(x => x.SubAssetKey);
m_materialEditor = new RemapEditorMaterial(materialKeys.Concat(textureKeys), GetEditorMap, SetEditorMap);
m_animationEditor = new RemapEditorAnimation(AnimationImporterUtil.EnumerateSubAssetKeys(m_parser.GLTF), GetEditorMap, SetEditorMap);
m_animationEditor = new RemapEditorAnimation(AnimationImporterUtil.EnumerateSubAssetKeys(m_data.GLTF), GetEditorMap, SetEditorMap);
}

enum Tabs
Expand All @@ -57,13 +56,13 @@ public override void OnInspectorGUI()
break;

case Tabs.Animation:
m_animationEditor.OnGUI(m_importer, m_parser);
m_animationEditor.OnGUI(m_importer, m_data);
RevertApplyRemapGUI(m_importer);
break;

case Tabs.Materials:
m_materialEditor.OnGUI(m_importer, m_parser,
new GltfTextureDescriptorGenerator(m_parser),
m_materialEditor.OnGUI(m_importer, m_data,
new GltfTextureDescriptorGenerator(m_data),
assetPath => $"{Path.GetFileNameWithoutExtension(assetPath)}.Textures",
assetPath => $"{Path.GetFileNameWithoutExtension(assetPath)}.Materials");
RevertApplyRemapGUI(m_importer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class RemapEditorAnimation : RemapEditorBase
public RemapEditorAnimation(IEnumerable<SubAssetKey> keys, EditorMapGetterFunc getter, EditorMapSetterFunc setter) : base(keys, getter, setter)
{ }

public void OnGUI(ScriptedImporter importer, GltfParser parser)
public void OnGUI(ScriptedImporter importer, GltfData data)
{
if (!HasKeys)
{
Expand All @@ -24,7 +24,7 @@ public void OnGUI(ScriptedImporter importer, GltfParser parser)
{
if (GUILayout.Button("Extract Animation ..."))
{
Extract(importer, parser);
Extract(importer, data);
}
EditorGUILayout.HelpBox("Extract subasset to external object and overwrite remap", MessageType.Info);
}
Expand All @@ -40,7 +40,7 @@ public void OnGUI(ScriptedImporter importer, GltfParser parser)
DrawRemapGUI<AnimationClip>(importer.GetExternalObjectMap());
}

public static void Extract(ScriptedImporter importer, GltfParser parser)
public static void Extract(ScriptedImporter importer, GltfData data)
{
if (string.IsNullOrEmpty(importer.assetPath))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class RemapEditorMaterial : RemapEditorBase
public RemapEditorMaterial(IEnumerable<SubAssetKey> keys, EditorMapGetterFunc getter, EditorMapSetterFunc setter) : base(keys, getter, setter)
{ }

public void OnGUI(ScriptedImporter importer, GltfParser parser,
public void OnGUI(ScriptedImporter importer, GltfData data,
ITextureDescriptorGenerator textureDescriptorGenerator,
Func<string, string> textureDir,
Func<string, string> materialDir)
Expand All @@ -34,7 +34,7 @@ public void OnGUI(ScriptedImporter importer, GltfParser parser,
{
if (GUILayout.Button("Extract Materials And Textures ..."))
{
ExtractMaterialsAndTextures(importer, parser, textureDescriptorGenerator, textureDir, materialDir);
ExtractMaterialsAndTextures(importer, data, textureDescriptorGenerator, textureDir, materialDir);
}
EditorGUILayout.HelpBox("Extract subasset to external object and overwrite remap", MessageType.Info);
}
Expand Down Expand Up @@ -63,7 +63,7 @@ public void OnGUI(ScriptedImporter importer, GltfParser parser,
}
}

void ExtractMaterialsAndTextures(ScriptedImporter self, GltfParser parser, ITextureDescriptorGenerator textureDescriptorGenerator, Func<string, string> textureDir, Func<string, string> materialDir)
void ExtractMaterialsAndTextures(ScriptedImporter self, GltfData data, ITextureDescriptorGenerator textureDescriptorGenerator, Func<string, string> textureDir, Func<string, string> materialDir)
{
if (string.IsNullOrEmpty(self.assetPath))
{
Expand All @@ -90,10 +90,10 @@ void ExtractMaterialsAndTextures(ScriptedImporter self, GltfParser parser, IText
.ToDictionary(kv => kv.Item1, kv => kv.Item2)
;

var assetPath = UnityPath.FromFullpath(parser.TargetPath);
var assetPath = UnityPath.FromFullpath(data.TargetPath);
var dirName = textureDir(assetPath.Value); // $"{assetPath.FileNameWithoutExtension}.Textures";
TextureExtractor.ExtractTextures(
parser,
data,
assetPath.Parent.Child(dirName),
textureDescriptorGenerator,
subAssets,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public static void Import(ScriptedImporter scriptedImporter, AssetImportContext
//
// Parse(parse glb, parser gltf json)
//
var parser = new GltfParser();
parser.ParsePath(scriptedImporter.assetPath);
var data = new AmbiguousGltfFileParser(scriptedImporter.assetPath).Parse();


//
// Import(create unity objects)
Expand All @@ -39,7 +39,7 @@ public static void Import(ScriptedImporter scriptedImporter, AssetImportContext
.Where(x => x.Value != null)
.ToDictionary(kv => new SubAssetKey(kv.Value.GetType(), kv.Key.name), kv => kv.Value);

using (var loader = new ImporterContext(parser, extractedObjects))
using (var loader = new ImporterContext(data, extractedObjects))
{
// Configure TextureImporter to Extracted Textures.
foreach (var textureInfo in loader.TextureDescriptorGenerator.Get().GetEnumerable())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ public class TextureExtractor
{
const string TextureDirName = "Textures";

GltfParser m_parser;
public GltfParser Parser => m_parser;
GltfData m_data;
public GltfData Data => m_data;

public glTF GLTF => m_parser.GLTF;
public IStorage Storage => m_parser.Storage;
public glTF GLTF => m_data.GLTF;
public IStorage Storage => m_data.Storage;

public readonly Dictionary<SubAssetKey, UnityPath> Textures = new Dictionary<SubAssetKey, UnityPath>();
private readonly IReadOnlyDictionary<SubAssetKey, Texture> m_subAssets;
UnityPath m_textureDirectory;

public TextureExtractor(GltfParser parser, UnityPath textureDirectory, IReadOnlyDictionary<SubAssetKey, Texture> subAssets)
public TextureExtractor(GltfData data, UnityPath textureDirectory, IReadOnlyDictionary<SubAssetKey, Texture> subAssets)
{
m_parser = parser;
m_data = data;
m_textureDirectory = textureDirectory;
m_textureDirectory.EnsureFolder();
m_subAssets = subAssets;
Expand Down Expand Up @@ -74,12 +74,12 @@ public void Extract(SubAssetKey key, TextureDescriptor texDesc)
/// <param name="importer"></param>
/// <param name="dirName"></param>
/// <param name="onCompleted"></param>
public static void ExtractTextures(GltfParser parser, UnityPath textureDirectory,
public static void ExtractTextures(GltfData data, UnityPath textureDirectory,
ITextureDescriptorGenerator textureDescriptorGenerator, IReadOnlyDictionary<SubAssetKey, Texture> subAssets,
Action<SubAssetKey, Texture2D> addRemap,
Action<IEnumerable<UnityPath>> onCompleted = null)
{
var extractor = new TextureExtractor(parser, textureDirectory, subAssets);
var extractor = new TextureExtractor(data, textureDirectory, subAssets);
foreach (var param in textureDescriptorGenerator.Get().GetEnumerable())
{
extractor.Extract(param.SubAssetKey, param);
Expand Down
66 changes: 66 additions & 0 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnityEngine;

namespace UniGLTF
{
public sealed class GltfData
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GltfParser から別れた。
ImporterContext にはこいつを渡す。

{
/// <summary>
/// Source file path.
/// Maybe empty if source file was on memory.
/// </summary>
public string TargetPath { get; }

/// <summary>
/// JSON source
/// </summary>
public string Json { get; }

/// <summary>
/// GLTF parsed from JSON
/// </summary>
public glTF GLTF { get; }

/// <summary>
/// Chunk Data.
/// Maybe empty if source file was not glb format.
/// </summary>
public IReadOnlyList<GlbChunk> Chunks { get; }

/// <summary>
/// URI access
/// </summary>
public IStorage Storage { get; }

/// <summary>
/// Migration Flags used by ImporterContext
/// </summary>
public MigrationFlags MigrationFlags { get; }

public GltfData(string targetPath, string json, glTF gltf, IReadOnlyList<GlbChunk> chunks, IStorage storage, MigrationFlags migrationFlags)
{
TargetPath = targetPath;
Json = json;
GLTF = gltf;
Chunks = chunks;
Storage = storage;
MigrationFlags = migrationFlags;
}

public static GltfData CreateFromGltfDataForTest(glTF gltf)
{
return new GltfData(
string.Empty,
string.Empty,
gltf,
new List<GlbChunk>(),
new SimpleStorage(new ArraySegment<byte>()),
new MigrationFlags()
);
}
}
}
24 changes: 12 additions & 12 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public class ImporterContext : IResponsibilityForDestroyObjects
IReadOnlyDictionary<SubAssetKey, UnityEngine.Object> _externalObjectMap;

public ImporterContext(
GltfParser parser,
GltfData data,
IReadOnlyDictionary<SubAssetKey, UnityEngine.Object> externalObjectMap = null,
ITextureDeserializer textureDeserializer = null)
{
Parser = parser;
TextureDescriptorGenerator = new GltfTextureDescriptorGenerator(Parser);
Data = data;
TextureDescriptorGenerator = new GltfTextureDescriptorGenerator(Data);
MaterialDescriptorGenerator = new GltfMaterialDescriptorGenerator();

_externalObjectMap = externalObjectMap ?? new Dictionary<SubAssetKey, UnityEngine.Object>();
Expand All @@ -33,17 +33,17 @@ public ImporterContext(
TextureFactory = new TextureFactory(textureDeserializer, _externalObjectMap
.Where(x => x.Value is Texture)
.ToDictionary(x => x.Key, x => (Texture)x.Value),
Parser.MigrationFlags.IsRoughnessTextureValueSquared);
Data.MigrationFlags.IsRoughnessTextureValueSquared);
MaterialFactory = new MaterialFactory(_externalObjectMap
.Where(x => x.Value is Material)
.ToDictionary(x => x.Key, x => (Material)x.Value));
}

#region Source
public GltfParser Parser { get; }
public String Json => Parser.Json;
public glTF GLTF => Parser.GLTF;
public IStorage Storage => Parser.Storage;
public GltfData Data { get; }
public String Json => Data.Json;
public glTF GLTF => Data.GLTF;
public IStorage Storage => Data.Storage;
#endregion

// configuration
Expand Down Expand Up @@ -235,17 +235,17 @@ public async Task LoadMaterialsAsync(IAwaitCaller awaitCaller = null)
{
awaitCaller = awaitCaller ?? new ImmediateCaller();

if (Parser.GLTF.materials == null || Parser.GLTF.materials.Count == 0)
if (Data.GLTF.materials == null || Data.GLTF.materials.Count == 0)
{
// no material. work around.
var param = MaterialDescriptorGenerator.Get(Parser, 0);
var param = MaterialDescriptorGenerator.Get(Data, 0);
var material = await MaterialFactory.LoadAsync(param, TextureFactory.GetTextureAsync, awaitCaller);
}
else
{
for (int i = 0; i < Parser.GLTF.materials.Count; ++i)
for (int i = 0; i < Data.GLTF.materials.Count; ++i)
{
var param = MaterialDescriptorGenerator.Get(Parser, i);
var param = MaterialDescriptorGenerator.Get(Data, i);
var material = await MaterialFactory.LoadAsync(param, TextureFactory.GetTextureAsync, awaitCaller);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static RuntimeGltfInstance Load(this ImporterContext self)
}

#if VRM_DEVELOP
Debug.Log($"{self.Parser.TargetPath}: {meassureTime.GetSpeedLog()}");
Debug.Log($"{self.Data.TargetPath}: {meassureTime.GetSpeedLog()}");
#endif

return task.Result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ namespace UniGLTF
/// </summary>
public sealed class GltfMaterialDescriptorGenerator : IMaterialDescriptorGenerator
{
public MaterialDescriptor Get(GltfParser parser, int i)
public MaterialDescriptor Get(GltfData data, int i)
{
if (!GltfUnlitMaterialImporter.TryCreateParam(parser, i, out var param))
if (!GltfUnlitMaterialImporter.TryCreateParam(data, i, out var param))
{
if (!GltfPbrMaterialImporter.TryCreateParam(parser, i, out param))
if (!GltfPbrMaterialImporter.TryCreateParam(data, i, out param))
{
// fallback
#if VRM_DEVELOP
Expand Down
Loading