mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-04-15 09:44:43 -05:00
59 lines
2.0 KiB
C#
59 lines
2.0 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using VRMShaders;
|
|
|
|
namespace UniGLTF
|
|
{
|
|
public sealed class RuntimeTextureSerializer : ITextureSerializer
|
|
{
|
|
public bool CanExportAsEditorAssetFile(Texture texture)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
public (byte[] bytes, string mime) ExportBytesWithMime(Texture2D texture, ColorSpace textureColorSpace)
|
|
{
|
|
try
|
|
{
|
|
var png = texture.EncodeToPNG();
|
|
if (png != null)
|
|
{
|
|
return (png, "image/png");
|
|
}
|
|
else
|
|
{
|
|
// Failed, because texture is compressed.
|
|
// ex. ".DDS" file, or Compression is enabled in Texture Import Settings.
|
|
return CopyTextureAndGetBytesWithMime(texture, textureColorSpace);
|
|
}
|
|
}
|
|
catch (ArgumentException ex)
|
|
{
|
|
// System.ArgumentException: not readable, the texture memory can not be accessed from scripts. You can make the texture readable in the Texture Import Settings.
|
|
|
|
// Failed, because texture is not readable.
|
|
Debug.LogWarning(ex);
|
|
|
|
// 単純に EncodeToPNG できないため、コピーしてから EncodeToPNG する。
|
|
return CopyTextureAndGetBytesWithMime(texture, textureColorSpace);
|
|
}
|
|
}
|
|
|
|
private static (byte[] bytes, string mime) CopyTextureAndGetBytesWithMime(Texture2D texture, ColorSpace colorSpace)
|
|
{
|
|
var copiedTex = TextureConverter.CopyTexture(texture, colorSpace, null);
|
|
var bytes = copiedTex.EncodeToPNG();
|
|
if (Application.isPlaying)
|
|
{
|
|
UnityEngine.Object.Destroy(copiedTex);
|
|
}
|
|
else
|
|
{
|
|
UnityEngine.Object.DestroyImmediate(copiedTex);
|
|
}
|
|
|
|
return (bytes, "image/png");
|
|
}
|
|
}
|
|
}
|