greggman / twgl.js

A Tiny WebGL helper Library
http://twgljs.org
MIT License
2.61k stars 258 forks source link

`drawType` in example conflicts with docs and type annotation #190

Open bchirlsbiodigital opened 2 years ago

bchirlsbiodigital commented 2 years ago

Refer to the following code from the dynamic buffers example:

    const arrays = {
      spread: { data: spreadArray, numComponents: 1 },
      height: { data: heightArray, numComponents: 1, drawType: gl.DYNAMIC_DRAW },
    };
    const bufferInfo = twgl.createBufferInfoFromArrays(gl, arrays);

If I'm following the documentation correctly, each array passed to createBufferInfoFromArrays should be an ArraySpec, which does not include the drawType property. It looks like the function does actually use the property, so I think it's the docs/types that are incomplete not the example.

Could you please clarify here or update the example, docs or types to reflect the proper way to set drawType on an attribute?

Thanks!

greggman commented 2 years ago

drawType is optional

https://github.com/greggman/twgl.js/blob/42291da89afb019d1b5e32cd98686aa07cca063d/src/attributes.js#L204

if it's not specified twgl will use STATIC_DRAW

https://github.com/greggman/twgl.js/blob/42291da89afb019d1b5e32cd98686aa07cca063d/src/attributes.js#L90

It's copied to the attribute info you linked to but if it's not set it will still be undefined and so still use STATIC_DRAW.

As for why set it, it's a hint to WebGL that you're going to update the buffer often, 'hint' means it's not required as it's only a hint.