Silent bug in buffers

Hello, there is a very unpleasant feature in the behavior of buffers. In the core/Buffers/buffer.ts file, there is a magic offset in the static GetFloatData method of the VertexBuffer class:

const remainder = offset % 4;
if (remainder) {
  offset = Math.max(0, offset - remainder);
}

which breaks picker (scene.pick) without any warnings.

This happens when unaligned data is transmitted. Despite the fact that the drawing is done quite correctly.
If you pass unaligned data with the forceCopy flag, then the Float32Array will fail.
I suggest displaying warnings when transferring unaligned data, but always copy them using Uint8Array objects.

For example as follows:

const offset = data.byteOffset + byteOffset;
const wrongAlign = (offset & 3) !== 0;
if (forceCopy || wrongAlign) {
  if(wrongAlign) console.warn(⁠Float array must be aligned by 4 bytes⁠)
  const result = new Uint8Array(count * Float32Array.BYTES_PER_ELEMENT);
  const source = new Uint8Array(data.buffer, offset, count * Float32Array.BYTES_PER_ELEMENT);
  result.set(source);
  return new Float32Array(result.buffer);
} else {
  return new Float32Array(data.buffer, offset, count);
}
1 Like

Wanna do a PR?