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

Importing HDRI texture on. NET Android platform throws exception: 'Specified method is not supported' #28

Open
CeSun opened this issue Oct 11, 2024 · 1 comment

Comments

@CeSun
Copy link

CeSun commented Oct 11, 2024

I guess it's because Stream on the Android platform doesn't support certain methods, and I've encountered similar issues using the SharpGLTF library before.

my code:

  public static Texture ImportTextureHdrFromStream(StreamReader streamReader, TextureImportSetting setting)
  {
      if (setting.FlipVertically)
      {
          StbImage.stbi_set_flip_vertically_on_load(1);
      }
      var imageResult = ImageResultFloat.FromStream(streamReader.BaseStream);
      if (setting.FlipVertically)
      {
          StbImage.stbi_set_flip_vertically_on_load(0);
      }
      if (imageResult != null)
      {
          var texture = new Texture
          {
              Width = (uint)imageResult.Width,
              Height = (uint)imageResult.Height,
              Channel = imageResult.Comp.ToTexChannel()
          };
          texture.IsGammaSpace = setting.IsGammaSpace;
          texture.HDRPixels = new(imageResult.Data);
          texture.IsHdrTexture = true;
          return texture;
      }
      throw new Exception("Load Texture error");
  }

The stream is from

new StreamReader(Activity.Assets.Open($"Resource/{Path}"));

When I make the following modifications, it can avoid this problem

 public static Texture ImportTextureHdrFromStream(StreamReader streamReader, TextureImportSetting setting)
 {
     MemoryStream ms = new MemoryStream();
     using (BinaryReader br = new BinaryReader(streamReader.BaseStream))
     {
         List<byte> buffer = new List<byte>();
         do
         {
             var data = br.ReadBytes(1024);
             if (data.Length <= 0)
                 break;
             ms.Write(data);
         } while (true);
     }


     if (setting.FlipVertically)
     {
         StbImage.stbi_set_flip_vertically_on_load(1);
     }
     var imageResult = ImageResultFloat.FromStream(ms);
     if (setting.FlipVertically)
     {
         StbImage.stbi_set_flip_vertically_on_load(0);
     }
     if (imageResult != null)
     {
         var texture = new Texture
         {
             Width = (uint)imageResult.Width,
             Height = (uint)imageResult.Height,
             Channel = imageResult.Comp.ToTexChannel()
         };
         texture.IsGammaSpace = setting.IsGammaSpace;
         texture.HDRPixels = new(imageResult.Data);
         texture.IsHdrTexture = true;
         return texture;
     }
     throw new Exception("Load Texture error");
 }
@rds1983
Copy link
Member

rds1983 commented Oct 11, 2024

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

2 participants