learn GLSL from zero
ToolsCreate with shadersShader MakerFree online shader builderToolkitShader works ready for your siteSketcher LabPaint your landing’s hero with shaders3D StudioThe Three.js editor in your browser
GamesPlay with shadersOnomatoShaderType a word, blow it upThe WatcherAn eye that follows youAetherLiquid glass with an eye insideBloomcoreA word grows a garden of lightPyrokineThrow fire at the wallVoltaicThe pyramid throws lightningSurfSurf a shader ocean

Lesson 8 of 10 · Shader patterns

Radial

The star that radiates. Lobes of fire.

In Going polar you used sin(ang) to make slices. Now you use it to deform a circle. If the radius changes with the angle, the circle becomes a shape.

The circle deformed by its own angle.

The trick: you take length(p) as distance from center, and compare it to a radius that oscillates with angle: 0.5 + 0.2 * sin(ang * N). Where N is the number of lobes.

sin(ang * N)The angle goes around one full turn. If you multiply by 5, the sine oscillates 5 times, 5 peaks, 5 valleys, 5 lobes. Change N and you change the shape.
TryChange 5. to 3., 3 arms. To 8., 8 lobes. Try adding + T to the angle to make it spin. Hint: sin(ang * 5. + T)

The code

void main(){
  vec2 p = (2.*gl_FragCoord.xy - R) / min(R.x, R.y);
  float ang = atan(p.y, p.x);
  float dist = length(p);
  float r = 0.5 + 0.25 * sin(ang * 5.);
  float d = dist - r;
  float shape = smoothstep(.02, -.02, d);
  float r2 = 0.2 + 0.1 * sin(ang * 5. + 3.14);
  float inner = smoothstep(.02, -.02, dist - r2);
  float shadow = smoothstep(0.,.3, length(p+vec2(.05,-.07))-r);
  float glow = exp(-max(d,0.)*3.) * .5;
  float core = exp(-dist*3.) * .6;
  vec3 col = vec3(.04,.03,.08) * shadow;
  col += vec3(.95,.4,.7) * shape;
  col += vec3(.4,.1,.6) * inner;
  col += vec3(.9,.3,.5) * glow;
  col += vec3(.95,.8,.9) * core;
  gl_FragColor = vec4(col, 1.);
}
close