From d770648c2f7acc392370823ba97acea3e42d61d1 Mon Sep 17 00:00:00 2001 From: Jan Hohenheim Date: Sat, 21 Jun 2025 17:07:43 +0200 Subject: [PATCH] Remove unused code in Perlin noise 1D impl (#19765) # Objective - Fix #19759 - The bigger permutation table only comes into play for higher dimensions than 1, when you start doing `PERMUTATION_TABLE[PERMUTATION_TABLE[index] + some_number]` - The bigger permutation table has no mathematical meaning, it's just there to avoid having to write more `& 0xFF` when doing multiple nested lookups in higher dimensions - But we only use 1D Perlin noise for the camera shake because we want the dimensions to be uncorrelated ## Solution - So, we can trim the permutation table down :) - This should be mathematically identical, as a wrapped value will still access the same element as an unwrapped value would in the bigger table - The comment was a bit misleading anyways. "mirror" did not refer to "mirrored values" but to "repeated values". ## Testing - Ran the example. Still behaves like before. --- examples/camera/2d_screen_shake.rs | 34 ++++++++---------------------- 1 file changed, 9 insertions(+), 25 deletions(-) diff --git a/examples/camera/2d_screen_shake.rs b/examples/camera/2d_screen_shake.rs index 0a0aaa780d..d14fdc49ea 100644 --- a/examples/camera/2d_screen_shake.rs +++ b/examples/camera/2d_screen_shake.rs @@ -226,21 +226,22 @@ mod perlin_noise { pub fn generate(x: f32) -> f32 { // Left coordinate of the unit-line that contains the input. - let xi0 = x.floor() as usize; + let x_floor = x.floor() as usize; // Input location in the unit-line. - let xf0 = x - xi0 as f32; + let xf0 = x - x_floor as f32; let xf1 = xf0 - 1.0; // Wrap to range 0-255. - let xi = xi0 & 0xFF; + let xi0 = x_floor & 0xFF; + let xi1 = (x_floor + 1) & 0xFF; // Apply the fade function to the location. let t = fade(xf0).clamp(0.0, 1.0); // Generate hash values for each point of the unit-line. - let h0 = PERMUTATION_TABLE[xi]; - let h1 = PERMUTATION_TABLE[xi + 1]; + let h0 = PERMUTATION_TABLE[xi0]; + let h1 = PERMUTATION_TABLE[xi1]; // Linearly interpolate between dot products of each gradient with its distance to the input location. let a = dot_grad(h0, xf0); @@ -263,8 +264,8 @@ mod perlin_noise { } } - // Perlin noise permutation table, the second half is a mirror of the first half. - const PERMUTATION_TABLE: [u8; 512] = [ + // Perlin noise permutation table. This is a random sequence of the numbers 0-255. + const PERMUTATION_TABLE: [u8; 256] = [ 0x97, 0xA0, 0x89, 0x5B, 0x5A, 0x0F, 0x83, 0x0D, 0xC9, 0x5F, 0x60, 0x35, 0xC2, 0xE9, 0x07, 0xE1, 0x8C, 0x24, 0x67, 0x1E, 0x45, 0x8E, 0x08, 0x63, 0x25, 0xF0, 0x15, 0x0A, 0x17, 0xBE, 0x06, 0x94, 0xF7, 0x78, 0xEA, 0x4B, 0x00, 0x1A, 0xC5, 0x3E, 0x5E, 0xFC, 0xDB, 0xCB, 0x75, @@ -282,23 +283,6 @@ mod perlin_noise { 0x91, 0xEB, 0xF9, 0x0E, 0xEF, 0x6B, 0x31, 0xC0, 0xD6, 0x1F, 0xB5, 0xC7, 0x6A, 0x9D, 0xB8, 0x54, 0xCC, 0xB0, 0x73, 0x79, 0x32, 0x2D, 0x7F, 0x04, 0x96, 0xFE, 0x8A, 0xEC, 0xCD, 0x5D, 0xDE, 0x72, 0x43, 0x1D, 0x18, 0x48, 0xF3, 0x8D, 0x80, 0xC3, 0x4E, 0x42, 0xD7, 0x3D, 0x9C, - 0xB4, 0x97, 0xA0, 0x89, 0x5B, 0x5A, 0x0F, 0x83, 0x0D, 0xC9, 0x5F, 0x60, 0x35, 0xC2, 0xE9, - 0x07, 0xE1, 0x8C, 0x24, 0x67, 0x1E, 0x45, 0x8E, 0x08, 0x63, 0x25, 0xF0, 0x15, 0x0A, 0x17, - 0xBE, 0x06, 0x94, 0xF7, 0x78, 0xEA, 0x4B, 0x00, 0x1A, 0xC5, 0x3E, 0x5E, 0xFC, 0xDB, 0xCB, - 0x75, 0x23, 0x0B, 0x20, 0x39, 0xB1, 0x21, 0x58, 0xED, 0x95, 0x38, 0x57, 0xAE, 0x14, 0x7D, - 0x88, 0xAB, 0xA8, 0x44, 0xAF, 0x4A, 0xA5, 0x47, 0x86, 0x8B, 0x30,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