From eeae55bb735bdd7b2151ef7814de11da16e938dd Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Sun, 27 Aug 2023 20:38:53 -0500 Subject: [PATCH] settled the noisy sea --- front/index.js | 10 ++++--- noisesearch | 49 ++++++++++++++++++++++++++++++++++ pirates/src/lib.rs | 66 ++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 114 insertions(+), 11 deletions(-) create mode 100644 noisesearch diff --git a/front/index.js b/front/index.js index fa8081a..4b7a1d5 100644 --- a/front/index.js +++ b/front/index.js @@ -1,8 +1,8 @@ var ctx; var image; var memory; -const width = 160; -const height = 144; +const width = 256; +const height = 224; function blit_frame() { ctx.putImageData(image, 0, 0); @@ -48,11 +48,15 @@ async function init() { ctx.textBaseline = 'top' ctx.textAlign = 'left'; + var frame = 1; const render = () => { + frame += 1; + console.log(frame); instance.exports.frame_entry(); - requestAnimationFrame(render); + //requestAnimationFrame(render); } + canvas.onclick = render; render(); } diff --git a/noisesearch b/noisesearch new file mode 100644 index 0000000..99e6a60 --- /dev/null +++ b/noisesearch @@ -0,0 +1,49 @@ +194f +0 +5 + 1 channel islands + 0 isle of wight (bay) + 3 cetltic sea in + 4 celtic peninsula + 4 le havr river +6- + +195f +0 +9 + 1 channel islands + 0 isle of wight + 3 cetltic sea in + 3 celtic peninsula + 1 le havr river + +197f +0 + 0 channel islands + 1 isle of wight + 4 cetltic sea in + 3 celtic peninsula + 0 le havr river + +5EA +11 + 0 channel islands + 1 isle of wight + 2 cetltic sea in + 4 celtic peninsula + 0 le havr river + +5a1704 +7 + 2+1 channel islands + 0 isle of wight (cool bay) + 4 cetltic sea in + 5 celtic peninsula + 3 le havr river + +B00B5 +14 + 3 channel islands + 0 isle of wight + 6 cetltic sea in + 4 celtic peninsula diff --git a/pirates/src/lib.rs b/pirates/src/lib.rs index 4202f6a..b67f846 100644 --- a/pirates/src/lib.rs +++ b/pirates/src/lib.rs @@ -25,8 +25,8 @@ fn draw_text(text: &str, x: i32, y: i32, size: u8) { } } -const WIDTH: usize = 160; -const HEIGHT: usize = 144; +const WIDTH: usize = 256; +const HEIGHT: usize = 224; #[no_mangle] static mut BUFFER: [u32; WIDTH * HEIGHT] = [0; WIDTH * HEIGHT]; @@ -35,6 +35,21 @@ static FRAME: AtomicU32 = AtomicU32::new(0); static mut RAND: noise::PerlinBuf = [0; 512]; +const MAP_WIDTH: usize = 12; +const MAP_HEIGHT: usize = 10; +static MAP: [u8; MAP_WIDTH * MAP_HEIGHT] = [ // should deflate to smaller than bit-unpacking code would + 0,1,1,1,1,1,1,1,1,0,0,0, + 0,1,1,1,1,1,1,1,1,0,0,0, + 0,0,0,1,1,1,1,1,1,1,0,0, + 1,1,1,1,0,1,1,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,1,1,1, + 0,0,0,0,1,0,0,0,0,1,1,1, + 0,0,0,0,1,1,0,0,1,1,1,1, + 1,1,0,0,1,1,1,1,1,1,1,1, + 0,1,1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1,1,1, +]; + #[no_mangle] pub unsafe extern fn frame_entry() { // calling from multiple threads is ub @@ -46,7 +61,7 @@ fn render_frame(buffer: &mut [u32; WIDTH*HEIGHT]) { if frame == 1 { unsafe { - RAND = noise::generate(); + RAND = noise::generate(0xB00B5); } } let rand = unsafe { @@ -55,15 +70,17 @@ fn render_frame(buffer: &mut [u32; WIDTH*HEIGHT]) { for y in 0..HEIGHT { for x in 0..WIDTH { - let point = Vector2::new((x+frame as usize) as f32,y as f32)* 3.0; + let point = Vector2::new(((x as usize + (15 as f32 * 64.0 * (12.0 + 3.0)) as usize + 1630) as f32),(y+ 1830) as f32)* 4.0; let mut n = 0.0; + n += (sample_map_inter(point / 64.0, &MAP)-0.5)* 0.7; n += noise::noise(point / 64.0, rand) / 1.0; n += noise::noise(point / 32.0, rand) / 2.0; n += noise::noise(point / 16.0, rand) / 4.0; n += noise::noise(point / 8.0, rand) / 8.0; n += noise::noise(point / 4.0, rand) / 16.0; + n += noise::noise(point / 2.0, rand) / 32.0; //buffer[y*WIDTH + x] = (((n*0.5+0.5)*256.0) as u32) << 16| 0xFF005000; - buffer[y*WIDTH + x] = if n > 0.002 { + buffer[y*WIDTH + x] = if n > 0.04 { 0xFF00FF00 } else { 0xFFFF0000 @@ -72,7 +89,40 @@ fn render_frame(buffer: &mut [u32; WIDTH*HEIGHT]) { } unsafe { blit_frame(); } - draw_text("hi from rust", 0,100,3); + //draw_text("hi from rust", 0,100,30); +} + +fn sample_map_inter(point: Vector2, map: &[u8]) -> f32 { + let x = libm::floorf(point.x) as usize % MAP_WIDTH; + let y = libm::floorf(point.y) as usize % MAP_HEIGHT; + let p0 = Vector2::new(libm::floorf(point.x), libm::floorf(point.y)); + let p1 = p0 + Vector2::new(1.0, 0.0); + let p2 = p0 + Vector2::new(0.0, 1.0); + let p3 = p0 + Vector2::new(1.0, 1.0); + + let tx = point.x - libm::floorf(point.x); + let ty = point.y - libm::floorf(point.y); + + let top = (1.0 - tx) * sample_map(p0, map) as f32 + tx * sample_map(p1, map) as f32; + let bot = (1.0 - tx) * sample_map(p2, map) as f32 + tx * sample_map(p3, map) as f32; + + (1.0-ty) * top + ty * bot +} + +fn sample_map(point: Vector2, map: &[u8]) -> u8 { + const MARGIN: usize = 3; + let x = libm::floorf(point.x) as usize % (MAP_WIDTH + MARGIN); + let y = libm::floorf(point.y) as usize % (MAP_HEIGHT + MARGIN); + + if x >= MAP_WIDTH || y >= MAP_HEIGHT { + if x-y > 12 { + return 1; + } + return 0; + } + + map[y*MAP_WIDTH + x] + } #[cfg(test)] @@ -92,8 +142,8 @@ mod noise { pub type PerlinBuf = [u32; 512]; - pub fn generate() -> PerlinBuf { - let mut rand = 42424242; + pub fn generate(seed: u32) -> PerlinBuf { + let mut rand = seed; let mut data: PerlinBuf = [0; 512]; for item in data.iter_mut() { rand = xorshift(rand);