unity.webp icon indicating copy to clipboard operation
unity.webp copied to clipboard

EncodeToWebP flips the texture

Open zerg4000 opened this issue 5 years ago • 1 comments

After EncodeToWebP result image wrong shot-006

`var textasset = Resources.Load<TextAsset> ("webp"); var bytes = textasset.bytes;

Error lError; Texture2D texture = Texture2DExt.CreateTexture2DFromWebP(bytes, lMipmaps: true, lLinear: true, lError: out lError);

if (lError == Error.Success) { image2.texture = texture; } else { Debug.LogError("Webp Load Error : " + lError.ToString()); }

RenderTexture tmp = RenderTexture.GetTemporary( texture.width, texture.height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Linear);

// Blit the pixels on texture to the RenderTexture Graphics.Blit(texture, tmp); // Backup the currently set RenderTexture RenderTexture previous = RenderTexture.active; // Set the current RenderTexture to the temporary one we created RenderTexture.active = tmp;

// Create a new readable Texture2D to copy the pixels to it Texture2D myTexture2D = new Texture2D(texture.width, texture.height);

// Copy the pixels from the RenderTexture to the new Texture myTexture2D.ReadPixels(new Rect(0, 0, tmp.width, tmp.height), 0, 0); myTexture2D.Apply();

// Reset the active RenderTexture RenderTexture.active = previous;

// Release the temporary RenderTexture RenderTexture.ReleaseTemporary(tmp);

// "myTexture2D" now has the same pixels from "texture" and it's readable.

string fExport = Application.dataPath + "/test.webp"; byte[] bExport = myTexture2D.EncodeToWebP(25, out lError); File.WriteAllBytes(fExport, bExport);`

zerg4000 avatar Jul 27 '20 12:07 zerg4000

That is problem to using webp on unity. Unity's texture need to flip updown.

To archive that flip like that

https://github.com/netpyoung/unity.webp/blob/76a58b03aadc57a3d9d22e21a04dd4bd83566136/unity_project/Assets/example4/EncodeToWebP.cs#L61

or using advanced config with WebPEncode API

https://developers.google.com/speed/webp/docs/api

netpyoung avatar Jul 27 '20 14:07 netpyoung

  • added shader for flip case(just uv's v to one minus)

  • https://github.com/netpyoung/unity.webp/blob/9219be2c51254e89837150e80e8d5643e0334a6d/unity_project/Assets/unity.webp/Resources/sg_UnlitFlipTexture.shadergraph

netpyoung avatar Oct 20 '23 12:10 netpyoung