geckosio / typed-array-buffer-schema

A Schema based Object to Buffer converter
BSD 3-Clause "New" or "Revised" License
105 stars 9 forks source link

Compression example from snapshot-interpolation giving odd results #10

Closed RobinVanCauter closed 3 years ago

RobinVanCauter commented 3 years ago

Describe the bug I'm trying to use the following schema and while the resulting object structure is correct, the values inside positions/velocities (which are nested inside state) are incorrectly de-serialized and lost. I would expect this to work, as you use this nested state structure inside the compression example over at https://github.com/geckosio/snapshot-interpolation#compression

const vector3Schema = BufferSchema.schema('vector3', {
  id: uint8,
  x: { type: float32 },
  y: { type: float32 },
  z: { type: float32 },
});

const snapshotSchema = BufferSchema.schema('snapshot', {
  id: { type: string8, length: 6 },
  state: { positions: [vector3Schema], velocities: [vector3Schema] },
  time: uint64,
});

export const snapshotModel = new Model(snapshotSchema);

const packet = snapshotModel.toBuffer(snapshot);
const deserialized = snapshotModel.fromBuffer(packet); // Returns incorrect x, y, z values

The following schema does not have the same problem ;

const snapshotSchema = BufferSchema.schema('snapshot', {
  id: { type: string8, length: 6 },
  state: [vector3Schema],
  time: uint64,
});
RobinVanCauter commented 3 years ago

I've found the issue is not with the nesting, but rather with me re-using the Vector3 schema for multiple arrays of values.

I suspect the schemaid is used as an anchor inside the ArrayBuffer, thus causing the values of my positions to be read from the velocity index instead.

Tested it with 2 distinct schemas instead and that worked without issues!

const positionSchema = BufferSchema.schema('position', {
  id: uint8,
  x: { type: float32 },
  y: { type: float32 },
  z: { type: float32 },
});

// Buffer thingy
const velocitySchema = BufferSchema.schema('velocity', {
  id: uint8,
  x: { type: float32 },
  y: { type: float32 },
  z: { type: float32 },
});

const snapshotSchema = BufferSchema.schema('snapshot', {
  id: { type: string8, length: 6 },
  state: { positions: [positionSchema], velocities: [velocitySchema] },
  time: uint64,
});

I'll close the issue as it's not really the bug I thought it was, maybe this limitation / behavior can be documented.

Codezilluh commented 2 years ago

This should be reopened (with a more accurate name), as this is probably fixable.