我在着色器中实现了一个光照贴图,然后我出现了这个错误,我不知道它意味着什么。有人知道我该怎么解决吗?自从我在中添加了灯光地图后,它就开始发生了
"Custom/HauntedHouseShader"中的着色器错误:第84行(d3d11上)数值类型构造函数的参数数量不正确
这是我的代码:
Shader "Custom/HauntedHouseShader" {
Properties {
_Texture ("Diffuse", 2D) = "white"{}
_Color ("Color", Color) = (0,0,0,1)
_TilingX ("TilingX", float) = 1
_TilingZ ("TilingZ", float) = 1
}
SubShader {
Pass{
Tags { "LightMode" = "ForwardBase"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 3.0
#include "UnityCG.cginc"
float4 _Color;
sampler2D _Texture;
float _TilingX;
float _TilingZ;
sampler2D unity_Lightmap;
float4 unity_LightmapST;
float4 _LightColor0;
struct VertexOutput
{
float4 pos : SV_POSITION;
float4 posWorld : TEXCOORD1;
float4 tex : TEXCOORD0;
float3 normalDir : TEXCOORD2;
float2 tex2 : TEXCOORD3;
};
struct VertexInput
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;
};
struct FragmentOutput
{
float4 color : COLOR;
};
VertexOutput vert (VertexInput i)
{
VertexOutput VOUT;
VOUT.tex = i.texcoord;
VOUT.pos = mul(UNITY_MATRIX_MVP,i.vertex);
VOUT.posWorld = mul(_Object2World,i.vertex);
VOUT.normalDir = normalize( mul(float4(i.normal,0.0),_World2Object).xyz);
VOUT.tex2 = i.texcoord.xy * unity_LightmapST.xy + unity_LightmapST.zw;
return VOUT;
}
FragmentOutput frag(VertexOutput v)
{
FragmentOutput FOUT;
float3 lightDir;
float atten;
if(_WorldSpaceLightPos0.w == 0.0){
atten = 1;
lightDir = normalize(_WorldSpaceLightPos0.xyz);
}else
{
float3 fragmentToLightSource = _WorldSpaceLightPos0.xyz - v.posWorld.xyz;
float dist = length(fragmentToLightSource);
atten = 1.0/dist;
lightDir = normalize(fragmentToLightSource);
}
float3 lightMap = float3(DecodeLightmap(tex2D(unity_Lightmap, v.tex2.xy)),0.0);
float3 normalDir = v.normalDir;
float4 tex = tex2D(_Texture, float4(v.posWorld.x * _TilingX,v.posWorld.z * _TilingZ,0.0,0.0));
float3 diffuseReflection = atten * _LightColor0.xyz * saturate(dot(normalDir, lightDir));
float3 lightFinal = UNITY_LIGHTMODEL_AMBIENT.xyz + diffuseReflection;
float4 col = float4(tex.xyz * lightFinal * _Color + lightMap,0.0);
FOUT.color = col;
return FOUT;
}
ENDCG
}
Pass{
Tags { "LightMode" = "ForwardAdd"}
Blend One One
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 3.0
float4 _Color;
sampler2D _Texture;
float _TilingX;
float _TilingZ;
float4 _LightColor0;
struct VertexOutput
{
float4 pos : SV_POSITION;
float4 posWorld : TEXCOORD1;
float4 tex : TEXCOORD0;
float3 normalDir : TEXCOORD2;
};
struct VertexInput
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;
float4 tangent : TANGENT;
};
struct FragmentOutput
{
float4 color : COLOR;
};
VertexOutput vert (VertexInput i)
{
VertexOutput VOUT;
VOUT.tex = i.texcoord;
VOUT.pos = mul(UNITY_MATRIX_MVP,i.vertex);
VOUT.posWorld = mul(_Object2World,i.vertex);
VOUT.normalDir = normalize( mul(float4(i.normal,0.0),_World2Object).xyz);
return VOUT;
}
FragmentOutput frag(VertexOutput v)
{
FragmentOutput FOUT;
float3 lightDir;
float atten;
if(_WorldSpaceLightPos0.w == 0.0){
atten = 1;
lightDir = normalize(_WorldSpaceLightPos0.xyz);
}else
{
float3 fragmentToLightSource = _WorldSpaceLightPos0.xyz - v.posWorld.xyz;
float dist = length(fragmentToLightSource);
atten = 1.0/dist;
lightDir = normalize(fragmentToLightSource);
}
float3 normalDir = v.normalDir;
float4 tex = tex2D(_Texture, float4(v.posWorld.x * _TilingX,v.posWorld.z * _TilingZ,0.0,0.0));
float3 diffuseReflection = atten * _LightColor0.xyz * saturate(dot(normalDir, lightDir));
float3 lightFinal = UNITY_LIGHTMODEL_AMBIENT.xyz + diffuseReflection;
float4 col = float4(tex.xyz * lightFinal * _Color ,0.0);
FOUT.color = col;
return FOUT;
}
ENDCG
}
}
FallBack "Diffuse"
}
错误来自行
float3 lightMap = float3(DecodeLightmap(tex2D(unity_Lightmap, v.tex2.xy)),0.0);
这显然是一个拼写错误,因为这里你试图从两个参数float3和float构建float3,这是没有意义的。也许你的意思是:
float3 lightMap = DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, v.tex2.xy));
或
float3 lightMap = float3(DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, v.tex2.xy)).xy, 0.0);
我认为造成混乱的原因是错误指向了错误的行号,原因是Unity 5编译会自动转换着色器源中的旧API(相当混乱),请尝试重新加载文件,看看它的实际外观。