<p>I'm working on a webgl pixel shader which is writing to a 16 bit floating point buffer (each color channel r,g,b,a is a 16 bit floating point number).</p>

<p>I'm trying to figure out if there are any decent ways to pack the following info into those 16 bits per color channel:</p>

<ul>
<li>normal (x,y,z where z is always negative)</li>
<li>uv coordinates (x,y between 0 and 1)</li>
<li>material index (an integer. 0 to 3 would be plenty)</li>
<li>distance (a positive float)</li>
</ul>

<p>I could live without distance if needed but the others are required.</p>

<p>I know that I can store just x,y of the normal and calculate z as needed since its always negative (the normal is in screenspace).</p>

<p>That still leaves 5 values to store in four 16 bit floats: normal.xy, uv.xy, material index.</p>

<p>Does anyone have any good methods for making this happen? It would be nice to have the distance too, otherwise I am limited to directional lighting instead of getting point lights as well.</p>

<p>This is in shadertoy if that context changes any answers.</p>

<p>I'll be sampling this buffer with nearest neighbor sampling, so you don't have to worry about it being stored in a way that interpolates well.</p>

<p>Thanks!</p>

<webgl>
Bitpacking into buffers with webgl (shadertoy)
Alan Wolfe
# Bitpacking into buffers with webgl (shadertoy)

(Asked by: username_0 on 2016-02-19)

I'm working on a webgl pixel shader which is writing to a 16 bit floating point buffer (each color channel r,g,b,a is a 16 bit floating point number).

I'm trying to figure out if there are any decent ways to pack the following info into those 16 bits per color channel:

* normal (x,y,z where z is always negative)
* uv coordinates (x,y between 0 and 1)
* material index (an integer. 0 to 3 would be plenty)
* distance (a positive float)

I could live without distance if needed but the others are required.

I know that I can store just x,y of the normal and calculate z as needed since its always negative (the normal is in screenspace).

That still leaves 5 values to store in four 16 bit floats: normal.xy, uv.xy, material index.

Does anyone have any good methods for making this happen? It would be nice to have the distance too, otherwise I am limited to directional lighting instead of getting point lights as well.

This is in shadertoy if that context changes any answers.

I'll be sampling this buffer with nearest neighbor sampling, so you don't have to worry about it being stored in a way that interpolates well.

Thanks!

**Tags:** webgl

---

## 0 answers

