Vector2/3/4 normalize divide by zero

Description of the problem

As a result of #10849 and 381b95a, the normalize method on vector objects no longer checks against divide-by-zero. I understand there was some discussion about whether the check for zero/nan/etc. should happen in divideScalar, and I can understand why you guys decided to leave it out.

But I think it makes less sense to remove the check for normalize, since the divide operation is not directly exposed in that case. Although a normalized zero-length vector is, as far as I can tell, undefined, it is a case that can happen quite often. And this is a pretty significant API change that will require going through a lot of code to manually add back in that check, and it’s so far undocumented. It’s also something that may not show up immediately in quick tests, since it’s a bit of a special case.

I suggest adding back in a divide-by-zero check to normalize, but leaving it out of divideScalar. If you like, you can add a console warning that the check may be removed in the future. And I think it’s significant enough to warrant a hotfix to r85.

At the very least, please update the documentation and release notes for r85.


Three.js version
  • Dev
  • r85
  • All of them
  • Chrome
  • Firefox
  • Internet Explorer
  • All of them
  • Windows
  • macOS
  • Linux
  • Android
  • iOS

Author: Fantashit

2 thoughts on “Vector2/3/4 normalize divide by zero

  1. I am not a fan of returning incorrect answers without warning, but if you feel it is a reasonable trade-off in this case, one option to handling the zero-vector case is:

    normalize: function () {
    	return this.divideScalar( this.length() || 1 );

Comments are closed.