我正在使用 ARCore 和 Unity 开发一个 AR 应用程序,我正在尝试实现一个自定义着色器,以应用基于 ARCore 环境 HDR 光估计的实时光照效果。着色器应该根据 ARCore 的光照估计数据来调整虚拟对象的光照。
但是,我遇到了闪烁问题,虚拟对象上的照明在明亮和黑暗之间快速变化。
这是着色器代码的简化版本:
`Shader "Custom/EnvironmentLight"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
// Here's where I'm trying to apply the light estimation data
col *= _GlobalLightEstimation; // _GlobalLightEstimation is a global variable that I'm updating in a script
return col;
}
ENDCG
}
}
}
在一个单独的脚本中,我根据 ARCore 的光估计数据更新 _GlobalLightEstimation:
`void Update()
{
// Get the light estimation data from ARCore
Frame.LightEstimate.CalculateEnvironmentalHdrMainLight(out DirectionalLight, out AmbientSphericalHarmonicsL2);
Shader.SetGlobalFloat("_GlobalLightEstimation", AmbientSphericalHarmonicsL2[0, 0]);
}
我已确认来自 ARCore 的光估计数据已正确更新,并且着色器已应用于我的虚拟对象。然而,闪烁问题仍然存在。关于可能导致此问题的原因以及如何修复它有什么想法吗?