Three.js: missing default value of stride for InterleavedBuffer class

Created on 28 May 2017  ·  3Comments  ·  Source: mrdoob/three.js

Description of the problem

Here at InterleavedBuffer class, it is suggested to have default value 0 for stride. If user forget to specify stride, this.stride would have value undefined, this would cascade to var stride = data.stride;, and further to _gl.vertexAttribPointer causing undefined webgl behavior.

Three.js version
  • [X] Dev
  • [x] r85
  • [ ] ...
Browser
  • [x] All of them
  • [ ] Chrome
  • [ ] Firefox
  • [ ] Internet Explorer
OS
  • [x] All of them
  • [ ] Windows
  • [ ] macOS
  • [ ] Linux
  • [ ] Android
  • [ ] iOS

Most helpful comment

Setting a default value for an argument means it is OK not to specify a value at all. However, in this case, a value must be set.

Also, if zero were the default, the next line could divide by zero.

three.js generally does not check parameter values.

All 3 comments

Setting a default value for an argument means it is OK not to specify a value at all. However, in this case, a value must be set.

Also, if zero were the default, the next line could divide by zero.

three.js generally does not check parameter values.

  1. Unfortunately, there is no way to forces user to set that value, unlike C++ it will be detected during compile time. If it must be set, why not throw an error in case it's not set instead of silently passes through and returns weird graphics behavior? As a new user of three.js, I wasted quite a few time finding out the issue, and i'm pretty sure I wouldn't be the only person who will fall into this pitfall.

  2. There is no way to prevent the user from assigning 0 to stride, which would cause divide-by-zero problem @WestLangley just mentioned.

  3. The API document here fails to give a clear description of what stride means. Based on my experience with OpenGL/WebGL API, the stride here is the number of primitive elements, float or int, for vertex attribute. So the count seems to be the number of vertices whose attributes are defined in the first argument array.

  4. And, correct me if I'm wrong, the this.count doesn't find any usage through out the entire WebGLRender, while the stride has been used in the critical GL call _gl.vertexAttribPointer. This also explains why after assigning stride 0, which is not exactly correct and this.count is now Infinity, it makes my rendering back to normal, because it has never been used any where in the framework.

So, I would suggest setting up a safe guard to check the value of stride and prompt user in case of undefined or 0.

Yes, this is JavaScript, and we have had these discussions before.

On the other hand, if you feel the documentation is inadequate or incorrect, this would be a great time to help us improve it. :)

In the code you referenced, count is the number of vertices. The property may, or may not, be used by the renderer, but it is a property that is available to the application layer if needed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

clawconduce picture clawconduce  ·  3Comments

boyravikumar picture boyravikumar  ·  3Comments

jack-jun picture jack-jun  ·  3Comments

Bandit picture Bandit  ·  3Comments

zsitro picture zsitro  ·  3Comments