Skip to content

Mirror::Compression

Functions to Compress Quaternions and Floats More...

Public Functions

Name
uint CompressQuaternion(Quaternion value)
Used to Compress Quaternion into 4 bytes
Quaternion DecompressQuaternion(uint packed)
Used to read a Compressed Quaternion from 4 bytes
uint ScaleToUInt(float value, float minFloat, float maxFloat, uint minUint, uint maxUint)
Scales float from minFloat->maxFloat to minUint->maxUint
float ScaleFromUInt(uint value, float minFloat, float maxFloat, uint minUint, uint maxUint)
Scales uint from minUint->maxUint to minFloat->maxFloat

Detailed Description

class Mirror::Compression;

Functions to Compress Quaternions and Floats

Uncompressed Quaternion = 32 * 4 = 128 bits => send 16 bytes

Quaternion is always normalized so we drop largest value and re-calculate it. We can encode which one is the largest using 2 bits

x^2 + y^2 + z^2 + w^2 = 1

2nd largest value has max size of 1/sqrt(2) We can encode the smallest three components in [-1/sqrt(2),+1/sqrt(2)] instead of [-1,+1]

c^2 + c^2 + 0 + 0 = 1

Sign of largest value doesnt matter

Q * vec3 == (-Q) * vec3

RotationPrecision

2/sqrt(2) / (2^bitCount - 1)

rotation precision +-0.00138 in range [-1,+1]

10 bits per value
2 + 10 * 3 = 32 bits => send 4 bytes

Links for more info:

GDC Talk

Post on Snapshot Compression

Public Functions Documentation

function CompressQuaternion

static inline uint CompressQuaternion(
    Quaternion value
)

Used to Compress Quaternion into 4 bytes

function DecompressQuaternion

static inline Quaternion DecompressQuaternion(
    uint packed
)

Used to read a Compressed Quaternion from 4 bytes

Quaternion is normalized

function ScaleToUInt

static inline uint ScaleToUInt(
    float value,
    float minFloat,
    float maxFloat,
    uint minUint,
    uint maxUint
)

Scales float from minFloat->maxFloat to minUint->maxUint

values out side of minFloat/maxFloat will return either 0 or maxUint

function ScaleFromUInt

static inline float ScaleFromUInt(
    uint value,
    float minFloat,
    float maxFloat,
    uint minUint,
    uint maxUint
)

Scales uint from minUint->maxUint to minFloat->maxFloat


Updated on 26 January 2021 at 13:37:21 UTC