Search Unity

Shader unexpected token v2f error

Discussion in 'Shaders' started by TareqProjects, Feb 16, 2017.

  1. TareqProjects

    TareqProjects

    Joined:
    Jun 27, 2016
    Posts:
    53
    Hello all,

    I am learning how to do shader programming, and using this video as a base:

    I am completely new to this, so apologies if I made a newbie mistake.

    I got to the bit where I implemented the vertex and fragment functions. In the video, he is able to have an object with a texture and a colour overlayed. I, on the other hand, am getting an error;
    syntax error: unexpected token 'v2f'
    Compiling Vertex program
    Platform defines: UNITY_ENABLE_REFLECTION_BUFFERS UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BOX_PROJECTION UNITY_SPECCUBE_BLENDING SHADER_API_DESKTOP

    I have made sure all varibles are written correctly, even went as far as making a new shader in Notepad++ and saving it but my shader does not compile.

    My code (there's a bunch of comments):
    Code (CSharp):
    1. Shader "MyShader/Test2"{
    2.     // Unity uses ShaderLab, consider this the frontend
    3.     // CG can be considered the backend
    4.     // Properties are like public variables
    5.     // This is just a frontend, we're not applying them to anything
    6.     Properties{
    7.         _MainTexture("Main Color (RGB) Test2!", 2D) = "white"{} // Internal reference name, public name (inspector), data type, initialising value (can be nothing)
    8.         _Color("Colour", Color) = (1,1,1,1)
    9.     }
    10.    
    11.     // Can have multiple SubShader to target different platforms
    12.     SubShader{
    13.        
    14.         // Pass takes data and draws them on to screen, can have multiple passes; each can do something that will interate how the object is rendered onto screen
    15.         // Each pass is a draw call, so 3 pass means 3 draw calls
    16.         Pass{
    17.  
    18.             // The main thing that applies shading to objects
    19.             CGPROGRAM
    20.                 // Need two functions; the Vertex and Fragment
    21.                 // Similar to C/C++, we tell CGPROGRAM function identifiers
    22.                 #pragma vertex vertexFunction
    23.                 #pragma fragment fragmentFunction
    24.                
    25.                 //Can also include external files to use different functions
    26.                 #include "UnityCG.cginc"
    27.                
    28.                 // first, We need to get data from the object to use
    29.                 struct appdata {
    30.                     // We pass in the values we want, such as verticies, normals, colour, uv
    31.                     float4 vertex : SV_POSITION; // data type to store, variable name, the type of data i'm getting
    32.                     float2 uv : TEXCOORD0;
    33.                 };
    34.                
    35.                 // We need an object from vertex to pass onto fragment, so we store this in a struct
    36.                 struct v2f {
    37.                     float4 position : SV_POSITION; // SV_POSITION has to do something with working with DX platforms
    38.                     float2 uv : TEXCOORD0;
    39.                 };
    40.  
    41.                 // Take the stuff from ShaderLab and import into CG
    42.                 float4 _Color;
    43.                 sampler2D _MainTexture
    44.  
    45.                 //Vertex
    46.                 //Build the object
    47.                 // Return type, function name, parameters (variable, input/output?)
    48.                 v2f vertexFunction (appdata IN) {
    49.                     v2f OUT;
    50.                     OUT.position = mul(UNITY_MATRIX_MVP, IN.vertex); // multiply (nvidia cg function)
    51.                     // MVP - Model View Projection - Get the model, get the view from camera, get the projection from the camera; send this to screen
    52.                     // Takes into account whether the camera is in perspective or orthographic
    53.                    
    54.                     OUT.uv = IN.uv;
    55.                     return OUT;
    56.                 }
    57.                    
    58.                 //Fragment
    59.                 //Colour it in
    60.                 fixed4 fragmentFunction (v2f IN) : SV_Target{
    61.                     // Take the colour
    62.                     float textureColor = tex2D(_MainTexture, IN.uv);
    63.  
    64.                     // Take the texture
    65.  
    66.                     return textureColor * _Color;
    67.                 }
    68.             ENDCG
    69.         }
    70.     }
    71.  
    72. }
    I understand that the compiler is basically saying that I am using something that is not recognised, however, I have defined "v2f" as a struct, so I am confused what I am doing wrong.

    Many thanks
     
  2. TareqProjects

    TareqProjects

    Joined:
    Jun 27, 2016
    Posts:
    53
    Nevermind, turns out I missed a semi-colon at line 43. Typical :oops:

    However only seems to change the colour and the texture doesn't seem to apply. Trying to apply the texture on to a Cube.
     
  3. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    262
    Line 43 should end with a semi-colon.
    Code (CSharp):
    1. sampler2D _MainTexture;
     
  4. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    262
    You got it :)

    Line 62 should also be a fixed4.
     
  5. TareqProjects

    TareqProjects

    Joined:
    Jun 27, 2016
    Posts:
    53

    Thanks for replying, I tried changing it to a fixed4 and float4 (the guy had it set to a float4 before). After saving the changes, I removed the material and then reapplied it with the shader, but I am still getting a solid colour.
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    6,994
    fixed vs half vs float is irrelevant on desktop platforms, they're literally the same thing because desktop GPUs always do everything as if they're floats regardless of the precision level requested. It does matter for mobile, but in this case either fixed or float precision should work. fixed4 exists primarily to handle color information.

    However are you sure you're looking at line 62? You're using a singular float instead of a float4 or fixed4.

    float textureColor = tex2D(_MainTexture, IN.uv);
    ^^^^^
    fixed4 textureColor = tex2D(_MainTexture, IN.uv);
     
  7. TareqProjects

    TareqProjects

    Joined:
    Jun 27, 2016
    Posts:
    53
    Yes, grizzly pointed that particular line out and I changed it to float4. What happens is that I get a solid colour but no texture, even though the sphere-preview shows the material having a texture, albeit distorted.
     
  8. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    6,994
    The only other issue I see is the appdata vertex variable should be POSITION and not SV_POSITION, the later should only be used for the output of the vertex shader function. However that shouldn't cause what you're seeing, only some compiler errors on specific platforms. Can you post your updated shader?
     
  9. TareqProjects

    TareqProjects

    Joined:
    Jun 27, 2016
    Posts:
    53
    Sure thing, only thing that has been changed is line 62; changed from float to float4

    Code (CSharp):
    1. Shader "MyShader/Test2"{
    2.     // Unity uses ShaderLab, consider this the frontend
    3.     // CG can be considered the backend
    4.     // Properties are like public variables
    5.     // This is just a frontend, we're not applying them to anything
    6.     Properties{
    7.         _MainTexture("Main Color (RGB) Test2!", 2D) = "white"{} // Internal reference name, public name (inspector), data type, initialising value (can be nothing)
    8.         _Color("Colour", Color) = (1,1,1,1)
    9.     }
    10.    
    11.     // Can have multiple SubShader to target different platforms
    12.     SubShader{
    13.        
    14.         // Pass takes data and draws them on to screen, can have multiple passes; each can do something that will interate how the object is rendered onto screen
    15.         // Each pass is a draw call, so 3 pass means 3 draw calls
    16.         Pass{
    17.  
    18.             // The main thing that applies shading to objects
    19.             CGPROGRAM
    20.                 // Need two functions; the Vertex and Fragment
    21.                 // Similar to C/C++, we tell CGPROGRAM function identifiers
    22.                 #pragma vertex vertexFunction
    23.                 #pragma fragment fragmentFunction
    24.                
    25.                 //Can also include external files to use different functions
    26.                 #include "UnityCG.cginc"
    27.                
    28.                 // first, We need to get data from the object to use
    29.                 struct appdata {
    30.                     // We pass in the values we want, such as verticies, normals, colour, uv
    31.                     float4 vertex : SV_POSITION; // data type to store, variable name, the type of data i'm getting
    32.                     float2 uv : TEXCOORD0;
    33.                 };
    34.                
    35.                 // We need an object from vertex to pass onto fragment, so we store this in a struct
    36.                 struct v2f {
    37.                     float4 position : SV_POSITION; // SV_POSITION has to do something with working with DX platforms
    38.                     float2 uv : TEXCOORD0;
    39.                 };
    40.  
    41.                 // Take the stuff from ShaderLab and import into CG
    42.                 float4 _Color;
    43.                 sampler2D _MainTexture;
    44.  
    45.                 //Vertex
    46.                 //Build the object
    47.                 // Return type, function name, parameters (variable, input/output?)
    48.                 v2f vertexFunction (appdata IN) {
    49.                     v2f OUT;
    50.                     OUT.position = mul(UNITY_MATRIX_MVP, IN.vertex); // multiply (nvidia cg function)
    51.                     // MVP - Model View Projection - Get the model, get the view from camera, get the projection from the camera; send this to screen
    52.                     // Takes into account whether the camera is in perspective or orthographic
    53.                    
    54.                     OUT.uv = IN.uv;
    55.                     return OUT;
    56.                 }
    57.                    
    58.                 //Fragment
    59.                 //Colour it in
    60.                 fixed4 fragmentFunction (v2f IN) : SV_Target{
    61.                     // Take the colour
    62.                     float4 textureColor = tex2D(_MainTexture, IN.uv);
    63.  
    64.                     // Take the texture
    65.  
    66.                     return textureColor * _Color;
    67.                 }
    68.             ENDCG
    69.         }
    70.     }
    71.  
    72. }
     
  10. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    262
    Yes, I had originally noticed this and believed the same but having tested the revised shader it would seem Unity may or may not warn of this issue. Try removing the SV_ from the input struct, recompile, then add it back. Unity will warn the first time then completely miss it thereafter.
    The inclusion of the SV_ in appdata is causing your UV's to become corrupt. Remove the SV_ prefix and you should be good to go:
    Code (CSharp):
    1. struct appdata {  
    2.     float4 vertex : POSITION;
    3.     float2 uv : TEXCOORD0;
    4. };
     
    TareqProjects likes this.
  11. TareqProjects

    TareqProjects

    Joined:
    Jun 27, 2016
    Posts:
    53
    That fixed it, I am guessing I was missing some package or the current version of Unity I'm using is doing something different. Thank you grizzly and bgolus