74 lines
1.8 KiB
JavaScript
74 lines
1.8 KiB
JavaScript
/**
|
|
* @typedef {Object} Vector3 A representation of a position in 3D space
|
|
* @property Float32 f32 x coordinate
|
|
* @property Float32 f32 y coordinate
|
|
* @property Float32 f32 z coordinate
|
|
*/
|
|
class Vector3 {
|
|
/**
|
|
* x coordinate
|
|
* @return {Float32} gets the value of x
|
|
*/
|
|
get x() {
|
|
return this._data.getFloat32(0, true);
|
|
}
|
|
/**
|
|
* x coordinate
|
|
* @param {Float32} sets the value of x
|
|
*/
|
|
set x(v) {
|
|
return this._data.setFloat32(0, v, true);
|
|
}
|
|
/**
|
|
* y coordinate
|
|
* @return {Float32} gets the value of y
|
|
*/
|
|
get y() {
|
|
return this._data.getFloat32(4, true);
|
|
}
|
|
/**
|
|
* y coordinate
|
|
* @param {Float32} sets the value of y
|
|
*/
|
|
set y(v) {
|
|
return this._data.setFloat32(4, v, true);
|
|
}
|
|
/**
|
|
* z coordinate
|
|
* @return {Float32} gets the value of z
|
|
*/
|
|
get z() {
|
|
return this._data.getFloat32(8, true);
|
|
}
|
|
/**
|
|
* z coordinate
|
|
* @param {Float32} sets the value of z
|
|
*/
|
|
set z(v) {
|
|
return this._data.setFloat32(8, v, true);
|
|
}
|
|
/**
|
|
* Get the struct representation of the object
|
|
* @return {Uint8Array} u8 array of the C struct.
|
|
*/
|
|
get bytes() {
|
|
return new Uint8Array(this._ptr);
|
|
}
|
|
/**
|
|
* Constructs a new Vector3
|
|
*
|
|
* @param {{x: Float32, y: Float32, z: Float32, }} init The arguments to construct the object.
|
|
* @param {ArrayBuffer} ptr The pointer to the C struct.
|
|
*/
|
|
constructor(init = {}, ptr = undefined) {
|
|
this._size = 12;
|
|
this._ptr = ptr?.buffer || new ArrayBuffer(this._size);
|
|
this._data = new DataView(this._ptr);
|
|
|
|
for (const key of Object.keys(init)) {
|
|
this[key] = init[key];
|
|
}
|
|
}
|
|
}
|
|
|
|
export default Vector3 |