Skip to content

Instantly share code, notes, and snippets.

@wayerr
Last active December 2, 2021 15:15
Show Gist options
  • Save wayerr/13035a5baf8f0c8c7c651b212f8d2bf9 to your computer and use it in GitHub Desktop.
Save wayerr/13035a5baf8f0c8c7c651b212f8d2bf9 to your computer and use it in GitHub Desktop.
Shader generating random placed fur strands for displacement
//#define DEBUG 1
void _show_1cell(float z, float x, output float r) {
float b = 0.01;
if ((x < b || z < b)) {
r = 1;
}
}
float _strand_curve(float x) {
return pow(abs(x - 0.5) * 2, 1.2);
}
float _cnoise(float z, float x, int i) {
return noise("cell", point(z, x, i));
}
float _calc_strand(
float dz, float dx,
float z, float x,
int scale, int i,
float slope, int count
) {
float sdz = dz / scale;
float sdx = dx / scale;
float nx = dx * _cnoise(z/dz, x/dx, i) / scale;
float nz = dz * _cnoise(z/dz, x/dx, i + count) / scale;
float fz = fmod(z - nz, dz) / sdz;
float fx = fmod(x - nx, dx) / sdx;
if( fz >= 1 || fz < 0 || fx < 0 || fx >= 1) {
// we must calc only one strand in [0, 1)
return 0;
}
float sc = _strand_curve(fx);
float f = sin((fz - sc) * M_PI_2) * slope;
float n = _cnoise(z / dz, x / dx, i + count * 2);
f *= n;
f = min(max(f, 0), 1);
f = f * pow(1 - fz, 2);
#ifdef DEBUG
if( abs(fz - sc) <= 0.001) {
f = 1;
}
_show_1cell(fz, fx, f);
#endif
return f;
}
shader toon_fur_shader(
float z = 0 [[
string label = "Z"
]],
float x = 0 [[
string label = "X"
]],
float scale = 1 [[
string label = "Scale"
]],
float slope = 10 [[
string label = "Slope"
]],
int count = 1 [[
string label = "Count"
]],
output float out = 0 [[
string label = "Factor"
]]
)
{
float dz = scale;
float dx = scale;
int cellScale = 2;
for (int m = 0; m < 2; ++m) {
for (int n = 0; n < 2; ++n) {
for (int i = 0; i < count; ++i) {
out = max(_calc_strand(
dz, dx,
z + m * dz / 2,
x + n * dx / 2,
cellScale,
i + count * n + count * 2 * m,
slope,
count
), out);
}
}
}
#ifdef DEBUG
_show_1cell(fmod(z, dz), fmod(x, dx), out);
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment