Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Float 2 error

Discussion in 'Shaders' started by awasem, Jul 14, 2017.

  1. awasem

    awasem

    Joined:
    Nov 25, 2015
    Posts:
    3
    Hi, I am getting this error

    "Shader error in 'Custom/WobbleEffectShader_Object': syntax error: unexpected token 'float2' at line 62 (on glcore)
    "
    if anyone could help that would be great, Thanks

    Code (CSharp):
    1. Shader "Custom/WobbleEffectShader_Object"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.  
    7.         _NoiseTex ("Noise Texture", 2D) = "white" {}
    8.  
    9.         _DistortionDamper ("Distortion Damper", Float) = 10
    10.         _DistortionSpreader ("Distortion Spreader", Float) = 100
    11.         _TimeDamper ("Time Damper", Float) = 20
    12.     }
    13.     SubShader
    14.     {
    15.         Tags { "RenderType"="Opaque" }
    16.         LOD 100
    17.  
    18.         Pass
    19.         {
    20.             CGPROGRAM
    21.             #pragma vertex vert
    22.             #pragma fragment frag
    23.             // make fog work
    24.             #pragma multi_compile_fog
    25.            
    26.             #include "UnityCG.cginc"
    27.  
    28.             struct appdata
    29.             {
    30.                 float4 vertex : POSITION;
    31.                 float2 uv : TEXCOORD0;
    32.             };
    33.  
    34.             struct v2f
    35.             {
    36.                 float2 uv : TEXCOORD0;
    37.                 UNITY_FOG_COORDS(1)
    38.                 float4 vertex : SV_POSITION;
    39.             };
    40.  
    41.             sampler2D _MainTex;
    42.             sampler2D _NoiseTex;
    43.  
    44.             float _DistortionDamper;
    45.             float _DistortionSpreader;
    46.             float _TimeDamper;
    47.  
    48.  
    49.             float4 _MainTex_ST;
    50.            
    51.             v2f vert (appdata v)
    52.             {
    53.                 v2f o;
    54.                 o.vertex = UnityObjectToClipPos(v.vertex);
    55.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    56.                 UNITY_TRANSFER_FOG(o,o.vertex);
    57.                 return o;
    58.             }
    59.            
    60.             fixed4 frag (v2f i) : SV_Target
    61.  
    62.             float2 offset = float2(
    63.                     tex2D( _NoiseTex, float2(IN.worldPosition.y / _DistortionSpreader, _Time[1]/_TimeDamper ) ).r ,
    64.                     tex2D( _NoiseTex, float2(_Time[1]/_TimeDamper, IN.worldPosition.x / _DistortionSpreader ) ).r  // Displacement in the Y direction
    65.  
    66.                 );
    67.  
    68.                 offset -= 0.5;
    69.  
    70.             {
    71.                 // sample the texture
    72.                 fixed4 col = tex2D(_MainTex, IN.texcoord + offset / _DistortionDamper i.uv);
    73.  
    74.  
    75.                 // apply fog
    76.                 UNITY_APPLY_FOG(i.fogCoord, col);
    77.                 return col;
    78.             }
    79.             ENDCG
    80.         }
    81.     }
    82. }
    83.  
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Here's a very basic fragment shader function:
    Code (CSharp):
    1. fixed4 frag (v2f i) : SV_Target
    2. {
    3.     return fixed4(1,1,1,1);
    4. }
    Lets break it down quickly:

    This is a function declaration. It describes the output value(s) (fixed4), name of the function (frag), and input arguments(s) (v2f i), and in the case of shader stage functions, the output semantic (SV_Target)

    The open brace is the start of the function body, the code the function actually runs

    This is the function body's expression, ie: the code that runs, and in this case return value

    The close brace is the end of the function body

    Line 62 is between the function declaration and the first open brace, so the compiler is trying to use that as part of the function declaration, but has no idea how to use a float2 here as the only thing between the colon : and open brace { should be an output semantic.

    TLDR; move the open brace on line 70 to line 61.