Search Unity

Parse Error: Syntax Error on Line 50

Discussion in 'Shaders' started by asperatology, Jul 3, 2015.

  1. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    For reference, I was really thinking it may have to do with UTF-8 with or without BOM signatures. But I couldn't see much difference when saving using all text editing tools, such as Visual Studio 2013, Sublime Text 3, Notepad, and Notepad++.

    Here's my Shader code:

    Code (CSharp):
    1. //Goal Outline Shader:
    2. //http://wiki.unity3d.com/index.php?title=Silhouette-Outlined_Diffuse
    3.  
    4. Shader "Custom/Outline" {
    5.     Properties {
    6.         _Color ("Main Color", Color) = (0.5, 0.5, 0.5, 1)
    7.         _OutlineColor ("Outline Color", Color) = (0, 0, 0, 1)
    8.         _Outline ("Outline Width", Range(0.0, 0.05)) = 0.005
    9.         _MainTex ("Base (RGB)", 2D) = "white" {}
    10.     }
    11.  
    12. CGINCLUDE
    13. #include "UnityCG.cginc"
    14.  
    15. struct appdata {
    16.     float4 vertex : POSITION;
    17.     float3 normal : NORMAL;
    18. }
    19.  
    20. struct vertex2float {
    21.     float4 pos : POSITION;
    22.     float4 color : COLOR;
    23. }
    24.  
    25. uniform float _Outline;
    26. uniform float4 _OutlineColor;
    27.  
    28.  
    29.     SubShader {
    30.         Tags { "RenderType"="Opaque" }
    31.         LOD 200
    32.      
    33.         CGPROGRAM
    34.         #pragma surface surf Lambert
    35.  
    36.         sampler2D _MainTex;
    37.  
    38.         struct Input {
    39.             float2 uv_MainTex;
    40.         };
    41.  
    42.         void surf (Input IN, inout SurfaceOutput o) {
    43.             half4 c = tex2D (_MainTex, IN.uv_MainTex);
    44.             o.Albedo = c.rgb;
    45.             o.Alpha = c.a;
    46.         }
    47.         ENDCG
    48.     }
    49.  
    50.     Fallback "Diffuse"   //<---------------   LINE 50
    51. }
    52.  
    Fallback "Diffuse" is on line 50, which is what the editor is telling me in the Inspector.



    But I never get any error messages in the Console. So, anyone knows what I need to do?

    I do know the error message really needs to be more verbose and obvious than just a "syntax error".
     
  2. Dolkar

    Dolkar

    Joined:
    Jun 8, 2013
    Posts:
    576
    You're missing an ENDCG for the CGINCLUDE block
     
  3. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Oh wow, I didn't see that.

    I wished it was telling me about this instead of a vague "syntax error" message. Otherwise, I couldn't even tell without help.

    Thank you!
     
  4. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    I just did a small Unity update. Apparently, I didn't update correctly. Now, I'm at the latest stable version.

    And now the Shader code is telling me I have a syntax error on line 16.

    Code (CSharp):
    1. //Goal Outline Shader:
    2. //http://wiki.unity3d.com/index.php?title=Silhouette-Outlined_Diffuse
    3.  
    4. Shader "Custom/Outline" {
    5.     Properties {
    6.         _Color ("Main Color", Color) = (0.5, 0.5, 0.5, 1)
    7.         _OutlineColor ("Outline Color", Color) = (0, 0, 0, 1)
    8.         _Outline ("Outline Width", Range(0.0, 0.05)) = 0.005
    9.         _MainTex ("Base (RGB)", 2D) = "white" {}
    10.     }
    11.  
    12. CGINCLUDE
    13. #include "UnityCG.cginc"
    14. ENDCG
    15.  
    16. struct appdata {    //<------------------------------- LINE 16, ERROR
    17.     float4 vertex : POSITION;
    18.     float3 normal : NORMAL;
    19. }
    20.  
    21. struct vertex2float {
    22.     float4 pos : POSITION;
    23.     float4 color : COLOR;
    24. }
    25.  
    26. uniform float _Outline;
    27. uniform float4 _OutlineColor;
    28.  
    29.  
    30.     SubShader {
    31.         Tags { "RenderType"="Opaque" }
    32.         LOD 200
    33.        
    34.         CGPROGRAM
    35.         #pragma surface surf Lambert
    36.  
    37.         sampler2D _MainTex;
    38.  
    39.         struct Input {
    40.             float2 uv_MainTex;
    41.         };
    42.  
    43.         void surf (Input IN, inout SurfaceOutput o) {
    44.             half4 c = tex2D (_MainTex, IN.uv_MainTex);
    45.             o.Albedo = c.rgb;
    46.             o.Alpha = c.a;
    47.         }
    48.         ENDCG
    49.     }
    50.  
    51.     Fallback "Diffuse"
    52. }
    53.  
    From the post above me, all I did was I added the ENDCG after CGINCLUDE. Doesn't seem to be the case.

    @Dolkar Would you know anything else that is wrong?
     
  5. Dolkar

    Dolkar

    Joined:
    Jun 8, 2013
    Posts:
    576
    The ENDCG should be at the end of the HLSL shader code, so before the SubShader keyword.

    You see, Unity's shaders basically have a language within a language. The first is Unity's ShaderLab, which allows you to set stuff like properties, tags, multiple passes, fallbacks, etc... The second is HLSL, the actual shader in a conventional sense, which is the actual code the graphics card can run. That's where you define your functions, structs and logic. It needs to be separated from the ShaderLab code by CGPROGRAM/CGINCLUDE and ENDCG keywords, otherwise you get errors because either the ShaderLab compiler is trying to read HLSL code, or the HLSL compiler attempts to compile ShaderLab keywords.
     
  6. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    @Dolkar

    I placed ENDCG just before the SubShader keyword, and now it's giving two different errors:



    And here's the updated ShaderLab codes:

    Code (CSharp):
    1. //Goal Outline Shader:
    2. //http://wiki.unity3d.com/index.php?title=Silhouette-Outlined_Diffuse
    3.  
    4. Shader "Custom/Outline" {
    5.     Properties {
    6.         _Color ("Main Color", Color) = (0.5, 0.5, 0.5, 1)
    7.         _OutlineColor ("Outline Color", Color) = (0, 0, 0, 1)
    8.         _Outline ("Outline Width", Range(0.0, 0.05)) = 0.005
    9.         _MainTex ("Base (RGB)", 2D) = "white" {}
    10.     }
    11.  
    12.     CGINCLUDE
    13.     #include "UnityCG.cginc"
    14.  
    15.     struct appdata {
    16.         float4 vertex : POSITION;
    17.         float3 normal : NORMAL;
    18.     }
    19.  
    20.     struct vertex2float {
    21.         float4 pos : POSITION;   //<-----------------    ERROR :  LINE 21
    22.         float4 color : COLOR;
    23.     }
    24.                                //<--------------------   ERROR : LINE 24.
    25.     uniform float _Outline;
    26.     uniform float4 _OutlineColor;
    27.     ENDCG           //<-----------------------   ENDCG Location
    28.  
    29.     SubShader {
    30.         Tags { "RenderType"="Opaque" }
    31.         LOD 200
    32.    
    33.         CGPROGRAM
    34.         #pragma surface surf Lambert
    35.  
    36.         sampler2D _MainTex;
    37.  
    38.         struct Input {
    39.             float2 uv_MainTex;
    40.         };
    41.  
    42.         void surf (Input IN, inout SurfaceOutput o) {
    43.             half4 c = tex2D (_MainTex, IN.uv_MainTex);
    44.             o.Albedo = c.rgb;
    45.             o.Alpha = c.a;
    46.         }
    47.         ENDCG
    48.     }
    49.     Fallback "Diffuse"
    50. }
     
  7. Dolkar

    Dolkar

    Joined:
    Jun 8, 2013
    Posts:
    576
    The definitions of structs need to end with a semicolon, like so:
    Code (CSharp):
    1.     struct appdata {
    2.         float4 vertex : POSITION;
    3.         float3 normal : NORMAL;
    4.     };
    I completely agree with you that the error messages are largely unhelpful... Perhaps this could be considered a bug, because even the hlsl compiler at least elaborates on the syntax error and gives "unexpected token 'struct'"