Skip to content

Instantly share code, notes, and snippets.

@Meshiest
Last active September 19, 2019 05:58
Show Gist options
  • Save Meshiest/0b1a5b3dc4a9337c359e94d33ba322a5 to your computer and use it in GitHub Desktop.
Save Meshiest/0b1a5b3dc4a9337c359e94d33ba322a5 to your computer and use it in GitHub Desktop.
Generate 3d simplex terrain saves for brickadia in your browser!
<!DOCTYPE html>
<script src="https://cdn.jsdelivr.net/npm/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>
// my identity :)
const cake = {
id: 'e7b7ea60-4746-a99a-e46c-e4837dae66ec',
name: 'cake',
};
// more documentation here: https://www.npmjs.com/package/brs-js
noise.seed(Math.random());
const size = 50; // 50x50x50 area
const brickSize = 40; // 8x8 cubes
const pos = [];
// Generate the 3d terrain
for (let x = 0; x < size; x++)
for (let y = 0; y < size; y++)
for (let z = 0; z < size; z++) {
// Generate a random heightmap around halfway through the cube
let height = (noise.simplex2(x / 20, y / 20) * 0.1 + 0.5) * size;
// 75% chance of rendering a cube
// Simplex noise returns a number between -1 and 1, so -1 < x < 0.5 is 3/4 of that space
// only render the cube if it's less than our 2d height
if(noise.simplex3(x / 10, y / 10, z / 10) < 0.5 && z < height)
pos.push({x, y, z}); // add it to our position array
}
const save = {
author: cake,
description: 'generated save',
brick_assets: [
'PB_DefaultBrick',
],
materials: [
'BMC_Plastic',
],
brick_owners: [cake],
bricks: pos
.map(({x, y, z}) => ({
size: [brickSize, brickSize, brickSize],
color: [
// These colors are simply overlaying different 3d simplex blobs over others
// makes a cool blotchy pattern
Math.floor(noise.simplex3(x / 10, y / 10, z / 10) * 128 + 128),
Math.floor(noise.simplex3(x / 10 + 300, y / 10 + 300, z / 10 + 300) * 128 + 128),
Math.floor(noise.simplex3(x / 10 + 9000, y / 10 + 9000, z / 10 + 9000) * 128 + 128),
255,
],
position: [
x * brickSize * 2,
y * brickSize * 2,
z * brickSize * 2,
],
})),
};
// Chrome lets you benchmark the sizes, check out the console to see how long it takes!
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