Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript
4.7 KB
Raw Blame
/**
 * Low-level sequential reader for FBX binary buffers.
 * Wraps a `DataView` and advances an internal byte offset as typed values are read.
 */
class BinaryReader {
    dv: DataView;
    offset: number;
    littleEndian: boolean;
    _textDecoder: TextDecoder;

    constructor (buffer: ArrayBuffer, littleEndian?: boolean) {

        this.dv = new DataView(buffer);
        this.offset = 0;
        this.littleEndian = (littleEndian !== undefined) ? littleEndian : true;
        this._textDecoder = new TextDecoder();

    }

    getOffset (): number {

        return this.offset;

    }

    size (): number {

        return this.dv.buffer.byteLength;

    }

    skip (length: number): void {

        this.offset += length;

    }

    // seems like true/false representation depends on exporter.
    // true: 1 or 'Y'(=0x59), false: 0 or 'T'(=0x54)
    // then sees LSB.
    getBoolean (): boolean {

        return (this.getUint8() & 1) === 1;

    }

    getBooleanArray (size: number): boolean[] {

        const a: boolean[] = [];

        for (let i = 0; i < size; i++) {

            a.push(this.getBoolean());

        }

        return a;

    }

    getUint8 (): number {

        const value = this.dv.getUint8(this.offset);
        this.offset += 1;
        return value;

    }

    getInt16 (): number {

        const value = this.dv.getInt16(this.offset, this.littleEndian);
        this.offset += 2;
        return value;

    }

    getInt32 (): number {

        const value = this.dv.getInt32(this.offset, this.littleEndian);
        this.offset += 4;
        return value;

    }

    getInt32Array (size: number): number[] {

        const a: number[] = [];

        for (let i = 0; i < size; i++) {

            a.push(this.getInt32());

        }

        return a;

    }

    getUint32 (): number {

        const value = this.dv.getUint32(this.offset, this.littleEndian);
        this.offset += 4;
        return value;

    }

    // JavaScript doesn't support 64-bit integer so calculate this here
    // 1 << 32 will return 1 so using multiply operation instead here.
    // There's a possibility that this method returns wrong value if the value
    // is out of the range between Number.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER.
    // TODO: safely handle 64-bit integer
    getInt64 (): number {

        let low, high;

        if (this.littleEndian) {

            low = this.getUint32();
            high = this.getUint32();

        } else {

            high = this.getUint32();
            low = this.getUint32();

        }

        // calculate negative value
        if (high & 0x80000000) {

            high = ~high & 0xFFFFFFFF;
            low = ~low & 0xFFFFFFFF;

            if (low === 0xFFFFFFFF) high = (high + 1) & 0xFFFFFFFF;

            low = (low + 1) & 0xFFFFFFFF;

            return - (high * 0x100000000 + low);

        }

        return high * 0x100000000 + low;

    }

    getInt64Array (size: number): number[] {

        const a: number[] = [];

        for (let i = 0; i < size; i++) {

            a.push(this.getInt64());

        }

        return a;

    }

    // Note: see getInt64() comment
    getUint64 (): number {

        let low, high;

        if (this.littleEndian) {

            low = this.getUint32();
            high = this.getUint32();

        } else {

            high = this.getUint32();
            low = this.getUint32();

        }

        return high * 0x100000000 + low;

    }

    getFloat32 (): number {

        const value = this.dv.getFloat32(this.offset, this.littleEndian);
        this.offset += 4;
        return value;

    }

    getFloat32Array (size: number): number[] {

        const a: number[] = [];

        for (let i = 0; i < size; i++) {

            a.push(this.getFloat32());

        }

        return a;

    }

    getFloat64 (): number {

        const value = this.dv.getFloat64(this.offset, this.littleEndian);
        this.offset += 8;
        return value;

    }

    getFloat64Array (size: number): number[] {

        const a: number[] = [];

        for (let i = 0; i < size; i++) {

            a.push(this.getFloat64());

        }

        return a;

    }

    getArrayBuffer (size: number): ArrayBuffer {

        const value = this.dv.buffer.slice(this.offset, this.offset + size) as ArrayBuffer;
        this.offset += size;
        return value;

    }

    getString (size: number): string {

        const start = this.offset;
        let a = new Uint8Array(this.dv.buffer, start, size);

        this.skip(size);

        const nullByte = a.indexOf(0);
        if (nullByte >= 0) a = new Uint8Array(this.dv.buffer, start, nullByte);

        return this._textDecoder.decode(a);

    }

}

export { BinaryReader }