define ITextureDeserializer

This commit is contained in:
Masataka SUMI
2021-05-27 18:54:27 +09:00
parent b67d2f4564
commit 02e51346f4
29 changed files with 88 additions and 72 deletions

View File

@@ -2,7 +2,7 @@
using UnityEditor;
using UnityEngine;
using VRMShaders;
using ColorSpace = UniGLTF.ColorSpace;
using ColorSpace = VRMShaders.ColorSpace;
namespace MeshUtility
{

View File

@@ -1,5 +1,6 @@
using System;
using UnityEngine;
using ColorSpace = VRMShaders.ColorSpace;
namespace UniGLTF
{
@@ -10,13 +11,13 @@ namespace UniGLTF
var dst = src.ConvertColorSpace(srcColorSpace, dstColorSpace);
return new float[] {dst.r, dst.g, dst.b, dst.a};
}
public static float[] ToFloat3(this Color src, ColorSpace srcColorSpace, ColorSpace dstColorSpace)
{
var dst = src.ConvertColorSpace(srcColorSpace, dstColorSpace);
return new float[] {dst.r, dst.g, dst.b};
}
public static Color ToColor4(this float[] src, ColorSpace srcColorSpace, ColorSpace dstColorSpace)
{
if (src == null || src.Length < 4)
@@ -27,7 +28,7 @@ namespace UniGLTF
return new Color(src[0], src[1], src[2], src[3]).ConvertColorSpace(srcColorSpace, dstColorSpace);
}
public static Color ToColor3(this float[] src, ColorSpace srcColorSpace, ColorSpace dstColorSpace)
{
if (src == null || src.Length < 3)
@@ -64,4 +65,4 @@ namespace UniGLTF
}
}
}
}
}

View File

@@ -44,7 +44,7 @@ namespace UniGLTF
MaterialDescriptorGenerator = new GltfMaterialDescriptorGenerator();
externalObjectMap = externalObjectMap ?? new Dictionary<SubAssetKey, UnityEngine.Object>();
TextureFactory = new TextureFactory(externalObjectMap
TextureFactory = new TextureFactory(new RuntimeTextureDeserializer(), externalObjectMap
.Where(x => x.Value is Texture)
.ToDictionary(x => x.Key, x => (Texture) x.Value));
MaterialFactory = new MaterialFactory(externalObjectMap

View File

@@ -1,5 +1,6 @@
using UnityEngine;
using VRMShaders;
using ColorSpace = VRMShaders.ColorSpace;
namespace UniGLTF
{

View File

@@ -1,6 +1,6 @@
using UnityEngine;
using VRMShaders;
using ColorSpace = VRMShaders.ColorSpace;
namespace UniGLTF
{

View File

@@ -2,7 +2,7 @@
using UniGLTF.UniUnlit;
using UnityEngine;
using VRMShaders;
using ColorSpace = VRMShaders.ColorSpace;
namespace UniGLTF
{

View File

@@ -1,6 +1,7 @@
using System;
using UnityEngine;
using VRMShaders;
using ColorSpace = VRMShaders.ColorSpace;
namespace UniGLTF
{

View File

@@ -3,8 +3,8 @@ using System.Linq;
using UniGLTF;
using UniJSON;
using UnityEngine;
using ColorSpace = UniGLTF.ColorSpace;
using VRMShaders;
using ColorSpace = VRMShaders.ColorSpace;
namespace VRM
{

View File

@@ -5,7 +5,7 @@ using UniGLTF;
using UniGLTF.ShaderPropExporter;
using UnityEngine;
using VRMShaders;
using ColorSpace = UniGLTF.ColorSpace;
using ColorSpace = VRMShaders.ColorSpace;
namespace VRM
{

View File

@@ -4,7 +4,7 @@ using UniGLTF;
using UniGLTF.Extensions.VRMC_materials_mtoon;
using UnityEngine;
using VRMShaders;
using ColorSpace = UniGLTF.ColorSpace;
using ColorSpace = VRMShaders.ColorSpace;
using OutlineWidthMode = MToon.OutlineWidthMode;
using RenderMode = MToon.RenderMode;

View File

@@ -4,7 +4,7 @@ using UniGLTF;
using UniGLTF.Extensions.VRMC_materials_mtoon;
using UnityEngine;
using VRMShaders;
using ColorSpace = UniGLTF.ColorSpace;
using ColorSpace = VRMShaders.ColorSpace;
using OutlineWidthMode = UniGLTF.Extensions.VRMC_materials_mtoon.OutlineWidthMode;
namespace UniVRM10

View File

@@ -5,7 +5,7 @@ using UniGLTF;
using UniGLTF.Extensions.VRMC_materials_mtoon;
using UniJSON;
using UnityEngine;
using ColorSpace = UniGLTF.ColorSpace;
using ColorSpace = VRMShaders.ColorSpace;
using RenderMode = MToon.RenderMode;
namespace UniVRM10

View File

@@ -1,10 +1,8 @@
using System;
using System.IO;
using System.Reflection;
using UniGLTF;
using UnityEditor;
using UnityEngine;
using ColorSpace = UniGLTF.ColorSpace;
namespace VRMShaders
{

View File

@@ -1,8 +1,8 @@
namespace UniGLTF
namespace VRMShaders
{
public enum ColorSpace
{
sRGB,
Linear,
}
}
}

View File

@@ -0,0 +1,11 @@
using System;
using System.Threading.Tasks;
using UnityEngine;
namespace VRMShaders
{
public interface ITextureDeserializer
{
Task<Texture2D> LoadTextureAsync(GetTextureBytesAsync getTextureBytesAsync, bool useMipmap, ColorSpace colorSpace);
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 47b889c85e814e7eae6f5b7f760c7ef3
timeCreated: 1622107590

View File

@@ -13,7 +13,7 @@ namespace VRMShaders
/// <summary>
/// Export する Texture2D のリスト。これが gltf.textures になる
/// </summary>
IReadOnlyList<(Texture2D, UniGLTF.ColorSpace)> Exported { get; }
IReadOnlyList<(Texture2D, ColorSpace)> Exported { get; }
/// <summary>
/// 指定の Texture を、 sRGB 色空間の値を持つ Texture に出力するように指示する。

View File

@@ -1,6 +1,6 @@
using UnityEngine;
namespace UniGLTF
namespace VRMShaders
{
/// <summary>
/// Texture2D を入力として byte[] を得る機能

View File

@@ -1,6 +1,4 @@
using UnityEngine;
using ColorSpace = UniGLTF.ColorSpace;
namespace VRMShaders
{

View File

@@ -1,8 +1,6 @@
using System;
using System.Linq;
using UnityEngine;
using ColorSpace = UniGLTF.ColorSpace;
namespace VRMShaders
{

View File

@@ -0,0 +1,22 @@
using System;
using System.Threading.Tasks;
using UnityEngine;
namespace VRMShaders
{
public sealed class RuntimeTextureDeserializer : ITextureDeserializer
{
public async Task<Texture2D> LoadTextureAsync(GetTextureBytesAsync getTextureBytesAsync, bool useMipmap, ColorSpace colorSpace)
{
var imageBytes = await getTextureBytesAsync();
var texture = new Texture2D(2, 2, TextureFormat.ARGB32, useMipmap, colorSpace == ColorSpace.Linear);
if (imageBytes != null)
{
texture.LoadImage(imageBytes);
}
return texture;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 170beedd62e24506acc42b7c04464e84
timeCreated: 1622107784

View File

@@ -2,7 +2,7 @@
using UnityEngine;
using VRMShaders;
namespace UniGLTF
namespace VRMShaders
{
public sealed class RuntimeTextureSerializer : ITextureSerializer
{

View File

@@ -1,8 +1,6 @@
using System;
using System.Linq;
using UnityEngine;
using ColorSpace = UniGLTF.ColorSpace;
namespace VRMShaders
{
@@ -11,6 +9,7 @@ namespace VRMShaders
public static Texture2D CreateEmptyTextureWithSettings(Texture src, ColorSpace dstColorSpace, bool dstNeedsAlpha)
{
var texFormat = dstNeedsAlpha ? TextureFormat.ARGB32 : TextureFormat.RGB24;
texFormat = TextureFormat.ARGB32;
var dst = new Texture2D(src.width, src.height, texFormat, src.HasMipMap(), dstColorSpace == ColorSpace.Linear);
dst.name = src.name;
dst.anisoLevel = src.anisoLevel;

View File

@@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using UniGLTF;
using UnityEngine;
using ColorSpace = UniGLTF.ColorSpace;
namespace VRMShaders
{

View File

@@ -3,40 +3,40 @@ using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using System.Threading.Tasks;
using ColorSpace = UniGLTF.ColorSpace;
namespace VRMShaders
{
public class TextureFactory : IDisposable
{
private readonly Dictionary<SubAssetKey, Texture> m_temporaryTextures = new Dictionary<SubAssetKey, Texture>();
private readonly Dictionary<SubAssetKey, Texture> m_textureCache = new Dictionary<SubAssetKey, Texture>();
private readonly IReadOnlyDictionary<SubAssetKey, Texture> m_externalMap;
private readonly ITextureDeserializer _textureDeserializer;
private readonly IReadOnlyDictionary<SubAssetKey, Texture> _externalMap;
private readonly Dictionary<SubAssetKey, Texture> _temporaryTextures = new Dictionary<SubAssetKey, Texture>();
private readonly Dictionary<SubAssetKey, Texture> _textureCache = new Dictionary<SubAssetKey, Texture>();
/// <summary>
/// Importer が動的に生成した Texture
/// </summary>
public IReadOnlyDictionary<SubAssetKey, Texture> ConvertedTextures => m_textureCache;
public IReadOnlyDictionary<SubAssetKey, Texture> ConvertedTextures => _textureCache;
/// <summary>
///
/// 外部から渡された、すでに存在する Texture (ex. Extracted Editor Asset)
/// </summary>
public IReadOnlyDictionary<SubAssetKey, Texture> ExternalTextures => m_externalMap;
public IReadOnlyDictionary<SubAssetKey, Texture> ExternalTextures => _externalMap;
public TextureFactory(IReadOnlyDictionary<SubAssetKey, Texture> externalTextures)
public TextureFactory(ITextureDeserializer textureDeserializer, IReadOnlyDictionary<SubAssetKey, Texture> externalTextures)
{
m_externalMap = externalTextures;
_textureDeserializer = textureDeserializer;
_externalMap = externalTextures;
}
public void Dispose()
{
foreach (var kv in m_temporaryTextures)
foreach (var kv in _temporaryTextures)
{
DestroyResource(kv.Value);
}
m_temporaryTextures.Clear();
m_textureCache.Clear();
_temporaryTextures.Clear();
_textureCache.Clear();
}
/// <summary>
@@ -46,7 +46,7 @@ namespace VRMShaders
public void TransferOwnership(Func<UnityEngine.Object, bool> take)
{
var transferredAssets = new HashSet<SubAssetKey>();
foreach (var x in m_textureCache)
foreach (var x in _textureCache)
{
if (take(x.Value))
{
@@ -56,23 +56,10 @@ namespace VRMShaders
foreach (var key in transferredAssets)
{
m_textureCache.Remove(key);
_textureCache.Remove(key);
}
}
async Task<Texture2D> LoadTextureAsync(GetTextureBytesAsync getTextureBytesAsync, bool useMipmap, ColorSpace colorSpace)
{
var imageBytes = await getTextureBytesAsync();
var texture = new Texture2D(2, 2, TextureFormat.ARGB32, useMipmap, colorSpace == ColorSpace.Linear);
if (imageBytes != null)
{
texture.LoadImage(imageBytes);
}
return texture;
}
/// <summary>
/// テクスチャーをロード、必要であれば変換して返す。
/// 同じものはキャッシュを返す
@@ -85,12 +72,12 @@ namespace VRMShaders
{
var subAssetKey = texDesc.SubAssetKey;
if (m_externalMap != null && m_externalMap.TryGetValue(subAssetKey, out var externalTexture))
if (_externalMap != null && _externalMap.TryGetValue(subAssetKey, out var externalTexture))
{
return externalTexture;
}
if (m_textureCache.TryGetValue(subAssetKey, out var cachedTexture))
if (_textureCache.TryGetValue(subAssetKey, out var cachedTexture))
{
return cachedTexture;
}
@@ -100,11 +87,11 @@ namespace VRMShaders
case TextureImportTypes.NormalMap:
{
// Runtime/SubAsset 用に変換する
var rawTexture = await LoadTextureAsync(texDesc.Index0, texDesc.Sampler.EnableMipMap, ColorSpace.Linear);
var rawTexture = await _textureDeserializer.LoadTextureAsync(texDesc.Index0, texDesc.Sampler.EnableMipMap, ColorSpace.Linear);
var convertedTexture = NormalConverter.Import(rawTexture);
convertedTexture.name = subAssetKey.Name;
convertedTexture.SetSampler(texDesc.Sampler);
m_textureCache.Add(subAssetKey, convertedTexture);
_textureCache.Add(subAssetKey, convertedTexture);
DestroyResource(rawTexture);
return convertedTexture;
}
@@ -116,18 +103,18 @@ namespace VRMShaders
if (texDesc.Index0 != null)
{
metallicRoughnessTexture = await LoadTextureAsync(texDesc.Index0, texDesc.Sampler.EnableMipMap, ColorSpace.Linear);
metallicRoughnessTexture = await _textureDeserializer.LoadTextureAsync(texDesc.Index0, texDesc.Sampler.EnableMipMap, ColorSpace.Linear);
}
if (texDesc.Index1 != null)
{
occlusionTexture = await LoadTextureAsync(texDesc.Index1, texDesc.Sampler.EnableMipMap, ColorSpace.Linear);
occlusionTexture = await _textureDeserializer.LoadTextureAsync(texDesc.Index1, texDesc.Sampler.EnableMipMap, ColorSpace.Linear);
}
var combinedTexture = OcclusionMetallicRoughnessConverter.Import(metallicRoughnessTexture,
texDesc.MetallicFactor, texDesc.RoughnessFactor, occlusionTexture);
combinedTexture.name = subAssetKey.Name;
combinedTexture.SetSampler(texDesc.Sampler);
m_textureCache.Add(subAssetKey, combinedTexture);
_textureCache.Add(subAssetKey, combinedTexture);
DestroyResource(metallicRoughnessTexture);
DestroyResource(occlusionTexture);
return combinedTexture;
@@ -135,18 +122,18 @@ namespace VRMShaders
case TextureImportTypes.sRGB:
{
var rawTexture = await LoadTextureAsync(texDesc.Index0, texDesc.Sampler.EnableMipMap, ColorSpace.sRGB);
var rawTexture = await _textureDeserializer.LoadTextureAsync(texDesc.Index0, texDesc.Sampler.EnableMipMap, ColorSpace.sRGB);
rawTexture.name = subAssetKey.Name;
rawTexture.SetSampler(texDesc.Sampler);
m_textureCache.Add(subAssetKey, rawTexture);
_textureCache.Add(subAssetKey, rawTexture);
return rawTexture;
}
case TextureImportTypes.Linear:
{
var rawTexture = await LoadTextureAsync(texDesc.Index0, texDesc.Sampler.EnableMipMap, ColorSpace.Linear);
var rawTexture = await _textureDeserializer.LoadTextureAsync(texDesc.Index0, texDesc.Sampler.EnableMipMap, ColorSpace.Linear);
rawTexture.name = subAssetKey.Name;
rawTexture.SetSampler(texDesc.Sampler);
m_textureCache.Add(subAssetKey, rawTexture);
_textureCache.Add(subAssetKey, rawTexture);
return rawTexture;
}
default:

View File

@@ -1,6 +1,5 @@
using System;
using UnityEngine;
using ColorSpace = UniGLTF.ColorSpace;
namespace VRMShaders
{

View File

@@ -2,7 +2,6 @@
using NUnit.Framework;
using UnityEditor;
using UnityEngine;
using ColorSpace = UniGLTF.ColorSpace;
namespace VRMShaders
{

View File

@@ -1,7 +1,6 @@
using NUnit.Framework;
using UnityEditor;
using UnityEngine;
using ColorSpace = UniGLTF.ColorSpace;
namespace VRMShaders
{