From 4557c9b2b8cc6fe57aeba6f62f7c413fe53f4633 Mon Sep 17 00:00:00 2001 From: Hakan Seven Date: Thu, 28 May 2026 22:43:13 +0300 Subject: [PATCH] fix(block): adapt sub-entity colours per render bg MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `tessellate_sub_local` and `build_nested_ref` baked `adapt_to_bg` results into the cached `BlockCache`, keyed against whatever bg was live at cache-build time. Paper-space content viewports render through model content with `paper_bg_color`, but if the cache had been built earlier against `bg_color` (or vice versa, after the recent layout-switch dance) the cached sub-wire colours were already adapted to the wrong bg and `expand_insert` emitted them verbatim. Store the raw `render_style_for` result on `LocalWire.color` / `NestedRef.ins_color` and run `adapt_to_bg` in `Batches::finalize` with the per-render `bg_color` that `expand_insert` already threads through. The same cached defn can now serve renders against any background — paper viewports get `paper_bg`-adapted sub-entities, model renders get `bg_color`-adapted sub-entities, no rebuild required. Double-adaptation (ByBlock sub inheriting an already-adapted `ctx.ins_color` from `tessellate_entity`) is idempotent: a pure white that adapted to black on a white bg stays black through a second adapt against the same bg. --- src/scene/block_cache.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/scene/block_cache.rs b/src/scene/block_cache.rs index bb1b0dc32..d46156e1e 100644 --- a/src/scene/block_cache.rs +++ b/src/scene/block_cache.rs @@ -356,9 +356,12 @@ fn build_nested_ref( doc: &CadDocument, bg_color: [f32; 4], ) -> NestedRef { - let (mut ins_color, ins_pat_len, ins_pat, ins_lw_px, _) = + // Store the RAW colour — `adapt_to_bg` runs at emit time + // (`Batches::finalize`) so the same cached defn can serve renders + // against different backgrounds without rebuilding. + let (ins_color, ins_pat_len, ins_pat, ins_lw_px, _) = crate::scene::render::render_style_for(doc, &EntityType::Insert(nested_ins.clone())); - ins_color = crate::scene::render::adapt_to_bg(ins_color, bg_color); + let _ = bg_color; NestedRef { block_name: nested_ins.block_name.clone(), @@ -391,8 +394,12 @@ fn tessellate_sub_local( return None; } + // Store the RAW colour. `Batches::finalize` applies `adapt_to_bg` + // with the per-render bg, so the cache no longer has to rebuild on + // BACKGROUND / layout-switch — the dynamic adaptation tracks the + // live bg at render time. let (sub_color, pat_len, pat, lw_px, aci) = crate::scene::render::render_style_for(doc, sub); - let sub_color = crate::scene::render::adapt_to_bg(sub_color, bg_color); + let _ = bg_color; let color_is_byblock = sub.common().color == AcadColor::ByBlock; let lt_is_byblock = sub.common().linetype.eq_ignore_ascii_case("byblock"); @@ -655,7 +662,7 @@ pub fn expand_insert( }; expand_defn(defn, &base_xform, &ctx, &mut batches, &mut visited, 0); } - Some(batches.finalize(&name, selected)) + Some(batches.finalize(&name, selected, bg_color)) } /// Emit a greeked filled rect (2 triangles) for a text LocalWire. Stays in @@ -1033,7 +1040,7 @@ impl BatchEntry { } impl Batches { - fn finalize(self, name: &str, selected: bool) -> Vec { + fn finalize(self, name: &str, selected: bool, bg_color: [f32; 4]) -> Vec { self.closed .into_iter() .chain(self.by_style.into_values()) @@ -1043,10 +1050,16 @@ impl Batches { } else { [b.min_x, b.min_y, b.max_x, b.max_y] }; + // RAW colour came from `tessellate_sub_local` (and from + // `expand_defn`'s ByBlock fallbacks); apply `adapt_to_bg` + // now so each render against a different bg gets the + // right pure-black ↔ pure-white flip without rebuilding + // the cached defn. + let color = crate::scene::render::adapt_to_bg(b.color, bg_color); WireModel { name: name.to_string(), points: b.points, - color: b.color, + color, selected, pattern_length: b.pattern_length, pattern: b.pattern,