Search code examples
c#unity-game-engineshaderhlsl

"incorrect number of arguments to numeric-type constructor at line" error in shader, Unity


Found a softbody asset for Unity on github that came with a shader. Due to a limited knowledge of shaders in Unity, I have no idea what the aforementioned error means.

Code (Line 22 is where the error is happening. I've marked it.):

Shader "Custom/Circle" {
    Properties {
        _Color ("Color", Color) = (1, 1, 1, 1)
    }

    SubShader {
        Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
        ZWrite Off
        Blend SrcAlpha OneMinusSrcAlpha

        Pass {
            CGPROGRAM
            #include "UnityCG.cginc"

            #pragma vertex vert_img
            #pragma fragment frag
            #pragma target 3.0

            float4 _Color;

            float4 frag(v2f_img i): COLOR {

                fixed4 transparent = float4(float3(_Color),0); // << ERROR IS HERE >>
                float distance = length(i.uv - float2(0.5, 0.5));
                float delta = fwidth(distance);
                float alpha = smoothstep(0.5, 0.5 - delta, distance);
                return lerp(transparent, _Color, alpha);
            }
            ENDCG
        }
    }
}

Solution

  • Two things wrong in your code:

    1.You are passing one parameter to float3 with float3(_Color).

    2.You are passing two parameters to float4 with float4(float3(_Color),0).

    float3 takes 3 parameters while float4 and fixed4 take 4 parameters. I think that the names says it it.

    You have to pass each individual color component (_Color.r, _Color.g, _Color.b) to the float3 then create float4 with that.

    That line of code should be:

    fixed4 transparent = float4(float3(_Color.r, _Color.g, _Color.b),0);
    

    Notice that _Color.a(alpha) is missing. That's what makes it transparent because the alpha is set to 0.


    I really don't think that float3 is required here so this should really work too:

    fixed4 transparent = float4(_Color.r, _Color.g, _Color.b, 0);
    

    If you need it to be opaque then provide the alpha component too or replace the 0 with 1.

    fixed4 transparent = float4(_Color.r, _Color.g, _Color.b, _Color.a);
    

    Disclaimer:

    I am not a shader programmer but that should solve your problem.