Now, i could not understand why the uniform variables were used in this way. The “val” passed was a constant. So, wouldn’t if make more sense with this:
Or, is there any specific reason behind using uniforms in Fragment shaders? I understand that using varying variables from vertex to fragment shader yields interpolation. But, is there any particular reason for using uniforms in Fragment shaders or is it just a way of passing data to the fragment shader?
If you write it as 0.5 , then it’s forever-and-ever going to be 0.5
If you use an uniform, in one drawcall you can set it to 0.5, in the next drawcall you can set it to 0.6
a uniform - constant throughout a drawcall, for all vertices and fragments and primitives
attributes - different values per vertex
varyings - interpolated values between processed vertex attributes.
textures - 2D arrays of constants
internals like gl_InstanceID, gl_PrimitiveID, gl_VertexID, - different and predictable on instance/primitive/vertex
To produce different fragment colors next to each other (from the same triangle, meaning from the same 3 processed vertices), you use the varyings (produced by attributes) to sample into textures, compare/multiply/add with constants.
And to produce different vertices from the same attributes, you use the instanceID/primitiveID/vertexID as a mutating factor, and possibly textures/uniforms as a second factor.