1
Fork 0

settled the noisy sea

This commit is contained in:
Andy Killorin 2023-08-27 20:38:53 -05:00
parent 4aeb138913
commit eeae55bb73
No known key found for this signature in database
GPG key ID: 8CB11B45B690DC2A
3 changed files with 114 additions and 11 deletions

View file

@ -1,8 +1,8 @@
var ctx; var ctx;
var image; var image;
var memory; var memory;
const width = 160; const width = 256;
const height = 144; const height = 224;
function blit_frame() { function blit_frame() {
ctx.putImageData(image, 0, 0); ctx.putImageData(image, 0, 0);
@ -48,11 +48,15 @@ async function init() {
ctx.textBaseline = 'top' ctx.textBaseline = 'top'
ctx.textAlign = 'left'; ctx.textAlign = 'left';
var frame = 1;
const render = () => { const render = () => {
frame += 1;
console.log(frame);
instance.exports.frame_entry(); instance.exports.frame_entry();
requestAnimationFrame(render); //requestAnimationFrame(render);
} }
canvas.onclick = render;
render(); render();
} }

49
noisesearch Normal file
View file

@ -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

View file

@ -25,8 +25,8 @@ fn draw_text(text: &str, x: i32, y: i32, size: u8) {
} }
} }
const WIDTH: usize = 160; const WIDTH: usize = 256;
const HEIGHT: usize = 144; const HEIGHT: usize = 224;
#[no_mangle] #[no_mangle]
static mut BUFFER: [u32; WIDTH * HEIGHT] = [0; WIDTH * HEIGHT]; 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]; 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] #[no_mangle]
pub unsafe extern fn frame_entry() { pub unsafe extern fn frame_entry() {
// calling from multiple threads is ub // calling from multiple threads is ub
@ -46,7 +61,7 @@ fn render_frame(buffer: &mut [u32; WIDTH*HEIGHT]) {
if frame == 1 { if frame == 1 {
unsafe { unsafe {
RAND = noise::generate(); RAND = noise::generate(0xB00B5);
} }
} }
let rand = unsafe { let rand = unsafe {
@ -55,15 +70,17 @@ fn render_frame(buffer: &mut [u32; WIDTH*HEIGHT]) {
for y in 0..HEIGHT { for y in 0..HEIGHT {
for x in 0..WIDTH { 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; 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 / 64.0, rand) / 1.0;
n += noise::noise(point / 32.0, rand) / 2.0; n += noise::noise(point / 32.0, rand) / 2.0;
n += noise::noise(point / 16.0, rand) / 4.0; n += noise::noise(point / 16.0, rand) / 4.0;
n += noise::noise(point / 8.0, rand) / 8.0; n += noise::noise(point / 8.0, rand) / 8.0;
n += noise::noise(point / 4.0, rand) / 16.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] = (((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 0xFF00FF00
} else { } else {
0xFFFF0000 0xFFFF0000
@ -72,7 +89,40 @@ fn render_frame(buffer: &mut [u32; WIDTH*HEIGHT]) {
} }
unsafe { blit_frame(); } 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<f32>, 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<f32>, 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)] #[cfg(test)]
@ -92,8 +142,8 @@ mod noise {
pub type PerlinBuf = [u32; 512]; pub type PerlinBuf = [u32; 512];
pub fn generate() -> PerlinBuf { pub fn generate(seed: u32) -> PerlinBuf {
let mut rand = 42424242; let mut rand = seed;
let mut data: PerlinBuf = [0; 512]; let mut data: PerlinBuf = [0; 512];
for item in data.iter_mut() { for item in data.iter_mut() {
rand = xorshift(rand); rand = xorshift(rand);