# @maptalks/tbn-packer

> pack normal and tangent data into a quaternion

Latest version **1.4.5** (published 2023-10-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install @maptalks/tbn-packer
pnpm add @maptalks/tbn-packer
yarn add @maptalks/tbn-packer
bun add @maptalks/tbn-packer
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.4.5 |
| Published | 2023-10-20 |
| First published | 2019-06-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 24.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 10 |
| Author | fuzhenn |
| Maintainers | fuzhenn |

## Links

- npm: https://www.npmjs.com/package/@maptalks/tbn-packer
- Repository: https://github.com/fuzhenn/tbn-packer
- Homepage: https://github.com/fuzhenn/tbn-packer#readme
- Issues: https://github.com/fuzhenn/tbn-packer/issues
- npm.io page: https://npm.io/package/@maptalks/tbn-packer

## Dependencies (1)

- [gl-matrix](https://npm.io/package/gl-matrix.md) ^2.6.1

## Recent versions

- 1.4.5 (latest) — 2023-10-20
- 1.4.4 — 2023-10-20
- 1.4.3 — 2023-10-20
- 1.4.2 — 2023-10-20
- 1.4.1 — 2023-09-29
- 1.4.0 — 2023-08-16
- 1.3.0 — 2023-08-15
- 1.2.8 — 2022-03-02
- 1.2.7 — 2021-08-26
- 1.2.6 — 2020-07-30
- 1.2.5 — 2020-06-17
- 1.2.4 — 2020-03-30
- 1.2.3 — 2020-03-30
- 1.2.2 — 2019-09-28
- 1.2.1 — 2019-06-17
- … 4 more at https://npm.io/package/@maptalks/tbn-packer/versions

## README

# tbn-packer
[![Circle CI](https://circleci.com/gh/fuzhenn/tbn-packer.svg?style=shield)](https://circleci.com/gh/fuzhenn/tbn-packer)

Pack tangent and normal data into a quaternion, a useful [tech published by crytek](http://www.crytek.com/download/izfrey_siggraph2011.pdf) to compress data push to GPU.

This is ported from C++ implementation of [google filament](https://github.com/google/filament).

## Usage

```js
import { packTangentFrame, unpackQuaternion } from "@maptalks/tbn-packer";
const tangent = [1, 0, 0, 1];
const normal = [0, 1, 0];

const q = [];
//pack tangent and normal to a quaternion
packTangentFrame(q, normal, tangent);
const n = [], t = [];
//unpack a given quaternion to a normal and tangent.
unpackQuaternion(q, n, t);
```

### GLSL Code to unpack

From google filament:

```glsl
/**
 * Extracts the normal vector of the tangent frame encoded in the specified quaternion.
 */
void toTangentFrame(const highp vec4 q, out highp vec3 n) {
    n = vec3( 0.0,  0.0,  1.0) +
        vec3( 2.0, -2.0, -2.0) * q.x * q.zwx +
        vec3( 2.0,  2.0, -2.0) * q.y * q.wzy;
}

/**
 * Extracts the normal and tangent vectors of the tangent frame encoded in the
 * specified quaternion.
 */
void toTangentFrame(const highp vec4 q, out highp vec3 n, out highp vec3 t) {
    toTangentFrame(q, n);
    t = vec3( 1.0,  0.0,  0.0) +
        vec3(-2.0,  2.0, -2.0) * q.y * q.yxw +
        vec3(-2.0,  2.0,  2.0) * q.z * q.zwx;
}
```

## Install

```shell
npm i @maptalks/tbn-packer
```

### Test
```shell
npm test
```

---
_Source: https://npm.io/package/@maptalks/tbn-packer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
