Closed artboard-studio closed 4 years ago
twgl guesses the number of components per element. 4 for anything with 'color' in the name, 2 for 'coord' in the name, 3 for everything else.
So you need to add numComponents: 2
to tell it it's guess of 3 is wrong
const arrays = {
a_position: {
numComponents: 2,
data: [
-0.5, 0.5,
-0.5, -0.5,
0.5, 0.5,
0.5, 0.5,
-0.5, -0.5,
0.5, -0.5
],
},
};
Also you wanted createProgramInfo
not createProgramFromSources
. The later just makes WebGLProgram
, it does not look up uniforms and attributes
const programInfo = twgl.createProgramInfo(gl, [vs, fs]);
I knew this is going to be a silly mistake. Thank you very much for the clarification.
For anyone else who would like to understand this, numComponents is the size of the vec being sent to GPU, twgl by default is looking for vec3 (x, y, z) since I am working in 2D and setting the z to 0.0 inside my vertex shader all the time, I was sending a vec2 (x, y) while twgl was expecting the z too. By setting the size (numComponents) to 2 I am telling TWGL to send in the attrib value as vec2 (or set the z yourself, I don't care).
FYI you can you size:2
as a synonym for numComponents:2
. I prefer numComponents
as size feels like it has too many meanings.
Could there be by any chance more examples for TWGL especially for webGL 2.0 and 2D stuff. Majority of the examples are webGL 1.0 and 3D I am trying to learn, and mostly by comparing examples so it would be very helpful.
Here is the code I am using, but no matter what I do, it fails to draw the shape:
Can anyone tell what I am missing?