mirror of
https://github.com/storytold/materialcraft.git
synced 2026-11-28 17:19:17 +01:00
Round the projected direction before shearing blur samples
This commit is contained in:
1 parent
4be6490a73
commit
fe78d92ed3
5 files changed
+89
-11
No files matched your search
@@ -21,8 +21,10 @@ pub(super) fn sheared(input: &Image, reach: f32, horizontal: bool, slope: f32, s
|
||||
// The slope is in texels. A full period along the line spans this many transverse UV periods.
|
||||
let uv_slope = slope * (length as f32 / across as f32);
|
||||
let shear = |image: &Image, slope: f32, format| {
|
||||
// Add the two transform columns before translating back to the texture center.
|
||||
let shifted = |along: f32, across: f32| (across + slope * along) - slope * 0.5;
|
||||
Image::generate_uv(width, height, image.kind(), |u, v| {
|
||||
let uv = if horizontal { [u, v + slope * (u - 0.5)] } else { [u + slope * (v - 0.5), v] };
|
||||
let uv = if horizontal { [u, shifted(u, v)] } else { [shifted(v, u), v] };
|
||||
crate::texture_sampler::sample(image, uv, [true; 2], format, None)
|
||||
})
|
||||
};
|
||||
|
||||
@@ -63,19 +63,19 @@ pub(super) fn blur(context: &CookContext, angle: f32) -> CookResult {
|
||||
}
|
||||
if tile == [true; 2] && context.w == input.width() && context.h == input.height() {
|
||||
// Compare the components before rounding the direction to the sample grid.
|
||||
let (sine, cosine) = f64::from(angle * std::f32::consts::TAU).sin_cos();
|
||||
let radians = angle * std::f32::consts::TAU;
|
||||
let (sine, cosine) = f64::from(radians).sin_cos();
|
||||
let horizontal = cosine.abs() / f64::from(context.w) > sine.abs() / f64::from(context.h);
|
||||
let slope = if horizontal { sine / cosine } else { cosine / sine };
|
||||
// An authored eighth turn has an exact unit slope even after radian conversion.
|
||||
let slope = if (angle * 8.0).fract() == 0.0 { slope.signum() as f32 } else { slope as f32 };
|
||||
// On a rectangle the dominant UV direction can have the smaller texel
|
||||
// component. The window spans the projection along the chosen texel axis.
|
||||
let along = if slope.abs() == 1.0 {
|
||||
major
|
||||
} else if horizontal {
|
||||
dx.abs()
|
||||
let (slope, along) = if (angle * 8.0).fract() == 0.0 {
|
||||
((sine * cosine).signum() as f32, major)
|
||||
} else {
|
||||
dy.abs()
|
||||
let sine = projected_sine(radians.abs()) * radians.signum();
|
||||
let cosine = projected_sine(radians.abs() + std::f32::consts::FRAC_PI_2);
|
||||
let (across, along) = if horizontal { (sine, cosine) } else { (cosine, sine) };
|
||||
(across / along, along.abs())
|
||||
};
|
||||
if let Some(image) = box_filter::sheared(&input, (radius - 0.5) * along, horizontal, slope, source_format, context.format) {
|
||||
return Ok(vec![image]);
|
||||
@@ -107,3 +107,25 @@ pub(super) fn blur(context: &CookContext, angle: f32) -> CookResult {
|
||||
pixel
|
||||
})])
|
||||
}
|
||||
|
||||
/// The wide filter stores both direction components before deriving the window and shear.
|
||||
fn projected_sine(radians: f32) -> f32 {
|
||||
use std::f64::consts::{FRAC_PI_2, PI, TAU};
|
||||
|
||||
let phase = f64::from(radians) % TAU;
|
||||
let folded = if phase <= FRAC_PI_2 {
|
||||
phase
|
||||
} else if phase <= 3.0 * FRAC_PI_2 {
|
||||
PI - phase
|
||||
} else {
|
||||
phase - TAU
|
||||
};
|
||||
let x = folded as f32;
|
||||
let square = x * x;
|
||||
// Evaluate the odd sine series on the central half turn. Each product rounds before the next sum.
|
||||
let mut tail = -1.0 / 39_916_800.0;
|
||||
for coefficient in [1.0 / 362_880.0, -1.0 / 5_040.0, 1.0 / 120.0, -1.0 / 6.0] {
|
||||
tail = tail * square + coefficient;
|
||||
}
|
||||
x + (tail * square) * x
|
||||
}
|
||||
@@ -521,6 +521,32 @@ fn oblique_rectangular_blur_projects_the_window_onto_its_filtering_axis() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn long_oblique_blur_keeps_the_projected_stripe_weight() {
|
||||
for (size, angle, strength, vertical_stripe, expected) in [
|
||||
([16, 128], 0.1, 41.96875, true, 0x3e66_e1d7),
|
||||
([128, 16], 0.265625, 46.6875, false, 0x3e30_2ae0),
|
||||
([128, 16], -139.0 / 512.0, 34.89026, false, 0x3e6c_7382),
|
||||
] {
|
||||
let [width, height] = size;
|
||||
let stripe = Image::gray(width, height, |x, y| {
|
||||
let on_line = if vertical_stripe { x == width / 2 } else { y == height / 2 };
|
||||
if on_line { 1.0 } else { 0.0 }
|
||||
});
|
||||
let params = [("intensity", Value::Float(strength)), ("angle", Value::Float(angle))];
|
||||
let image = cook("directional_blur", &[Some(stripe)], ¶ms, size, true);
|
||||
assert_eq!(image.get_gray(i64::from(width / 2), i64::from(height / 2), false).to_bits(), expected, "{angle}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn long_oblique_blur_centers_the_shear_at_texel_precision() {
|
||||
let impulse = Image::gray(512, 256, |x, y| if (x, y) == (11, 254) { 1.0 } else { 0.0 }).with_integer_sampling(true);
|
||||
let params = [("intensity", Value::Float(7.8787503)), ("angle", Value::Float(0.0625))];
|
||||
let result = cook("directional_blur", &[Some(impulse)], ¶ms, [512, 256], true);
|
||||
assert_eq!([253, 254, 255].map(|y| result.get_gray(10, y, false).to_bits()), [0x3d0c_9a80, 0x3ce7_57e2, 0x3bbc_f1c9]);
|
||||
}
|
||||
|
||||
fn signed_modular_field(width: u32, height: u32) -> Image {
|
||||
Image::generate(width, height, Kind::Color, |x, y| {
|
||||
std::array::from_fn(|c| {
|
||||
|
||||
@@ -95,6 +95,34 @@ The independent one-stamp control divides two scalar graph inputs to drive the g
|
||||
|
||||
Separate pairs of overlapping stamps revealed a remaining difference. Draw a negative .25 root square in maximum mode above a positive .25 additive child on black. In the 32 × 16 F32 probe at finite gain −1, MaterialCraft matched 224/512 pixels. The other 288 differed by .25. The same geometry also differs with negative infinite gain. Four RGBA controls with undefined gain still report a local cook error. These results and the finite-roughness differences in the preceding root comparison keep both roots in progress. All 373 evaluation and node release tests, formatting, strict workspace Clippy, dependency layers and WebAssembly checks passed.
|
||||
|
||||
### Direction and sample coordinates of long oblique blurs
|
||||
|
||||
Across the 78 original settings in [the rectangular blur comparison](#oblique-directional-blur-on-rectangular-images), the number of differing color pixels fell from 1,258 to 646 and the number of differing grayscale pixels fell from 438 to 130. Each application reproduced its own original on all 3,489,792 pixels in 60 saved settings per root, maximum error zero. Both roots remain in progress.
|
||||
|
||||
| Root (inventory index) | Distinct settings | U8 exact / total · max | U16 exact / total · max | F16 exact / total · max | F32 exact / total · max |
|
||||
|---|---:|---:|---:|---:|---:|
|
||||
| Blur HQ (55) | 78 | 706,504/706,560 · 1 | 1,169,141/1,169,408 · 257 | 591,816/591,872 · 0.00439453125 | 1,192,949/1,193,216 · 0.0039215982 |
|
||||
| Blur HQ Grayscale (56) | 78 | 706,545/706,560 · 1 | 1,169,358/1,169,408 · 257 | 591,857/591,872 · 0.00390625 | 1,193,166/1,193,216 · 0.0039215982 |
|
||||
|
||||
A single white stripe isolates the projected width. Draw a line through the middle column or row of a black 16-bit input, perpendicular to the filtering axis. Use both 128 × 16 and 16 × 128 orientations for turns .03125, .0625, .1, .125, .1875, .2421875, .265625, .3125, .375, .4375, .5625, −.0625, −.125, −.1875, −.271484375, .0791015625, .1201171875, .2099609375, .337890625, .4033203125, −.3583984375 and .53125. Use radii 2.18064117, 2.623046875, 2.91796875, 3.17349243, 3.734375, 4.18359375, 4.97953796, 5.2578125, 5.84778023, 6.8125, 7.14850616, 7.5703125, 8.96439362, 9.298828125, 10.65226746, 12.376953125, 13.50491333 and 14.31640625 texels. Multiply by 256 divided by the shorter side for the intensity. Seed is `455833 + 17j + h` for zero-based orientation and radius indices. Across 792 RGBA F32 images, **1,577,984/1,622,016** pixels match, maximum 2.38418579e−7. The same test with four additional turns (37, −119, 303 and −431 divided by 1024) matches all **294,912/294,912** pixels, maximum zero.
|
||||
|
||||
The center weight distinguishes the stored single-precision direction from an unrounded sine and cosine. The measured approximation folds a positive radian phase into the central half turn, evaluates the odd sine series through degree eleven in single precision and uses a quarter-turn shift for cosine. The ratio of the two stored components gives the shear. An off-center impulse distinguishes adding the two matrix terms before subtracting the center translation. At 512 × 256, intensity 7.8787503, 1/16 turn and white at (11,254), the three red-channel F32 words at (10,253) through (10,255) are `3d0c9a80`, `3ce757e2` and `3bbcf1c9`.
|
||||
|
||||
The single-impulse control uses (width, height, turn, radius in texels) of (512,256,.0625,7.8787503), (256,512,.0625,7.8787503), (512,128,.4375,7.0537505), (128,512,.4375,7.0537505) and (256,256,−.0849609375,6.71875). Each has six white-pixel locations at (⌊w/2⌋+5,⌊h/3⌋−1), (⌊w/5⌋+1,⌊h/2⌋+7), (w−3,7), (11,h−2), (⌊w/3⌋+11,⌊h/5⌋+2) and (1,1). Use seed `462021 + 23j + h` for zero-based series and position indices. All **2,752,512/2,752,512** RGBA F32 pixels match across 30 renders, maximum zero.
|
||||
|
||||
The sparse fields use the same tuples with (256,128,.1875,4.8675003) inserted before the last tuple. In filtering-axis coordinates, start the white columns at 3, 10, 16, 23, 25 or 30 and repeat every 32 texels. In zero-based column `i` place a point every 16 rows starting at `(3i + 5h) mod 16` for zero-based field `h`. Use seed `460039 + 17j + h`. F32 matches **2,948,850/2,949,120** pixels in 36 settings, maximum .000616334379, up from 2,555,364 exact. U8 matches **983,040/983,040** in the 12 settings using fields 1 and 4, maximum zero.
|
||||
|
||||
The independent 312-case primitive series from the preceding comparison has the following raw pixel counts.
|
||||
|
||||
| Storage | Exact pixels before | Exact pixels after | Pixels compared | Maximum after |
|
||||
|---|---:|---:|---:|---:|
|
||||
| U8 | 344,024 | 344,030 | 344,064 | 1 |
|
||||
| U16 | 342,166 | 342,561 | 344,064 | 63 |
|
||||
| F16 | 319,077 | 343,693 | 344,064 | .0009765625 |
|
||||
| F32 | 1,545,530 | 1,864,772 | 1,949,696 | .000969707966 |
|
||||
|
||||
Both release regressions failed before the fix and passed afterward. All 320 node release tests, formatting, strict workspace Clippy, dependency layers and WebAssembly checks passed.
|
||||
|
||||
## 2026-11-20 root comparisons
|
||||
|
||||
### Hue, saturation, lightness and Replace Color
|
||||
|
||||
@@ -154,8 +154,8 @@ Creased matches 10,947,840/10,947,840 pixels across 100 distinct settings throug
|
||||
| [blend_switch.sbs/multi_switch_grayscale](../assets/sbs/blend_switch.sbs) | 20 | No | [Parity: 5 cases](parity-fixes.md#verified-cases) |
|
||||
| [blend_switch.sbs/switch](../assets/sbs/blend_switch.sbs) | 2 | No | [Parity: 4 cases](parity-fixes.md#verified-cases) |
|
||||
| [blend_switch.sbs/switch_grayscale](../assets/sbs/blend_switch.sbs) | 2 | No | [Parity: 6 cases](parity-fixes.md#verified-cases) |
|
||||
| [blur_hq.sbs/blur_hq](../assets/sbs/blur_hq.sbs) | 1 | No | [In progress · 3,659,798/3,661,056 pixels across 78 distinct settings](parity-fixes.md#oblique-directional-blur-on-rectangular-images) |
|
||||
| [blur_hq.sbs/blur_hq_grayscale](../assets/sbs/blur_hq.sbs) | 1 | No | [In progress · 3,660,618/3,661,056 pixels across 78 distinct settings](parity-fixes.md#oblique-directional-blur-on-rectangular-images) |
|
||||
| [blur_hq.sbs/blur_hq](../assets/sbs/blur_hq.sbs) | 1 | No | [In progress · 3,660,410/3,661,056 pixels across 78 distinct settings](parity-fixes.md#direction-and-sample-coordinates-of-long-oblique-blurs) |
|
||||
| [blur_hq.sbs/blur_hq_grayscale](../assets/sbs/blur_hq.sbs) | 1 | No | [In progress · 3,660,926/3,661,056 pixels across 78 distinct settings](parity-fixes.md#direction-and-sample-coordinates-of-long-oblique-blurs) |
|
||||
| [bnw_spots_1.sbs/bnw_spots_1](../assets/sbs/bnw_spots_1.sbs) | 0 | Yes | [In progress · 3,177,940/3,177,984 pixels across 125 settings](parity-fixes.md#pixel-center-rounding-at-the-near-stamp-vertex) · [64,000/64,000 downstream pixels](parity-fixes.md#isolating-the-generators-in-spots-bricks-and-cells) · [Circular stamp and opacity controls](parity-fixes.md#circular-stamp-coverage-and-zero-stamp-opacity) |
|
||||
| [bnw_spots_3.sbs/bnw_spots_3](../assets/sbs/bnw_spots_3.sbs) | 0 | Yes | [Parity · 118 settings, 3,159,552/3,159,552 final pixels](parity-fixes.md#pixel-center-rounding-at-the-near-stamp-vertex) · [27,136/27,136 downstream pixels](parity-fixes.md#isolating-the-generators-in-spots-bricks-and-cells) · [Circular stamp and opacity controls](parity-fixes.md#circular-stamp-coverage-and-zero-stamp-opacity) · [Intermediate recheck](parity-fixes.md#interpolated-coordinates-on-clipped-squares-and-quarter-turns) |
|
||||
| [brick_1.sbs/brick_1](../assets/sbs/brick_1.sbs) | 0 | Yes | [Parity · 131 renders, 3,194,368/3,194,368 pixels](parity-fixes.md#fractional-stamp-vertices-and-the-last-covered-pixel) · [64,000/64,000 downstream pixels](parity-fixes.md#isolating-the-generators-in-spots-bricks-and-cells) · [Earlier generator settings](parity-fixes.md#square-and-rectangular-pattern-generators) |
|
||||
|
||||
Reference in new issue
Block a user