xxxxxxxxxx
144
// see https://www.onirom.fr/wiki/blog/07-12-2023_tiny_code_3x3_pixels_patterns/
// and integer cube or even voxel editor (although this sketch is more generic)
let m = []
let ms = 48
let cs = 12
function setup() {
pixelDensity(1)
createCanvas(400, 400)
background(0)
rectMode(CORNERS)
randomSeed(0)
let ps = 2
for (let y = 0; y < ms; y += 3*ps) {
for (let x = 0; x < ms; x += 3*ps) {
let r = random()
if (r > 0.5) {
pattern(56, x, y, ps)
} else {
pattern(146, x, y, ps)
}
}
}
}
function pattern(i,x,y,s) {
for(k = 0; k < 3*s; k += 1) {
for(p = 0; p < 3*s; p += 1) {
j = floor(k/s)+(floor(p/s)+floor(p/s)+floor(p/s))
b = i & (1 << j)
let index = ((x+k) + (y+p) * ms)
if (b) {
m[index] = i
} else {
m[index] = 0
}
}
}
}
function drawCube(buffer, ox, oy, w, h, a, l, wl) {
ox = floor(ox)
oy = floor(oy)
let ca = cos(a)
let sa = sin(a)
for (let i = l; i >= 0; i -= 1) {
for (let y = 0; y < h; y += 1) {
for (let x = 0; x < w; x += 1) {
let xx = ((w / 2 - x) * ca - (h / 2 - y) * sa)
let yy = ((w / 2 - x) * sa + (h / 2 - y) * ca)
let px = (ox + (xx >> 1))
let py = (oy + (yy >> 2) + i)
if (px >= 0 && px < width && py >= 0 && py < height) {
let index = (px + py * width) * 4
let c = 255 - (i << 2)
if (i > 0 && x > 2) {
// shade left face + ignore top face
c -= 32
}
if (i > wl) {
// below water color
let ca = c * 0.25
buffer[index + 0] = 0+ca
buffer[index + 1] = 16+ca
buffer[index + 2] = 32+ca
} else {
buffer[index + 0] = c
buffer[index + 1] = c
buffer[index + 2] = c
}
// fake shadow (just a face slightly offseted away)
if (i == l) {
for (let j = 0; j < 5; j += 1) {
index -= j * 4 - width * 4
buffer[index + 0] -= 8
buffer[index + 1] -= 8
buffer[index + 2] -= 8
}
}
}
}
}
}
}
function draw() {
background(0, 16, 32)
loadPixels()
let a = radians(45)
for (let y = 0; y < ms; y += 1) {
for (let x = 0; x < ms; x += 1) {
let p = m[x + y * ms]
if (p) {
let index = (x + y * width) * 4
pixels[index + 0] = 255
pixels[index + 1] = 255
pixels[index + 2] = 255
}
}
}
let ox = round((cs * sqrt(2)) / 2) / 2
let sortedCubes = []
for (let y = 0; y < ms; y += 1) {
for (let x = 0; x < ms; x += 1) {
let p = m[x + y * ms]
if (p) {
let cx = width / 2 + x * ox - y * ox
let cy = height / 8 + (x / 2) * ox + (y / 2) * ox
drawCube(pixels, cx, cy, cs, cs, a, cs * 4, floor(1+cs * 4 * abs(sin((x + y) / 32 + frameCount / 64))))
//let dst = (x - y) + (x >> 1) + (y >> 1)// - z
//sortedCubes.push([cx, cy])
}
}
}
// sorting is useless since there is a single layer so we just draw by Y anyway
/*
sortedCubes.sort((ca, cb) => {
if (ca[1] > cb[1]) {
return 1
} else {
return -1
}
})
for (let i = 0; i < sortedCubes.length; i += 1) {
let cube = sortedCubes[i]
//drawCube(pixels, cube[0], cube[1], cs, cs, a)
}
*/
updatePixels()
}