forked from 1b-agent/opencadstudio-upstream
feat(gradient): selectable gradient types + working creation
GRADIENT silently degraded to a plain hatch: add_hatch rebuilt the render model from the DXF entity but never encoded the gradient on it, so the committed fill lost its gradient entirely. The entity now carries the full gradient record (name, angle, colour stops, solid-fill flag), and the standard shape set became selectable: Linear / Cylinder / Spherical / Hemispherical / Curved plus an Invert toggle, offered as prompt buttons on the GRADIENT command and as a Gradient type dropdown + Invert box in the properties panel. All nine standard DXF names round-trip (inverted linear persists by swapping the colour stops), and the shaders shape each kind distinctly — mirrored cylinder, eased curve, dome-profiled hemisphere — on both the native and web pipelines without changing the GPU struct sizes. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9bfcad18b9
commit
930d8c51ea
8 files changed
+290
-32
No files matched your search
+48
-5
@@ -1418,14 +1418,13 @@ impl Scene {
|
||||
gradient_color1 = stop(0);
|
||||
let color2 = stop(1).unwrap_or(color);
|
||||
let angle_deg = dxf.pattern_angle.to_degrees() as f32;
|
||||
// Spherical / hemispherical / curved names are radial fills; linear
|
||||
// and cylinder names run along the angle.
|
||||
let n = dxf.gradient_color.name.to_ascii_uppercase();
|
||||
let radial = n.contains("SPHERICAL") || n.contains("CURVED");
|
||||
let (kind, invert) =
|
||||
model::hatch_model::GradientKind::from_name(&dxf.gradient_color.name);
|
||||
model::hatch_model::HatchPattern::Gradient {
|
||||
angle_deg,
|
||||
color2,
|
||||
radial,
|
||||
kind,
|
||||
invert,
|
||||
}
|
||||
} else if dxf.is_solid {
|
||||
model::hatch_model::HatchPattern::Solid
|
||||
@@ -1885,6 +1884,50 @@ impl Scene {
|
||||
} else {
|
||||
1.0
|
||||
};
|
||||
// A gradient fill must be encoded on the DXF entity itself: the render
|
||||
// model is rebuilt from the entity below (`add_entity` →
|
||||
// `hatch_model_from_dxf`), so a gradient kept only on the command's
|
||||
// model silently degraded to a plain pattern hatch.
|
||||
if let crate::scene::model::hatch_model::HatchPattern::Gradient {
|
||||
angle_deg,
|
||||
color2,
|
||||
kind,
|
||||
invert,
|
||||
} = &model.pattern
|
||||
{
|
||||
let to_color = |c: [f32; 4]| acadrust::types::Color::Rgb {
|
||||
r: (c[0] * 255.0).round().clamp(0.0, 255.0) as u8,
|
||||
g: (c[1] * 255.0).round().clamp(0.0, 255.0) as u8,
|
||||
b: (c[2] * 255.0).round().clamp(0.0, 255.0) as u8,
|
||||
};
|
||||
dxf.is_solid = true;
|
||||
dxf.gradient_color.enabled = true;
|
||||
dxf.gradient_color.name = kind.dxf_name(*invert).to_string();
|
||||
// The render model reads the gradient angle from pattern_angle
|
||||
// (radians); the gradient record keeps its own copy for the file.
|
||||
dxf.pattern_angle = (*angle_deg as f64).to_radians();
|
||||
dxf.gradient_color.angle = (*angle_deg as f64).to_radians();
|
||||
dxf.gradient_color.is_single_color = false;
|
||||
// Linear has no INV name in the standard set: persist an inverted
|
||||
// linear by swapping the colour stops instead.
|
||||
let (c0, c1) =
|
||||
if *invert && matches!(kind, crate::scene::model::hatch_model::GradientKind::Linear)
|
||||
{
|
||||
(*color2, model.color)
|
||||
} else {
|
||||
(model.color, *color2)
|
||||
};
|
||||
dxf.gradient_color.colors = vec![
|
||||
acadrust::entities::hatch::GradientColorEntry {
|
||||
value: 0.0,
|
||||
color: to_color(c0),
|
||||
},
|
||||
acadrust::entities::hatch::GradientColorEntry {
|
||||
value: 1.0,
|
||||
color: to_color(c1),
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
// `add_entity` already builds the render model from the DXF entity via
|
||||
// `hatch_model_from_dxf` and inserts it with a correct `world_origin`
|
||||
|
||||
@@ -29,6 +29,93 @@ pub struct PatFamily {
|
||||
pub dashes: Vec<f32>,
|
||||
}
|
||||
|
||||
/// The standard gradient fill shapes. Spherical / Hemispherical run from the
|
||||
/// boundary centre outward; the others run along `angle_deg`.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum GradientKind {
|
||||
Linear,
|
||||
Cylinder,
|
||||
Spherical,
|
||||
Hemispherical,
|
||||
Curved,
|
||||
}
|
||||
|
||||
impl GradientKind {
|
||||
pub const ALL: [GradientKind; 5] = [
|
||||
GradientKind::Linear,
|
||||
GradientKind::Cylinder,
|
||||
GradientKind::Spherical,
|
||||
GradientKind::Hemispherical,
|
||||
GradientKind::Curved,
|
||||
];
|
||||
|
||||
/// Radial fills shade from the boundary centre outward.
|
||||
pub fn radial(self) -> bool {
|
||||
matches!(self, GradientKind::Spherical | GradientKind::Hemispherical)
|
||||
}
|
||||
|
||||
pub fn label(self) -> &'static str {
|
||||
match self {
|
||||
GradientKind::Linear => "Linear",
|
||||
GradientKind::Cylinder => "Cylinder",
|
||||
GradientKind::Spherical => "Spherical",
|
||||
GradientKind::Hemispherical => "Hemispherical",
|
||||
GradientKind::Curved => "Curved",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_label(label: &str) -> Option<Self> {
|
||||
Self::ALL.iter().copied().find(|k| k.label() == label)
|
||||
}
|
||||
|
||||
/// Parse the DXF gradient name (`LINEAR`, `INVCYLINDER`, …) into the kind
|
||||
/// plus its inverted flag. Unknown names read as Linear.
|
||||
pub fn from_name(name: &str) -> (Self, bool) {
|
||||
let n = name.trim().to_ascii_uppercase();
|
||||
let invert = n.starts_with("INV");
|
||||
let base = n.trim_start_matches("INV");
|
||||
let kind = if base.contains("CYL") {
|
||||
GradientKind::Cylinder
|
||||
} else if base.contains("HEMI") {
|
||||
GradientKind::Hemispherical
|
||||
} else if base.contains("SPHER") {
|
||||
GradientKind::Spherical
|
||||
} else if base.contains("CURV") {
|
||||
GradientKind::Curved
|
||||
} else {
|
||||
GradientKind::Linear
|
||||
};
|
||||
(kind, invert)
|
||||
}
|
||||
|
||||
/// The DXF gradient name. Linear has no INV variant in the standard set —
|
||||
/// an inverted linear is persisted by swapping the colour stops instead.
|
||||
pub fn dxf_name(self, invert: bool) -> &'static str {
|
||||
match (self, invert) {
|
||||
(GradientKind::Linear, _) => "LINEAR",
|
||||
(GradientKind::Cylinder, false) => "CYLINDER",
|
||||
(GradientKind::Cylinder, true) => "INVCYLINDER",
|
||||
(GradientKind::Spherical, false) => "SPHERICAL",
|
||||
(GradientKind::Spherical, true) => "INVSPHERICAL",
|
||||
(GradientKind::Hemispherical, false) => "HEMISPHERICAL",
|
||||
(GradientKind::Hemispherical, true) => "INVHEMISPHERICAL",
|
||||
(GradientKind::Curved, false) => "CURVED",
|
||||
(GradientKind::Curved, true) => "INVCURVED",
|
||||
}
|
||||
}
|
||||
|
||||
/// Shape selector for the GPU shader (`grad_kind` low bits).
|
||||
pub fn shader_kind(self) -> u32 {
|
||||
match self {
|
||||
GradientKind::Linear => 0,
|
||||
GradientKind::Cylinder => 1,
|
||||
GradientKind::Spherical => 2,
|
||||
GradientKind::Hemispherical => 3,
|
||||
GradientKind::Curved => 4,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Hatch fill pattern.
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum HatchPattern {
|
||||
@@ -36,13 +123,13 @@ pub enum HatchPattern {
|
||||
Solid,
|
||||
/// One or more line families (PAT format).
|
||||
Pattern(Vec<PatFamily>),
|
||||
/// Two-stop gradient from `color` to `color2`. Linear along `angle_deg`
|
||||
/// unless `radial`, in which case it runs from the boundary centre outward
|
||||
/// (spherical / hemispherical / curved fills).
|
||||
/// Two-stop gradient from `color` to `color2`, shaped by `kind`; `invert`
|
||||
/// swaps the two stops.
|
||||
Gradient {
|
||||
angle_deg: f32,
|
||||
color2: [f32; 4],
|
||||
radial: bool,
|
||||
kind: GradientKind,
|
||||
invert: bool,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -85,7 +85,8 @@ pub struct HatchInstance {
|
||||
/// an instance whose boundary failed to tessellate); 0 = the rasterized
|
||||
/// triangles already bound the fill, skip the test.
|
||||
pub poly_test: u32, // 116
|
||||
pub _pad1: u32, // 120
|
||||
/// Gradient shape (`GradientKind::shader_kind`), bit 4 = inverted stops.
|
||||
pub grad_kind: u32, // 120
|
||||
pub _pad2: u32, // 124
|
||||
}
|
||||
|
||||
@@ -223,10 +224,12 @@ impl HatchGpu {
|
||||
let family_offset = families.len() as u32;
|
||||
let mut family_count = 0u32;
|
||||
|
||||
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, radial } => {
|
||||
if *radial {
|
||||
HatchPattern::Gradient { angle_deg, color2, kind, invert } => {
|
||||
grad_kind = kind.shader_kind() | if *invert { 16 } else { 0 };
|
||||
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.
|
||||
@@ -319,7 +322,7 @@ impl HatchGpu {
|
||||
family_count,
|
||||
draw_depth: h.draw_depth,
|
||||
poly_test: 1,
|
||||
_pad1: 0,
|
||||
grad_kind,
|
||||
_pad2: 0,
|
||||
});
|
||||
continue;
|
||||
@@ -367,7 +370,7 @@ impl HatchGpu {
|
||||
// boundary complexity, so a hatch-dense drawing can't overflow
|
||||
// the device buffer limit. Each instance draws its AABB quad.
|
||||
poly_test: 1,
|
||||
_pad1: 0,
|
||||
grad_kind,
|
||||
_pad2: 0,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -105,16 +105,19 @@ impl HatchWebGpu {
|
||||
bgl1: &wgpu::BindGroupLayout,
|
||||
) -> Self {
|
||||
// ── Decode pattern mode (mirrors WipeoutGpu::new) ─────────────────
|
||||
// The gradient shape (kind + invert bit) rides in the mode's high
|
||||
// bits — the 96-byte uniform has no spare slot.
|
||||
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, radial } => {
|
||||
if *radial {
|
||||
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.
|
||||
(3u32, *color2, 0.0, 0.0)
|
||||
(3u32 | gk, *color2, 0.0, 0.0)
|
||||
} else {
|
||||
let r = angle_deg.to_radians();
|
||||
(2u32, *color2, r.cos(), r.sin())
|
||||
(2u32 | gk, *color2, r.cos(), r.sin())
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user