mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-08-11 03:55:58 -05:00
Merge pull request #1403 from Santarh/textureUncompressed
Ignore texture compression settings while exporting glTF / VRM.
This commit is contained in:
@@ -17,10 +17,10 @@ namespace UniGLTF
|
||||
///
|
||||
/// を更新し、textures の index を返す
|
||||
///
|
||||
/// もっとも根本の Exporter クラスのみが呼び出すべきである。
|
||||
/// 他の拡張機能などが呼び出すべきではない。
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="gltf"></param>
|
||||
/// <param name="bufferIndex"></param>
|
||||
/// <param name="texture"></param>
|
||||
/// <returns>gltf texture index</returns>
|
||||
public static int PushGltfTexture(ExportingGltfData data, Texture2D texture, ColorSpace textureColorSpace, ITextureSerializer textureSerializer)
|
||||
{
|
||||
|
||||
@@ -46,8 +46,6 @@ namespace UniGLTF
|
||||
private set;
|
||||
}
|
||||
|
||||
public ITextureExporter TextureExporter => m_textureExporter;
|
||||
|
||||
protected virtual IMaterialExporter CreateMaterialExporter()
|
||||
{
|
||||
return new MaterialExporter();
|
||||
@@ -65,7 +63,8 @@ namespace UniGLTF
|
||||
}
|
||||
}
|
||||
|
||||
TextureExporter m_textureExporter;
|
||||
protected ITextureExporter TextureExporter => _textureExporter;
|
||||
private TextureExporter _textureExporter;
|
||||
|
||||
GltfExportSettings m_settings;
|
||||
|
||||
@@ -236,7 +235,7 @@ namespace UniGLTF
|
||||
#region Materials and Textures
|
||||
Materials = uniqueUnityMeshes.GetUniqueMaterials().ToList();
|
||||
|
||||
m_textureExporter = new TextureExporter(textureSerializer);
|
||||
_textureExporter = new TextureExporter(textureSerializer);
|
||||
|
||||
var materialExporter = CreateMaterialExporter();
|
||||
_gltf.materials = Materials.Select(x => materialExporter.ExportMaterial(x, TextureExporter, m_settings)).ToList();
|
||||
@@ -378,7 +377,7 @@ namespace UniGLTF
|
||||
ExportExtensions(textureSerializer);
|
||||
|
||||
// Extension で Texture が増える場合があるので最後に呼ぶ
|
||||
var exported = m_textureExporter.Export();
|
||||
var exported = _textureExporter.Export();
|
||||
for (var exportedTextureIdx = 0; exportedTextureIdx < exported.Count; ++exportedTextureIdx)
|
||||
{
|
||||
var (unityTexture, colorSpace) = exported[exportedTextureIdx];
|
||||
|
||||
@@ -118,7 +118,7 @@ namespace VRM
|
||||
VRM.meta.title = meta.Title;
|
||||
if (meta.Thumbnail != null)
|
||||
{
|
||||
VRM.meta.texture = GltfTextureExporter.PushGltfTexture(_data, meta.Thumbnail, ColorSpace.sRGB, textureSerializer);
|
||||
VRM.meta.texture = TextureExporter.RegisterExportingAsSRgb(meta.Thumbnail, needsAlpha: true);
|
||||
}
|
||||
|
||||
VRM.meta.licenseType = meta.LicenseType;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user