TextureImporterConfigurator

This commit is contained in:
ousttrue
2021-03-15 20:01:20 +09:00
parent 83ac94423c
commit 18fd38182d
9 changed files with 120 additions and 35 deletions

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
@@ -35,6 +36,12 @@ namespace UniGLTF
externalObjectMap.Where(x => x.Value != null).Select(x => (x.Value.name, x.Value)).Concat(
EnumerateTexturesFromUri(externalObjectMap, parser, UnityPath.FromUnityPath(scriptedImporter.assetPath).Parent))))
{
// settings TextureImporters
foreach (var textureInfo in parser.EnumerateTextures())
{
TextureImporterConfigurator.Configure(textureInfo, loaded.TextureFactory.ExternalMap);
}
loaded.InvertAxis = reverseAxis;
loaded.Load();
loaded.ShowMeshes();

View File

@@ -129,39 +129,13 @@ namespace UniGLTF
EditorApplication.delayCall += () =>
{
// Wait for the texture assets to be imported
foreach (var kv in extractor.Textures)
{
var targetPath = kv.Key;
var param = kv.Value;
// TextureImporter
var targetTextureImporter = AssetImporter.GetAtPath(targetPath) as TextureImporter;
if (targetTextureImporter != null)
{
switch (param.TextureType)
{
case GetTextureParam.TextureTypes.StandardMap:
#if VRM_DEVELOP
Debug.Log($"{targetPath} => linear");
#endif
targetTextureImporter.sRGBTexture = false;
targetTextureImporter.SaveAndReimport();
break;
case GetTextureParam.TextureTypes.NormalMap:
#if VRM_DEVELOP
Debug.Log($"{targetPath} => normalmap");
#endif
targetTextureImporter.textureType = TextureImporterType.NormalMap;
targetTextureImporter.SaveAndReimport();
break;
}
}
else
{
throw new FileNotFoundException(targetPath);
}
// remap
var externalObject = AssetDatabase.LoadAssetAtPath<UnityEngine.Texture2D>(targetPath);
if (externalObject != null)

View File

@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace UniGLTF
{
public static class TextureImporterConfigurator
{
public static void ConfigureNormalMap(Texture2D texture)
{
var path = UnityPath.FromAsset(texture);
if (AssetImporter.GetAtPath(path.Value) is TextureImporter textureImporter)
{
#if VRM_DEVELOP
Debug.Log($"{path} => normalmap");
#endif
textureImporter.textureType = TextureImporterType.NormalMap;
textureImporter.SaveAndReimport();
}
else
{
throw new System.IO.FileNotFoundException($"{path}");
}
}
public static void ConfigureLinear(Texture2D texture)
{
var path = UnityPath.FromAsset(texture);
if (AssetImporter.GetAtPath(path.Value) is TextureImporter textureImporter)
{
#if VRM_DEVELOP
Debug.Log($"{path} => linear");
#endif
textureImporter.sRGBTexture = false;
textureImporter.SaveAndReimport();
}
else
{
throw new System.IO.FileNotFoundException($"{path}");
}
}
public static void Configure(GetTextureParam textureInfo, IDictionary<string, Texture2D> ExternalMap)
{
switch (textureInfo.TextureType)
{
case GetTextureParam.TextureTypes.NormalMap:
{
if (ExternalMap.TryGetValue(textureInfo.GltflName, out Texture2D external))
{
ConfigureNormalMap(external);
}
}
break;
case GetTextureParam.TextureTypes.StandardMap:
{
if (ExternalMap.TryGetValue(textureInfo.ConvertedName, out Texture2D external))
{
ConfigureLinear(external);
}
}
break;
case GetTextureParam.TextureTypes.sRGB:
break;
default:
throw new NotImplementedException();
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 193b7c6393807a04f8aafabc23478f8e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -56,6 +56,7 @@ namespace UniGLTF
};
#endif
}
m_textureFactory = new TextureFactory(loadTextureAsync, externalObjectMap);
m_materialFactory = new MaterialFactory(GLTF, Storage, externalObjectMap);
}

View File

@@ -83,9 +83,10 @@ namespace UniGLTF
var src = gltf.materials[i];
var material = MaterialFactory.CreateMaterial(i, src, ShaderName);
var standardParam = StandardTexture(gltf, src);
if (src.pbrMetallicRoughness != null)
var standardParam = default(GetTextureParam);
if (src.pbrMetallicRoughness != null || src.occlusionTexture != null)
{
standardParam = StandardTexture(gltf, src);
if (src.pbrMetallicRoughness.baseColorFactor != null && src.pbrMetallicRoughness.baseColorFactor.Length == 4)
{
var color = src.pbrMetallicRoughness.baseColorFactor;

View File

@@ -46,10 +46,11 @@ namespace UniGLTF
public delegate Task<Texture2D> GetTextureAsyncFunc(IAwaitCaller awaitCaller, glTF gltf, GetTextureParam param);
public class TextureFactory : IDisposable
{
Dictionary<string, Texture2D> m_externalMap;
public readonly Dictionary<string, Texture2D> ExternalMap;
public bool TryGetExternal(GetTextureParam param, bool used, out Texture2D external)
{
if (param.Index0.HasValue && m_externalMap != null)
if (param.Index0.HasValue && ExternalMap != null)
{
var cacheName = param.ConvertedName;
if (param.TextureType == GetTextureParam.TextureTypes.NormalMap)
@@ -61,7 +62,7 @@ namespace UniGLTF
return true;
}
}
if (m_externalMap.TryGetValue(cacheName, out external))
if (ExternalMap.TryGetValue(cacheName, out external))
{
m_textureCache.Add(cacheName, new TextureLoadInfo(external, used, true));
return true;
@@ -79,7 +80,7 @@ namespace UniGLTF
LoadTextureAsync = loadTextureAsync;
if (externalMap != null)
{
m_externalMap = externalMap
ExternalMap = externalMap
.Select(kv => (kv.Item1, kv.Item2 as Texture2D))
.Where(kv => kv.Item2 != null)
.ToDictionary(kv => kv.Item1, kv => kv.Item2);

View File

@@ -4,6 +4,7 @@ using UnityEngine;
using UniGLTF;
using System;
using System.Collections.Generic;
using System.Linq;
namespace VRM
{
@@ -58,14 +59,24 @@ namespace VRM
var parser = new GltfParser();
parser.ParseGlb(File.ReadAllBytes(path));
Action<IEnumerable<string>> onCompleted = _ =>
Action<IEnumerable<string>> onCompleted = texturePaths =>
{
//
// after textures imported
//
var map = texturePaths.Select(x =>
{
var texture = AssetDatabase.LoadAssetAtPath(x, typeof(Texture2D));
return (texture.name, texture);
}).ToArray();
using (var context = new VRMImporterContext(parser))
{
var editor = new VRMEditorImporterContext(context, prefabPath);
foreach (var textureInfo in parser.EnumerateTextures())
{
TextureImporterConfigurator.Configure(textureInfo, map.ToDictionary(x => x.name, x => x.texture as Texture2D));
}
context.Load();
editor.SaveAsAsset();
}

View File

@@ -49,9 +49,14 @@ namespace VRM
var texture = AssetDatabase.LoadAssetAtPath(x, typeof(Texture2D));
return (texture.name, texture);
}).ToArray();
using (var context = new VRMImporterContext(parser, null, map))
{
var editor = new VRMEditorImporterContext(context, prefabPath);
foreach (var textureInfo in parser.EnumerateTextures())
{
TextureImporterConfigurator.Configure(textureInfo, map.ToDictionary(x => x.name, x => x.texture as Texture2D));
}
context.Load();
editor.SaveAsAsset();
}