Created
September 19, 2019 03:51
-
-
Save Meshiest/321d69b30039c36e8f9cd5395a5019f3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> | |
// ported code from http://en.wikipedia.org/wiki/Hilbert_curve | |
function xy2d (n, p) { | |
p = {x: p.x, y: p.y}; | |
var r = {x: 0, y: 0}, | |
s, | |
d=0; | |
for (s=(n/2)|0; s>0; s=(s/2)|0) { | |
r.x = (p.x & s) > 0 ? 1 : 0; | |
r.y = (p.y & s) > 0 ? 1 : 0; | |
d += s * s * ((3 * r.x) ^ r.y); | |
rot(s, p, r); | |
} | |
return d; | |
} | |
//convert d to (x,y) | |
function d2xy(n, d) { | |
var r = {x: 0, y: 0}, | |
p = {x: 0, y: 0}, | |
s, | |
t=d; | |
for (s=1; s<n; s*=2) { | |
r.x = 1 & (t/2); | |
r.y = 1 & (t ^ rx); | |
rot(s, p, r); | |
p.x += s * r.x; | |
p.y += s * r.y; | |
t /= 4; | |
} | |
return p; | |
} | |
//rotate/flip a quadrant appropriately | |
function rot(n, p, r) { | |
if (r.y === 0) { | |
if (r.x === 1) { | |
p.x = n-1 - p.x; | |
p.y = n-1 - p.y; | |
} | |
//Swap x and y | |
var t = p.x; | |
p.x = p.y; | |
p.y = t; | |
} | |
} | |
function v2rgb(v) { | |
return ((v & 0xf800) << 8) | ((v & 0x7e0) << 5) | ((v & 0x1f) << 3); | |
} | |
function putData(arr, size, coord, v) { | |
var rgb = v2rgb(v); | |
arr[[coord.x, coord.y]] = [ | |
(rgb & 0xff0000) >> 16, | |
(rgb & 0xff00) >> 8, | |
rgb & 0xff, | |
0xff, | |
]; | |
} | |
const size = 256; | |
const area = size * size; | |
const colors = {}; | |
for (var i = 0; i < size; i++) { | |
for (var j = 0; j < size; j++) { | |
var p = {x: j, y: i}; | |
putData(colors, size, p, xy2d(size, p)); | |
} | |
} | |
console.log(colors); | |
const cake = { | |
id: 'e7b7ea60-4746-a99a-e46c-e4837dae66ec', | |
name: 'cake', | |
}; | |
noise.seed(Math.random()); | |
const save = { | |
author: { | |
id: 'e7b7ea60-4746-a99a-e46c-e4837dae66ec', | |
name: 'cake', | |
}, | |
description: 'generated save', | |
brick_assets: [ | |
'PB_DefaultBrick', | |
], | |
materials: [ | |
'BMC_Plastic', | |
], | |
brick_owners: [cake], | |
bricks: Array.from({length: area}).map((_, i) => ({ | |
size: [5, 5, 2], | |
color: colors[[i % size, Math.floor(i / size)]], | |
position: [ | |
(i % size) * 10, | |
Math.floor(i / size) * 10, | |
Math.floor(Math.abs(noise.simplex2((i % size) / size, Math.floor(i / size) / size)) * 256) | |
], | |
})), | |
}; | |
console.log(xy2d(size, {x: 5, y: 0})) | |
// 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