您的位置:首页 > 移动开发 > Unity3D

UnityColor Space的设置,Gamma还是Linear

2017-05-09 09:41 253 查看
参考文章:
【图形学】我理解的伽马校正(Gamma Correction)
Gamma Correction and Why It Matters
Rendering 3 Combining Textures
GAMMA AND LINEAR SPACE - WHAT THEY ARE AND HOW THEY DIFFER
Linear rendering overview

总结:
1:关于伽马校正Gamma Correction
CRT显示屏时代,其亮度和输入的电压不是线性关系,而是一个幂率(pow-law)关系。这个Gamma值决定了曲线的形状,显示器的这个Gamma就叫做display Gamma。早期CRT的Gamma取值一般是2.5左右。
由于这个原因,一幅原始图像直接显示在CRT显示器上,会显得偏暗,所以拍摄好的图像,在存储到文件时,会进行一个叫encoding Gamma的矫正处理,用来抵消一部分CRT显示器的偏差。
CRT被LCD屏取代后,为了向前兼容,LCD在制作时专门拟合出Display Gamma偏差的效果。
后来微软等公司提出了sRGB标准,推荐的显示器display Gamma值一般取值为2.2,同时推荐encoding Gamma值为0.45(1/2.2)。这样0.45和2.2一抵消为0.45*2.2=1。
此过程中的encoding Gamma处理就是伽马矫正:Gamma Correction。







如图,左边一幅图encoding Gamma时偏亮,右边display Gamma时偏暗:



2:sRGB标准的图片
目前大多数互联网和存储在电脑上的图片都是经过0.45伽马校正的。
注意这里指的是正常的图片,如果是法线贴图、视差图等,是不存在校正问题的。

3:Unity中的设置
在Unity中
Edit -> Project Settings -> Player -> Other Settings

有个Color Space的选项,可以选择Gamma和Linear两个选项。
其含义是Unity是否对输入的默认贴图(注意法线贴图等不存在这个问题)进行转换处理。
选择Gamma时,Unity不会在后台将图片进行转换,输入的即使是经过矫正的图片,Unity也不会处理。
选择Linear时,Unity会利用GPU对图片进行采样,剔除矫正,转换到线性空间,然后才将此图片传递给Shader处理,处理后的图片会再加上Gamma矫正,再显示到屏幕上。



Unity从Gamma空间转换到Linear空间的过程需要GPU支持,目前Android平台要求GL ES 3.0以上,IOS也有一定的API版本要求,所以移动平台上默认是Gamma空间选项。

如果Unity中选择的是Linear空间选项,而一幅图片已知是Linear的,不需要再经过矫正剔除了,在该图片的Import选项中,取消勾选sRGB(Color Texture)选项。



4:Unity中的光照贴图
官网原文:
The lighting calculations in the lightmapper are always done in linear space (see documentation on the Lighting Window for more information). The lightmaps are always stored in gamma space. This means that the lightmap textures are identical no matter
whether you’re in gamma or linear color space.

When you are in linear color space, the texture sample gets converted from gamma to linear space when sampling the texture. When you’re in gamma color space, no conversion is needed. Therefore, when you change the color space setting, you must rebake lightmaps:
This happens automatically when Unity’s lighting is set to auto bake (which is the default).

光照贴图始终是在线性空间生成的,但是其存储时存储在Gamma空间,所以其可以适应Gamma和Linear两种颜色空间。不过当更改了颜色空间设置时,需要重新生成光照贴图。

5:Unity中如何选择这两个选项
如果没有开启Linear Color Space,那么实际上Shader得到的贴图的亮度是有偏差的,即使Shader计算过程没有错,但是显示在屏幕上的亮度也会失真,如果是一个表面不规则的形状,例如球体表面,那么失真很明显,除非自己在Shader中做Gamma矫正。

因此,在做VR开发,PC端开发时,尤其是使用采用PBR方式的两个标准材质球时,最好开启Linear Color Space方式。

6:OpenGL中的贴图
看其中一篇参考资料时,发现OpenGL提供了剔除Gamma矫正的方法,就是在加载贴图时,使用GL_SRGB8选项,这个功能需要OpenGL 3.3以上版本,或者OpenGL ES 3.0以上版本:

From the demo included in this article (some parameters swapped with their actual values for clarity):

1
glTexImage2D(GL_TEXTURE_2D,
0, GL_RGB8, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);


This will load the texture in an uncorrected color space. However, if the data in the texture file is in the sRGB color space, we ought to change the third parameter to 
GL_SRGB8
,
yielding:

1
glTexImage2D(GL_TEXTURE_2D,
0, GL_SRGB8, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);


This will ensure OpenGL corrects the texture data when we look them up.

如有错误,欢迎留言指正,或者QQ283255021
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Unity