mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-04-25 15:47:26 -05:00
TextureImportParam の GltfName, ConvertedName を UnityObjectName に単純化
This commit is contained in:
parent
03c0ad69c6
commit
66a7cfc768
|
|
@ -50,34 +50,25 @@ namespace UniGLTF
|
|||
s_foldMaterials = EditorGUILayout.Foldout(s_foldMaterials, "Remapped Materials");
|
||||
if (s_foldMaterials)
|
||||
{
|
||||
importer.DrawRemapGUI<UnityEngine.Material>(parser.GLTF.materials.Select(x => x.name));
|
||||
importer.DrawRemapGUI<UnityEngine.Material>(parser.GLTF.materials.Select(x => new SubAssetKey(typeof(Material), x.name)));
|
||||
}
|
||||
|
||||
s_foldTextures = EditorGUILayout.Foldout(s_foldTextures, "Remapped Textures");
|
||||
if (s_foldTextures)
|
||||
{
|
||||
var names = enumTextures(parser)
|
||||
.Select(((SubAssetKey, TextureImportParam) kv) =>
|
||||
importer.DrawRemapGUI<UnityEngine.Texture2D>(enumTextures(parser)
|
||||
.Where(x =>
|
||||
{
|
||||
var x = kv.Item2;
|
||||
if (x.TextureType != TextureImportTypes.StandardMap && !string.IsNullOrEmpty(x.Uri))
|
||||
var (key, param) = x;
|
||||
if ((param.TextureType == TextureImportTypes.sRGB || param.TextureType == TextureImportTypes.NormalMap) && !string.IsNullOrEmpty(param.Uri))
|
||||
{
|
||||
// GLTF の 無変換テクスチャーをスキップする
|
||||
return null;
|
||||
}
|
||||
|
||||
switch (x.TextureType)
|
||||
{
|
||||
case TextureImportTypes.NormalMap:
|
||||
return x.GltfName;
|
||||
|
||||
default:
|
||||
return x.ConvertedName;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
})
|
||||
.Where(x => !string.IsNullOrEmpty(x))
|
||||
;
|
||||
importer.DrawRemapGUI<UnityEngine.Texture2D>(names);
|
||||
.Select(x => x.Key)
|
||||
);
|
||||
}
|
||||
|
||||
if (GUILayout.Button("Clear"))
|
||||
|
|
@ -101,9 +92,9 @@ namespace UniGLTF
|
|||
return;
|
||||
}
|
||||
|
||||
Action<Texture2D> addRemap = externalObject =>
|
||||
Action<SubAssetKey, Texture2D> addRemap = (key, externalObject) =>
|
||||
{
|
||||
self.AddRemap(new AssetImporter.SourceAssetIdentifier(typeof(UnityEngine.Texture2D), externalObject.name), externalObject);
|
||||
self.AddRemap(new AssetImporter.SourceAssetIdentifier(key.Type, key.Name), externalObject);
|
||||
};
|
||||
Action<IEnumerable<UnityPath>> onCompleted = _ =>
|
||||
{
|
||||
|
|
@ -134,9 +125,9 @@ namespace UniGLTF
|
|||
Directory.CreateDirectory(path);
|
||||
}
|
||||
|
||||
foreach (var asset in importer.GetSubAssets<Material>(importer.assetPath))
|
||||
foreach (var (key, asset) in importer.GetSubAssets<Material>(importer.assetPath))
|
||||
{
|
||||
asset.ExtractSubAsset($"{path}/{asset.name}.mat", false);
|
||||
asset.ExtractSubAsset($"{path}/{key.Name}.mat", false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,37 +23,34 @@ namespace UniGLTF
|
|||
AssetDatabase.ImportAsset(importer.assetPath, ImportAssetOptions.ForceUpdate);
|
||||
}
|
||||
|
||||
public static IEnumerable<T> GetSubAssets<T>(this ScriptedImporter importer, string assetPath) where T : UnityEngine.Object
|
||||
public static IEnumerable<(SubAssetKey, T)> GetSubAssets<T>(this ScriptedImporter importer, string assetPath) where T : UnityEngine.Object
|
||||
{
|
||||
return AssetDatabase
|
||||
.LoadAllAssetsAtPath(assetPath)
|
||||
.Where(x => AssetDatabase.IsSubAsset(x))
|
||||
.Where(x => x is T)
|
||||
.Select(x => x as T);
|
||||
.Select(x => (new SubAssetKey(typeof(T), x.name), x as T));
|
||||
}
|
||||
|
||||
public static void DrawRemapGUI<T>(this ScriptedImporter importer, IEnumerable<string> names) where T : UnityEngine.Object
|
||||
public static void DrawRemapGUI<T>(this ScriptedImporter importer, IEnumerable<SubAssetKey> keys) where T : UnityEngine.Object
|
||||
{
|
||||
EditorGUI.indentLevel++;
|
||||
{
|
||||
var map = importer.GetExternalObjectMap()
|
||||
.Select(x => (x.Key.name, x.Value as T))
|
||||
.Where(x => x.Item2 != null)
|
||||
.ToDictionary(x => x.Item1, x => x.Item2)
|
||||
;
|
||||
foreach (var name in names)
|
||||
var map = importer.GetExternalObjectMap();
|
||||
foreach (var key in keys)
|
||||
{
|
||||
if (string.IsNullOrEmpty(name))
|
||||
if (string.IsNullOrEmpty(key.Name))
|
||||
{
|
||||
throw new System.ArgumentNullException();
|
||||
continue;
|
||||
}
|
||||
|
||||
EditorGUILayout.BeginHorizontal();
|
||||
EditorGUILayout.PrefixLabel(name);
|
||||
map.TryGetValue(name, out T value);
|
||||
EditorGUILayout.PrefixLabel(key.Name);
|
||||
map.TryGetValue(new AssetImporter.SourceAssetIdentifier(key.Type, key.Name), out UnityEngine.Object value);
|
||||
var asset = EditorGUILayout.ObjectField(value, typeof(T), true) as T;
|
||||
if (asset != value)
|
||||
{
|
||||
// update
|
||||
importer.SetExternalUnityObject(new AssetImporter.SourceAssetIdentifier(value), asset);
|
||||
}
|
||||
EditorGUILayout.EndHorizontal();
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||
using System.IO;
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using UniGLTF;
|
||||
using System.Linq;
|
||||
using VRMShaders;
|
||||
|
||||
|
|
@ -18,11 +19,11 @@ namespace UniGLTF
|
|||
public glTF GLTF => m_parser.GLTF;
|
||||
public IStorage Storage => m_parser.Storage;
|
||||
|
||||
public readonly Dictionary<UnityPath, TextureImportParam> Textures = new Dictionary<UnityPath, TextureImportParam>();
|
||||
UnityEngine.Texture2D[] m_subAssets;
|
||||
public readonly Dictionary<SubAssetKey, UnityPath> Textures = new Dictionary<SubAssetKey, UnityPath>();
|
||||
(SubAssetKey Key, UnityEngine.Texture2D Value)[] m_subAssets;
|
||||
UnityPath m_textureDirectory;
|
||||
|
||||
public TextureExtractor(GltfParser parser, UnityPath textureDirectory, UnityEngine.Texture2D[] subAssets)
|
||||
public TextureExtractor(GltfParser parser, UnityPath textureDirectory, (SubAssetKey, UnityEngine.Texture2D)[] subAssets)
|
||||
{
|
||||
m_parser = parser;
|
||||
m_textureDirectory = textureDirectory;
|
||||
|
|
@ -41,28 +42,29 @@ namespace UniGLTF
|
|||
return Path.GetExtension(uri).ToLower();
|
||||
}
|
||||
|
||||
public void Extract(TextureImportParam param)
|
||||
public void Extract(SubAssetKey key, TextureImportParam param)
|
||||
{
|
||||
if (Textures.Values.Contains(param))
|
||||
if (Textures.ContainsKey(key))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
UnityPath targetPath = default;
|
||||
if (!string.IsNullOrEmpty(param.Uri) && !param.ExtractConverted)
|
||||
{
|
||||
targetPath = m_textureDirectory.Child(param.GltfFileName);
|
||||
// use GLTF external
|
||||
// targetPath = m_textureDirectory.Child(key.Name);
|
||||
}
|
||||
else
|
||||
{
|
||||
UnityPath targetPath = default;
|
||||
|
||||
switch (param.TextureType)
|
||||
{
|
||||
case TextureImportTypes.StandardMap:
|
||||
{
|
||||
// write converted texture
|
||||
var subAsset = m_subAssets.FirstOrDefault(x => x.name == param.ConvertedName);
|
||||
targetPath = m_textureDirectory.Child(param.ConvertedFileName);
|
||||
var (_, subAsset) = m_subAssets.FirstOrDefault(x => x.Equals(key));
|
||||
targetPath = m_textureDirectory.Child($"{key.Name}.png");
|
||||
File.WriteAllBytes(targetPath.FullPath, subAsset.EncodeToPNG().ToArray());
|
||||
targetPath.ImportAsset();
|
||||
break;
|
||||
|
|
@ -71,15 +73,14 @@ namespace UniGLTF
|
|||
default:
|
||||
{
|
||||
// write original bytes
|
||||
targetPath = m_textureDirectory.Child(param.GltfFileName);
|
||||
targetPath = m_textureDirectory.Child($"{key.Name}{param.Ext}");
|
||||
File.WriteAllBytes(targetPath.FullPath, param.Index0().Result.ToArray());
|
||||
targetPath.ImportAsset();
|
||||
break;
|
||||
}
|
||||
}
|
||||
Textures.Add(key, targetPath);
|
||||
}
|
||||
|
||||
Textures.Add(targetPath, param);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -93,37 +94,44 @@ namespace UniGLTF
|
|||
/// <param name="dirName"></param>
|
||||
/// <param name="onCompleted"></param>
|
||||
public static void ExtractTextures(GltfParser parser, UnityPath textureDirectory,
|
||||
EnumerateAllTexturesDistinctFunc textureEnumerator, Texture2D[] subAssets, Action<Texture2D> addRemap,
|
||||
EnumerateAllTexturesDistinctFunc textureEnumerator, (SubAssetKey, Texture2D)[] subAssets,
|
||||
Action<SubAssetKey, Texture2D> addRemap,
|
||||
Action<IEnumerable<UnityPath>> onCompleted = null)
|
||||
{
|
||||
var extractor = new TextureExtractor(parser, textureDirectory, subAssets);
|
||||
foreach (var (key, x) in textureEnumerator(parser))
|
||||
{
|
||||
extractor.Extract(x);
|
||||
extractor.Extract(key, x);
|
||||
}
|
||||
|
||||
EditorApplication.delayCall += () =>
|
||||
{
|
||||
// Wait for the texture assets to be imported
|
||||
|
||||
foreach (var kv in extractor.Textures)
|
||||
foreach (var (key, targetPath) in extractor.Textures)
|
||||
{
|
||||
var targetPath = kv.Key;
|
||||
var param = kv.Value;
|
||||
|
||||
// remap
|
||||
var externalObject = targetPath.LoadAsset<Texture2D>();
|
||||
if (externalObject != null)
|
||||
{
|
||||
addRemap(externalObject);
|
||||
addRemap(key, externalObject);
|
||||
}
|
||||
}
|
||||
|
||||
if (onCompleted != null)
|
||||
{
|
||||
onCompleted(extractor.Textures.Keys);
|
||||
onCompleted(extractor.Textures.Values);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public static class KeyValuePariExtensions
|
||||
{
|
||||
public static void Deconstruct<T, U>(this KeyValuePair<T, U> pair, out T key, out U value)
|
||||
{
|
||||
key = pair.Key;
|
||||
value = pair.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -68,7 +68,7 @@ namespace UniGLTF
|
|||
{
|
||||
case TextureImportTypes.NormalMap:
|
||||
{
|
||||
if (ExternalMap.TryGetValue(textureInfo.GltfName, out Texture2D external))
|
||||
if (ExternalMap.TryGetValue(textureInfo.UnityObjectName, out Texture2D external))
|
||||
{
|
||||
ConfigureSize(external);
|
||||
ConfigureNormalMap(external);
|
||||
|
|
@ -78,7 +78,7 @@ namespace UniGLTF
|
|||
|
||||
case TextureImportTypes.StandardMap:
|
||||
{
|
||||
if (ExternalMap.TryGetValue(textureInfo.ConvertedName, out Texture2D external))
|
||||
if (ExternalMap.TryGetValue(textureInfo.UnityObjectName, out Texture2D external))
|
||||
{
|
||||
ConfigureSize(external);
|
||||
ConfigureLinear(external);
|
||||
|
|
@ -88,7 +88,7 @@ namespace UniGLTF
|
|||
|
||||
case TextureImportTypes.sRGB:
|
||||
{
|
||||
if (ExternalMap.TryGetValue(textureInfo.GltfName, out Texture2D external))
|
||||
if (ExternalMap.TryGetValue(textureInfo.UnityObjectName, out Texture2D external))
|
||||
{
|
||||
ConfigureSize(external);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ namespace MeshUtility
|
|||
value = prefab;
|
||||
isPrefab = true;
|
||||
}
|
||||
catch (ArgumentException ex)
|
||||
catch (ArgumentException)
|
||||
{
|
||||
// Debug.LogWarning(ex);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -71,7 +71,7 @@ namespace UniGLTF
|
|||
offset, scale,
|
||||
metallicFactor,
|
||||
roughnessFactor);
|
||||
var key = new SubAssetKey(typeof(Texture2D), param.ConvertedName);
|
||||
var key = new SubAssetKey(typeof(Texture2D), param.UnityObjectName);
|
||||
return (key, param);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ using VRMShaders;
|
|||
|
||||
namespace UniGLTF
|
||||
{
|
||||
public delegate IEnumerable<(SubAssetKey, TextureImportParam)> EnumerateAllTexturesDistinctFunc(GltfParser parser);
|
||||
public delegate IEnumerable<(SubAssetKey Key, TextureImportParam Param)> EnumerateAllTexturesDistinctFunc(GltfParser parser);
|
||||
|
||||
/// <summary>
|
||||
/// Texture 生成に関して
|
||||
|
|
@ -86,10 +86,6 @@ namespace UniGLTF
|
|||
Func<(SubAssetKey, TextureImportParam), bool> add = (kv) =>
|
||||
{
|
||||
var (key, textureInfo) = kv;
|
||||
if (string.IsNullOrEmpty(textureInfo.Uri) && key.Name != textureInfo.ExtractKey)
|
||||
{
|
||||
throw new System.Exception();
|
||||
}
|
||||
return used.Add(key);
|
||||
};
|
||||
for (int i = 0; i < parser.GLTF.materials.Count; ++i)
|
||||
|
|
|
|||
|
|
@ -31,63 +31,5 @@ namespace UniGLTF
|
|||
Type = t;
|
||||
Name = name;
|
||||
}
|
||||
|
||||
// public bool Enable => Type != null;
|
||||
|
||||
// /// <summary>
|
||||
// /// GetExternalObjectMap() の Key
|
||||
// /// </summary>
|
||||
// public string UnityObjectName => $"{Type.Name}.{Name}";
|
||||
|
||||
// /// <summary>
|
||||
// /// Extract 先
|
||||
// /// </summary>
|
||||
// public string AssetFileName => $"{Name}{Ext}";
|
||||
|
||||
// /// <summary>
|
||||
// /// CustomEditor用
|
||||
// /// </summary>
|
||||
// public string Label => Name;
|
||||
|
||||
// public static SubAssetKey CreateMaterialKey(glTFMaterial material)
|
||||
// {
|
||||
// return new SubAssetKey(typeof(Material), material.name, ".mat");
|
||||
// }
|
||||
|
||||
// public static SubAssetKey CreateMaterialKey(Material material)
|
||||
// {
|
||||
// return new SubAssetKey(typeof(Material), material.name, ".mat");
|
||||
// }
|
||||
|
||||
// public static SubAssetKey CreateTextureKey(string name, string ext)
|
||||
// {
|
||||
// return new SubAssetKey(typeof(Texture2D), name, ext);
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
// ExtractKey = GetExtractKey(textureType, gltfName, ConvertedName, uri);
|
||||
|
||||
// public readonly string ExtractKey;
|
||||
|
||||
// public static string GetExtractKey(TextureImportTypes type, string gltfName, string convertedName, string uri)
|
||||
// {
|
||||
// if (type == TextureImportTypes.StandardMap)
|
||||
// {
|
||||
// // metallic, smooth, occlusion
|
||||
// return convertedName;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// if (!string.IsNullOrEmpty(uri))
|
||||
// {
|
||||
// // external image
|
||||
// return Path.GetFileNameWithoutExtension(uri);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// // texture name
|
||||
// return gltfName;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
|
|
|||
|
|
@ -33,34 +33,38 @@ namespace UniGLTF
|
|||
|
||||
public static (SubAssetKey, TextureImportParam Param) CreateSRGB(GltfParser parser, int textureIndex, Vector2 offset, Vector2 scale)
|
||||
{
|
||||
var name = CreateNameExt(parser.GLTF, textureIndex, TextureImportTypes.sRGB);
|
||||
var gltfTexture = parser.GLTF.textures[textureIndex];
|
||||
var gltfImage = parser.GLTF.images[gltfTexture.source];
|
||||
var name = TextureImportName.GetExtractKey(TextureImportTypes.sRGB, gltfTexture.name, null, gltfImage.uri);
|
||||
var sampler = CreateSampler(parser.GLTF, textureIndex);
|
||||
GetTextureBytesAsync getTextureBytesAsync = () => Task.FromResult(ToArray(parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, textureIndex)));
|
||||
|
||||
var key = new SubAssetKey(typeof(Texture2D), name.GltfName);
|
||||
var param = new TextureImportParam(name, offset, scale, sampler, TextureImportTypes.sRGB, default, default, getTextureBytesAsync, default, default, default, default, default);
|
||||
var key = new SubAssetKey(typeof(Texture2D), name);
|
||||
var param = new TextureImportParam(name, gltfImage.GetExt(), gltfImage.uri, offset, scale, sampler, TextureImportTypes.sRGB, default, default, getTextureBytesAsync, default, default, default, default, default);
|
||||
return (key, param);
|
||||
}
|
||||
|
||||
public static (SubAssetKey, TextureImportParam Param) CreateNormal(GltfParser parser, int textureIndex, Vector2 offset, Vector2 scale)
|
||||
{
|
||||
var name = CreateNameExt(parser.GLTF, textureIndex, TextureImportTypes.NormalMap);
|
||||
var gltfTexture = parser.GLTF.textures[textureIndex];
|
||||
var gltfImage = parser.GLTF.images[gltfTexture.source];
|
||||
var name = TextureImportName.GetExtractKey(TextureImportTypes.NormalMap, gltfTexture.name, null, gltfImage.uri);
|
||||
var sampler = CreateSampler(parser.GLTF, textureIndex);
|
||||
GetTextureBytesAsync getTextureBytesAsync = () => Task.FromResult(ToArray(parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, textureIndex)));
|
||||
var key = new SubAssetKey(typeof(Texture2D), name.GltfName);
|
||||
var param = new TextureImportParam(name, offset, scale, sampler, TextureImportTypes.NormalMap, default, default, getTextureBytesAsync, default, default, default, default, default);
|
||||
var key = new SubAssetKey(typeof(Texture2D), name);
|
||||
var param = new TextureImportParam(name, gltfImage.GetExt(), gltfImage.uri, offset, scale, sampler, TextureImportTypes.NormalMap, default, default, getTextureBytesAsync, default, default, default, default, default);
|
||||
return (key, param);
|
||||
}
|
||||
|
||||
public static TextureImportParam CreateStandard(GltfParser parser, int? metallicRoughnessTextureIndex, int? occlusionTextureIndex, Vector2 offset, Vector2 scale, float metallicFactor, float roughnessFactor)
|
||||
{
|
||||
TextureImportName name = default;
|
||||
string name = default;
|
||||
|
||||
GetTextureBytesAsync getMetallicRoughnessAsync = default;
|
||||
SamplerParam sampler = default;
|
||||
if (metallicRoughnessTextureIndex.HasValue)
|
||||
{
|
||||
name = CreateNameExt(parser.GLTF, metallicRoughnessTextureIndex.Value, TextureImportTypes.StandardMap);
|
||||
var gltfTexture = parser.GLTF.textures[metallicRoughnessTextureIndex.Value];
|
||||
name = TextureImportName.GetExtractKey(TextureImportTypes.StandardMap, gltfTexture.name, gltfTexture.name + ".standard", parser.GLTF.images[gltfTexture.source].uri);
|
||||
sampler = CreateSampler(parser.GLTF, metallicRoughnessTextureIndex.Value);
|
||||
getMetallicRoughnessAsync = () => Task.FromResult(ToArray(parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, metallicRoughnessTextureIndex.Value)));
|
||||
}
|
||||
|
|
@ -68,15 +72,16 @@ namespace UniGLTF
|
|||
GetTextureBytesAsync getOcclusionAsync = default;
|
||||
if (occlusionTextureIndex.HasValue)
|
||||
{
|
||||
if (string.IsNullOrEmpty(name.GltfName))
|
||||
var gltfTexture = parser.GLTF.textures[occlusionTextureIndex.Value];
|
||||
if (string.IsNullOrEmpty(name))
|
||||
{
|
||||
name = CreateNameExt(parser.GLTF, occlusionTextureIndex.Value, TextureImportTypes.StandardMap);
|
||||
name = TextureImportName.GetExtractKey(TextureImportTypes.StandardMap, gltfTexture.name, gltfTexture.name + ".standard", parser.GLTF.images[gltfTexture.source].uri);
|
||||
}
|
||||
sampler = CreateSampler(parser.GLTF, occlusionTextureIndex.Value);
|
||||
getOcclusionAsync = () => Task.FromResult(ToArray(parser.GLTF.GetImageBytesFromTextureIndex(parser.Storage, occlusionTextureIndex.Value)));
|
||||
}
|
||||
|
||||
return new TextureImportParam(name, offset, scale, sampler, TextureImportTypes.StandardMap, metallicFactor, roughnessFactor, getMetallicRoughnessAsync, getOcclusionAsync, default, default, default, default);
|
||||
return new TextureImportParam(name, ".png", null, offset, scale, sampler, TextureImportTypes.StandardMap, metallicFactor, roughnessFactor, getMetallicRoughnessAsync, getOcclusionAsync, default, default, default, default);
|
||||
}
|
||||
|
||||
public static TextureImportName CreateNameExt(glTF gltf, int textureIndex, TextureImportTypes textureType)
|
||||
|
|
|
|||
|
|
@ -147,11 +147,11 @@ namespace VRM
|
|||
//
|
||||
var subAssets = m_context.TextureFactory.Textures
|
||||
.Where(x => x.IsUsed)
|
||||
.Select(x => x.Texture)
|
||||
.Select(x => (new SubAssetKey(typeof(Texture2D), x.Texture.name), x.Texture))
|
||||
.ToArray();
|
||||
var vrmTextures = new VRMMtoonMaterialImporter(m_context.VRM);
|
||||
var dirName = $"{m_prefabPath.FileNameWithoutExtension}.Textures";
|
||||
TextureExtractor.ExtractTextures(m_context.Parser, m_prefabPath.Parent.Child(dirName), vrmTextures.EnumerateAllTexturesDistinct, subAssets, _ => { }, onTextureReloaded);
|
||||
TextureExtractor.ExtractTextures(m_context.Parser, m_prefabPath.Parent.Child(dirName), vrmTextures.EnumerateAllTexturesDistinct, subAssets, (_x, _y) => { }, onTextureReloaded);
|
||||
}
|
||||
|
||||
bool SaveAsAsset(UnityEngine.Object o)
|
||||
|
|
|
|||
|
|
@ -97,7 +97,7 @@ namespace VRM
|
|||
public IEnumerable<(SubAssetKey, TextureImportParam)> EnumerateTexturesForMaterial(GltfParser parser, int i)
|
||||
{
|
||||
// mtoon
|
||||
if (!TryCreateParam(parser, i, out MaterialImportParam param))
|
||||
if (TryCreateParam(parser, i, out MaterialImportParam param))
|
||||
{
|
||||
// unlit
|
||||
if (!GltfUnlitMaterial.TryCreateParam(parser, i, out param))
|
||||
|
|
@ -109,7 +109,7 @@ namespace VRM
|
|||
|
||||
foreach (var kv in param.TextureSlots)
|
||||
{
|
||||
var key = new SubAssetKey(typeof(Texture2D), kv.Key);
|
||||
var key = new SubAssetKey(typeof(Texture2D), kv.Value.UnityObjectName);
|
||||
yield return (key, kv.Value);
|
||||
}
|
||||
}
|
||||
|
|
@ -120,10 +120,6 @@ namespace VRM
|
|||
Func<(SubAssetKey, TextureImportParam), bool> add = (kv) =>
|
||||
{
|
||||
var (key, textureInfo) = kv;
|
||||
if (key.Name != textureInfo.ExtractKey)
|
||||
{
|
||||
throw new System.Exception();
|
||||
}
|
||||
return used.Add(key);
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -39,10 +39,10 @@ namespace UniVRM10
|
|||
}
|
||||
|
||||
// meta
|
||||
importer.DrawRemapGUI<VRM10MetaObject>(new string[] { VRM10MetaObject.ExtractKey });
|
||||
importer.DrawRemapGUI<VRM10MetaObject>(new SubAssetKey[] { VRM10MetaObject.ExtractKey });
|
||||
|
||||
// expression avatar
|
||||
importer.DrawRemapGUI<VRM10ExpressionAvatar>(new string[] { VRM10ExpressionAvatar.ExtractKey });
|
||||
importer.DrawRemapGUI<VRM10ExpressionAvatar>(new SubAssetKey[] { VRM10ExpressionAvatar.ExtractKey });
|
||||
|
||||
// expressions
|
||||
importer.DrawRemapGUI<VRM10Expression>(vrm.Expressions.Select(x => CreateKey(x).ExtractKey));
|
||||
|
|
@ -73,7 +73,7 @@ namespace UniVRM10
|
|||
|
||||
// meta
|
||||
{
|
||||
foreach (var asset in importer.GetSubAssets<VRM10MetaObject>(importer.assetPath))
|
||||
foreach (var (key, asset) in importer.GetSubAssets<VRM10MetaObject>(importer.assetPath))
|
||||
{
|
||||
asset.ExtractSubAsset($"{path}/{asset.name}.asset", false);
|
||||
}
|
||||
|
|
@ -81,13 +81,13 @@ namespace UniVRM10
|
|||
|
||||
{
|
||||
// expressions
|
||||
foreach (var asset in importer.GetSubAssets<VRM10Expression>(importer.assetPath))
|
||||
foreach (var (key, asset) in importer.GetSubAssets<VRM10Expression>(importer.assetPath))
|
||||
{
|
||||
asset.ExtractSubAsset($"{path}/{asset.name}.asset", false);
|
||||
}
|
||||
|
||||
// expressions
|
||||
foreach (var asset in importer.GetSubAssets<VRM10ExpressionAvatar>(importer.assetPath))
|
||||
foreach (var (key, asset) in importer.GetSubAssets<VRM10ExpressionAvatar>(importer.assetPath))
|
||||
{
|
||||
asset.ExtractSubAsset($"{path}/{asset.name}.asset", false);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UniGLTF;
|
||||
|
||||
namespace UniVRM10
|
||||
{
|
||||
|
|
@ -173,11 +174,11 @@ namespace UniVRM10
|
|||
return 0;
|
||||
}
|
||||
|
||||
public string ExtractKey
|
||||
public SubAssetKey ExtractKey
|
||||
{
|
||||
get
|
||||
{
|
||||
return $"Expression.{this}";
|
||||
return new SubAssetKey(typeof(VRM10Expression), this.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,39 +0,0 @@
|
|||
using UniGLTF;
|
||||
|
||||
namespace UniVRM10
|
||||
{
|
||||
public static class ExtractKeyExtensions
|
||||
{
|
||||
static ExpressionKey Key(UniGLTF.Extensions.VRMC_vrm.Expression e)
|
||||
{
|
||||
if (e.Preset == UniGLTF.Extensions.VRMC_vrm.ExpressionPreset.custom)
|
||||
{
|
||||
return ExpressionKey.CreateCustom(e.Name);
|
||||
}
|
||||
else
|
||||
{
|
||||
return ExpressionKey.CreateFromPreset(e.Preset);
|
||||
}
|
||||
}
|
||||
|
||||
// public static SubAssetKey ToExtractKey(this UniGLTF.Extensions.VRMC_vrm.Expression expression)
|
||||
// {
|
||||
// var key = Key(expression);
|
||||
// return new SubAssetKey(typeof(VRM10Expression), key.ToString(), ".asset");
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// for SubAssetName
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
// public static string ExtractKey(this Expression expression)
|
||||
// {
|
||||
// ExpressionKey key =
|
||||
// (expression.Preset == ExpressionPreset.custom)
|
||||
// ? ExpressionKey.CreateCustom(expression.Name)
|
||||
// : ExpressionKey.CreateFromPreset(expression.Preset)
|
||||
// ;
|
||||
// return key.ExtractKey;
|
||||
// }
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c7e8029ad471efc47b138a76295aa425
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -13,7 +13,7 @@ namespace UniVRM10
|
|||
[CreateAssetMenu(menuName = "VRM10/ExpressionAvatar")]
|
||||
public sealed class VRM10ExpressionAvatar : ScriptableObject
|
||||
{
|
||||
public const string ExtractKey = "ExpressionAvatar";
|
||||
public static UniGLTF.SubAssetKey ExtractKey => new UniGLTF.SubAssetKey(typeof(VRM10ExpressionAvatar), "ExpressionAvatar");
|
||||
|
||||
[SerializeField]
|
||||
public List<VRM10Expression> Clips = new List<VRM10Expression>();
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MeshUtility;
|
||||
using UniGLTF;
|
||||
using UnityEngine;
|
||||
|
||||
|
||||
|
|
@ -10,7 +11,7 @@ namespace UniVRM10
|
|||
[CreateAssetMenu(menuName = "VRM10/MetaObject")]
|
||||
public class VRM10MetaObject : ScriptableObject
|
||||
{
|
||||
public const string ExtractKey = "Meta";
|
||||
public static SubAssetKey ExtractKey => new SubAssetKey(typeof(VRM10MetaObject), "Meta");
|
||||
|
||||
[SerializeField]
|
||||
public string ExporterVersion;
|
||||
|
|
|
|||
|
|
@ -7,20 +7,6 @@ namespace UniVRM10
|
|||
{
|
||||
public static class ExpressionExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// for SubAssetName
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string ExtractKey(this Expression expression)
|
||||
{
|
||||
ExpressionKey key =
|
||||
(expression.Preset == ExpressionPreset.custom)
|
||||
? ExpressionKey.CreateCustom(expression.Name)
|
||||
: ExpressionKey.CreateFromPreset(expression.Preset)
|
||||
;
|
||||
return key.ExtractKey;
|
||||
}
|
||||
|
||||
public static UniVRM10.MorphTargetBinding Build10(this MorphTargetBind bind, GameObject root, RuntimeUnityBuilder.ModelMap loader, VrmLib.Model model)
|
||||
{
|
||||
var libNode = model.Nodes[bind.Node.Value];
|
||||
|
|
|
|||
|
|
@ -245,7 +245,7 @@ namespace UniVRM10
|
|||
{
|
||||
var src = vrm.Meta;
|
||||
m_meta = ScriptableObject.CreateInstance<VRM10MetaObject>();
|
||||
m_meta.name = VRM10MetaObject.ExtractKey;
|
||||
m_meta.name = VRM10MetaObject.ExtractKey.Name;
|
||||
controller.Meta = m_meta;
|
||||
m_meta.Name = src.Name;
|
||||
m_meta.Version = src.Version;
|
||||
|
|
@ -286,14 +286,14 @@ namespace UniVRM10
|
|||
controller.Expression.ExpressionAvatar = ScriptableObject.CreateInstance<VRM10ExpressionAvatar>();
|
||||
|
||||
m_exressionAvatar = controller.Expression.ExpressionAvatar;
|
||||
m_exressionAvatar.name = VRM10ExpressionAvatar.ExtractKey;
|
||||
m_exressionAvatar.name = VRM10ExpressionAvatar.ExtractKey.Name;
|
||||
|
||||
foreach (var expression in vrm.Expressions)
|
||||
{
|
||||
var clip = ScriptableObject.CreateInstance<UniVRM10.VRM10Expression>();
|
||||
clip.Preset = expression.Preset;
|
||||
clip.ExpressionName = expression.Name;
|
||||
clip.name = Key(expression).ExtractKey;
|
||||
clip.name = Key(expression).ExtractKey.Name;
|
||||
clip.IsBinary = expression.IsBinary.GetValueOrDefault();
|
||||
clip.OverrideBlink = expression.OverrideBlink;
|
||||
clip.OverrideLookAt = expression.OverrideLookAt;
|
||||
|
|
|
|||
|
|
@ -224,18 +224,18 @@ namespace UniVRM10
|
|||
// thumbnail
|
||||
var imageIndex = vrm.Meta.ThumbnailImage.Value;
|
||||
var gltfImage = parser.GLTF.images[imageIndex];
|
||||
var name = new TextureImportName(TextureImportTypes.sRGB, gltfImage.name, gltfImage.GetExt(), "");
|
||||
var name = TextureImportName.GetExtractKey(TextureImportTypes.sRGB, gltfImage.name, null, gltfImage.uri);
|
||||
|
||||
GetTextureBytesAsync getBytesAsync = () =>
|
||||
{
|
||||
var bytes = parser.GLTF.GetImageBytes(parser.Storage, imageIndex);
|
||||
return Task.FromResult(GltfTextureImporter.ToArray(bytes));
|
||||
};
|
||||
var param = new TextureImportParam(name, Vector2.zero, Vector2.one, default, TextureImportTypes.sRGB, default, default,
|
||||
var param = new TextureImportParam(name, gltfImage.GetExt(), gltfImage.uri, Vector2.zero, Vector2.one, default, TextureImportTypes.sRGB, default, default,
|
||||
getBytesAsync, default, default,
|
||||
default, default, default
|
||||
);
|
||||
var key = new SubAssetKey(typeof(Texture2D), name.GltfName);
|
||||
var key = new SubAssetKey(typeof(Texture2D), name);
|
||||
value = (key, param);
|
||||
return true;
|
||||
}
|
||||
|
|
@ -261,10 +261,6 @@ namespace UniVRM10
|
|||
Func<(SubAssetKey, TextureImportParam), bool> add = (kv) =>
|
||||
{
|
||||
var (key, textureInfo) = kv;
|
||||
if (key.Name != textureInfo.ExtractKey)
|
||||
{
|
||||
throw new System.Exception();
|
||||
}
|
||||
return used.Add(key);
|
||||
};
|
||||
for (int i = 0; i < parser.GLTF.materials.Count; ++i)
|
||||
|
|
|
|||
|
|
@ -118,9 +118,8 @@ namespace VRMShaders
|
|||
public IEnumerable<TextureLoadInfo> Textures => m_textureCache.Values;
|
||||
|
||||
|
||||
async Task<TextureLoadInfo> GetOrCreateBaseTexture(TextureImportParam param, GetTextureBytesAsync getTextureBytesAsync, RenderTextureReadWrite colorSpace, bool used)
|
||||
async Task<TextureLoadInfo> GetOrCreateBaseTexture(string name, TextureImportParam param, GetTextureBytesAsync getTextureBytesAsync, RenderTextureReadWrite colorSpace, bool used)
|
||||
{
|
||||
var name = param.GltfName;
|
||||
if (m_textureCache.TryGetValue(name, out TextureLoadInfo cacheInfo))
|
||||
{
|
||||
return cacheInfo;
|
||||
|
|
@ -194,17 +193,9 @@ namespace VRMShaders
|
|||
/// <returns></returns>
|
||||
public async Task<Texture2D> GetTextureAsync(TextureImportParam param)
|
||||
{
|
||||
//
|
||||
// ExtractKey で External とのマッチングを試みる
|
||||
//
|
||||
// Normal => GltfName
|
||||
// Standard => ConvertedName
|
||||
// sRGB => GltfName
|
||||
// Linear => GltfName
|
||||
//
|
||||
if (param.Index0 != null && ExternalMap != null)
|
||||
{
|
||||
if (ExternalMap.TryGetValue(param.ExtractKey, out Texture2D external))
|
||||
if (ExternalMap.TryGetValue(param.UnityObjectName, out Texture2D external))
|
||||
{
|
||||
return external;
|
||||
}
|
||||
|
|
@ -215,11 +206,11 @@ namespace VRMShaders
|
|||
case TextureImportTypes.NormalMap:
|
||||
// Runtime/SubAsset 用に変換する
|
||||
{
|
||||
if (!m_textureCache.TryGetValue(param.ConvertedName, out TextureLoadInfo info))
|
||||
if (!m_textureCache.TryGetValue(param.UnityObjectName, out TextureLoadInfo info))
|
||||
{
|
||||
var baseTexture = await GetOrCreateBaseTexture(param, param.Index0, RenderTextureReadWrite.Linear, false);
|
||||
var baseTexture = await GetOrCreateBaseTexture(param.UnityObjectName + ".normal_base", param, param.Index0, RenderTextureReadWrite.Linear, false);
|
||||
var converted = NormalConverter.Import(baseTexture.Texture);
|
||||
converted.name = param.ConvertedName;
|
||||
converted.name = param.UnityObjectName;
|
||||
info = new TextureLoadInfo(converted, true, false);
|
||||
m_textureCache.Add(converted.name, info);
|
||||
}
|
||||
|
|
@ -229,20 +220,20 @@ namespace VRMShaders
|
|||
case TextureImportTypes.StandardMap:
|
||||
// 変換する
|
||||
{
|
||||
if (!m_textureCache.TryGetValue(param.ConvertedName, out TextureLoadInfo info))
|
||||
if (!m_textureCache.TryGetValue(param.UnityObjectName, out TextureLoadInfo info))
|
||||
{
|
||||
TextureLoadInfo baseTexture = default;
|
||||
if (param.Index0 != null)
|
||||
{
|
||||
baseTexture = await GetOrCreateBaseTexture(param, param.Index0, RenderTextureReadWrite.Linear, false);
|
||||
baseTexture = await GetOrCreateBaseTexture(param.UnityObjectName + ".metallicRoughness", param, param.Index0, RenderTextureReadWrite.Linear, false);
|
||||
}
|
||||
TextureLoadInfo occlusionBaseTexture = default;
|
||||
if (param.Index1 != null)
|
||||
{
|
||||
occlusionBaseTexture = await GetOrCreateBaseTexture(param, param.Index1, RenderTextureReadWrite.Linear, false);
|
||||
occlusionBaseTexture = await GetOrCreateBaseTexture(param.UnityObjectName + ".occlusion", param, param.Index1, RenderTextureReadWrite.Linear, false);
|
||||
}
|
||||
var converted = OcclusionMetallicRoughnessConverter.Import(baseTexture.Texture, param.MetallicFactor, param.RoughnessFactor, occlusionBaseTexture.Texture);
|
||||
converted.name = param.ConvertedName;
|
||||
converted.name = param.UnityObjectName;
|
||||
info = new TextureLoadInfo(converted, true, false);
|
||||
m_textureCache.Add(converted.name, info);
|
||||
}
|
||||
|
|
@ -251,14 +242,12 @@ namespace VRMShaders
|
|||
|
||||
default:
|
||||
{
|
||||
var baseTexture = await GetOrCreateBaseTexture(param, param.Index0, RenderTextureReadWrite.sRGB, true);
|
||||
var baseTexture = await GetOrCreateBaseTexture(param.UnityObjectName, param, param.Index0, RenderTextureReadWrite.sRGB, true);
|
||||
return baseTexture.Texture;
|
||||
}
|
||||
}
|
||||
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,17 +22,10 @@ namespace VRMShaders
|
|||
public const string METALLIC_GLOSS_PROP = "_MetallicGlossMap";
|
||||
public const string OCCLUSION_PROP = "_OcclusionMap";
|
||||
|
||||
|
||||
public readonly TextureImportName Name;
|
||||
public string GltfName => Name.GltfName;
|
||||
public string GltfFileName => Name.GltfFileName;
|
||||
public string ConvertedName => Name.ConvertedName;
|
||||
public string ConvertedFileName => Name.ConvertedFileName;
|
||||
public string Uri => Name.Uri;
|
||||
public readonly string UnityObjectName;
|
||||
public readonly string Ext;
|
||||
public readonly string Uri;
|
||||
|
||||
public string ExtractKey => Name.ExtractKey;
|
||||
|
||||
|
||||
public Vector2 Offset;
|
||||
public Vector2 Scale;
|
||||
|
||||
|
|
@ -41,7 +34,7 @@ namespace VRMShaders
|
|||
public readonly TextureImportTypes TextureType;
|
||||
public readonly float MetallicFactor;
|
||||
public readonly float RoughnessFactor;
|
||||
|
||||
|
||||
public readonly GetTextureBytesAsync Index0;
|
||||
public readonly GetTextureBytesAsync Index1;
|
||||
public readonly GetTextureBytesAsync Index2;
|
||||
|
|
@ -54,7 +47,7 @@ namespace VRMShaders
|
|||
/// </summary>
|
||||
public bool ExtractConverted => TextureType == TextureImportTypes.StandardMap;
|
||||
|
||||
public TextureImportParam(TextureImportName name, Vector2 offset, Vector2 scale, SamplerParam sampler, TextureImportTypes textureType, float metallicFactor, float roughnessFactor,
|
||||
public TextureImportParam(string name, string ext, string uri, Vector2 offset, Vector2 scale, SamplerParam sampler, TextureImportTypes textureType, float metallicFactor, float roughnessFactor,
|
||||
GetTextureBytesAsync i0,
|
||||
GetTextureBytesAsync i1,
|
||||
GetTextureBytesAsync i2,
|
||||
|
|
@ -62,7 +55,9 @@ namespace VRMShaders
|
|||
GetTextureBytesAsync i4,
|
||||
GetTextureBytesAsync i5)
|
||||
{
|
||||
Name = name;
|
||||
UnityObjectName = name;
|
||||
Ext = ext;
|
||||
Uri = uri;
|
||||
Offset = offset;
|
||||
Scale = scale;
|
||||
Sampler = sampler;
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user