lygia
/math
/pack
)Pack a float into a 4D vector. From https://github.com/mrdoob/three.js/blob/acdda10d5896aa10abdf33e971951dbf7bd8f074/src/renderers/shaders/ShaderChunk/packing.glsl
Use:
<vec4> pack(<float> v)
#ifndef CONST_PACKING
#define CONST_PACKING
const float PackUpscale = 256. / 255.; // fraction -> 0..1 (including 1)
const float UnpackDownscale = 255. / 256.; // 0..1 -> fraction (excluding 1)
const vec3 PackFactors = vec3( 256. * 256. * 256., 256. * 256., 256. );
const vec4 UnpackFactors = UnpackDownscale / vec4( PackFactors, 1. );
const float ShiftRight8 = 1. / 256.;
#endif
#ifndef FNC_PACK
#define FNC_PACK
vec4 pack( const in float v ) {
vec4 r = vec4( fract( v * PackFactors ), v );
r.yzw -= r.xyz * ShiftRight8; // tidy overflow
return r * PackUpscale;
}
#endif
Use:
<float4> pack(<float> v)
#ifndef CONST_PACKING
#define CONST_PACKING
static const float PackUpscale = 256. / 255.; // fraction -> 0..1 (including 1)
static const float UnpackDownscale = 255. / 256.; // 0..1 -> fraction (excluding 1)
static const float3 PackFactors = float3( 256. * 256. * 256., 256. * 256., 256. );
static const float4 UnpackFactors = UnpackDownscale / float4( PackFactors, 1. );
static const float ShiftRight8 = 1. / 256.;
#endif
#ifndef FNC_PACK
#define FNC_PACK
float4 pack( const in float v ) {
float4 r = float4( frac( v * PackFactors ), v );
r.yzw -= r.xyz * ShiftRight8; // tidy overflow
return r * PackUpscale;
}
#endif
Use:
<float4> pack(<float> v)
#ifndef CONST_PACKING
#define CONST_PACKING
constant float PackUpscale = 256. / 255.; // fraction -> 0..1 (including 1)
constant float UnpackDownscale = 255. / 256.; // 0..1 -> fraction (excluding 1)
constant float3 PackFactors = float3( 256. * 256. * 256., 256. * 256., 256. );
constant float4 UnpackFactors = UnpackDownscale / float4( PackFactors, 1. );
constant float ShiftRight8 = 1. / 256.;
#endif
#ifndef FNC_PACK
#define FNC_PACK
float4 pack( const float v ) {
float4 r = float4( fract( v * PackFactors ), v );
r.yzw -= r.xyz * ShiftRight8; // tidy overflow
return r * PackUpscale;
}
#endif
const PackUpscale: f32 = 256. / 255.; // fraction -> 0..1 (including 1)
const UnpackDownscale: f32 = 255. / 256.; // 0..1 -> fraction (excluding 1)
const PackFactors = vec3f( 256. * 256. * 256., 256. * 256., 256. );
const UnpackFactors = UnpackDownscale / vec4f( PackFactors, 1. );
const ShiftRight8: f32 = 1. / 256.;
fn pack(v: f32) -> vec4f {
var r = vec4( fract( v * PackFactors ), v );
r.yzw -= r.xyz * ShiftRight8; // tidy overflow
return r * PackUpscale;
}
LYGIA is dual-licensed under the Prosperity License and the Patron License for sponsors and contributors.
Sponsors and contributors are automatically added to the Patron License and they can ignore the any non-commercial rule of the Prosperity Licensed software (please take a look to the exception).
It's also possible to get a permanent comercial license hook to a single and specific version of LYGIA.
Sign up for the news letter bellow, joing the LYGIA's channel on Discord or follow the Github repository