-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(scripting/v8): Add unpackers for Vector2, Vector3, and Vector4 u… #2946
base: master
Are you sure you want to change the base?
Conversation
…sing msgpack - Define constants for EXT_VECTOR2, EXT_VECTOR3, and EXT_VECTOR4 - Implement unpacker for Vector2 that reads 2 float values (x, y) from Uint8Array - Implement unpacker for Vector3 that reads 3 float values (x, y, z) from Uint8Array - Implement unpacker for Vector4 that reads 4 float values (x, y, z, w) from Uint8Array - Convert Uint8Array to Buffer for reading float values
// Vector2 unpacker | ||
codec.addExtUnpacker(EXT_VECTOR2, (data) => { | ||
// Converting Uint8Array to Buffer | ||
const buffer = Buffer.from(data); | ||
// Reading 3 float values from the buffer | ||
const x = buffer.readFloatLE(0); | ||
const y = buffer.readFloatLE(4); | ||
return [x, y]; | ||
}); | ||
|
||
// Vector3 unpacker | ||
codec.addExtUnpacker(EXT_VECTOR3, (data) => { | ||
// Converting Uint8Array to Buffer | ||
const buffer = Buffer.from(data); | ||
// Reading 3 float values from the buffer | ||
const x = buffer.readFloatLE(0); | ||
const y = buffer.readFloatLE(4); | ||
const z = buffer.readFloatLE(8); | ||
return [x, y, z]; | ||
}); | ||
|
||
// Vector4 unpacker | ||
codec.addExtUnpacker(EXT_VECTOR4, (data) => { | ||
// Converting Uint8Array to Buffer | ||
const buffer = Buffer.from(data); | ||
// Reading 3 float values from the buffer | ||
const x = buffer.readFloatLE(0); | ||
const y = buffer.readFloatLE(4); | ||
const z = buffer.readFloatLE(8); | ||
const w = buffer.readFloatLE(12); | ||
return [x, y, z, w]; | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is equivalent.
const vectorUnpacker = (data => [...new Float32Array(data.buffer)]);
codec.addExtUnpacker(EXT_VECTOR2, vectorUnpacker);
codec.addExtUnpacker(EXT_VECTOR3, vectorUnpacker);
codec.addExtUnpacker(EXT_VECTOR4, vectorUnpacker);
And this one should improve number precision.
const vectorUnpacker = (data => Array.from(new Float32Array(data.buffer), (v) => Number(v.toPrecision(7))));
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, @thelindat! That’s a great idea, and I really like the improvement for precision. I’ll go ahead and push this change.
433a327
to
7805416
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a good idea, but also sounds like it will break any existing code which uses the current behavior.
I can't imagine much/any code would use the current behavior though, so it might be fine?
Actually, I knew this question would come up when I first developed this idea, but I thought this code would add a logical behavior for vector processing. I don't know how many projects use the old way, but I find this code makes more sense. |
That's what I was told a while back so I ended up manually handling the serialisation - but the team hasn't been very shy about breaking changes, and getting serialisation for all JS features is fairly important (getting bigint, date, maps, and sets would be nice). Though if this is added it would be nice to have a vector class added by cfx, rather than just getting an array. |
Goal of this PR
This PR aims to add unpackers for
Vector2
,Vector3
andVector4
using msgpack-lite to handle the conversion of Uint8Array data to Buffer and read float values for each vector type.How is this PR achieving the goal
EXT_VECTOR2
,EXT_VECTOR3
, andEXT_VECTOR4
.This PR applies to the following area(s)
Successfully tested on
Game builds: 3258
Platforms: Windows, Linux
Checklist
Code example:
Explanation
Without these unpackers, when receiving data encoded with msgpack, we would get a Buffer containing the msgpack type and other data. This Buffer would need to be manually converted and parsed to extract the float values for x, y, z, and w. The unpackers simplify this process by automatically converting the Uint8Array to a Buffer and reading the float values, returning them as an array. This makes it easier to work with vector data in JavaScript when using msgpack for serialization and deserialization.