lib8: noise: use wrapping shifts to fix sampling errors

This commit is contained in:
Victoria Fischer 2024-11-24 18:55:11 +01:00
parent 859177a65c
commit facb95c363

View File

@ -25,9 +25,9 @@ const fn P(x: u8) -> u8 {
noise_cube[x as usize] noise_cube[x as usize]
} }
fn inoise8_raw(x: u16, y: u16) -> u8 { fn inoise8_raw(x: u16, y: u16) -> i8 {
let X: u8 = x.unsigned_shr(8) as u8; let X: u8 = x.wrapping_shr(8) as u8;
let Y: u8 = y.unsigned_shr(8) as u8; let Y: u8 = y.wrapping_shr(8) as u8;
let A: u8 = P(X) + Y; let A: u8 = P(X) + Y;
let AA: u8 = P(A); let AA: u8 = P(A);
@ -39,8 +39,8 @@ fn inoise8_raw(x: u16, y: u16) -> u8 {
let mut u: u8 = x as u8; let mut u: u8 = x as u8;
let mut v: u8 = y as u8; let mut v: u8 = y as u8;
let xx: i8 = ((x as u8).unsigned_shr(1) & 0x7f) as i8; let xx: i8 = ((x as u8).wrapping_shr(1) & 0x7f) as i8;
let yy: i8 = ((y as u8).unsigned_shr(1) & 0x7f) as i8; let yy: i8 = ((y as u8).wrapping_shr(1) & 0x7f) as i8;
let N: i8 = 0x80u8 as i8; let N: i8 = 0x80u8 as i8;
u = ease8InOutQuad(u); u = ease8InOutQuad(u);
@ -49,11 +49,11 @@ fn inoise8_raw(x: u16, y: u16) -> u8 {
let X1: i8 = lerp7by8(grad8(P(AA), xx, yy), grad8(P(BA), xx.wrapping_sub(N), yy), u); let X1: i8 = lerp7by8(grad8(P(AA), xx, yy), grad8(P(BA), xx.wrapping_sub(N), yy), u);
let X2: i8 = lerp7by8(grad8(P(AB), xx, yy.wrapping_sub(N)), grad8(P(BB), xx.wrapping_sub(N), yy.wrapping_sub(N)), u); let X2: i8 = lerp7by8(grad8(P(AB), xx, yy.wrapping_sub(N)), grad8(P(BB), xx.wrapping_sub(N), yy.wrapping_sub(N)), u);
return lerp7by8(X1, X2, v) as u8; return lerp7by8(X1, X2, v);
} }
pub fn inoise8(x: i16, y: i16) -> u8 { pub fn inoise8(x: i16, y: i16) -> u8 {
let mut n: i8 = inoise8_raw(x as u16, y as u16) as i8; let mut n: i8 = inoise8_raw(x as u16, y as u16);
n = n.wrapping_add(64); n = n.wrapping_add(64);
return n.saturating_add(n) as u8; return (n as u8).saturating_add(n as u8);
} }