- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 36.1k
 
math: move to es6 classes #19997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
math: move to es6 classes #19997
Conversation
        
          
                src/math/Vector3.js
              
                Outdated
          
        
      | import { Quaternion } from './Quaternion.js'; | ||
| 
               | 
          ||
| const _vector = new Vector3(); | ||
| let _vector; | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hoisting does not work anymore, because Vector3 is not a function.
Vec3 instance will be assigned at the end of this file.
        
          
                src/math/Vector3.js
              
                Outdated
          
        
      | } ); | ||
| } | ||
| 
               | 
          ||
| _vector = new Vector3(); | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it work to do const _vector = new Vector3(); here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That works! (I tested)
So, other internal use instances should be declared at the bottom of the file too?
Such as: const _quaternion = new Quaternion(); in
Lines 1 to 7 in f549b79
| import { MathUtils } from './MathUtils.js'; | |
| import { Quaternion } from './Quaternion.js'; | |
| let _vector; | |
| const _quaternion = new Quaternion(); | |
| class Vector3 { | 
and
Lines 1 to 11 in f549b79
| import { Vector3 } from './Vector3.js'; | |
| const _v1 = new Vector3(); | |
| let _m1; | |
| const _zero = new Vector3( 0, 0, 0 ); | |
| const _one = new Vector3( 1, 1, 1 ); | |
| const _x = new Vector3(); | |
| const _y = new Vector3(); | |
| const _z = new Vector3(); | |
| class Matrix4 { | 
also maybe
Lines 4 to 9 in f549b79
| import { MathUtils } from './MathUtils.js'; | |
| const _matrix = new Matrix4(); | |
| const _quaternion = new Quaternion(); | |
| class Euler { | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, other internal use instances should be declared at the bottom of the file too?
Yes, I think so!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, done it with 8bcfccd
        
          
                src/math/Euler.js
              
                Outdated
          
        
      | 
               | 
          ||
| this._x = value; | ||
| this._onChangeCallback(); | ||
| static get DefaultOrder() { | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should do a setter for this one too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for reviewing.
Sorry, I got it wrong, I thought DefaultOrder is read-only too.
I will fix it.
Before that, I have a question: How do we declare Public class fields?
The readable-writable static value can be declare like
class MyClass {
  static xxx = 1;
  constructor( x ) {
    this.x = x
  }
}However It seems Buble transpiler does not support the syntax, while Babel support that
Which option would be preferable in three.js (with Buble)?
class MyClass {
  constructor( x ) {
    this.x = x
  }
}
MyClass.xxx = 1;or using Object.defineProperty
class MyClass {
  constructor( x ) {
    this.x = x
  }
}
Object.defineProperty(
  MyClass,
  'xxx',
  {
    value: 1,
    configurable: true,
    writable: true
  }
);or
let _xxx = 1;
class MyClass {
  static get xxx = () => {
    return _xxx;
  }
  static set xxx = ( value ) => {
    _xxx = value;
  }
  constructor( x ) {
    this.x = x
  }
}or other?
Thank you in advance🙇♂️
maybe @DefinitelyMaybe or @ianpurvis could give a suggestion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class MyClass { constructor( x ) { this.x = x } } MyClass.xxx = 1;
I think this style is nice for read/write 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I'll do that then!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For future reference:
There are only three things in the current r120 dev build where Buble is being used.
Curves, Geometries and Materials.
When Buble is no longer needed, i.e. after converting everything to es6 classes, we should have a look back into this
class MyClass {
  static xxx = 1;
  constructor( x ) {
    this.x = x
  }
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nvm. I've got Buble's purpose backwards. Buble is our 'convert classes back to support IE' tool and output it as build/three.js.
| 
           static is a keyword for methods last time I checked. will have a look around. ah. here we go  | 
    
| 
           Fixed   | 
    
| 
           Can you also revert  Euler.RotationOrders = [ 'XYZ', 'YZX', 'ZXY', 'XZY', 'YXZ', 'ZYX' ]; | 
    
| 
           Should we also do   | 
    
| 
           I'll revert  However, I personally think  
 Sould we do   | 
    
| 
           I think we should give the read-only getter a try? That feels like the right spirit for type checking. I'm not a benchmark master, but I ran my cheap benchmark on some different type checking. TLDR, performance is all similar. Vanilla   | 
    
          
 I don't think this is the right time to give things a try. 
 We used to use   | 
    
          
 It doesn't feel good to turn   | 
    
| 
           Fair enough, let's go with   | 
    
| 
           I understand performance is paramount. Also, I checked some in the example folder and working correctly.  | 
    
| 
           Thanks!  | 
    
| 
           Thank you so much for your kind advice and all help🙇♂️  | 
    
| 
           FYI: three.js/test/unit/src/core/Object3D.tests.js Lines 243 to 247 in e9e5d0e 
 The properties of Vector3 can only contain x, y, z, and not isVector3.But isVector3.prototype.isVector3 can pass the test.
I think we should declare   | 
    
| 
           thank you! I was starting to look into why my unit tests were failing and was approaching a similar conclusion. I hadn't figured out how to work around it though. Great stuff! cc @linbingquan  | 
    
A part of #19986
This PR contains:
The rest of the math folder will be continued in separate PR.
The following are already completed in other PRs :