跳到主要内容
← Signed Distance Field

Signed Distance Field

SDF Subtraction

shader-learning 自学第 5 课:SDF Subtraction

实时渲染 · 2D 片段 GLSL

第 5 课:SDF Subtraction

实现

uniform vec2 iResolution;

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;
}

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

  vec2 center1 = iResolution * vec2(0.4, 0.5);
  vec2 center2 = iResolution * vec2(0.6, 0.5);

  float r1 = iResolution.y * 0.4;
  float r2 = iResolution.y * 0.4;

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

  float t = max(c1,-c2); // subtraction result
  t = clamp(t, 0.0, 1.0);

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