Skip to content

Commit 91c9bb0

Browse files
committed
Add unmix/inverse lerp
1 parent b1b77ef commit 91c9bb0

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

Diff for: src/doc/stdlib.md

+6
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ the computations are performed component-by-component (separately for `x`,
166166
$ x*(1-\alpha) + y*(\alpha) $
167167

168168

169+
*`type`* **`unmix`** (*`type`* `x`, *`type`* `y`, *`type`* `alpha`) <br> *`type`* **`unmix`** (*`type`* `x`, *`type`* `y, float alpha`)
170+
: The `unmix` function returns a inverse of `mix`, similar to inverse linear interpolation:
171+
$ (x - (\alpha)) / (y - x) $
172+
The x and y value can't be the same, as we can't divide by zero.
173+
174+
169175
*`type`* **`select`** (*`type`* `x`, *`type`* `y`, *`type`* `cond`) <br> *`type`* **`select`** (*`type`* `x`, *`type`* `y, float cond`) <br> *`type`* **`select`** (*`type`* `x`, *`type`* `y, int cond`)
170176
: The `select` function returns `x` if `cond` is zero, or `y`
171177
if `cond` is nonzero. This is roughly equivalent to `(cond ? y : x)`,

Diff for: src/shaders/stdosl.h

+26
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,32 @@ float mix (float x, float y, float a) BUILTIN;
164164
closure color mix (closure color x, closure color y, float a) { return x*(1-a) + y*a; }
165165
closure color mix (closure color x, closure color y, color a) { return x*(1-a) + y*a; }
166166

167+
#if 0
168+
normal unmix (normal x, normal y, normal a) { return (a - x) / (y - x); }
169+
normal unmix (normal x, normal y, float a) { return (a - x) / (y - x); }
170+
vector unmix (vector x, vector y, vector a) { return (a - x) / (y - x); }
171+
vector unmix (vector x, vector y, float a) { return (a - x) / (y - x); }
172+
point unmix (point x, point y, point a) { return (a - x) / (y - x); }
173+
point unmix (point x, point y, float a) { return (a - x) / (y - x); }
174+
color unmix (color x, color y, color a) { return (a - x) / (y - x); }
175+
color unmix (color x, color y, float a) { return (a - x) / (y - x); }
176+
float unmix (float x, float y, float a) { return (a - x) / (y - x); }
177+
#else
178+
normal unmix (normal x, normal y, normal a) BUILTIN;
179+
normal unmix (normal x, normal y, float a) BUILTIN;
180+
vector unmix (vector x, vector y, vector a) BUILTIN;
181+
vector unmix (vector x, vector y, float a) BUILTIN;
182+
point unmix (point x, point y, point a) BUILTIN;
183+
point unmix (point x, point y, float a) BUILTIN;
184+
color unmix (color x, color y, color a) BUILTIN;
185+
color unmix (color x, color y, float a) BUILTIN;
186+
float unmix (float x, float y, float a) BUILTIN;
187+
#endif
188+
closure color unmix (closure color x, closure color y, float a) { return (a - x) / (y - x); }
189+
closure color unmix (closure color x, closure color y, color a) { return (a - x) / (y - x); }
190+
191+
// TODO: ADD REMAP FROM MIX AND UNMIX
192+
167193
normal select (normal x, normal y, normal cond) BUILTIN;
168194
vector select (vector x, vector y, vector cond) BUILTIN;
169195
point select (point x, point y, point cond) BUILTIN;

0 commit comments

Comments
 (0)