上面这个脚本报错:undeclaredidentifier”_AmbientColor"
时间: 2024-03-23 16:36:57 浏览: 62
经典:扫雷英雄榜——C++常见错误及解决方案.pdf
4星 · 用户满意度95%
抱歉,这是因为在上面修改的代码片段中,_AmbientColor变量也没有定义。你可以尝试在Properties块中添加_AmbientColor属性,然后在Pass块的frag函数中使用UnityGlobalAmbientColor来获取Unity的全局环境光颜色。
以下是修改过的Shader代码:
```shader
Shader "Custom/URPHighLight" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_BumpMap ("Normal Map", 2D) = "bump" {}
_HighLightColor ("HighLight Color", Color) = (1,1,1,1)
_HighLightIntensity ("HighLight Intensity", Range(0,1)) = 0.2
_HighLightSpeed ("HighLight Speed", Range(0,1)) = 0.2
_AmbientColor ("Ambient Color", Color) = (1,1,1,1)
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 100
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata {
float4 vertex : POSITION;
float3 normal : NORMAL;
float2 uv : TEXCOORD0;
};
struct v2f {
float2 uv : TEXCOORD0;
float3 worldNormal : TEXCOORD1;
float3 worldPos : TEXCOORD2;
UNITY_FOG_COORDS(3)
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
sampler2D _BumpMap;
float4 _Color;
float _Glossiness;
float _Metallic;
float4 _HighLightColor;
float _HighLightIntensity;
float _HighLightSpeed;
float4 _AmbientColor;
v2f vert (appdata v) {
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
o.uv = v.uv;
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target {
// calculate lighting
float3 worldNormal = normalize(i.worldNormal);
float3 worldPos = i.worldPos;
float3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz - worldPos);
float3 albedo = tex2D(_MainTex, i.uv).rgb * _Color.rgb;
float3 specular = _HighLightColor.rgb * _HighLightIntensity;
float3 viewDir = normalize(UnityWorldSpaceViewDir(worldPos));
float3 halfDir = normalize(worldLightDir + viewDir);
float nh = max(0, dot(worldNormal, halfDir));
float spec = pow(nh, 100 * _Glossiness);
float4 c = float4(0,0,0,0);
c.rgb += albedo * (nh * _HighLightIntensity + spec * specular);
c.rgb += albedo * UnityGlobalAmbientColor.rgb * _AmbientColor.rgb;
c.a = _Color.a;
return c;
}
ENDCG
}
}
FallBack "Diffuse"
}
```
在修改后的代码中,我们新增了一个_AmbientColor属性,并在frag函数中使用UnityGlobalAmbientColor来获取Unity的全局环境光颜色,并与_AmbientColor属性混合计算。
阅读全文