Reputation: 753
Probably a simple question but I'm learning how WebGL shaders work. My understanding is a vec4 should be composed as something like vec4(0.5) or vec4(0.5, 0.5, 0.5, 0.5) or vec4(vec2(0.5), vec2(0.5)) for example, so why does this example seem to work? It contains a vec4 inside a vec2. I'm also using Three.js.
vec4 myVec = vec4(vec2(0.5), vec2(0.5, vec4(0.5)));
But this doesn't work:
vec4 myVec = vec4(vec2(0.5), vec2(vec4(0.5), 0.5));
Upvotes: 0
Views: 603
Reputation: 10165
Excerpt from this page:
Casting a higher-dimensional vector to a lower-dimensional vector is also achieved with these constructors:
vec4 a = vec4(-1.0, 2.5, 4.0, 1.0);
vec3 b = vec3(a); // = vec3(-1.0, 2.5, 4.0)
vec2 c = vec2(b); // = vec2(-1.0, 2.5)
Casting a lower-dimensional vector to a higher-dimensional vector is achieved by supplying these constructors with the correct number of components:
vec2 a = vec2(0.1, 0.2);
vec3 b = vec3(0.0, a); // = vec3(0.0, 0.1, 0.2)
vec4 c = vec4(b, 1.0); // = vec4(0.0, 0.1, 0.2, 1.0)
So basically...
vec2(0.5, vec4(0.5))
works because after the first argument, 0.5, there is still one argument left to insert from the vec4.
vec2(vec4(0.5), 0.5)
doesn't work, because the 2 arguments for the vec2 call are populated from the first 2 elements of the vec4, meaning there are no more arguments to populate, so the last 0.5 has nowhere to go.
The better way to do it would be..
vec2(0.5, myvec4.x)
and vec2(myvec4.x, 0.5)
Upvotes: 2