Typed Array Buffer Schema (objects to buffer converter)

logo

Typed Array Buffer Schema

Hi everyone!

Just like the last resource I shared, this is not really three.js related, but I’m sure many of you can use it.

It’s a library to convert JavaScript Objects to ArrayBuffers.
Here is a introduction video I’ve made. And you will find the source code on github.

Usage

import { BufferSchema, Model } from '@geckos.io/typed-array-buffer-schema'
import { uint8, int16, uint16, uint64, string8 } from '@geckos.io/typed-array-buffer-schema'

// create the player schema
const playerSchema = BufferSchema.schema('player', {
  id: uint8,
  name: string8,
  x: { type: int16, digits: 2 },
  y: { type: int16, digits: 2 }
})

// create the main schema
const mainSchema = BufferSchema.schema('main', {
  time: uint64,
  tick: uint16,
  players: [playerSchema],
})

// create the main model
const mainModel = new Model(mainSchema)

// your game state
const gameState = {
  time: new Date().getTime(),
  tick: 32580,
  players: [
    { id: 0, name: 'Player1', x: -14.43, y: 47.78 },
    { id: 1, name: 'Player2', x: 21.85, y: -78.48 }
  ]
}

// toBuffer()
const buffer = mainModel.toBuffer(gameState)

// fromBuffer()
const data = mainModel.fromBuffer(buffer)
2 Likes