Compression Module
Intrinsic-backed vertex decompression helpers for packed positions, normals, UVs, and colors.
Turn the guide into code
Take the key idea from this page into the playground and validate it in a real shader instead of leaving it as theory.
Open PlaygroundIntrinsic-backed vertex decompression helpers for packed positions, normals, UVs, and colors.
Import
import Compression
This module has no module imports.
Usage Examples
Read Packed Vertex Attributes
Use Compression when vertex buffers store compact packed data and the shader needs normalized positions, normals, UVs, or colors.
import Compression
attributes {
positionPacked: uint
normalPacked: uint
uvPacked: uint
colorPacked: uint
}
float3 localPos = Compression::decompressPosition_10_10_10(attributes.positionPacked);
float3 normal = Compression::decompressOctahedral16(attributes.normalPacked);
float2 uv = Compression::decompressUV16_16(attributes.uvPacked);
float4 color = Compression::decompressColor8888(attributes.colorPacked);
output.position = resources.modelViewProjection * float4(localPos, 1.0);
output.normal = normal;
output.uv = uv;
output.color = color;
Keep Bandwidth Down for Dense Meshes
Use decompression helpers when the mesh is bandwidth-bound, such as particles, crowds, impostors, foliage, or mobile/WebGL targets.
import Compression
float3 n = Compression::decompressOctahedral16(attributes.normalPacked);
float2 uv = Compression::decompressUV16_16(attributes.uvPacked);
float windMask = sample(resources.windMask, resources.linearSampler, uv).r;
float bend = sin(resources.time + attributes.position.x * 0.15) * windMask;
float3 bentPosition = attributes.position + n * bend * resources.windStrength;
output.position = resources.viewProjection * float4(bentPosition, 1.0);
Constants
None.
Structs
No structs found.
Enums
No enums found.
Functions
decompressPosition_10_10_10
Decode a packed 10-10-10 position
decompressPosition_10_10_10 :: (uint packed) -> float3
decompressOctahedral16
Decode a 16-bit octahedral normal from two packed UNorm8 channels
decompressOctahedral16 :: (uint packed) -> float3
decompressUV16_16
Decode packed UNorm16 UV coordinates
decompressUV16_16 :: (uint packed) -> float2
decompressColor8888
Decode packed UNorm8 color
decompressColor8888 :: (uint packed) -> float4
Source
Generated from compiler-reference/modules/Compression.bwsl.