[Daily Development of Unity3D] Color Space of Unity3D [Full Analysis]

Recommended reading

Hello everyone, I am a Buddhist engineer ☆The Quiet Little Magic Dragon☆ . I update Unity development skills from time to time. If you find it useful, please remember to click three times.

I. Introduction

Edit→Player Settings→Other Settings→Rendering→Color Space(颜色空间)

Let’s put a picture first to get a feel for it:
Insert image description here
In the Unity packaging options, there is a Color Space option, and there are two options Gamma: and .Linear

The main function is to set whether Unity converts the input default map. This problem does not exist with normal maps.

GammaWhen , Unity will not convert the image in the background. Even if the input image is a corrected image, Unity will not process it. For example, it will not Remove Gamma Correction去除伽马校正happen Gamma Correction伽马校正unless you implement it manually.

LinearWhen , Unity will carry out the process of unifying linear space, that is Gamma Correction, for sRGB texture, Unity will use the GPU to sample the image, 剔除矫正Remove Gamma Correctionconvert it to linear space, and then pass it to the Shader for processing, and the processed image will be added Gamma矫正. displayed on the screen again.

In short, Gammathe input and output will not be processed, Linearbut the sRGB texture will be processed 剔除矫正,转换到线性空间,再传给Shader处理.

Next, let’s take a look at the principles of Gamma and Linear, as well as the texture types that support Linear color space, as well as the hardware and features that support Linear space.

2. Text

2-1. Texture and Linear

Unity selects the sRGB (Color Texture) option in the import settings panel of the imported map . Check it to tell Unity that this is an sRGB map . It needs to encode sRGB to obtain data for lighting calculations. If it is not checked, it means that this is an sRGB map. A linear map whose color data can be directly used in lighting calculations.

Of course, in addition to the sRGB (Color Texture) option, other types, such as Normal Map and Light Map , are Linear. Unity will handle them after setting the type.
Insert image description here
Insert image description here

So when will sRGB maps be used?

For example, when exporting textures, textures that provide color information, such as Albedo and Specular Color, are encoded in sRGB. The purpose of this is to make the textures look more comfortable and more in line with the way the human eye perceives them.

Therefore, in this case, you can export the sRBG map for use. However, sRBG encoding is not performed for maps such as normal maps and metallicity maps that are used to store numerical information, so they are used as linear maps.

2-2. What is sRGB

What is mentioned above 解码Gammais only the mechanism under ideal conditions.

However, the decoded Gamma value of each monitor is not uniform, which will cause the same picture to look different on different monitors.

The solution is to introduce Color Profile, which is the correspondence between RGB values ​​and color wavelengths.

Each picture has a Color Profile. After the monitor receives this file, it converts it into the RGB value required for the screen to output the specific wavelength according to the monitor's own Color Profile.

In other words, the Color Profile has both the wavelength value and the Gamma correction.

sRGB is the most common Color Profile and is a color standard jointly developed by Microsoft and Hewlett-Packard and other manufacturers.

The sRGB color space is the Gamma color space.

2-3. Hardware feature support

Mainly supported by two hardware features:

sRGB Frame Buffer

  • Left gamma correction before outputting Shader's calculation results to the display
  • When read as a texture, the stored color is automatically converted from sRGB space to linear space.
  • When calling ReadPixels() and readBackImage(), the color in the sRGB space will be directly returned.
  • sRGB Frame Buffer only supports 8-bit format per channel and does not support floating point format.
  • After HDR is turned on, the rendering results will first be drawn into FB in floating point format, and finally drawn into sRGB FB and output to
    sRGB Sampler.
  • Convert sRGB textures to linear sampling
  • Using hardware features to complete linear sampling of the sRGB texture and gamma correction of the shader calculation results is faster than correcting the texture sampling and calculation results in the shader.

2-4. Linear Space Linear Space

In the real world, if the intensity of light doubles, the brightness will also double. This is a linear relationship.

In order to ensure mathematical accuracy, lighting calculations should be performed in linear space, which means that input values ​​in sRGB format need to be sRGB decoded first - there is the **sRGB (Color Texture)** option in the texture import settings panel.

If you check to enable sRGB , the sampled values ​​will be automatically decoded into sRGB during sampling .

In addition, a buffer in sRGB format is set up, and the GPU will automatically perform sRGB encoding before the shader writes to the color buffer .

Two underlying graphics APIs are used here : sRGB Sampler and sRGB Frame Buffer .
Insert image description here

(If Unity uses the Gamma color space, the sRGB setting has no effect because the texture will always be linearly sampled.)

Meaning of the sRGB (Color Texture) setting: This should always check for non-hdr color textures (like albedo and highlight colors). If the texture stores information with a specific meaning and you need to get the exact value in the shader (for example, smoothness or metallicity), uncheck this box. This box is checked by default.

In fact, the picture will become brighter after unchecking sRGB in unity. I think the actual situation may be as follows: (Pictures without sRGB will be encoded into sRGB format)

Insert image description here
sRGB is only valid for RGB channels, not for alpha channels

The alpha value does not participate in any calculation of encoding gamma and decoding gamma. It enters the shader directly and is read by the shader;

Take the MetallicSmoothness map as an example. The Smoothness stored in the Alpha channel is inherently linear. Whether the texture is checked or not with sRGB, it will not affect it. The shader can correctly read its value.

2-5. Advantages and disadvantages of Gamma and Linear

Linear linear color space

Linear linear color space is a marking method that is closer to showing the propagation of light, using linear relationships to process colors.

In the Linear linear color space, changes in color values ​​are more accurate and natural, enabling more real-world lighting effects.

Gamma gamma color space

Gamma Gamma color space is a way that is more consistent with human eye perception and uses non-linear relationships to process colors.

In the Gamma color space, changes in color values ​​are more in line with the perceptual characteristics of the human eye and can better present aspects such as contrast and brightness.

in conclusion

For projects that require more realistic lighting effects, it would be more appropriate to use Linear linear color space. The default color space is Gamma color space.

All textures that require the human eye to participate in being created should be sRGB (such as pictures drawn by art).

All textures calculated by computer (such as noise, mask, lightmap) should be Linear.

This is easy to explain. Only when seeing things with the human eye do we need to consider display characteristics and correction issues.

It is not necessary for the computer. From the computer's perspective, it is just ordinary data. Naturally, it is best to choose Linear directly.

3. Postscript

If you find this article useful, don't forget to click "Follow" to avoid getting lost and continue to share more useful articles about Unity.


Your likes are support for the blogger. If you have any questions, please leave a message:

The blogger’s homepage has contact information.

The blogger also has many treasure articles waiting for you to discover:

Column direction Introduction
Unity3D develops small games Mini game development tutorial Share some small games developed using the Unity3D engine and share some tutorials on making small games.
Unity3D from entry to advanced getting Started Get inspiration from self-study Unity, summarize the route of learning Unity from scratch, and have knowledge of C# and Unity.
Unity3D UGUI UGUI A full analysis of Unity's UI system UGUI, starting from the basic controls of UGUI, and then comprehensively teaching the principles and use of UGUI.
Reading data in Unity3D file reading Use Unity3D to read txt documents, json documents, xml documents, csv documents, and Excel documents.
Unity3D data collection data collection Array collection: Knowledge sharing on data collections such as arrays, Lists, dictionaries, stacks, and linked lists.
Unity3D VR/AR (virtual simulation) development virtual reality Summarize the common virtual simulation needs of bloggers and explain them with cases.
Unity3D plug-in plug-in Mainly share some plug-in usage methods used in Unity development, plug-in introduction, etc.
Daily development of Unity3D daily records Mainly used by bloggers in daily development, including methods and techniques, development ideas, code sharing, etc.
Unity3D daily BUG daily records Record the bugs and pitfalls encountered during project development using the Unity3D editor so that future generations can have some reference.

Guess you like

Origin blog.csdn.net/q764424567/article/details/133927994