countOneBits
countOneBits returns the number of bits set to 1 in the binary representation of an integer or integer vector (also known as popcount / Hamming weight). For vector inputs it operates component-wise and outputs an integer value per component.
Core Advantages
It wraps a population-count style operation into a single node that can map to the GPU's native bit-count instruction on WebGPU backends, making it extremely cheap to derive measures like “weight” or “density” from compact bitmasks or hashed values directly in shaders, without unpacking data on the CPU.
Common Uses
Counting how many feature flags are enabled in a packed integer and using that count to control material intensity or blend ratios
Applying countOneBits to hash values derived from screen coordinates or tile IDs to generate repeatable pseudo-random patterns, flicker, or dithering effects
In GPGPU-style computations, volumetric rendering, or layered effects, using the number of set bits as a weight / level indicator to classify or group different samples or channels
How to adjust
To control the behavior of countOneBits you adjust the integer (or integer vector) that you feed into it: flipping each individual bit (0 ↔ 1) causes a discrete jump in the output, so the result is inherently step-like rather than smoothly varying. A common pattern is to first build a uint / uvecN via bitwise ops or a hash function, then pass it through countOneBits to obtain a 0–32 range per 32-bit component, normalize that value (for example by dividing by a constant) and map it to color, opacity, or intensity. For vector inputs, each component is counted independently, which is useful when you want multiple channels or samples to fall into different bins in one go. According to the source documentation this node is intended to be used with WebGPURenderer running on a WebGPU backend, so you should ensure your renderer / backend combination supports the required bit operations.
Code Examples
1// Use countOneBits to count how many bits are set to 1 in a flag value
2const flags = uint( 0x0000000F ); // lowest 4 bits are 1
3const enabledCount = countOneBits( flags ); // returns 4
4
5// Normalize the result and use it as emissive strength
6const glowStrength = enabledCount.float().div( 8.0 );
7
8meshStandardNodeMaterial( {
9 emissiveNode: color( '#00ffff' ).mul( glowStrength )
10} );