<p>I am doing some animation jobs. I need to pack some pivots into UV and then my shader can read them. </p>

<p>I need to pack 4 float3 into a float4. Therefore, I need to pack each float3 into a  float.</p>

<p>These 4 float3 are (model space position1, direction1, model space position2, direction2). I know how to handle the directions because they are normalized. I can use something like:</p>

<pre><code> #define f3_f(c) (dot(round((c) * 255), float3(65536, 256, 1)))
 #define f_f3(f) (frac((f) / float3(16777216, 65536, 256)))
</code></pre>

<p>But How can I pack these positions? I am using SM3.0 and I can't use bitwise operations</p>

CC BY-SA 4.0
2019-12-31T12:07:51.577Z
In general you cannot store $N$ bytes into $\frac{N}{3}$ bytes without some form of lossy compression. Depending on the different needs, assumptions, and constraints, entirely different compression methods may be adequate.
lightxbulb
CC BY-SA 4.0
2020-01-01T20:11:24.903Z
Unit directions can always be represented in two floats (longitude & latitude), and you could store them quantized as a pair of half-floats. Does one of your directions point toward the other position? If that's true, you could store one position as 3 half-floats, and the other position as a half-float distance. That would be a total of 8 half floats, or 1 float4.
David
CC BY-SA 4.0
2020-01-03T10:55:37.793Z
Are you sure you cannot add more attributes? you should have up to 16 vec4s worth of attribute space for the vertex shader input data.
ratchet freak
<shader>
How to pack 3 floats into one?
TIANLUN ZHU
# How to pack 3 floats into one?

(Asked by: username_0 on 2019-12-31)

I am doing some animation jobs. I need to pack some pivots into UV and then my shader can read them.

I need to pack 4 float3 into a float4. Therefore, I need to pack each float3 into a float.

These 4 float3 are (model space position1, direction1, model space position2, direction2). I know how to handle the directions because they are normalized. I can use something like:

```
 #define f3_f(c) (dot(round((c) * 255), float3(65536, 256, 1)))
 #define f_f3(f) (frac((f) / float3(16777216, 65536, 256)))

```

But How can I pack these positions? I am using SM3.0 and I can't use bitwise operations

**Tags:** shader

> **Comments:**
> **username_1**: In general you cannot store $N$ bytes into $\frac{N}{3}$ bytes without some form of lossy compression. Depending on the different needs, assumptions, and constraints, entirely different compression methods may be adequate.  
> **username_2**: Unit directions can always be represented in two floats (longitude & latitude), and you could store them quantized as a pair of half-floats. Does one of your directions point toward the other position? If that's true, you could store one position as 3 half-floats, and the other position as a half-float distance. That would be a total of 8 half floats, or 1 float4.  
> **username_3**: Are you sure you cannot add more attributes? you should have up to 16 vec4s worth of attribute space for the vertex shader input data.  

---

## 0 answers

