forked from 1b-agent/opencadstudio-upstream
feat(gradient): align editing and rendering
This commit is contained in:
commit
2a852f4479
37 files changed
+539
-371
No files matched your search
@@ -262,21 +262,26 @@ impl StorageHatchBatch {
|
||||
let mut grad_kind = 0u32;
|
||||
let (mode, color2, grad_cos, grad_sin, grad_min, grad_range) = match &h.pattern {
|
||||
HatchPattern::Solid => (1u32, [0.0; 4], 0.0, 0.0, 0.0, 1.0),
|
||||
HatchPattern::Gradient { angle_deg, color2, kind, invert } => {
|
||||
HatchPattern::Gradient {
|
||||
angle_deg,
|
||||
color2,
|
||||
kind,
|
||||
invert,
|
||||
shift,
|
||||
} => {
|
||||
grad_kind = kind.shader_kind() | if *invert { 16 } else { 0 };
|
||||
let frame = h.gradient_frame(*angle_deg, *shift);
|
||||
if kind.radial() {
|
||||
// Radial fill: the boundary is stored relative to its
|
||||
// centre (`world_origin`), so the centre is the local
|
||||
// origin; radius = the farthest boundary vertex. mode 3.
|
||||
let radius = radial_radius(&h.boundary);
|
||||
(3u32, *color2, 0.0, 0.0, 0.0, radius)
|
||||
let center = frame.map_or([0.0, 0.0], |frame| {
|
||||
[frame.center[0] as f32, frame.center[1] as f32]
|
||||
});
|
||||
let radius = frame.map_or(1.0, |frame| frame.radius as f32);
|
||||
(3u32, *color2, center[0], center[1], 0.0, radius)
|
||||
} else {
|
||||
let r = angle_deg.to_radians();
|
||||
// Gradient projection range (proj_min / proj_range) —
|
||||
// computed at upload time, identical to per-hatch path.
|
||||
let (gmin, gmax) = boundary_projection_range(&h.boundary, r);
|
||||
let grange = (gmax - gmin).max(1.0);
|
||||
(2u32, *color2, r.cos(), r.sin(), gmin, grange)
|
||||
let minimum = frame.map_or(0.0, |frame| frame.projection_min as f32);
|
||||
let span = frame.map_or(1.0, |frame| frame.projection_span as f32);
|
||||
(2u32, *color2, r.cos(), r.sin(), minimum, span)
|
||||
}
|
||||
}
|
||||
HatchPattern::Pattern(fams) => {
|
||||
@@ -635,42 +640,6 @@ impl LineFamilyGpu {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Helpers ───────────────────────────────────────────────────────────────
|
||||
|
||||
/// Project every boundary vertex onto the gradient direction
|
||||
/// `(cos θ, sin θ)` and return the (min, max) projection. Used to set
|
||||
/// up the gradient's normalized parameter range. Same math as the
|
||||
/// per-hatch path; duplicated here to keep this module self-contained.
|
||||
fn boundary_projection_range(boundary: &[[f32; 2]], theta: f32) -> (f32, f32) {
|
||||
let (cs, sn) = (theta.cos(), theta.sin());
|
||||
let mut lo = f32::INFINITY;
|
||||
let mut hi = f32::NEG_INFINITY;
|
||||
for &[x, y] in boundary {
|
||||
if !x.is_finite() || !y.is_finite() {
|
||||
continue;
|
||||
}
|
||||
let p = x * cs + y * sn;
|
||||
if p < lo { lo = p; }
|
||||
if p > hi { hi = p; }
|
||||
}
|
||||
if !lo.is_finite() {
|
||||
return (0.0, 1.0);
|
||||
}
|
||||
(lo, hi)
|
||||
}
|
||||
|
||||
/// Radius of a radial gradient — the distance from the boundary centre (the
|
||||
/// local origin, since vertices are stored relative to it) to the farthest
|
||||
/// vertex, so `t = 1` (the end colour) reaches the corners.
|
||||
fn radial_radius(boundary: &[[f32; 2]]) -> f32 {
|
||||
boundary
|
||||
.iter()
|
||||
.filter(|p| p[0].is_finite() && p[1].is_finite())
|
||||
.map(|p| (p[0] * p[0] + p[1] * p[1]).sqrt())
|
||||
.fold(0.0_f32, f32::max)
|
||||
.max(1.0)
|
||||
}
|
||||
|
||||
// PatFamily is re-exported by hatch_model so we don't need to import
|
||||
// it explicitly anywhere else — but rust needs the type referenced to
|
||||
// confirm the layout assumption above.
|
||||
|
||||
@@ -165,7 +165,7 @@ impl TextureHatch {
|
||||
let (mode, color2, grad_cos, grad_sin) = match &model.pattern {
|
||||
HatchPattern::Solid => (1u32, [0.0f32; 4], 0.0f32, 0.0f32),
|
||||
HatchPattern::Pattern(_) => (0u32, [0.0f32; 4], 0.0f32, 0.0f32),
|
||||
HatchPattern::Gradient { angle_deg, color2, kind, invert } => {
|
||||
HatchPattern::Gradient { angle_deg, color2, kind, invert, .. } => {
|
||||
let gk = (kind.shader_kind() | if *invert { 16 } else { 0 }) << 8;
|
||||
if kind.radial() {
|
||||
// Radial: centre is the local origin; grad_cos/sin unused.
|
||||
@@ -191,7 +191,6 @@ impl TextureHatch {
|
||||
}
|
||||
|
||||
let origin = model.world_origin;
|
||||
let drift = [0.0f32, 0.0f32];
|
||||
let (mesh_points, indices) = model.fill_mesh();
|
||||
if mesh_points.is_empty() || indices.is_empty() {
|
||||
return None;
|
||||
@@ -239,33 +238,31 @@ impl TextureHatch {
|
||||
});
|
||||
|
||||
// ── Gradient projection range (snapped-local space) ───────────────
|
||||
let (grad_min, grad_range) = if mode == 2 {
|
||||
let projs: Vec<f32> = model
|
||||
.boundary
|
||||
.iter()
|
||||
.filter(|v| v[0].is_finite() && v[1].is_finite())
|
||||
.map(|&[x, y]| (x + drift[0]) * grad_cos + (y + drift[1]) * grad_sin)
|
||||
.collect();
|
||||
if projs.is_empty() {
|
||||
(0.0, 1.0)
|
||||
} else {
|
||||
let proj_min = projs.iter().cloned().fold(f32::INFINITY, f32::min);
|
||||
let proj_max = projs.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
|
||||
// Floor matches the storage backend.
|
||||
(proj_min, (proj_max - proj_min).max(1.0))
|
||||
}
|
||||
} else if mode == 3 {
|
||||
// Radial: range = the farthest boundary vertex from the centre.
|
||||
let radius = model
|
||||
.boundary
|
||||
.iter()
|
||||
.filter(|v| v[0].is_finite() && v[1].is_finite())
|
||||
.map(|&[x, y]| (x * x + y * y).sqrt())
|
||||
.fold(0.0_f32, f32::max)
|
||||
.max(1.0);
|
||||
(0.0, radius)
|
||||
let base_mode = mode & 0xFF;
|
||||
let frame = match &model.pattern {
|
||||
HatchPattern::Gradient {
|
||||
angle_deg, shift, ..
|
||||
} => model.gradient_frame(*angle_deg, *shift),
|
||||
_ => None,
|
||||
};
|
||||
let (grad_min, grad_range, radial_center) = if base_mode == 2 {
|
||||
frame.map_or((0.0, 1.0, [0.0, 0.0]), |frame| {
|
||||
(
|
||||
frame.projection_min as f32,
|
||||
frame.projection_span as f32,
|
||||
[0.0, 0.0],
|
||||
)
|
||||
})
|
||||
} else if base_mode == 3 {
|
||||
frame.map_or((0.0, 1.0, [0.0, 0.0]), |frame| {
|
||||
(
|
||||
0.0,
|
||||
frame.radius as f32,
|
||||
[frame.center[0] as f32, frame.center[1] as f32],
|
||||
)
|
||||
})
|
||||
} else {
|
||||
(0.0, 1.0)
|
||||
(0.0, 1.0, [0.0, 0.0])
|
||||
};
|
||||
|
||||
// ── Pack the data texture: families | dashes ─────────────────────
|
||||
@@ -355,8 +352,8 @@ impl TextureHatch {
|
||||
// Clamp like the desktop renderer so scale==0 can't make perp_step 0
|
||||
// → round(perp/0)=NaN → an invisible hatch.
|
||||
scale: model.scale.max(1e-6),
|
||||
grad_cos,
|
||||
grad_sin,
|
||||
grad_cos: if base_mode == 3 { radial_center[0] } else { grad_cos },
|
||||
grad_sin: if base_mode == 3 { radial_center[1] } else { grad_sin },
|
||||
grad_min,
|
||||
grad_range,
|
||||
origin: [origin[0] as f32, origin[1] as f32],
|
||||
|
||||
Reference in new issue
Block a user