Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hard coded, UniVRM to export JPGs instead of PNGs #65

Open
gm3 opened this issue Jan 18, 2023 · 0 comments
Open

Hard coded, UniVRM to export JPGs instead of PNGs #65

gm3 opened this issue Jan 18, 2023 · 0 comments

Comments

@gm3
Copy link
Owner

gm3 commented Jan 18, 2023

if updated it will break

using System;
using UnityEngine;

namespace VRMShaders
{
    public sealed class RuntimeTextureSerializer : ITextureSerializer
    {
        public bool CanExportAsEditorAssetFile(Texture texture, ColorSpace exportColorSpace)
        {
            return false;
        }

        public (byte[] bytes, string mime) ExportBytesWithMime(Texture2D texture, ColorSpace exportColorSpace)
        {
            try
            {
                var jpg = texture.EncodeToJPG();
                if (jpg != null)
                {
                    return (jpg, "image/jpg");
                }
                else
                {
                    // Failed, because texture is compressed.
                    // ex. ".DDS" file, or Compression is enabled in Texture Import Settings.
                    return CopyTextureAndGetBytesWithMime(texture, exportColorSpace);
                }
            }
            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, exportColorSpace);
            }
        }

        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;
            var copiedTex = TextureConverter.CopyTexture(texture, colorSpace, needsAlpha, null);
            var bytes = copiedTex.EncodeToJPG();
            if (Application.isPlaying)
            {
                UnityEngine.Object.Destroy(copiedTex);
            }
            else
            {
                UnityEngine.Object.DestroyImmediate(copiedTex);
            }

            return (bytes, "image/jpg");
        }
    }
}

I changed every PNG to JPG and it worked

@gm3 gm3 closed this as completed Feb 22, 2023
@gm3 gm3 reopened this Apr 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant