- Home /
How do I create a Texture2D via script that bypasses sRGB sampling?
Hi there,
I need help on creating a Texture2D with a script that bypasses sRGB sampling.
Specifically, the texture is created using Texture2D(), is filled with software drawing functions, and is used to render GUI.
This is a problem with an asset I purchased from the asset store called uWebKit. Apparently it does not support the Linear color model. In Gamma, it appears normal, but in Linear (the space we are using) it appears very dark.
The Unity scripting manual briefly explains that it is possible to create a Texture2D in either Linear or Gamma space, but does not go into any detail as to how you can do this, or at least does not give any obvious explanation:
Documentation on Texture2D.Texture2D()
The texture will be width by height size, with a given format, with mipmaps or without and in either the linear or sRGB color space.
If anyone can help me with this problem, it'd be strongly appreaciated and very helpful. Many thanks in advance for taking the time to help.
Answer by manutoo · Sep 14, 2012 at 08:54 AM
The documentation is incomplete, they forgot to write the (optional) last parameter, you can see it in the Metadata :
public Texture2D(int width, int height, TextureFormat format, bool mipmap, bool linear);
Thought I'd chime in with a note -- a better name for the variable would be "bypassSRGBColorSpace", like the corresponding checkbox in the texture import GUI. The "linear" boolean doesn't magically make your texture use linear color space if you're using gamma -- it bypasses processing if linear is turned on. The name is unhelpful.
@eheimburg, linear means the texture contains linear colors, so I think it's correctly named.
It's not. linear would imply that it would convert the texture when you are in gamma color space, but this is not the case.
No it isn't, but it's perfectly reasonable to assume that linear means it would make the image use linear colors. Hence the need for a useful variable name.
Agreed. Na$$anonymous$$g should match texture importer inspector. Else you're left with guesswork.
Answer by SantiagoMartin · Sep 29, 2017 at 05:15 PM
Hi, I'm dealing with the same problem. I create a Texture2D with bool linear=true to bypass sRGB compensation before entering the shader. My texture, a cosine gray scale function, is set to a shader that applies a gamma correction, col = pow(col, 1/_gamma); - I have my own reasons to do this, I'm working with optometrists in a research project -. The result should be the same no matter if I'm in Gamma Color Space or in Linear Color Space, as far as I know. But it is not. You can see here three results for gamma = 1.72, the firsts two in Linear Color Space (with bool linear true and false) and the third in Gamma Color Space (the results is independent of bool linear value) (this looks right for me). Any suggestion? Thanks
Color space LINEAR - Texture2D bool linear = true (left) and false (right)
Color space GAMMA - Texture2D bool linear = true or false (same result)
Your answer
![](https://koobas.hobune.stream/wayback/20220613075828im_/https://answers.unity.com/themes/thub/images/avi.jpg)