- Home /
UnityWebRequest.GetTexture returns incorrect Linear or Gamma or whatever
My project uses linear space lighting. When I get a texture using: var www = UnityWebRequest.GetTexture( filename,true); yield return www.Send(); var tex = DownloadHandlerTexture.GetContent(www); And assign it to a sprite the result looks like every pixel has had sqrt() applied to it. (i.e. linear/gamma correction applied)
using: var www = new WWW( filename); yield return www; var tex = new Texture2D(4,4,TextureFormat.RGB24, false,false); // Size gets overridden www.LoadImageIntoTexture(tex);
Works. Is there a way to use the more efficient UnityWebRequest to get the same results? Thanks, David Wu
Answer by Bunny83 · Sep 07, 2020 at 07:51 AM
This might be related to this question over here. However it's hard to tell since we don't even know what kind of image you're loading.
Thx but trying to use your tool I gor a weird result, I might didn't understood : This is the map I load :
and this is the result I got :
Using 2019.4.4f1 in linear mode
coded like this :
byte[] spriteBytes;
using (var fileStream = System.IO.File.OpenRead(filename))
using (var reader = new System.IO.BinaryReader(fileStream))
{
var png = PNGTools.ReadPNGFile(reader);
for (int i = png.chunks.Count - 1; i >= 0; i--)
{
var c = png.chunks[i];
if (c.type == EChunkType.gA$$anonymous$$A ||
c.type == EChunkType.pHYs ||
c.type == EChunkType.sRBG)
{
png.chunks.RemoveAt(i);
}
}
using (var mem = new $$anonymous$$emoryStream())
{
PNGTools.WritePNGFile(png, new BinaryWriter(mem));
spriteBytes = mem.ToArray();
}
_$$anonymous$$ap.LoadRawTextureData(spriteBytes);
_$$anonymous$$ap.anisoLevel = 0;
_$$anonymous$$ap.filter$$anonymous$$ode = Filter$$anonymous$$ode.Point;
_$$anonymous$$ap.wrap$$anonymous$$ode = TextureWrap$$anonymous$$ode.Clamp;
}
The most weird is using "_$$anonymous$$ap.LoadImage(imgRequest.downloadHandler.data)" give me better result but still not the same as the map.
Your code looks strange. First of all my PNG tools obviously only work with png files. Any other formats are not supported. Second your usage of LoadRawTextureData makes no sense here. This method expects preformatted pixel data as it is represented in memory. You can not load any image file format with this method. You have to use LoadImage to actually load a png file as texture.
Note that you don't have to load the image from a file. Instead of a file stream you can use a memory stream, just like we used for writing the modified png data.
Yes you're right use of LoadRawTextureData doesn't make sense, but I still having diferences between the original and the draw map (PNG format like the 1st I sent : https://answers.unity.com/storage/temp/167072-fade.png).
It seems that alpha isn't used, or quality wasn't the same, I don't understand why :