Several small issues with Color3 and Color4

I am working on an application that heavily uses thin instances, so I am filling a lot buffers. I found 2 issues while doing this:

  1. Color3 and Color4.asArray are slow:
    I tried filling a buffer using Color4.asArray and found out it is quite slow. Looking at the implementation, it creates first an array and then uses its internal toArray method to fill the array. I havent looked in detail why they are so slow, but I expect that writing to an array with zero elements, i.e. writing beyond the array end is slow.



My suggestion is to replace the implementation with a simple
return [this.r, this.g, this.b] and return [this.r, this.g, this.b, this.a]

  1. Color3.toArray and Color.toArray accept different kinds of arrays, making the Color4 variant unusable for my usecase.

Color3 accepts a FloatArray:

Color4 only accepts a plain array:

For my usecase of filling a Float32Buffer, I need the Color4-variant also accept a FloatArray. I think it is OK to just make both accept both types of array if there is a usecase for the plain array, otherwise, I vote for making the Color4 variant accept only a FloatArray.

Bonus suggestion:
There was a breaking change in Babylon 5: The behaviour of Color4.fromHexString was changed so that the alpha defaults to 1 if a hex string with only rgb is passed:

Lets add this behaviour in the description.

I am willing to open Bug reports and prepare pull requests if I get some positive feedback here. But I also do not mind if anyone takes over.

Best regards


1 Like

Please feel free to create the PR for it :slight_smile: It is amazing when the community is contributing :slight_smile:

1 Like

Did it: Fix Color3.asArray, Color4.asArray, Color4.toArray and docs for Color… by axeljaeger · Pull Request #12346 · BabylonJS/Babylon.js · GitHub