Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between gamma space and linear space in unity

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the difference between gamma space and linear space in unity". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Please follow Xiaobian's train of thought to study and learn "what is the difference between gamma space and linear space in unity"?

I. Overview

I haven't written an article for a long time. Today, I write a summary of my personal understanding of gamma space and linear space. After consulting and learning various materials, I can be regarded as a personal note.

2. Gamma color space and linear color space

In fact, for the understanding of color space, I personally understand it like this: the ultimate goal of all color spaces is to be finally put into the human eye and be able to basically reproduce the colors of nature.

With this purpose in mind, some of the following understandings will be more well-founded.

2.1 perception of the human eye

Since the goal is us, then we need to have a basic understanding of the perception of our own eyes: the perception of light intensity is non-linear.

What is linear and what is nonlinear, from a mathematical point of view, is whether the change of the independent variable is proportional to the change of the dependent variable (default is 1). If it is proportional, that is, y = kx, then the change is linear.

If it is not in a fixed proportion, then the change is nonlinear, and nonlinearity is the most common change relationship in nature.

Human perception of changes in many environmental factors is nonlinear, for example, for scales, it is based on proportional relationships rather than linear relationships, for decibels, for pain levels, and so on.

Back to the perception of light intensity, the human eye's perception of the change of light intensity is nonlinear, which is the conclusion drawn through experiments. If you put a candle in a completely dark room, the change in the perceived light intensity is more obvious.

If 100 candles have been placed in the room, put one candle again, the light intensity change brought about by the newly added candle is not as strong as the original 0 to 1 (the default light intensity increment for each candle is the same).

It can be seen that the human eye's perception of the highlighted part is not as strong as that of the dark part.

2.2 limited storage space

It is mentioned above that the perceptual characteristics of the human eye, then what about the light in nature? The light intensity in nature is proportional to its corresponding power, and the corresponding range is extremely large: 100000lux in sunlight, starlight 0.0003 lux....

If such a wide range of light intensity changes are to be stored, the use of memory and transmission bandwidth is unbearable.

At present, the mainstream in the industry, for the expression of color brightness, uses 8-bit, that is, 8bit, from 0-255to represent. Gradually, there is also a 32-bit true color, which is certainly not in this discussion.

2.3 Gamma space

Based on the arguments of 1 and 2, then there is a basic idea of how to store the results of natural lighting in the actual picture: compress the natural light into an 8-bit image with a function close to the perceptual curve of the human eye. at this time, the graphics obtained are the color results after compression.

The so-called Gamma compression, its essence is this compression function, is to Vout = VinGamma to compress.

Now the industry mentioned Gamma = 2.2, which is a value obtained by the industry after repeated measurements, so that more dark details can be retained in the range of 256grayscale:

The two figures above can basically explain the source of Gamma = 2.2. the perceptual ability of the human eye is close to the power function of n = 1 and 2.2. of course, there are different values in different environments, probably in the range of 1.82.5.

2.4 Linear space

After understanding the nonlinear space-Gamma space, we can naturally understand the linear space, which is the curve n = 1 in figure 2 above. Why mention the linear space? Because our camera's perception of light intensity is based on linear space.

To take a simple example, two photons are projected onto the camera and the light intensity is twice the photon intensity, of course, we already know that the human eye is not twice the light intensity.

While the pictures in the industry are stored in Gamma space, then the camera to the final picture, will go through a coding process, this is the so-called Gamma coding, that is: Vout = Vin (12.2) this process.

III. Gamma compensation

Now that the pictures we take through the camera are eventually stored in gamma space format (known as sRGB by industry standard), do we view the pictures on the monitor as well as the results of sRGB? The answer is no.

The industry has already compressed the original natural light, so the industry must convert the compressed image back through a certain way to get a more natural image. This process is Gamma compensation, also known as Gamma correction.

Now that we know what function is used for compression, the process of decompression is naturally a process of inversion, which can be obtained: Vout = Vin2.2.

This step is automatically performed by industry monitors by default, so the colors we finally submit to the monitor need to meet the corresponding relationship.

A picture is used to show the whole process from sampling to display:

Summary: sampling generation, using Gamma coding, which is the industry standard, display process, using Gamma compensation, which is also the industry standard, select gamma = 2.2.This is the value that the industry tests according to the human eye closer to the human eye perception curve.

4. Using linear space and Gamma space in Unity

In the field of graphics, technology is constantly improving and exploring, and applications have always been deferred updates (for the need for downward compatibility).

Of course, in the past, the existence of gamma space ignored the difference of this part and carried out optical calculation directly based on the storage map of gamma space.

But the optical calculation in the engine (in shader) is based on the formula of linear space, which will make a big difference. The formula we calculate is based on linear space, but the input data is based on the format of gamma space storage and the result of image acquisition.

As the input of the optical calculation formula, the output is naturally wrong. In the past, the game industry has been in the stage of tolerance for this, and it can also be adjusted through art to get relatively indifferent calculation results.

In recent years, the gradual promotion of PBR technology, the calculation of light is more stringent, which promotes the gradual promotion of linear space in the game industry.

4.1 the process of Gamma space

In gamma space, in the process of optical calculation of shader, the result of image sampling is directly brought into the formula for calculation, and the obtained color is stored in colorbuff, and then submitted to the display. After a gamma compensation, the final color is obtained.

4.2 the processing process of linear space

In linear space, for all pictures, the default is that pictures are stored linearly. Therefore, if the original image is the storage mode of sRGB in Gamma space, you need to check the sRGB flag, so that when performing shader calculation, you will first make a gamma compensation.

The color is converted from the gamma space to the linear space, then the correct illumination calculation is carried out, and the result is finally converted back to the gamma space (gamma compression). Finally, it is submitted to the display for a gamma compensation to get the final color.

Use a diagram to show the flow of these two processes (refer directly to the diagram of the article):

In a word, the essence of gamma space and linear space in unity is to store pictures in sRGB format, carry out different lighting calculations, carry out different processes for different lighting calculations, and get different results with different accuracy. finally, they all need to be unified into gamma space.

The format is submitted to the monitor for gamma compensation to get the final display picture.

Thank you for your reading, the above is the content of "what is the difference between gamma space and linear space in unity". After the study of this article, I believe you have a deeper understanding of the difference between gamma space and linear space in unity. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report