Repository navigation
Viewport: user channels can be viewed on their own #987
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1153,6 +1153,13 @@ impl Session { | |
| } | ||
| } | ||
| } | ||
|
|
||
| if let ViewMode::Channel(channel) = self.view | ||
| && !channel.is_shaded() | ||
| && !self.doc.project.texture_sets.iter().any(|set| set.has_channel(channel)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check the active texture set when removing the viewed channel. If sets 0 and 1 both contain Check channel membership in the active texture set. Keep the channel view when that set still contains the channel.
|
||
| { | ||
| self.view = ViewMode::Material; | ||
| } | ||
| } | ||
|
|
||
| /// Whether a point edit, an uncommitted path preview, or a document edit can be undone | ||
|
|
@@ -1619,6 +1626,75 @@ impl Session { | |
| Some(out) | ||
| } | ||
|
|
||
| /// Unlit RGBA8 pixels of `channel` on texture set `texture_set` at `extent` (width, height), | ||
| /// reading the set's CPU composite (or the channel's default where unallocated or absent). | ||
| /// Returns None for a missing set, invalid or oversized extent, or allocation failure. | ||
| pub fn channel_view_rgba(&self, texture_set: usize, channel: Channel, extent: (u32, u32)) -> Option<Vec<u8>> { | ||
| use rayon::prelude::*; | ||
|
|
||
| let (columns, rows) = extent; | ||
| if columns == 0 || rows == 0 || columns > texturecraft_core::MAX_RESOLUTION || rows > texturecraft_core::MAX_RESOLUTION { | ||
| return None; | ||
| } | ||
|
|
||
| let cache = self.caches.get(texture_set)?; | ||
| let (width, height) = cache.size; | ||
| if width == 0 || height == 0 { | ||
| return None; | ||
| } | ||
|
|
||
| let pixel_count = usize::try_from(u64::from(columns).checked_mul(u64::from(rows))?).ok()?; | ||
| let byte_count = pixel_count.checked_mul(4)?; | ||
| let kind = channel.kind(); | ||
| let default_pixel = encode_channel_view_pixel(kind, channel.default_value()); | ||
|
|
||
| let mut out = Vec::new(); | ||
| out.try_reserve_exact(byte_count).ok()?; | ||
| out.resize(byte_count, 0); | ||
|
|
||
| let Some(image) = cache.composite.channels.get(&channel) else { | ||
| for pixel in out.as_chunks_mut::<4>().0 { | ||
| *pixel = default_pixel; | ||
| } | ||
| return Some(out); | ||
| }; | ||
|
|
||
| let components = channel.components(); | ||
| let row_bytes = columns as usize * 4; | ||
| out.par_chunks_exact_mut(row_bytes).enumerate().for_each(|(y, row)| { | ||
| let sy = (y as u64 * u64::from(height) / u64::from(rows)) as u32; | ||
| let ty = sy / TILE; | ||
| let mut cached_tx = u32::MAX; | ||
| let mut cached_tile: Option<&[u16]> = None; | ||
|
|
||
| for (x, pixel) in row.as_chunks_mut::<4>().0.iter_mut().enumerate() { | ||
| let sx = (x as u64 * u64::from(width) / u64::from(columns)) as u32; | ||
| let tx = sx / TILE; | ||
| if tx != cached_tx { | ||
| cached_tx = tx; | ||
| cached_tile = image.tile(tx, ty); | ||
| } | ||
| let Some(tile) = cached_tile else { | ||
| *pixel = default_pixel; | ||
| continue; | ||
| }; | ||
|
|
||
| let offset = ((sy % TILE) * TILE + (sx % TILE)) as usize * components; | ||
| let value = if components == 3 { | ||
| [ | ||
| texturecraft_compose::store::decode(kind, tile.get(offset).copied().unwrap_or(0)), | ||
| texturecraft_compose::store::decode(kind, tile.get(offset + 1).copied().unwrap_or(0)), | ||
| texturecraft_compose::store::decode(kind, tile.get(offset + 2).copied().unwrap_or(0)), | ||
| ] | ||
| } else { | ||
| [texturecraft_compose::store::decode(kind, tile.get(offset).copied().unwrap_or(0)); 3] | ||
| }; | ||
| *pixel = encode_channel_view_pixel(kind, value); | ||
| } | ||
| }); | ||
| Some(out) | ||
| } | ||
|
|
||
| /// Mask values at texture-set coordinates in request order; None without an enabled mask | ||
| /// Uncovered and out-of-range texels read zero; neighbourhood filters also read surrounding texels | ||
| pub fn mask_at(&self, id: LayerId, texels: &[(u32, u32)]) -> Option<Vec<f32>> { | ||
|
|
@@ -1663,6 +1739,28 @@ impl Session { | |
| } | ||
| } | ||
|
|
||
| /// Encode a decoded channel sample for the unlit `t_aux` display pass (which shows bytes verbatim) | ||
| fn encode_channel_view_pixel(kind: texturecraft_core::ChannelKind, value: [f32; 3]) -> [u8; 4] { | ||
| let byte = |component: f32| (component.clamp(0.0, 1.0) * 255.0 + 0.5) as u8; | ||
| match kind { | ||
| texturecraft_core::ChannelKind::Color => [ | ||
| byte(texturecraft_texel::linear_to_srgb(value[0].clamp(0.0, 1.0))), | ||
| byte(texturecraft_texel::linear_to_srgb(value[1].clamp(0.0, 1.0))), | ||
| byte(texturecraft_texel::linear_to_srgb(value[2].clamp(0.0, 1.0))), | ||
| u8::MAX, | ||
| ], | ||
| texturecraft_core::ChannelKind::NormalMap => [byte(value[0]), byte(value[1]), byte(value[2]), u8::MAX], | ||
| texturecraft_core::ChannelKind::Scalar => { | ||
| let gray = byte(value[0]); | ||
| [gray, gray, gray, u8::MAX] | ||
| } | ||
| texturecraft_core::ChannelKind::Signed => { | ||
| let gray = byte(value[0] * 0.5 + 0.5); | ||
| [gray, gray, gray, u8::MAX] | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Memory use reported by [`Session::memory`]. | ||
| #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] | ||
| pub struct Memory { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| use serde_json::{Value, json}; | ||
| use texturecraft_engine::core::Channel; | ||
| use texturecraft_engine::geom::primitives; | ||
| use texturecraft_engine::{Session, ViewMode, execute}; | ||
|
|
||
| fn plane_session() -> Session { | ||
| let mut s = Session::new_project("t", primitives::plane(2.0, 1), "builtin:plane", None, 128).unwrap(); | ||
| s.update(); | ||
| s | ||
| } | ||
|
|
||
| #[test] | ||
| fn viewport_channels_and_view_command_accept_present_user_channels() { | ||
| let mut s = plane_session(); | ||
| let before: Vec<Channel> = s.doc.project.texture_set(0).unwrap().viewport_channels().collect(); | ||
| assert!(!before.contains(&Channel::User0)); | ||
|
|
||
| let rejected = execute(&mut s, "viewport.view", &json!({"mode": "user0"})); | ||
| assert!(rejected.is_err(), "User0 is absent from the active texture set"); | ||
| assert_eq!(s.view, ViewMode::Material); | ||
|
|
||
| execute(&mut s, "textureSet.channel.add", &json!({"channel": "user0"})).unwrap(); | ||
| let after: Vec<Channel> = s.doc.project.texture_set(0).unwrap().viewport_channels().collect(); | ||
| assert_eq!(after.last(), Some(&Channel::User0)); | ||
| assert_eq!(&after[..before.len()], before.as_slice()); | ||
|
|
||
| execute(&mut s, "viewport.view", &json!({"mode": "user0"})).unwrap(); | ||
| assert_eq!(s.view, ViewMode::Channel(Channel::User0)); | ||
|
|
||
| // Removing the viewed unshaded channel returns to the lit material view | ||
| execute(&mut s, "textureSet.channel.remove", &json!({"channel": "user0"})).unwrap(); | ||
| assert_eq!(s.view, ViewMode::Material); | ||
| } | ||
|
|
||
| #[test] | ||
| fn user_channel_view_rgba_shows_painted_values_and_updates_on_undo() { | ||
| let mut s = plane_session(); | ||
| execute(&mut s, "textureSet.channel.add", &json!({"channel": "user0"})).unwrap(); | ||
| execute(&mut s, "viewport.view", &json!({"mode": "user0"})).unwrap(); | ||
| s.update(); | ||
|
|
||
| let unpainted = s.channel_view_rgba(0, Channel::User0, (128, 128)).unwrap(); | ||
| let center = (64 * 128 + 64) * 4; | ||
| assert_eq!(&unpainted[center..center + 4], &[0, 0, 0, 255], "default User0 is black"); | ||
|
|
||
| execute(&mut s, "tool.material", &json!({"channel": "user0", "solo": true, "value": 0.8})).unwrap(); | ||
| execute(&mut s, "tool.brush", &json!({"size": 40, "hardness": 100, "flow": 100})).unwrap(); | ||
| execute(&mut s, "tool.strokeUv", &json!({"points": [[0.5, 0.5]]})).unwrap(); | ||
| s.update(); | ||
|
|
||
| let painted = s.channel_view_rgba(0, Channel::User0, (128, 128)).unwrap(); | ||
| assert!( | ||
| painted[center] > 180 && painted[center] == painted[center + 1] && painted[center + 1] == painted[center + 2], | ||
| "painted gray: {:?}", | ||
| &painted[center..center + 4] | ||
| ); | ||
| assert_eq!(painted[center + 3], 255); | ||
| assert_eq!(&painted[0..4], &[0, 0, 0, 255], "unpainted corner stays black"); | ||
|
|
||
| // Undo restores the unpainted channel view | ||
| execute(&mut s, "edit.undo", &Value::Null).unwrap(); | ||
| s.update(); | ||
| let undone = s.channel_view_rgba(0, Channel::User0, (128, 128)).unwrap(); | ||
| assert_eq!(&undone[center..center + 4], &[0, 0, 0, 255]); | ||
|
|
||
| // Invalid or oversized extents fail cleanly without allocating | ||
| assert!(s.channel_view_rgba(0, Channel::User0, (0, 128)).is_none()); | ||
| assert!(s.channel_view_rgba(0, Channel::User0, (u32::MAX, 1)).is_none()); | ||
| assert!(s.channel_view_rgba(99, Channel::User0, (64, 64)).is_none()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn unshaded_color_and_signed_channels_encode_as_display_views() { | ||
| let mut s = plane_session(); | ||
| execute(&mut s, "textureSet.channels", &json!({"add": ["diffuse", "displacement"]})).unwrap(); | ||
| execute(&mut s, "layer.add.fill", &json!({})).unwrap(); | ||
| execute(&mut s, "layer.fill.set", &json!({"channel": "diffuse", "color": [0.25, 0.0, 1.0]})).unwrap(); | ||
| s.update(); | ||
|
|
||
| // Linear 0.25 encodes above mid-gray in sRGB (~137), matching BaseColor display | ||
| let diffuse = s.channel_view_rgba(0, Channel::Diffuse, (64, 64)).unwrap(); | ||
| let mid = (32 * 64 + 32) * 4; | ||
| assert!(diffuse[mid] > 120 && diffuse[mid + 1] == 0 && diffuse[mid + 2] == 255, "sRGB diffuse: {:?}", &diffuse[mid..mid + 4]); | ||
|
|
||
| // Signed displacement 0 maps to mid-gray (~128), matching the Height channel view | ||
| let displacement = s.channel_view_rgba(0, Channel::Displacement, (64, 64)).unwrap(); | ||
| assert!(displacement[mid].abs_diff(128) <= 1, "zero displacement is mid-gray: {:?}", &displacement[mid..mid + 4]); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Restoring a bake-preview view skips the removed-channel check.
If a channel view is active when a bake starts, the bake saves it in
restore. While the bake runs, the guard insync_cacheschecks only the current view, which is Material. If the channel is removed during the bake, the guard does not clear the saved value.bake_preview_endthen restores the removed channel without a membership check. The viewport can show that channel's default values until a latersync_cachescall resets the view.Check the saved channel against the texture sets' channels before
bake_preview_endassignsself.view. Fall back toViewMode::Materialwhen no set contains the channel. Use the same membership test as the guard insync_caches.