Skip to content

Instantly share code, notes, and snippets.

@Meshiest
Created September 20, 2019 03:57
Show Gist options
  • Save Meshiest/12320862d78070df2d1b3cfcd3292985 to your computer and use it in GitHub Desktop.
Save Meshiest/12320862d78070df2d1b3cfcd3292985 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<script src="https://cdn.jsdelivr.net/gh/meshiest/brs-js/dist/dist.js"></script>
<script src="https://cdn.jsdelivr.net/gh/josephg/noisejs/perlin.js"></script>
<a id="anchor" download="generated.brs">Download</a>
<script>
const cake = {
id: 'e7b7ea60-4746-a99a-e46c-e4837dae66ec',
name: 'cake',
};
noise.seed(Math.random());
const size = 600;
const area = size * size;
const brickSize = 10;
const color = (x, y) => {
x /= 60;
y /= 60;
const r = Math.floor(noise.perlin2(x, y) * 80 + 80)
const g = Math.floor(noise.perlin2(x - 3000, y - 3000) * 80 + 80)
const b = Math.floor(noise.perlin2(x + 2000, y + 2000) * 80 + 80)
return [r, g, b, 255];
}
const pos = (x, y) => {
const z = Math.floor(
(noise.perlin2(x / 50, y / 50)) * 300 +
(noise.perlin2(x / 10, y / 10)) * 100 + 400
);
return [
x * brickSize * 2,
y * brickSize * 2,
z,
];
}
const save = {
author: cake,
description: 'generated save',
map: 'JavaScript',
brick_owners: [cake],
bricks: Array.from({length: area})
.map((_, i) => ({x: Math.floor(i % size), y: Math.floor(i / size)}))
.filter(({x, y}) =>
Math.abs(noise.perlin2(x/ 15, y/ 15)) < 0.1 &&
Math.hypot(x-size/2, y-size/2) < size/2 ||
Math.hypot(x-size/2, y-size/2) > size/2 - 5 &&
Math.hypot(x-size/2, y-size/2) < size/2
)
.map(({x, y}) => ({
size: [brickSize, brickSize, brickSize],
color: color(x, y),
position: pos(x, y),
})),
};
// console.log(BRS.read(BRS.write(save)));
console.time('Generate');
const blob = new Blob([new Uint8Array(BRS.write(save))]);
console.timeEnd('Generate');
anchor.href = URL.createObjectURL(blob);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment