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

Stuttering when loading VRM files asynchronously #1982

Closed
Gunheui opened this issue Feb 9, 2023 · 1 comment · Fixed by #2039
Closed

Stuttering when loading VRM files asynchronously #1982

Gunheui opened this issue Feb 9, 2023 · 1 comment · Fixed by #2039

Comments

@Gunheui
Copy link

Gunheui commented Feb 9, 2023

Environments (please complete the following information):

  • UniVRM version: 0.107.0
  • Unity version: Unity-2021.3.11f1
  • OS: Windows 10

Describe the bug

-VRM Runtime Import

Unity's API texture.LoadImage stutters when loading a VRM file during runtime, which is used while importing textures. While looking for a solution, I found and used the UnityAsyncImageLoader Github page, and it has improved considerably. The code below is an improved UnityTextureDeserialize. It's not a bug, but I hope it helps a lot :)

Github Page

Changed Code

using System.Threading.Tasks;
using UnityEngine;

namespace VRMShaders
{
    /// <summary>
    /// Unity の ImageConversion.LoadImage を用いて PNG/JPG の読み込みを実現する
    /// </summary>
    public sealed class UnityTextureDeserializer : ITextureDeserializer
    {
        public async Task<Texture2D> LoadTextureAsync(DeserializingTextureInfo textureInfo, IAwaitCaller awaitCaller)
        {
            switch (textureInfo.DataMimeType)
            {
                case "image/png":
                    break;
                case "image/jpeg":
                    break;
                default:
                    if (string.IsNullOrEmpty(textureInfo.DataMimeType))
                    {
                        Debug.Log($"Texture image MIME type is empty.");
                    }
                    else
                    {
                        Debug.Log($"Texture image MIME type `{textureInfo.DataMimeType}` is not supported.");
                    }
                    break;
            }
            
            var texture = new Texture2D(2, 2);
            if (textureInfo.ImageData != null)
            {
                
                //texture.LoadImage(textureInfo.ImageData); // Prev
                
                //Curr
                if (textureInfo.DataMimeType == "image/png")
                {
                    texture = await AsyncImageLoader.CreateFromImageAsync(textureInfo.ImageData,
                        new AsyncImageLoader.LoaderSettings()
                        {
                            format = AsyncImageLoader.FreeImage.Format.FIF_PNG,
                            linear = textureInfo.ColorSpace == ColorSpace.Linear
                        });   
                }
                else
                {
                    texture = await AsyncImageLoader.CreateFromImageAsync(textureInfo.ImageData,
                        new AsyncImageLoader.LoaderSettings()
                        {
                            format = AsyncImageLoader.FreeImage.Format.FIF_JPEG,
                            linear = textureInfo.ColorSpace == ColorSpace.Linear
                        });   
                }


                texture.wrapModeU = textureInfo.WrapModeU;
                texture.wrapModeV = textureInfo.WrapModeV;
                texture.filterMode = textureInfo.FilterMode;
                await awaitCaller.NextFrame();
            }

            return texture;
        }
    }
}



@Gunheui Gunheui added the bug Something isn't working label Feb 9, 2023
@ousttrue ousttrue added this to the v0.109 milestone Mar 8, 2023
@ousttrue
Copy link
Contributor

ousttrue commented Mar 9, 2023

Thank you for reporting.

I will improve the documentation by showing this code as a sample.

@ousttrue ousttrue modified the milestones: v0.109, v0.110 Mar 9, 2023
@ousttrue ousttrue added document/sample and removed bug Something isn't working labels Mar 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants