包含此页的版本:
不含此页的版本:
上面的示例不考虑任何环境照明或光探头。让我们解决这个问题!
事实证明,我们只需添加一行代码即可做到这一点。环境和光探针光探针存储有关光线如何穿过场景中的空间的信息。在给定空间中排列的光源探针集合可以改善移动对象的光照和该空间内的静态LOD场景。更多信息
请参阅术语表数据传递给着色器在 GPU 上运行的程序。更多信息
请参阅术语表在球形谐波形式中,UnityCG.cginc 包含文件中的 ShadeSH9 函数会在给定世界空间法线的情况下完成对其进行评估的所有工作。
Shader "Lit/Diffuse With Ambient"
{
Properties
{
[NoScaleOffset] _MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Pass
{
Tags {"LightMode"="ForwardBase"}
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "UnityLightingCommon.cginc"
struct v2f
{
float2 uv : TEXCOORD0;
fixed4 diff : COLOR0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata_base v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.texcoord;
half3 worldNormal = UnityObjectToWorldNormal(v.normal);
half nl = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz));
o.diff = nl * _LightColor0;
// the only difference from previous shader:
// in addition to the diffuse lighting from the main light,
// add illumination from ambient or light probes
// ShadeSH9 function from UnityCG.cginc evaluates it,
// using world space normal
o.diff.rgb += ShadeSH9(half4(worldNormal,1));
return o;
}
sampler2D _MainTex;
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
col *= i.diff;
return col;
}
ENDHLSL
}
}
}
事实上,这个着色器开始看起来与内置的 Legacy Diffuse 着色器非常相似!