我在我的着色器中实现了一个光照贴图,然后我收到了这个错误,我无法弄清楚它的含义。有谁知道如何解决这个问题?它开始发生,因为我添加了光照贴图。
'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"
}
答案 0 :(得分:0)
错误来自行
float3 lightMap = float3(DecodeLightmap(tex2D(unity_Lightmap, v.tex2.xy)),0.0);
这显然是一个错字,因为在这里你试图从2个参数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(非常混乱),尝试重新加载文件并查看它看起来如何。