demo · v131

CAD-style section-plane cutaway

The motivating CAD use case: a 3D model viewer needs to "cut through" geometry with an arbitrary plane (a section view) without uploading or modifying the mesh. With clip_distances the vertex shader writes a signed distance to the plane; fragments on one side are clipped cleanly. Drag the plane.

the wgsl in use

// 1) enable the optional feature when requesting the device
const device = await adapter.requestDevice({
  requiredFeatures: ["clip-distances"]
});

// 2) declare the clip distance in the vertex shader
struct VOut {
  @builtin(position) p: vec4f,
  @builtin(clip_distances) c: array<f32, 1>,
  @location(0) col: vec3f,
}

@vertex fn vs(@location(0) pos: vec3f) -> VOut {
  var o: VOut;
  o.p = mvp * vec4f(pos, 1.0);
  // signed distance from the section plane
  o.c[0] = dot(pos, plane.xyz) + plane.w;
  return o;
}

see also