【转载】Unity Shader - 在 URP 获取 Ambient(环境光) 颜色

转自 https://blog.csdn.net/linjf520/article/details/120783291

之前在 Unity Built-in 管线中,我们在自定义 shader 中,可以使用一下代码来获取 Ambient 环境光的颜色:

fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.rgb

但是在 Unity URP 中,发现不生效了,虽然 URP 中也有定义这个宏,定义在:#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/UnityInput.hlsl"#define UNITY_LIGHTMODEL_AMBIENT (glstate_lightmodel_ambient * 2)

后来发现在 unity 论坛中有人问同样的问题:Get Ambient Color in custom shader

最终给出这 half3(unity_SHAr.w, unity_SHAg.w, unity_SHAb.w) 来获取 Lighting Setting 中的处理后的 SH 环境光颜色,代码如下:

half4 frag(v2f i) : SV_Target
{
	half3 ambient = half3(unity_SHAr.w, unity_SHAg.w, unity_SHAb.w);
	return half4(ambient, 1);
}

运行效果:

感谢 Bigmoth 分享的其他 更适合的 _GlossyEnvironmentColor.rgb 的方式

posted @ 2024-06-25 08:45  bakabird1998  阅读(40)  评论(0编辑  收藏  举报