mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-04-25 15:47:26 -05:00
Introduce ImporterContextSettings for handling loading animation and
axes conversion. Refs: https://github.com/vrm-c/UniVRM/pull/1719#discussion_r908105469
This commit is contained in:
parent
f8120fb22d
commit
8881882873
|
|
@ -42,9 +42,10 @@ namespace UniGLTF
|
|||
.ToDictionary(kv => new SubAssetKey(kv.Value.GetType(), kv.Key.name), kv => kv.Value);
|
||||
|
||||
var materialGenerator = GetMaterialDescriptorGenerator(renderPipeline);
|
||||
var importerContextSettings = new ImporterContextSettings(loadAnimation: true, invertAxis: reverseAxis);
|
||||
|
||||
using (var data = new AutoGltfFileParser(scriptedImporter.assetPath).Parse())
|
||||
using (var loader = new ImporterContext(data, extractedObjects, materialGenerator: materialGenerator))
|
||||
using (var loader = new ImporterContext(data, extractedObjects, materialGenerator: materialGenerator, settings: importerContextSettings))
|
||||
{
|
||||
// Configure TextureImporter to Extracted Textures.
|
||||
foreach (var textureInfo in loader.TextureDescriptorGenerator.Get().GetEnumerable())
|
||||
|
|
@ -52,7 +53,6 @@ namespace UniGLTF
|
|||
TextureImporterConfigurator.Configure(textureInfo, loader.TextureFactory.ExternalTextures);
|
||||
}
|
||||
|
||||
loader.InvertAxis = reverseAxis;
|
||||
var loaded = loader.Load();
|
||||
loaded.ShowMeshes();
|
||||
|
||||
|
|
|
|||
|
|
@ -12,12 +12,14 @@ namespace UniGLTF
|
|||
/// </summary>
|
||||
public class ImporterContext : IResponsibilityForDestroyObjects
|
||||
{
|
||||
private readonly ImporterContextSettings _settings;
|
||||
|
||||
public ITextureDescriptorGenerator TextureDescriptorGenerator { get; protected set; }
|
||||
public IMaterialDescriptorGenerator MaterialDescriptorGenerator { get; protected set; }
|
||||
public TextureFactory TextureFactory { get; }
|
||||
public MaterialFactory MaterialFactory { get; }
|
||||
public AnimationClipFactory AnimationClipFactory { get; }
|
||||
public bool LoadAnimation { get; set; } = true;
|
||||
private bool LoadAnimation => _settings.LoadAnimation;
|
||||
|
||||
public IReadOnlyDictionary<SubAssetKey, UnityEngine.Object> ExternalObjectMap;
|
||||
|
||||
|
|
@ -29,12 +31,15 @@ namespace UniGLTF
|
|||
/// <param name="externalObjectMap">外部オブジェクトのリスト(主にScriptedImporterのRemapで使う)</param>
|
||||
/// <param name="textureDeserializer">Textureロードをカスタマイズする</param>
|
||||
/// <param name="materialGenerator">Materialロードをカスタマイズする(URP向け)</param>
|
||||
/// <param name="settings">ImporterContextの設定</param>
|
||||
public ImporterContext(
|
||||
GltfData data,
|
||||
IReadOnlyDictionary<SubAssetKey, UnityEngine.Object> externalObjectMap = null,
|
||||
ITextureDeserializer textureDeserializer = null,
|
||||
IMaterialDescriptorGenerator materialGenerator = null)
|
||||
IMaterialDescriptorGenerator materialGenerator = null,
|
||||
ImporterContextSettings settings = null)
|
||||
{
|
||||
_settings = settings ?? new ImporterContextSettings();
|
||||
Data = data;
|
||||
TextureDescriptorGenerator = new GltfTextureDescriptorGenerator(Data);
|
||||
MaterialDescriptorGenerator = materialGenerator ?? MaterialDescriptorGeneratorUtility.GetValidGltfMaterialDescriptorGenerator();
|
||||
|
|
@ -66,7 +71,7 @@ namespace UniGLTF
|
|||
/// <summary>
|
||||
/// GLTF から Unity に変換するときに反転させる軸
|
||||
/// </summary>
|
||||
public Axes InvertAxis = Axes.Z;
|
||||
private Axes InvertAxis => _settings.InvertAxis;
|
||||
|
||||
public static List<string> UnsupportedExtensions = new List<string>
|
||||
{
|
||||
|
|
|
|||
19
Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContextSettings.cs
Normal file
19
Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContextSettings.cs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
namespace UniGLTF
|
||||
{
|
||||
public class ImporterContextSettings
|
||||
{
|
||||
public bool LoadAnimation { get; }
|
||||
public Axes InvertAxis { get; }
|
||||
|
||||
/// <summary>
|
||||
/// ImporterContextの設定を指定する。
|
||||
/// </summary>
|
||||
/// <param name="loadAnimation">アニメーションをインポートする場合はtrueを指定(初期値はtrue)</param>
|
||||
/// <param name="invertAxis">GLTF から Unity に変換するときに反転させる軸を指定(初期値はAxes.Z)</param>
|
||||
public ImporterContextSettings(bool loadAnimation = true, Axes invertAxis = Axes.Z)
|
||||
{
|
||||
LoadAnimation = loadAnimation;
|
||||
InvertAxis = invertAxis;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 091d1bfaecd542b6a039fb7a3e3676f8
|
||||
timeCreated: 1723863327
|
||||
|
|
@ -76,10 +76,11 @@ namespace VRM
|
|||
var map = texturePaths
|
||||
.Select(x => x.LoadAsset<Texture>())
|
||||
.ToDictionary(x => new SubAssetKey(x), x => x as UnityEngine.Object);
|
||||
var settings = new ImporterContextSettings();
|
||||
|
||||
// 確実に Dispose するために敢えて再パースしている
|
||||
using (var data = new GlbFileParser(vrmPath).Parse())
|
||||
using (var context = new VRMImporterContext(new VRMData(data), externalObjectMap: map, loadAnimation: true))
|
||||
using (var context = new VRMImporterContext(new VRMData(data), externalObjectMap: map, settings: settings))
|
||||
{
|
||||
var editor = new VRMEditorImporterContext(context, prefabPath);
|
||||
foreach (var textureInfo in context.TextureDescriptorGenerator.Get().GetEnumerable())
|
||||
|
|
|
|||
|
|
@ -25,12 +25,11 @@ namespace VRM
|
|||
IReadOnlyDictionary<SubAssetKey, Object> externalObjectMap = null,
|
||||
ITextureDeserializer textureDeserializer = null,
|
||||
IMaterialDescriptorGenerator materialGenerator = null,
|
||||
bool loadAnimation = false)
|
||||
: base(data.Data, externalObjectMap, textureDeserializer, materialGenerator ?? VrmMaterialDescriptorGeneratorUtility.GetValidVrmMaterialDescriptorGenerator(data.VrmExtension))
|
||||
ImporterContextSettings settings = null)
|
||||
: base(data.Data, externalObjectMap, textureDeserializer, materialGenerator ?? VrmMaterialDescriptorGeneratorUtility.GetValidVrmMaterialDescriptorGenerator(data.VrmExtension), settings ?? new ImporterContextSettings(false))
|
||||
{
|
||||
_data = data;
|
||||
TextureDescriptorGenerator = new VrmTextureDescriptorGenerator(Data, VRM);
|
||||
LoadAnimation = loadAnimation;
|
||||
}
|
||||
|
||||
#region OnLoad
|
||||
|
|
|
|||
|
|
@ -39,11 +39,12 @@ namespace VRM
|
|||
{
|
||||
materialGen = materialGeneratorCallback(vrm.VrmExtension);
|
||||
}
|
||||
var importerContextSettings = new ImporterContextSettings(loadAnimation);
|
||||
using (var loader = new VRMImporterContext(
|
||||
vrm,
|
||||
textureDeserializer: textureDeserializer,
|
||||
materialGenerator: materialGen,
|
||||
loadAnimation: loadAnimation))
|
||||
settings: importerContextSettings))
|
||||
{
|
||||
if (metaCallback != null)
|
||||
{
|
||||
|
|
@ -96,11 +97,12 @@ namespace VRM
|
|||
{
|
||||
materialGen = materialGeneratorCallback(vrm.VrmExtension);
|
||||
}
|
||||
var importerContextSettings = new ImporterContextSettings(loadAnimation: loadAnimation);
|
||||
using (var loader = new VRMImporterContext(
|
||||
vrm,
|
||||
textureDeserializer: textureDeserializer,
|
||||
materialGenerator: materialGen,
|
||||
loadAnimation: loadAnimation))
|
||||
settings: importerContextSettings))
|
||||
{
|
||||
if (metaCallback != null)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -34,6 +34,7 @@ namespace UniVRM10
|
|||
/// <param name="materialGenerator">this loader use specified material generation strategy.</param>
|
||||
/// <param name="vrmMetaInformationCallback">return callback that notify meta information before loading.</param>
|
||||
/// <param name="ct">CancellationToken</param>
|
||||
/// <param name="importerContextSettings">Importer context settings.</param>
|
||||
/// <returns>vrm-1.0 instance. Maybe return null if unexpected error was raised.</returns>
|
||||
public static async Task<Vrm10Instance> LoadPathAsync(
|
||||
string path,
|
||||
|
|
@ -44,7 +45,8 @@ namespace UniVRM10
|
|||
ITextureDeserializer textureDeserializer = null,
|
||||
IMaterialDescriptorGenerator materialGenerator = null,
|
||||
VrmMetaInformationCallback vrmMetaInformationCallback = null,
|
||||
CancellationToken ct = default)
|
||||
CancellationToken ct = default,
|
||||
ImporterContextSettings importerContextSettings = null)
|
||||
{
|
||||
awaitCaller ??= Application.isPlaying
|
||||
? new RuntimeOnlyAwaitCaller()
|
||||
|
|
@ -64,7 +66,8 @@ namespace UniVRM10
|
|||
textureDeserializer,
|
||||
materialGenerator,
|
||||
vrmMetaInformationCallback,
|
||||
ct);
|
||||
ct,
|
||||
importerContextSettings);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -82,6 +85,7 @@ namespace UniVRM10
|
|||
/// <param name="materialGenerator">this loader use specified material generation strategy.</param>
|
||||
/// <param name="vrmMetaInformationCallback">return callback that notify meta information before loading.</param>
|
||||
/// <param name="ct">CancellationToken</param>
|
||||
/// <param name="importerContextSettings">Importer context settings.</param>
|
||||
/// <returns>vrm-1.0 instance. Maybe return null if unexpected error was raised.</returns>
|
||||
public static async Task<Vrm10Instance> LoadBytesAsync(
|
||||
byte[] bytes,
|
||||
|
|
@ -92,7 +96,8 @@ namespace UniVRM10
|
|||
ITextureDeserializer textureDeserializer = null,
|
||||
IMaterialDescriptorGenerator materialGenerator = null,
|
||||
VrmMetaInformationCallback vrmMetaInformationCallback = null,
|
||||
CancellationToken ct = default)
|
||||
CancellationToken ct = default,
|
||||
ImporterContextSettings importerContextSettings = null)
|
||||
{
|
||||
awaitCaller ??= Application.isPlaying
|
||||
? new RuntimeOnlyAwaitCaller()
|
||||
|
|
@ -108,7 +113,8 @@ namespace UniVRM10
|
|||
textureDeserializer,
|
||||
materialGenerator,
|
||||
vrmMetaInformationCallback,
|
||||
ct);
|
||||
ct,
|
||||
importerContextSettings);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -127,6 +133,7 @@ namespace UniVRM10
|
|||
/// <param name="materialGenerator">this loader use specified material generation strategy.</param>
|
||||
/// <param name="vrmMetaInformationCallback">return callback that notify meta information before loading.</param>
|
||||
/// <param name="ct">CancellationToken</param>
|
||||
/// <param name="importerContextSettings">Importer context settings.</param>
|
||||
/// <returns>vrm-1.0 instance. Maybe return null if unexpected error was raised.</returns>
|
||||
public static async Task<Vrm10Instance> LoadGltfDataAsync(
|
||||
GltfData gltfData,
|
||||
|
|
@ -137,7 +144,8 @@ namespace UniVRM10
|
|||
ITextureDeserializer textureDeserializer = null,
|
||||
IMaterialDescriptorGenerator materialGenerator = null,
|
||||
VrmMetaInformationCallback vrmMetaInformationCallback = null,
|
||||
CancellationToken ct = default)
|
||||
CancellationToken ct = default,
|
||||
ImporterContextSettings importerContextSettings = null)
|
||||
{
|
||||
awaitCaller ??= Application.isPlaying
|
||||
? new RuntimeOnlyAwaitCaller()
|
||||
|
|
@ -152,7 +160,8 @@ namespace UniVRM10
|
|||
textureDeserializer,
|
||||
materialGenerator,
|
||||
vrmMetaInformationCallback,
|
||||
ct);
|
||||
ct,
|
||||
importerContextSettings);
|
||||
}
|
||||
|
||||
private static async Task<Vrm10Instance> LoadAsync(
|
||||
|
|
@ -164,7 +173,8 @@ namespace UniVRM10
|
|||
ITextureDeserializer textureDeserializer,
|
||||
IMaterialDescriptorGenerator materialGenerator,
|
||||
VrmMetaInformationCallback vrmMetaInformationCallback,
|
||||
CancellationToken ct)
|
||||
CancellationToken ct,
|
||||
ImporterContextSettings importerContextSettings = null)
|
||||
{
|
||||
ct.ThrowIfCancellationRequested();
|
||||
if (awaitCaller == null)
|
||||
|
|
@ -181,7 +191,8 @@ namespace UniVRM10
|
|||
textureDeserializer,
|
||||
materialGenerator,
|
||||
vrmMetaInformationCallback,
|
||||
ct);
|
||||
ct,
|
||||
importerContextSettings);
|
||||
if (instance != null)
|
||||
{
|
||||
if (ct.IsCancellationRequested)
|
||||
|
|
@ -230,7 +241,8 @@ namespace UniVRM10
|
|||
ITextureDeserializer textureDeserializer,
|
||||
IMaterialDescriptorGenerator materialGenerator,
|
||||
VrmMetaInformationCallback vrmMetaInformationCallback,
|
||||
CancellationToken ct)
|
||||
CancellationToken ct,
|
||||
ImporterContextSettings importerContextSettings = null)
|
||||
{
|
||||
ct.ThrowIfCancellationRequested();
|
||||
if (awaitCaller == null)
|
||||
|
|
@ -256,7 +268,8 @@ namespace UniVRM10
|
|||
textureDeserializer,
|
||||
materialGenerator,
|
||||
vrmMetaInformationCallback,
|
||||
ct);
|
||||
ct,
|
||||
importerContextSettings);
|
||||
}
|
||||
|
||||
private static async Task<Vrm10Instance> TryMigratingFromVrm0XAsync(
|
||||
|
|
@ -313,7 +326,8 @@ namespace UniVRM10
|
|||
ITextureDeserializer textureDeserializer,
|
||||
IMaterialDescriptorGenerator materialGenerator,
|
||||
VrmMetaInformationCallback vrmMetaInformationCallback,
|
||||
CancellationToken ct)
|
||||
CancellationToken ct,
|
||||
ImporterContextSettings importerContextSettings = null)
|
||||
{
|
||||
ct.ThrowIfCancellationRequested();
|
||||
if (awaitCaller == null)
|
||||
|
|
@ -330,7 +344,8 @@ namespace UniVRM10
|
|||
vrm10Data,
|
||||
textureDeserializer: textureDeserializer,
|
||||
materialGenerator: materialGenerator,
|
||||
useControlRig: controlRigGenerationOption != ControlRigGenerationOption.None))
|
||||
useControlRig: controlRigGenerationOption != ControlRigGenerationOption.None,
|
||||
settings: importerContextSettings))
|
||||
{
|
||||
// 1. Load meta information if callback was available.
|
||||
if (vrmMetaInformationCallback != null)
|
||||
|
|
|
|||
|
|
@ -29,9 +29,10 @@ namespace UniVRM10
|
|||
IReadOnlyDictionary<SubAssetKey, UnityEngine.Object> externalObjectMap = null,
|
||||
ITextureDeserializer textureDeserializer = null,
|
||||
IMaterialDescriptorGenerator materialGenerator = null,
|
||||
bool useControlRig = false
|
||||
bool useControlRig = false,
|
||||
ImporterContextSettings settings = null
|
||||
)
|
||||
: base(vrm.Data, externalObjectMap, textureDeserializer)
|
||||
: base(vrm.Data, externalObjectMap, textureDeserializer, settings: settings)
|
||||
{
|
||||
if (vrm == null)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -18,10 +18,8 @@ namespace UniVRM10
|
|||
IReadOnlyDictionary<SubAssetKey, UnityEngine.Object> externalObjectMap = null,
|
||||
ITextureDeserializer textureDeserializer = null,
|
||||
IMaterialDescriptorGenerator materialGenerator = null)
|
||||
: base(data, externalObjectMap, textureDeserializer, materialGenerator)
|
||||
: base(data, externalObjectMap, textureDeserializer, materialGenerator, new ImporterContextSettings(invertAxis: Axes.X))
|
||||
{
|
||||
InvertAxis = Axes.X;
|
||||
|
||||
m_vrma = GetExtension(Data);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user