UnityStandardBRDF包含文件包含Unity_GlossyEnvironment函数。它包含所有用于转换粗糙度,对立方体贴图采样以及从HDR转换的代码。
作用:根据粗糙度采样对应mipmap级别的环境贴图
采样环境贴图得到间接光的环境反射的例子(镜面反射):
UnityIndirect CreateIndirectLight(Interpolators i, float3 viewDir)
{
UnityIndirect indirectLight;
indirectLight.diffuse = 0;
indirectLight.specular = 0;
// base pass 才计算环境光
if defined(FORWARD_BASE_PASS)
// 计算从反射出来的方向
// viewDir是片元到摄像机的向量
// i.normal是片元在世界空间下的法线方向
float3 reflectionDir = reflect(-viewDir, i.normal)
// 用于计算的结构体
Unity_GlossyEnvironmentData envData;
envData.roughness = 1 - _Smoothness; // 粗糙度
envData.reflUVW = reflectionDir; // 对CubeMap采样的方向
indirectLight.specular = Unity_GlossyEnvironment(UNITY_PASS_TEXCUBE(unity_SpecCube0), unity_SpecCube0_HDR, envData);
#endif
return indirectLight;
}
UNITY_PASS_TEXCUBE是什么?
源码:
// Macros to declare textures and samplers, possibly separately. For platforms
// that have separate samplers & textures (like DX11), and we'd want to conserve
// the samplers.
// - UNITY_DECLARE_TEX*_NOSAMPLER declares a texture, without a sampler.
// - UNITY_SAMPLE_TEX*_SAMPLER samples a texture, using sampler from another texture.
// That another texture must also be actually used in the current shader, otherwise
// the correct sampler will not be set.
#if defined(SHADER_API_D3D11) || defined(SHADER_API_XBOXONE) || defined(UNITY_COMPILER_HLSLCC) || defined(SHADER_API_PSSL) || (defined(SHADER_TARGET_SURFACE_ANALYSIS) && !defined(SHADER_TARGET_SURFACE_ANALYSIS_MOJOSHADER))
#define UNITY_PASS_TEXCUBE(tex) tex, sampler##tex
#else
// DX9 style HLSL syntax; same object for texture+sampler
#define UNITY_PASS_TEXCUBE(tex) tex
#endif
如果是D3D11:
Unity_GlossyEnvironment(UNITY_PASS_TEXCUBE(unity_SpecCube0), unity_SpecCube0_HDR, envData);
就相当于
Unity_GlossyEnvironment(unity_SpecCube0, samplerunity_SpecCube0, unity_SpecCube0_HDR, envData);
Unity_GlossyEnviroment源码:
half perceptualRoughnessToMipmapLevel(half perceptualRoughness)
{
// 0~1的粗糙度转换成mipmap级别,UNITY_SPECCUBE_LOD_STEPS是mipmap的最大级别
return perceptualRoughness * UNITY_SPECCUBE_LOD_STEPS;
}
half3 Unity_GlossyEnvironment (UNITY_ARGS_TEXCUBE(tex), half4 hdr, Unity_GlossyEnvironmentData glossIn)
{
half perceptualRoughness = glossIn.roughness;
// TODO: CAUTION: remap from Morten may work only with offline convolution, see impact with runtime convolution!
// For now disabled
#if 0
// 涉及如何创建mipmap的详细信息, 注释掉的内容
// 真正的计算三线性过滤mipmap不同级别之间的关系
float m = PerceptualRoughnessToRoughness(perceptualRoughness); // m is the real roughness parameter
const float fEps = 1.192092896e-07F; // smallest such that 1.0+FLT_EPSILON != 1.0 (+1e-4h is NOT good here. is visibly very wrong)
float n = (2.0/max(fEps, m*m))-2.0; // remap to spec power. See eq. 21 in --> https://dl.dropboxusercontent.com/u/55891920/papers/mm_brdf.pdf
n /= 4; // remap from n_dot_h formulatino to n_dot_r. See section "Pre-convolved Cube Maps vs Path Tracers" --> https://s3.amazonaws.com/docs.knaldtech.com/knald/1.0.0/lys_power_drops.html
perceptualRoughness = pow( 2/(n+2), 0.25); // remap back to square root of real roughness (0.25 include both the sqrt root of the conversion and sqrt for going from roughness to perceptualRoughness)
#else
// MM: came up with a surprisingly close approximation to what the #if 0'ed out code above does.
// 得到与上面#if 0差不多效果的代码,修正了粗糙度与mipmap级别之间的关系不是线性的问题,计算量小很多。
perceptualRoughness = perceptualRoughness*(1.7 - 0.7*perceptualRoughness);
#endif
half mip = perceptualRoughnessToMipmapLevel(perceptualRoughness);
half3 R = glossIn.reflUVW;
half4 rgbm = UNITY_SAMPLE_TEXCUBE_LOD(tex, R, mip); // 对cubemap进行采样
return DecodeHDR(rgbm, hdr);
}
UNITY_ARGS_TEXCUBE(tex)是什么?
源码:
// Macros to declare textures and samplers, possibly separately. For platforms
// that have separate samplers & textures (like DX11), and we'd want to conserve
// the samplers.
// - UNITY_DECLARE_TEX*_NOSAMPLER declares a texture, without a sampler.
// - UNITY_SAMPLE_TEX*_SAMPLER samples a texture, using sampler from another texture.
// That another texture must also be actually used in the current shader, otherwise
// the correct sampler will not be set.
#if defined(SHADER_API_D3D11) || defined(SHADER_API_XBOXONE) || defined(UNITY_COMPILER_HLSLCC) || defined(SHADER_API_PSSL) || (defined(SHADER_TARGET_SURFACE_ANALYSIS) && !defined(SHADER_TARGET_SURFACE_ANALYSIS_MOJOSHADER))
#define UNITY_ARGS_TEXCUBE(tex) TextureCube tex, SamplerState sampler##tex
#else
// DX9 style HLSL syntax; same object for texture+sampler
#define UNITY_ARGS_TEXCUBE(tex) samplerCUBE tex
#endif
如果是D3D11:
half3 Unity_GlossyEnvironment (UNITY_ARGS_TEXCUBE(tex), half4 hdr, Unity_GlossyEnvironmentData glossIn)
相当于
half3 Unity_GlossyEnvironment (TextureCube tex, SamplerState samplertex, half4 hdr, Unity_GlossyEnvironmentData glossIn)
其中samplertex是配合UNITY_SAMPLE_TEXCUBE_LOD的
UNITY_SAMPLE_TEXCUBE_LOD是什麽?
源码:
// Macros to declare textures and samplers, possibly separately. For platforms
// that have separate samplers & textures (like DX11), and we'd want to conserve
// the samplers.
// - UNITY_DECLARE_TEX*_NOSAMPLER declares a texture, without a sampler.
// - UNITY_SAMPLE_TEX*_SAMPLER samples a texture, using sampler from another texture.
// That another texture must also be actually used in the current shader, otherwise
// the correct sampler will not be set.
#if defined(SHADER_API_D3D11) || defined(SHADER_API_XBOXONE) || defined(UNITY_COMPILER_HLSLCC) || defined(SHADER_API_PSSL) || (defined(SHADER_TARGET_SURFACE_ANALYSIS) && !defined(SHADER_TARGET_SURFACE_ANALYSIS_MOJOSHADER))
#define UNITY_SAMPLE_TEXCUBE_LOD(tex,coord,lod) tex.SampleLevel (sampler##tex,coord, lod)
#else
// DX9 style HLSL syntax; same object for texture+sampler
#define UNITY_SAMPLE_TEXCUBE_LOD(tex,coord,lod) texCUBElod (tex, half4(coord, lod))
#endif
如果是D3D11:
half4 rgbm = UNITY_SAMPLE_TEXCUBE_LOD(tex, R, mip);
相当于:
half4 rgbm = tex.SampleLevel (samplertex, R, mip);
DecodeHDR是什么样的?
因为立方体贴图包含HDR(高动态范围)颜色,这使其可以包含大于1的亮度值。我们必须将样本从HDR格式转换为RGB。
HDR数据使用RGBM格式存储在四个通道中。因此,我们必须采样一个half4值,然后进行转换。
RGBM包含三个RGB通道,以及一个包含幅度因子的M通道。通过将它们乘以 来计算最终的RGB值。这里,x 是标量,y 是指数,存储在解码指令的前两个部分中。
// Decodes HDR textures
// handles dLDR, RGBM formats
inline half3 DecodeHDR (half4 data, half4 decodeInstructions)
{
// Take into account texture alpha if decodeInstructions.w is true(the alpha value affects the RGB channels)
half alpha = decodeInstructions.w * (data.a - 1.0) + 1.0;
// If Linear mode is not supported we can skip exponent part
#if defined(UNITY_COLORSPACE_GAMMA)
return (decodeInstructions.x * alpha) * data.rgb;
#else
# if defined(UNITY_USE_NATIVE_HDR)
return decodeInstructions.x * data.rgb; // Multiplier for future HDRI relative to absolute conversion.
# else
return (decodeInstructions.x * pow(alpha, decodeInstructions.y)) * data.rgb;
# endif
#endif
}
M通道的转换是必需的,因为当存储在纹理中时,它被限制为0到1范围内的8位值。所以 X 指令将其放大,并且 y指令使它成为非线性的,就像伽玛空间一样。