Skip to content

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

@CeSun

Description

@CeSun

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");
 }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions