npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vox-parser

v0.2.2

Published

Vox format parser

Downloads

23

Readme

vox-parser

Build Status Code Coverage Gzip Size

vox-parser is a tiny library for parsing voxel models saved in vox format used by MagicalVoxel. Example models and specification of vox file format can be found here.

Installation

To use library install it with npm:

npm install vox-parser

And then in node.js environment:

const { parse } = require('vox-parser');

Or using ES2015 modules:

import { parse } from 'vox-parser';

Enviroment that you are using needs to support ArrayBuffer and DataView built-in objects (or corresponding polyfills).

Use

const result = parse(buffer);
console.log(result);

where buffer is an ArrayBuffer instance.

To understand the structure of result it is higly recommended to check the official specification.

Output consists of basic information about file:

{
  id: 'VOX ', // file id (if different than 'VOX ' throws error)
  version: 150, // file version
  body: { ... }, // file contents
}

body contains information about all the chunks contained in the file. Basic chunk structure looks like this:

{
  id: 'MAIN', // name of chunk
  numContent: 0, // length of chunk content in bytes
  numChildren: 25458, // length of chunk children in bytes
  content?: { ... }, // content of chunk if numContent > 0
  children?: [ ... ], // chunk children if numChildren > 0
}

Meaning of different chunks is described in specification. Below you can check structure of different chunks.

PACK

{
  numModels: 4,
}

SIZE

{
  x: 31,
  y: 7,
  z: 24,
}

XYZI

{
  numVoxels: 808,
  voxels: [
    { x: 19, y: 5, z: 6, i: 100 },
    ...,
  ],
}

RGBA

{
  palette: [
    { r: 255, g: 255, b: 255, a: 255 },
    ...,
  ],
}

MATT

{
  id: 1,
  materialType: 'diffuse' | 'metal' | 'glass' | 'emissive',
  materialWeight: 0.43,
  properties: [
    { property: 'plastic', value: 0.1 },
    { property: 'roughness', value: 0.1 },
    { property: 'specular', value: 0.1 },
    { property: 'ior', value: 0.1 },
    { property: 'attenuation', value: 0.1 },
    { property: 'power', value: 0.1 },
    { property: 'glow', value: 0.1 },
    { property: 'isTotalPower', value: null },
  ],
}

Default palette is not included in the librabry.

Examples

Node.js

In node.js environment the easiest way to play with voxel models is to simply read them. The basic example would be:

const fs = require('fs');
const { parse } = require('vox-parser');

fs.readFile('example.vox', (err, buf) => {
  if (err) throw new Error(err);
  const result = parse(buf.buffer);
  console.log(result);
})

If you won't specify format to readFile it will result in binary buffer, in node.js 4.x and higher instances of Buffer are also instances of Uint8Array so you can access ArrayBuffer by buffer property to supply it to parse function.

Browser

In browser you will probably need to fetch file located on the server. Basic example using fetch would be:

import { parse } from 'vox-parser';

fetch('/static/example.vox')
  .then(response => response.arrayBuffer())
  .then(arrayBuffer => {
    const result = parse(arrayBuffer);
    console.log(result);
  });

fetch provides an API to parse response to ArrayBuffer, so the only thing left to do is to pass parsed response to parse function.