mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-04-25 07:28:51 -05:00
ITextureSerializer に、出力予定アセットに対する事前処理の定義を追加
This commit is contained in:
parent
c51cf3a7be
commit
6fed2d0f2a
|
|
@ -1,6 +1,5 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
|
|
@ -8,7 +7,7 @@ namespace VRMShaders
|
|||
{
|
||||
public sealed class EditorTextureSerializer : ITextureSerializer
|
||||
{
|
||||
private readonly RuntimeTextureSerializer m_runtimeSerializer = new RuntimeTextureSerializer();
|
||||
private readonly RuntimeTextureSerializer _runtimeSerializer = new RuntimeTextureSerializer();
|
||||
|
||||
/// <summary>
|
||||
/// Texture をオリジナルのテクスチャアセット(png/jpg)ファイルのバイト列そのまま出力してよいかどうか判断する。
|
||||
|
|
@ -56,7 +55,19 @@ namespace VRMShaders
|
|||
return (bytes, mime);
|
||||
}
|
||||
|
||||
return m_runtimeSerializer.ExportBytesWithMime(texture, exportColorSpace);
|
||||
return _runtimeSerializer.ExportBytesWithMime(texture, exportColorSpace);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 出力に使用したいテクスチャに対して、Unity のエディタアセットとしての圧縮設定を OFF にする。
|
||||
/// </summary>
|
||||
public void ModifyTextureAssetBeforeExporting(Texture texture)
|
||||
{
|
||||
if (EditorTextureUtility.TryGetAsEditorTexture2DAsset(texture, out var texture2D, out var assetImporter))
|
||||
{
|
||||
assetImporter.textureCompression = TextureImporterCompression.Uncompressed;
|
||||
assetImporter.SaveAndReimport();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
|
|
@ -22,5 +22,12 @@ namespace VRMShaders
|
|||
/// 具体的には Texture2D をコピーする際に、コピー先の Texture2D の色空間を決定するために使用する。
|
||||
/// </summary>
|
||||
(byte[] bytes, string mime) ExportBytesWithMime(Texture2D texture, ColorSpace exportColorSpace);
|
||||
|
||||
/// <summary>
|
||||
/// エクスポートに使用したい Texture に対して、事前準備を行う。
|
||||
///
|
||||
/// たとえば UnityEditor においては、Texture Asset の圧縮設定を OFF にしたりしたい。
|
||||
/// </summary>
|
||||
void ModifyTextureAssetBeforeExporting(Texture texture);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
using System;
|
||||
using UnityEngine;
|
||||
using VRMShaders;
|
||||
|
||||
namespace VRMShaders
|
||||
{
|
||||
|
|
@ -39,6 +38,11 @@ namespace VRMShaders
|
|||
}
|
||||
}
|
||||
|
||||
public void ModifyTextureAssetBeforeExporting(Texture texture)
|
||||
{
|
||||
// NOTE: Do nothing.
|
||||
}
|
||||
|
||||
private static (byte[] bytes, string mime) CopyTextureAndGetBytesWithMime(Texture2D texture, ColorSpace colorSpace)
|
||||
{
|
||||
var needsAlpha = texture.format != TextureFormat.RGB24;
|
||||
|
|
|
|||
|
|
@ -10,12 +10,12 @@ namespace VRMShaders
|
|||
/// </summary>
|
||||
public sealed class TextureExporter : IDisposable, ITextureExporter
|
||||
{
|
||||
private readonly ITextureSerializer m_textureSerializer;
|
||||
private readonly ITextureSerializer _textureSerializer;
|
||||
private readonly List<TextureExportParam> _exportingList = new List<TextureExportParam>();
|
||||
|
||||
public TextureExporter(ITextureSerializer textureSerializer)
|
||||
{
|
||||
m_textureSerializer = textureSerializer;
|
||||
_textureSerializer = textureSerializer;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
|
@ -94,7 +94,13 @@ namespace VRMShaders
|
|||
|
||||
var param = new TextureExportParam(TextureExportTypes.OcclusionMetallicRoughness, ColorSpace.Linear,
|
||||
metallicSmoothTexture, occlusionTexture, smoothness, false,
|
||||
() => OcclusionMetallicRoughnessConverter.Export(metallicSmoothTexture, smoothness, occlusionTexture));
|
||||
() =>
|
||||
{
|
||||
_textureSerializer.ModifyTextureAssetBeforeExporting(metallicSmoothTexture);
|
||||
_textureSerializer.ModifyTextureAssetBeforeExporting(occlusionTexture);
|
||||
return OcclusionMetallicRoughnessConverter.Export(metallicSmoothTexture, smoothness,
|
||||
occlusionTexture);
|
||||
});
|
||||
if (TryGetExistsParam(param, out var existsIdx))
|
||||
{
|
||||
// Return cacehd
|
||||
|
|
@ -116,7 +122,11 @@ namespace VRMShaders
|
|||
}
|
||||
|
||||
var param = new TextureExportParam(TextureExportTypes.Normal, ColorSpace.Linear, src, default, default,
|
||||
false, () => NormalConverter.Export(src));
|
||||
false, () =>
|
||||
{
|
||||
_textureSerializer.ModifyTextureAssetBeforeExporting(src);
|
||||
return NormalConverter.Export(src);
|
||||
});
|
||||
if (TryGetExistsParam(param, out var existsIdx))
|
||||
{
|
||||
// Return cached;
|
||||
|
|
@ -136,12 +146,15 @@ namespace VRMShaders
|
|||
{
|
||||
// get Texture2D
|
||||
var texture2D = src as Texture2D;
|
||||
if (m_textureSerializer.CanExportAsEditorAssetFile(texture2D, exportColorSpace))
|
||||
if (_textureSerializer.CanExportAsEditorAssetFile(texture2D, exportColorSpace))
|
||||
{
|
||||
// do nothing
|
||||
// NOTE: 生のファイルとして出力可能な場合、何もせずそのまま Texture2D を渡す。
|
||||
// ただし、この場合のみ圧縮設定をオフにしなかった場合、挙動としてバグっぽく見えるので、これもオフにする。
|
||||
_textureSerializer.ModifyTextureAssetBeforeExporting(src);
|
||||
}
|
||||
else
|
||||
{
|
||||
_textureSerializer.ModifyTextureAssetBeforeExporting(src);
|
||||
texture2D = TextureConverter.CopyTexture(src, exportColorSpace, needsAlpha, null);
|
||||
}
|
||||
return texture2D;
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user