fuckingtech/public/assets/glsl/lygia/sample/2DCube.glsl
Elias Rhouzlane bf76c6b086 fix: code
2025-04-06 17:23:52 +02:00

70 lines
No EOL
2.2 KiB
GLSL

#include "../sampler.glsl"
#include "../math/saturate.glsl"
/*
contributors: Patricio Gonzalez Vivo
description: Use a 2D texture as a 3D one
use: <vec4> sample2DCube(in <SAMPLER_TYPE> lut, in <vec3> xyz)
options:
- SAMPLER_FNC(TEX, UV): optional depending the target version of GLSL (texture2D(...) or texture(...))
- SAMPLE2DCUBE_CELL_SIZE
- SAMPLE2DCUBE_CELLS_PER_SIDE: defaults to 8
- SAMPLE2DCUBE_FNC
license:
- Copyright (c) 2021 Patricio Gonzalez Vivo under Prosperity License - https://prosperitylicense.com/versions/3.0.0
- Copyright (c) 2021 Patricio Gonzalez Vivo under Patron License - https://lygia.xyz/license
*/
#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