- Home /
Packing multiple UInts into Mesh Data (UVs) with Shader Graph?
I am currently sending small valued unsigned integers to a shader graph. The values are used as indices for the Sample Texture 2D Array node, so they select which texture to use and hence could be small integer values as there aren't many textures in the 2D array. I send 9 values which I am currently sending as vertex colors and UVs on a runtime generated mesh. So for instance one index value is sent as the red channel of the vertex color, etc. My shader currently works, but I was hoping to optimize it by packing in multiple values into single float so that for instance all 9 values could be sent in just the vertex color r, g, b channels (Using UVs is also an option). I currently pack the values into the floats and use a custom node such as
void UnpackVector_float(float In, out float X, out float Y, out float Z) { uint uIn = asuint(In); Z = (uIn << 24) >> 24; Y = (uIn << 16) >> 24; X = (uIn << 8) >> 24; }
to unpack them in the shader. I also set some of the exponent bits in case the float would be set to 0 because of denormalization. If I hardcode an input float into this unpack function in the shader graph it works fine, but when the data comes from the vertex color node or the UV nodes it doesn't work and produces a glitchy effect seen in the image, which I presume happens because fractional indicies are passed to the Sample Texture 2D Array node. From my research it seems like the vertex color data or UV data is not passed to the GPU as full 32 bit floats and is compressed or is imprecise for non 0..1 range floats. Is there any way around this? I would want to be able to pass binary/uint data to the shader if possible to get around these issues, but I don't believe this is possible in shader graph? Is there a way to pack these indicies in the vertex color or UVs that will maintain its precision?
Answer by Eimhin · Apr 10 at 05:50 AM
I've ended up using a different approach of storing the data by shifting it in decimal. For instance, one value can be packed as the one, ten and hundred digits, and another packed in the thousand, ten thousand, and hundred thousand digits (e.g. putting 123 and 987 as 123987) etc. This should work in a UV channel since the UV values shouldn't be clamped and 32 bit floats can exactly represent integers up to 2^24. Not ideal, but solves the problem for now.