fix(linetype): A-type endpoint alignment so dashed lines end on a dash

Dashed/dash-dot lines could end in a gap ("empty space at the end"),
especially inside paperspace viewports. The dash pattern was only centred
on each wire, which makes the two ends symmetric but does not force them
onto a solid dash.

Implement AutoCAD "A" (aligned) linetypes: the line begins AND ends on a
solid dash, interior dashes stay nominal, and the two end dashes stretch
symmetrically to absorb the remainder (so parallel lines share an
identical interior). wire_distances computes the end-dash length D and
total length L for a dash-first pattern (D = (L - k*P + a) / 2); the GPU
in_dash walk force-lights the two end regions and phases the interior to
resume right after the start dash. Lines shorter than one pattern period
draw solid.

Only alternating dash-first patterns are aligned; gap/dot-first,
consecutive-dash, NaN-break (plinegen=false) and solid lines keep the
prior behaviour. Native and web (wasm) shaders kept in sync.

Refs #13.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Hakan SevenandClaude Opus 4.8 committed 2026-07-13 23:50:25 +03:00
1 parent c0844ad55c
commit 9b1a495549
3 files changed
+162 -50

No files matched your search

+35 -10
View File
@@ -56,6 +56,9 @@ struct InstanceIn {
// Double-single low residuals of the endpoints.
@location(10) pos_a_low: vec3<f32>,
@location(11) pos_b_low: vec3<f32>,
// "A"-type endpoint alignment: end-dash length + total wire length.
@location(12) align_end: f32,
@location(13) align_total: f32,
}
// Draw-order depth bias: shifts clip-space z so 2D entities of different
@@ -77,6 +80,8 @@ struct VertexOut {
// projects below one pixel on screen. See the LOD branch in
// `fs_main`.
@location(5) @interpolate(flat) min_elem: f32,
@location(6) @interpolate(flat) align_end: f32,
@location(7) @interpolate(flat) align_total: f32,
}
@vertex fn vs_main(@builtin(vertex_index) vid: u32, in: InstanceIn) -> VertexOut {
@@ -153,18 +158,13 @@ struct VertexOut {
out.pat0 = in.pat0;
out.pat1 = in.pat1;
out.min_elem = min_elem;
out.align_end = in.align_end;
out.align_total = in.align_total;
return out;
}
// Returns true if arc-length `dist` falls inside a dash or on a dot.
fn in_dash(dist: f32, pat_len: f32, p0: vec4<f32>, p1: vec4<f32>) -> bool {
let d = ((dist % pat_len) + pat_len) % pat_len;
var pos = 0.0f;
// A dot is a zero-length element: render it as a fixed ~1.5 px mark
// (half-width ~0.75 px in world units) so it stays visible at any zoom
// instead of vanishing with its zero world-length. Mirrors the hatch
// shader's pixel-snapped dot. (#149)
let dot_half = u.world_per_pixel * 0.75;
fn in_dash(dist: f32, pat_len: f32, p0: vec4<f32>, p1: vec4<f32>, align_end: f32, align_total: f32) -> bool {
let elems = array<f32, 8>(p0.x, p0.y, p0.z, p0.w, p1.x, p1.y, p1.z, p1.w);
// Real element count = (index of last non-zero) + 1. Trailing 0.0 slots
// are padding; a 0.0 within this range is a real dot.
@@ -172,6 +172,31 @@ fn in_dash(dist: f32, pat_len: f32, p0: vec4<f32>, p1: vec4<f32>) -> bool {
for (var i = 0u; i < 8u; i++) {
if elems[i] != 0.0 { count = i + 1u; }
}
var d: f32;
if align_total > 0.0 {
// "A"-type alignment: the line begins and ends with a solid dash of
// length `align_end`. Force the two end regions lit, then phase the
// interior so the element AFTER the first dash resumes exactly at
// `align_end` (the interior meets each end dash on a gap boundary).
if dist <= align_end || dist >= align_total - align_end {
return true;
}
var first_dash = 0.0;
for (var i = 0u; i < count; i++) {
if elems[i] > 0.0 { first_dash = elems[i]; break; }
}
d = ((dist - align_end + first_dash) % pat_len + pat_len) % pat_len;
} else {
d = ((dist % pat_len) + pat_len) % pat_len;
}
var pos = 0.0f;
// A dot is a zero-length element: render it as a fixed ~1.5 px mark
// (half-width ~0.75 px in world units) so it stays visible at any zoom
// instead of vanishing with its zero world-length. Mirrors the hatch
// shader's pixel-snapped dot. (#149)
let dot_half = u.world_per_pixel * 0.75;
for (var i = 0u; i < count; i++) {
let elem = elems[i];
if elem == 0.0 {
@@ -197,7 +222,7 @@ fn in_dash(dist: f32, pat_len: f32, p0: vec4<f32>, p1: vec4<f32>) -> bool {
// test and return solid colour — also saves the per-fragment
// arc-length math + `discard`.
if in.min_elem >= u.world_per_pixel {
if !in_dash(in.distance, in.pattern_length, in.pat0, in.pat1) {
if !in_dash(in.distance, in.pattern_length, in.pat0, in.pat1, in.align_end, in.align_total) {
discard;
}
}
@@ -213,7 +238,7 @@ fn in_dash(dist: f32, pat_len: f32, p0: vec4<f32>, p1: vec4<f32>) -> bool {
@fragment fn fs_black(in: VertexOut) -> @location(0) vec4<f32> {
if in.pattern_length > 0.0 {
if in.min_elem >= u.world_per_pixel {
if !in_dash(in.distance, in.pattern_length, in.pat0, in.pat1) {
if !in_dash(in.distance, in.pattern_length, in.pat0, in.pat1, in.align_end, in.align_total) {
discard;
}
}
+33 -7
View File
@@ -28,7 +28,11 @@ struct WireConst {
half_width: f32,
pattern_length: f32,
draw_depth: f32,
_pad: f32,
align_end: f32,
align_total: f32,
_pad0: f32,
_pad1: f32,
_pad2: f32,
}
@group(1) @binding(0) var<storage, read> wire_consts: array<WireConst>;
@@ -52,6 +56,8 @@ struct VertexOut {
@location(3) pat0: vec4<f32>,
@location(4) pat1: vec4<f32>,
@location(5) @interpolate(flat) min_elem: f32,
@location(6) @interpolate(flat) align_end: f32,
@location(7) @interpolate(flat) align_total: f32,
}
@vertex fn vs_main(@builtin(vertex_index) vid: u32, in: InstanceIn) -> VertexOut {
@@ -111,18 +117,38 @@ struct VertexOut {
out.pat0 = c.pat0;
out.pat1 = c.pat1;
out.min_elem = min_elem;
out.align_end = c.align_end;
out.align_total = c.align_total;
return out;
}
fn in_dash(dist: f32, pat_len: f32, p0: vec4<f32>, p1: vec4<f32>) -> bool {
let d = ((dist % pat_len) + pat_len) % pat_len;
var pos = 0.0f;
let dot_half = u.world_per_pixel * 0.75;
fn in_dash(dist: f32, pat_len: f32, p0: vec4<f32>, p1: vec4<f32>, align_end: f32, align_total: f32) -> bool {
let elems = array<f32, 8>(p0.x, p0.y, p0.z, p0.w, p1.x, p1.y, p1.z, p1.w);
var count = 0u;
for (var i = 0u; i < 8u; i++) {
if elems[i] != 0.0 { count = i + 1u; }
}
var d: f32;
if align_total > 0.0 {
// "A"-type alignment: the line begins and ends with a solid dash of
// length `align_end`. Force the two end regions lit, then phase the
// interior so the element AFTER the first dash resumes exactly at
// `align_end` (the interior meets each end dash on a gap boundary).
if dist <= align_end || dist >= align_total - align_end {
return true;
}
var first_dash = 0.0;
for (var i = 0u; i < count; i++) {
if elems[i] > 0.0 { first_dash = elems[i]; break; }
}
d = ((dist - align_end + first_dash) % pat_len + pat_len) % pat_len;
} else {
d = ((dist % pat_len) + pat_len) % pat_len;
}
var pos = 0.0f;
let dot_half = u.world_per_pixel * 0.75;
for (var i = 0u; i < count; i++) {
let elem = elems[i];
if elem == 0.0 {
@@ -141,7 +167,7 @@ fn in_dash(dist: f32, pat_len: f32, p0: vec4<f32>, p1: vec4<f32>) -> bool {
@fragment fn fs_main(in: VertexOut) -> @location(0) vec4<f32> {
if in.pattern_length > 0.0 {
if in.min_elem >= u.world_per_pixel {
if !in_dash(in.distance, in.pattern_length, in.pat0, in.pat1) {
if !in_dash(in.distance, in.pattern_length, in.pat0, in.pat1, in.align_end, in.align_total) {
discard;
}
}
@@ -156,7 +182,7 @@ fn in_dash(dist: f32, pat_len: f32, p0: vec4<f32>, p1: vec4<f32>) -> bool {
@fragment fn fs_black(in: VertexOut) -> @location(0) vec4<f32> {
if in.pattern_length > 0.0 {
if in.min_elem >= u.world_per_pixel {
if !in_dash(in.distance, in.pattern_length, in.pat0, in.pat1) {
if !in_dash(in.distance, in.pattern_length, in.pat0, in.pat1, in.align_end, in.align_total) {
discard;
}
}