Search Unity

How to use uniforms on Unity shaders?

Discussion in 'Shaders' started by Alco6, Jul 13, 2020.

  1. Alco6

    Alco6

    Joined:
    Oct 19, 2018
    Posts:
    13
    Hi there,
    I am quite new on Shaders.

    I would like to know how to declare uniforms.
    I am following the famous Bookofshaders. Where we have the following code:
    Code (CSharp):
    1. #ifdef GL_ES
    2. precision mediump float;
    3. #endif
    4.  
    5. uniform vec2 u_resolution;
    6. uniform float u_time;
    7.  
    8. vec3 colorA = vec3(0.149,0.141,0.912);
    9. vec3 colorB = vec3(1.000,0.833,0.224);
    10.  
    11. void main() {
    12.     vec3 color = vec3(0.0);
    13.  
    14.     float pct = abs(sin(u_time));
    15.  
    16.     // Mix uses pct (a value from 0-1) to
    17.     // mix the two colors
    18.     color = mix(colorA, colorB, pct);
    19.  
    20.     gl_FragColor = vec4(color,1.0);
    21. }

    However when I try to do the same in Unity I get an error coming from the declaration of the uniform:

    Code (CSharp):
    1.  
    2. Shader "ColorShader00" { // defines the name of the shader
    3.    SubShader { // Unity chooses the subshader that fits the GPU best
    4.       Pass { // some shaders require multiple passes
    5.          GLSLPROGRAM // here begins the part in Unity's GLSL
    6.  
    7.          #ifdef VERTEX // here begins the vertex shader
    8.          
    9.  
    10.          void main() // all vertex shaders define a main() function
    11.          {
    12.                 gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    13.                
    14.          }
    15.  
    16.          #endif // here ends the definition of the vertex shader
    17.  
    18.  
    19.          #ifdef FRAGMENT // here begins the fragment shader
    20.          uniform vec4 _SinTime
    21.  
    22.          void main() // all fragment shaders define a main() function
    23.          {
    24.        
    25.             vec3 colorA = vec3(0.149,0.141,0.912);
    26.             vec3 colorB = vec3(1.000,0.833,0.224);
    27.  
    28.             float pct = abs(_SinTime.y));
    29.  
    30.             // Mix uses pct (a value from 0-1) to
    31.             // mix the two colors
    32.             color = mix(colorA, colorB, pct);
    33.  
    34.             gl_FragColor = vec4(color, 1.0);
    35.                
    36.          }
    37.  
    38.          #endif // here ends the definition of the fragment shader
    39.  
    40.          ENDGLSL // here ends the part in GLSL
    41.       }
    42.    }
    43. }
    44.  

    What am I doing wrong?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    You're missing a semi-colon at the end of that line...
     
  3. Alco6

    Alco6

    Joined:
    Oct 19, 2018
    Posts:
    13
    Thats true, my bad. Thanks!
    Anyway the main problem is not there.
    I want it to change color over time but it remains cyan. all the time.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    If that's not working, I honestly don't know. I don't ever write GLSL shaders for Unity. The only guess I have is I usually see the uniform defined outside of the #ifdef blocks.