unsafe List <(Texture2D, int)> LoadAnimation(string loadPath) { List <ValueTuple <Texture2D, int> > ret = new List <ValueTuple <Texture2D, int> >(); TextAsset textasset = Resources.Load <TextAsset>(loadPath); byte[] bytes = textasset.bytes; WebPAnimDecoderOptions option = new WebPAnimDecoderOptions { use_threads = 1, color_mode = WEBP_CSP_MODE.MODE_RGBA }; Demux.WebPAnimDecoderOptionsInit(ref option); fixed(byte *p = bytes) { IntPtr ptr = (IntPtr)p; WebPData webpdata = new WebPData { bytes = ptr, size = new UIntPtr((uint)bytes.Length) }; IntPtr dec = Demux.WebPAnimDecoderNew(ref webpdata, ref option); WebPAnimInfo anim_info = new WebPAnimInfo(); Demux.WebPAnimDecoderGetInfo(dec, ref anim_info); Debug.LogWarning($"{anim_info.frame_count} {anim_info.canvas_width}/{anim_info.canvas_height}"); int size = anim_info.canvas_width * 4 * anim_info.canvas_height; WebPAnimDecoder decoder = (WebPAnimDecoder)Marshal.PtrToStructure(dec, typeof(WebPAnimDecoder)); decoder.config_.options.flip = 1; decoder.config_.options.no_fancy_upsampling = 1; Marshal.StructureToPtr(decoder, dec, true); IntPtr unmanagedPointer = new IntPtr(); int timestamp = 0; for (int i = 0; i < anim_info.frame_count; ++i) { int result = Demux.WebPAnimDecoderGetNext(dec, ref unmanagedPointer, ref timestamp); if (result != 1) { Debug.LogError("WTF"); } int lWidth = anim_info.canvas_width; int lHeight = anim_info.canvas_height; bool lMipmaps = false; bool lLinear = false; Texture2D texture = new Texture2D(lWidth, lHeight, TextureFormat.RGBA32, lMipmaps, lLinear); texture.LoadRawTextureData(unmanagedPointer, size); texture.Apply(updateMipmaps: false, makeNoLongerReadable: true); ret.Add((texture, timestamp)); } Demux.WebPAnimDecoderReset(dec); Demux.WebPAnimDecoderDelete(dec); } return(ret); }
unsafe List <(Texture2D, int)> LoadAnimation3(string loadPath) { List <ValueTuple <Texture2D, int> > ret = new List <ValueTuple <Texture2D, int> >(); TextAsset textasset = Resources.Load <TextAsset>(loadPath); byte[] bytes = textasset.bytes; WebPAnimDecoderOptions option = new WebPAnimDecoderOptions { use_threads = 1, color_mode = WEBP_CSP_MODE.MODE_RGBA }; var config = new WebPDecoderConfig(); if (Decode.WebPInitDecoderConfig(ref config) == 0) { throw new Exception("WebPInitDecoderConfig failed. Wrong version?"); } Demux.WebPAnimDecoderOptionsInit(ref option); fixed(byte *p = bytes) { IntPtr ptr = (IntPtr)p; var webpdata = new WebPData { bytes = ptr, size = new UIntPtr((uint)bytes.Length) }; WebPAnimDecoderOptions opt = new WebPAnimDecoderOptions(); Demux.WebPAnimDecoderOptionsInit(ref opt); IntPtr webPAnimDecoderPtr = Demux.WebPAnimDecoderNewInternal(ref webpdata, ref opt, Demux.WEBP_DEMUX_ABI_VERSION); Debug.Log($"webPAnimDecoderPtr = {webPAnimDecoderPtr}"); WebPAnimDecoder decoder = (WebPAnimDecoder)Marshal.PtrToStructure(webPAnimDecoderPtr, typeof(WebPAnimDecoder)); //int width = 400; //int height = 400; { //config.input.has_alpha = 1; //config.options.bypass_filtering = 1; //config.options.no_fancy_upsampling = 1; config.options.use_threads = 1; //config.options.no_fancy_upsampling = 0; //config.options.use_cropping = 0; //config.options.use_scaling = 1; //config.options.scaled_width = width; //config.options.scaled_height = height; config.options.flip = 1; //config.options.dithering_strength = 100; config.output.colorspace = WEBP_CSP_MODE.MODE_RGBA; //config.output.is_external_memory = 1; //config.output.width = width; //config.output.height = height; } decoder.config_ = config; Marshal.StructureToPtr(decoder, webPAnimDecoderPtr, true); IntPtr dec = webPAnimDecoderPtr; WebPAnimInfo anim_info = new WebPAnimInfo(); Demux.WebPAnimDecoderGetInfo(dec, ref anim_info); Debug.LogWarning($"{anim_info.frame_count} {anim_info.canvas_width}/{anim_info.canvas_height}"); int size = anim_info.canvas_width * 4 * anim_info.canvas_height; IntPtr unmanagedPointer = new IntPtr(); int timestamp = 0; for (int i = 0; i < anim_info.frame_count; ++i) { int result = Demux.WebPAnimDecoderGetNext(dec, ref unmanagedPointer, ref timestamp); int lWidth = anim_info.canvas_width; int lHeight = anim_info.canvas_height; bool lMipmaps = false; bool lLinear = false; Texture2D texture = new Texture2D(lWidth, lHeight, TextureFormat.RGBA32, lMipmaps, lLinear); texture.LoadRawTextureData(unmanagedPointer, size); texture.Apply(updateMipmaps: false, makeNoLongerReadable: true); ret.Add((texture, timestamp)); } Demux.WebPAnimDecoderReset(dec); Demux.WebPAnimDecoderDelete(dec); } return(ret); }