Instanced buffer with stride 1

Heya, there’s a little issue with the Mesh class’s instanced buffer that I stumbled onto. When the stride is 1 and a float is used to set the (per-instance) value, a runtime error occurs because the function _processInstancedBuffers assumes that the value is an object containing either function toArray or copyToArray. Here’s a little playground showing the issue.

A simple workaround is to pass a Vector2 object instead of a float, as shown in this playground. Alternatively a simple object can be created that has the required function, but this isn’t any easier IMO.

A simple solution is to add a check for a single float value, for example like this:

if (Number.isFinite(value)) {
    data[offset] = value;
} else if (value.toArray) {
    value.toArray(data, offset);
} else {
    value.copyToArray(data, offset);
}

And here’s a playground that implements this solution by overriding _processInstancedBuffers.

I’m happy to make a little PR for this, just lemme know if the solution is a go or if there’s a better way to handle it. :+1: :slightly_smiling_face:

Sounds all good to me but maybe you could change the check to:

            if (value.toArray) {
                value.toArray(data, offset);
            } else if (value.copyToArray) {
                value.copyToArray(data, offset);
            } else {
                 data[offset] = value;
            }

I ll merge the PR as soon as it has been made :slight_smile:

1 Like

Done :+1:, here’s the PR: https://github.com/BabylonJSaBabylon.js/pull/11076

1 Like

Thanks a lot this will be in the next nightly in a couple hours

1 Like