Skip to content

Instantly share code, notes, and snippets.

@behreajj
Last active May 22, 2021 12:29
Show Gist options
  • Save behreajj/ce10cca6c337373f797d0d579181dada to your computer and use it in GitHub Desktop.
Save behreajj/ce10cca6c337373f797d0d579181dada to your computer and use it in GitHub Desktop.
linearGradient.osl
float scalarProj(vector a, vector b) {
return dot(a, b) / dot(b, b);
}
float uquantize(float x, int levels) {
return select(
max(0.0, (ceil(x * levels) - 1.0) / (levels - 1.0)),
x, levels < 2);
}
shader linearGradient(
point Origin = point(-1.0, 0.0, 0.0),
point Dest = point(1.0, 0.0, 0.0),
point Point = P,
int Quantize = 0,
output float Fac = 0.0,
output float Dot = 0.0,
output point Vector = 0.0) {
point a = Point - Origin;
point b = Dest - Origin;
Dot = scalarProj(a, b);
Fac = uquantize(clamp(Dot, 0.0, 1.0), Quantize);
Vector = Origin + b * Fac;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment