Godot Version
4.6.1
Question
A bit of a technical one, but I’m trying to pack 32 bits worth of data into each 32 bit channel of a Color, and use that as Custom Data for a vertex shader.
I’m guessing this is a limitation of floating point values, but the Color format seems to lose the 4 smallest bits in each of its channels, when set manually. This means I can only use 28 bits worth of data, which complicates my use case for this a fair bit!
For context, I’m using the SurfaceTool for custom mesh generation, so I believe I only have access to the Color variable as a method of passing Custom Data into a given vertex. The shader in question decomposes the bits in each 32 bit Color, and uses sets of 4 or 8 bits from them for various uses.
Is there’s any known way to pass a full set of 32 bits into a Color, or otherwise into the Custom Data of a vertex? Unless I’m misunderstanding something, I can’t see a way to get any use out of the last 4 bits per channel of Custom Data otherwise, in any circumstance.
Here’s a sample script for demonstrating the problem. As you can see, the interger bitmask stores the full 32 bits of data, as does each channel of a Vector4i, but Color seems to drop the final four bits.
extends Node3D
func print_bits(value: int, padding: int = 32) -> String:
return String.num_uint64(value, 2).pad_zeros(padding)
func _ready():
# Bitmask containing 32 bits of data
var base_bitmask = 0b0001_1010_0000_1100_1000_1011_1010_0110
# A Color and Vector4i, to test formats for packing bitwise data into channels
var base_colour = Color(0.0, 0.0, 0.0, 0.0)
var base_vector_int = Vector4i(0, 0, 0, 0)
# Sets the first data channel of the Color or Vector4 to store the bitmask
base_colour.r = base_bitmask
base_vector_int.x = base_bitmask
# Display actual bitwise data stored in each format
print ("BASE BITMASK: ", print_bits(base_bitmask, 32))
print ("\nCOLOR BITS R: ", print_bits(base_colour.r, 32))
print ("VECTOR BITS X: ", print_bits(base_vector_int.x, 32))
Output:

Note that the final 4 bits of the Color are simply lost. I’m unsure why this is, or how to work around it. Any help would be very welcome.

