Cogwheel brush

To talk about brush creation and controllers
mrange
 
Posts: 7
Joined: 06 Mar 2020, 22:22
Location: Oslo

Cogwheel brush

Postby mrange » 06 Mar 2020, 23:11

I thought I share a cogwheel shader brush for those that are interested.

Code: Select all
// From: http://mercury.sexy/hg_sdf/
float mod1(inout float p, float size) {
  float halfsize = size*0.5;
  float c = floor((p + halfsize)/size);
  p = mod(p + halfsize, size) - halfsize;
  return c;
}

float2 toPolar(float2 p) {
  return float2(length(p), atan2(p.y, p.x));
}

float2 toRect(float2 p) {
  return float2(p.x*cos(p.y), p.x*sin(p.y));
}

// From: http://www.iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm
float unevenCapsule(float2 p, float r1, float r2, float h) {
  p.x = abs(p.x);
  float b = (r1-r2)/h;
  float a = sqrt(1.0-b*b);
  float k = dot(p,float2(-b,a));
  if(k < 0.0) return length(p) - r1;
  if(k > a*h) return length(p-float2(0.0,h)) - r2;
  return dot(p, float2(a,b)) - r1;
}

float circle(float2 p, float r) {
  return length(p) - r;
}

// From: http://www.iquilezles.org/www/articles/smin/smin.htm
float softMin(float a, float b, float k)
{
    float h = clamp(0.5+0.5*(b-a)/k, 0.0, 1.0);
    return lerp(b, a, h) - k*h*(1.0-h);
}

float cogwheel(float2 p, float time) {
  float pi = 3.141592654;
  float tau = 2.0*pi;

  float scale = 2000.0;
  float cogRadius = 0.02*scale;
  float outerRadius = 0.30*scale;
  float innerRadius = 0.25*scale;
  float embeddedRadius = (0.125/2.0)*scale;
  float embeddedOffset = 0.15*scale;
  float smooth = 0.0375*scale;

  float dc = circle(p, innerRadius); 
  float2 pp = toPolar(p);
  pp.y += time*2 + tau/32.0;
  float2 cpp = pp;
  mod1(cpp.y, tau/16.0);
  cpp.y += PI/2.0;
  float2 cp = toRect(cpp);
  float ds = unevenCapsule(cp, 0.05, cogRadius, outerRadius);
  float dcw = softMin(ds, dc, smooth);
//  float dcw = min(ds, dc);
  float dic = circle(p, embeddedRadius);
  float2 ipp = pp;
  mod1(ipp.y, tau/6.0);
  float2 ip = toRect(ipp);
  float dic2 = circle(ip - float2(embeddedOffset, 0.0), embeddedRadius);
  float di = min(dic, dic2);
  return max(dcw, -di);
}

float df(float2 p, float time)
{
  float d =cogwheel(p, time);

  d = abs(d) - 5.0;

  return d;
}

float4 main(idatas i)
{
  float fuzzy = 1.0;

  float d = df(i.pos - i.strokePos, 0.01*i.nbUserStroke);

  float s = smoothstep(-fuzzy, fuzzy, d);
  float4 b = float4(0.0, 0.0, 0.0, 0.0);
  float4 col = lerp(i.color, b, s);
  return col;
}
--
mrange

User avatar
u2bleank
 
Posts: 1115
Joined: 11 Jun 2012, 10:41

Re: Cogwheel brush

Postby u2bleank » 22 Mar 2020, 12:16

I'm really happy that someone start to use the BSL seriously :happy:
Anyway you code produce nothing here. Can you share the .bkbrush ? ( I don't have time now to debug the thing myself now )

mrange
 
Posts: 7
Joined: 06 Mar 2020, 22:22
Location: Oslo

Re: Cogwheel brush

Postby mrange » 22 Mar 2020, 12:24

Hi.

I am unsure what a .bkbrush is? I am quite new to Black Ink so I might have failed to implement the brush in a portable way. You know what they say, works on my machine.
--
mrange

mrange
 
Posts: 7
Joined: 06 Mar 2020, 22:22
Location: Oslo

Re: Cogwheel brush

Postby mrange » 22 Mar 2020, 18:28

Took me some time to figure out how to create a user brush but I think I succeeded now. Share using dropbox link:

https://www.dropbox.com/s/ol6f8g15vdq0u ... brush?dl=0
--
mrange

User avatar
u2bleank
 
Posts: 1115
Joined: 11 Jun 2012, 10:41

Re: Cogwheel brush

Postby u2bleank » 27 Mar 2020, 10:34

it works !

:yeah:
Image


Return to Brush Making