Search code examples
unity-game-engineshader

incorrect number of arguments to numeric-type constructor


So I've been following this tutorial to make a portal shader and I understand by looking elsewhere that I'm apparently missing the expected number of arguments. Error locations indicated"

        fixed4 frag (v2f i) : SV_Target
        {
            fixed2 uvs = fixed2(i.uv.x, i.uv.y + (_Time.y * _Speed)); <ERROR 1>
            fixed4 col = tex2D(_MainTex, i.uv);

            UNITY_APPLY_FOG(i.fogCoord, col);
            return fixed4(col.rgb * _Color.rgb, col.a * i.uv.y * _Intensity); <ERROR 2>
        }

At line 58 (first error) there are only two arguments requested by the fixed2 call. It's only calling for two things, which I would expect would be fulfilled by i.uv.x and i.uv.y, and that the addition shouldn't affect the number of arguments..

At line 62 (second error), from what I can tell it's returning the arguments R,G,B (multiplied by user input in the _Color variable), and A (multiplied by the Intensity variable), so that should be working, too, right?

I should note that the second error is only thrown in the absence of line 58, and that these errors are thrown even if I change them to fixed1, fixed2, fixed3, or fixed4, just as experiments.

Seeing as I've reproduced his code exactly, I can't begin to know where to look that would be causing this error.

The whole code is as follows (I've commented out the problem lines, and it works fine without them, just without the desired functionality):

Shader "Unlit/Shader_PortalVortex"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _Color ("Color", Color) = (1, 1, 1, 1)
        _Intensity ("Intensity", Range(0, 1)) = 1
        _Speed ("Speed", Range(0, 1)) = 0.5
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" "Queue"="Transparent+2"}
        ZTest Greater
        Blend SrcAlpha OneMinusSrcAlpha
        Cull Front
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            float4 _Color;
            float4 _Intensity;
            float4 _Speed;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                //fixed2 uvs = fixed2(i.uv.x, i.uv.y + (_Time.y * _Speed));
                fixed4 col = tex2D(_MainTex, i.uv);

                UNITY_APPLY_FOG(i.fogCoord, col);
                //return fixed4(col.rgb * _Color.rgb, col.a * i.uv.y * _Intensity);
            }
            ENDCG
        }
    }
}
  

Thanks in advance.


Solution

  • Could be happening because of trying to do calculations of mismatching types.

    Most likely it’s because you’re multiplying ‘_Time.y’ by ‘float4 _Speed’.

    float4 _Intensity; 
    float4 _Speed; 
    
    // vs
    
    // Incorrect arguments because 
    // you’re implicitly calling: ‘fixed2(x, y + (a * (p, q, r, s));’ // error
    fixed2 uvs = fixed2(i.uv.x, i.uv.y + (_Time.y * _Speed));
    

    Same thing for line 62 and Intensity

    1. Cast: Try casting to native HLSL types such as ‘float2’. They are more precise than ‘fixed’, which only usually takes 11 bits or make Speed and Intensity float

    2. Debugging: Try removing ’Time’ and ‘Speed’ variables one by one and running each time to see if one of them makes the error or both.

    HLSL in Unity reference