Regression from 5.21.0 to 5.22.0 - Color3 FromHexString is not a constructor

Hello BJS Team,

I noticed an error when going from 5.21 to 5.22 that is still unresolved on 5.25.

I used to import color3 like that (to reduce size of the bundle) :

import { Color3 } from "@babylonjs/core/Maths/math";

When I used it on a library in my project using webpack I have an issue

Error in mounted hook: “TypeError: babylonjs_core_Maths_math__WEBPACK_IMPORTED_MODULE_4_.Color3.FromHexString is not a constructor”

Code raising this issue is :
this.gridMaterial.lineColor = new Color3.FromHexString(this.model.lineColor);
Same code is working well on 5.21.0

Sorry I didn’t reproduce the error.
I have looked very fast in your code last change and I guess it might be from this commit :

If you have any idea on why it happens and how to fix it i would be happy to be using last bjs version as i am now stuck on 5.21.x

Well, that is because it is not a constructor :slight_smile:

We moved to a new js flavor (was es5, now es2021), and finally it is not correct to use new on anything other than a constructor. Remove the new, and everything will work as expected. Color.FromHexString returns a new color as a static method.

2 Likes

I stupidly read Color3.FromHexString is not a “function” instead of not a “constructor”…
It fix my problem, thanks a lot !

2 Likes

heyas, may I know what classes are affected ? It seems like my codes no longer work (breaks back-compat) and its not limited to Color class only. Is there a list anywhere for reference ?

Can you elaborate what code is not working?

Something like this:

let newColor;
	if (typeof color === "number"){
		newColor = new Color3(color, color, color);
	} else if (typeof color === "string"){
		switch(color){
			case "red":
				newColor = new Color3.Red();
				break;
			case "yellow":
				newColor = new Color3.Yellow();
				break;
		}
	}
	if (obj.AI && obj.AI.userCanSelect === true){
		obj.overlayColor = newColor;
		obj.renderOverlay = state;
	}

newColor = new Color3.Yellow(); is throwing errors but newColor = new Color3(color, color, color); isn’t. So, which is correct? ditto for the ton of diffuseColors etc in materials…

those are not constructors. you are using them wrong. Color.Yellow() is a static function, and so is Red or any other one of those.
Remove the “new”.

ic, so just to be clear… Matrix.Identity() doesn’t need a new but if I created an identity matrix manually, it’d need a new Matrix(blahblah)?

a constructor needs new, static functions don’t. So yes, your example is right.

1 Like

Ok, I got it…now I have to go add a whole bunch of todos and chk every module imported AGAIN! T.T