跳到主要内容
← Signed Distance Field

Signed Distance Field

SDF Soft MinMax

shader-learning 自学第 11 课:SDF Soft MinMax

实时渲染 · 2D 片段 GLSL

第 11 课:SDF Soft MinMax

实现

uniform vec2 iResolution;
uniform float iTime;

const vec3 shapeColor      = vec3(1.0, 0.3, 0.3);
const vec3 backgroundColor = vec3(0.12);

float sdfCircle(vec2 center, float r, vec2 pos) {
  return distance(center, pos) - r;
}

float softMin(float a, float b, float k) {
  float res = exp(-k * a) + exp(-k * b);
  return -log(res) / k;
}

void main() {
  vec2 uv = gl_FragCoord.xy;

  float dx = cos(iTime) * 0.5 + 0.5;
  dx *= 0.3;

  vec2 center1 = iResolution * vec2(0.2 + dx, 0.5);
  vec2 center2 = iResolution * vec2(0.8 - dx, 0.5);

  float r1 = iResolution.x * 0.15;
  float r2 = iResolution.x * 0.15;

  float c1 = sdfCircle(center1, r1, uv);
  float c2 = sdfCircle(center2, r2, uv);

  float t = softMin(c1, c2, 0.025);
  t = clamp(t, 0.0, 1.0);

  gl_FragColor = vec4(mix(shapeColor, backgroundColor, t), 1.0);
}