Search code examples
unity-game-enginecg

Shader error : incorrect number of arguments to numeric-type constructor - Unity3D


I was implementing a lightmap into my shader and then I got this error and I can't figure out what it means. Does any one know how I can fix this? It started happening since I added the light map in.

Shader error in 'Custom/HauntedHouseShader': incorrect number of arguments to numeric-type constructor at line 84 (on d3d11)

This is my code:

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"
}

Solution

  • Well the error is from the line

    float3 lightMap = float3(DecodeLightmap(tex2D(unity_Lightmap, v.tex2.xy)),0.0);
    

    which is a clearly a typo, since here you try to construct float3 from 2 arguments float3 and float, which is meaningless. Maybe you meant:

    float3 lightMap = DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, v.tex2.xy));
    

    or

    float3 lightMap = float3(DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, v.tex2.xy)).xy, 0.0);
    

    I assume that what makes the confusion here is that error points to a wrong line number, the reason is that Unity 5 compilation makes some automatic converions of older APIs in your shader source (quite confusing), try reloading the file and see how it actually looks.