LYGIA Shader Library

2DCube (lygia/sample/2DCube)

Use a 2D texture as a 3D one

Dependencies:

Use:

<vec4> sample2DCube(in <SAMPLER_TYPE> lut, in <vec3> xyz)

Check it on Github



#ifndef SAMPLE2DCUBE_CELLS_PER_SIDE
#define SAMPLE2DCUBE_CELLS_PER_SIDE 8.0
#endif

#ifndef SAMPLE2DCUBE_FNC
#define SAMPLE2DCUBE_FNC(TEX, UV) SAMPLER_FNC(TEX, saturate(UV))
#endif

#ifndef FNC_SAMPLE2DCUBE
#define FNC_SAMPLE2DCUBE
vec4 sample2DCube(in SAMPLER_TYPE lut, in vec3 xyz) {

#if defined(SAMPLE2DCUBE_CELL_SIZE)
    const float cellsSize = SAMPLE2DCUBE_CELL_SIZE;
    float cellsPerSide = sqrt(cellsSize);
    float cellsFactor = 1.0/cellsPerSide;
    float lutSize = cellsPerSide * cellsSize;
    float lutSizeFactor = 1.0/lutSize;

#elif defined(SAMPLE2DCUBE_CELLS_PER_SIDE)
    const float cellsPerSide = SAMPLE2DCUBE_CELLS_PER_SIDE;
    const float cellsSize = cellsPerSide * cellsPerSide;
    const float cellsFactor = 1.0/cellsPerSide;
    const float lutSize = cellsPerSide * cellsSize;
    const float lutSizeFactor = 1.0/lutSize;
#endif

    #ifndef SAMPLE2DCUBE_FLIP_Z
    xyz.z = 1.0 - xyz.z;
    #endif

    xyz *= (cellsSize-1.0);
    float iz = floor(xyz.z);

    float x0 = mod(iz, cellsPerSide) * cellsSize;
    float y0 = floor(iz * cellsFactor) * cellsSize;

    float x1 = mod(iz + 1.0, cellsPerSide) * cellsSize;
    float y1 = floor((iz + 1.0) * cellsFactor) * cellsSize;

    vec2 uv0 = vec2(x0 + xyz.x + 0.5, y0 + xyz.y + 0.5) * lutSizeFactor;
    vec2 uv1 = vec2(x1 + xyz.x + 0.5, y1 + xyz.y + 0.5) * lutSizeFactor;

    #ifndef SAMPLE2DCUBE_FLIP_Y
    uv0.y = 1.0 - uv0.y;
    uv1.y = 1.0 - uv1.y;
    #endif

    return mix( SAMPLE2DCUBE_FNC(lut, uv0), 
                SAMPLE2DCUBE_FNC(lut, uv1), 
                xyz.z - iz);
}
#endif 

Dependencies:

Use:

<float4> sample2DCube(in <SAMPLER_TYPE> lut, in <float3> xyz)

Check it on Github



#ifndef SAMPLE2DCUBE_CELLS_PER_SIDE
#define SAMPLE2DCUBE_CELLS_PER_SIDE 8.0
#endif

#ifndef SAMPLE2DCUBE_FNC
#define SAMPLE2DCUBE_FNC(TEX, UV) SAMPLER_FNC(TEX, saturate(UV))
#endif

#ifndef FNC_SAMPLE2DCUBE
#define FNC_SAMPLE2DCUBE
float4 sample2DCube(in SAMPLER_TYPE lut, in float3 xyz) {

#if defined(SAMPLE2DCUBE_CELL_SIZE)
    const float cellsSize = SAMPLE2DCUBE_CELL_SIZE;
    float cellsPerSide = sqrt(cellsSize);
    float cellsFactor = 1.0/cellsPerSide;
    float lutSize = cellsPerSide * cellsSize;
    float lutSizeFactor = 1.0/lutSize;

#elif defined(SAMPLE2DCUBE_CELLS_PER_SIDE)
    const float cellsPerSide = SAMPLE2DCUBE_CELLS_PER_SIDE;
    const float cellsSize = cellsPerSide * cellsPerSide;
    const float cellsFactor = 1.0/cellsPerSide;
    const float lutSize = cellsPerSide * cellsSize;
    const float lutSizeFactor = 1.0/lutSize;
#endif

    xyz *= (cellsSize-1.0);
    float iz = floor(xyz.z);

    float x0 = mod(iz, cellsPerSide) * cellsSize;
    float y0 = floor(iz * cellsFactor) * cellsSize;

    float x1 = mod(iz + 1.0, cellsPerSide) * cellsSize;
    float y1 = floor((iz + 1.0) * cellsFactor) * cellsSize;

    float2 uv0 = float2(x0 + xyz.x + 0.5, y0 + xyz.y + 0.5) * lutSizeFactor;
    float2 uv1 = float2(x1 + xyz.x + 0.5, y1 + xyz.y + 0.5) * lutSizeFactor;

    #ifndef SAMPLE2DCUBE_FLIP_Y
    uv0.y = 1.0 - uv0.y;
    uv1.y = 1.0 - uv1.y;
    #endif

    return lerp(SAMPLE2DCUBE_FNC(lut, uv0), 
                SAMPLE2DCUBE_FNC(lut, uv1), 
                xyz.z - iz );
}
#endif 

Licenses

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.

Get the latest news and releases

Sign up for the news letter bellow, joing the LYGIA's channel on Discord or follow the Github repository