Skip to content

Commit

Permalink
feat(shader-ast-stdlib): add level correction fns
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Aug 14, 2021
1 parent 094ab36 commit 54963e7
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 3 deletions.
169 changes: 169 additions & 0 deletions packages/shader-ast-stdlib/src/color/levels.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import {
$x,
$y,
$z,
defn,
float,
FLOAT0,
FLOAT05,
FLOAT1,
FloatTerm,
indexMat,
lt,
madd,
Mat3Term,
max,
min,
mul,
pow,
reciprocal,
ret,
sub,
ternary,
Vec2Term,
vec3,
Vec3Term,
VEC3_0,
VEC3_1,
} from "@thi.ng/shader-ast";
import { fit01, fitClamped } from "../math/fit";

/**
* Takes normalized `mid` level and converts it into an exponent for gamma
* correction using {@link levelAdjustGamma}.
*
* @remarks
* Reference: https://stackoverflow.com/a/48859502/294515
*/
export const midLevelGamma = defn(
"float",
"midLevelGamma",
["float"],
(mid) => [
ret(
reciprocal(
ternary(
lt(mid, FLOAT05),
min(float(9.99), madd(float(9), sub(1, mul(mid, 2)), 1)),
max(float(0.01), sub(1, sub(mul(mid, 2), 1)))
)
)
),
]
);

/**
* Inline function. Similar to {@link midLevelGamma}, but for RGB values (with
* potentially varying settings per channel). To be used with
* {@link levelAdjustGammaRGB}.
*
* @param mid
*/
export const midLevelGammaRGB = (mid: Vec3Term) =>
vec3(
midLevelGamma($x(mid)),
midLevelGamma($y(mid)),
midLevelGamma($z(mid))
);

/**
* Applies level remapping from `input` to `output` (each given as
* `vec2(min,max)`) with `gamma` correction. Results will be clamped to [0..1]
* range.
*/
export const levelAdjustGamma = defn(
"float",
"levelAdjustGamma",
["float", "float", "vec2", "vec2"],
(x, gamma, input, output) => [
ret(
fit01<FloatTerm>(
pow(
fitClamped<FloatTerm>(
x,
$x(input),
$y(input),
FLOAT0,
FLOAT1
),
gamma
),
$x(output),
$y(output)
)
),
]
);

/**
* Similar to {@link levelAdjustGamma}, but for RGB values (with potentially
* varying settings per channel).
*
* @remarks
* Note: Only the first to columns of the `mat3` args will be used (for WebGL1
* compatibility):
*
* - 1st column = RGB min values
* - 2nd column = RGB max values
*/
export const levelAdjustGammaRGB = defn(
"vec3",
"levelAdjustGammaRGB",
["vec3", "vec3", "mat3", "mat3"],
(x, gamma, input, output) => [
ret(
fit01<Vec3Term>(
pow(
fitClamped<Vec3Term>(
x,
indexMat(input, 0),
indexMat(input, 1),
VEC3_0,
VEC3_1
),
gamma
),
indexMat(output, 0),
indexMat(output, 1)
)
),
]
);

/**
* Inline function. Similar to {@link levelAdjustGamma}, but first computes
* gamma from given `mid` level.
*
* @remarks
* Also see {@link midLevelGamma}.
*
* @param x - float
* @param mid - float
* @param input - vec2(min,max)
* @param output - vec2(min,max)
*/
export const levelAdjustMid = (
x: FloatTerm,
mid: FloatTerm,
input: Vec2Term,
output: Vec2Term
) => levelAdjustGamma(x, midLevelGamma(mid), input, output);

/**
* Inline function. Similar to {@link levelAdjustMid}, but for RGB values (with
* potentially varying settings per channel).
*
* @remarks
* Also see {@link levelAdjustGammaRGB}, {@link midLevelGammaRGB}.
*
* @param x
* @param mid
* @param input
* @param output
*/
export const levelAdjustMidRGB = (
x: Vec3Term,
mid: Vec3Term,
input: Mat3Term,
output: Mat3Term
) => levelAdjustGammaRGB(x, midLevelGammaRGB(mid), input, output);
1 change: 1 addition & 0 deletions packages/shader-ast-stdlib/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from "./api";

export * from "./color/aces-film";
export * from "./color/levels";
export * from "./color/linear-srgb";
export * from "./color/luminance";
export * from "./color/porter-duff";
Expand Down
5 changes: 2 additions & 3 deletions packages/shader-ast-stdlib/src/sdf/polyhedra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import {
abs,
add,
defn,
div,
dot,
FLOAT0,
FLOAT1,
FloatTerm,
max,
PHI,
pow,
reciprocal,
ret,
sub,
TaggedFn2,
Expand Down Expand Up @@ -84,7 +83,7 @@ const defGDF = (
(acc: FloatTerm, v) => add(acc, pow(abs(dot(p, v)), e)),
FLOAT0
),
div(FLOAT1, e)
reciprocal(e)
),
r
)
Expand Down

0 comments on commit 54963e7

Please sign in to comment.