3d Cubes ⚙️

#3d weirdo cubes build from some planes

Log in to post a comment.

Canvas.setpenopacity(1);

const turtle = new Turtle();

const camera = {
    pos: [230, -120, 0],
    pan: [160, -25, 0],
    f: 120,
}
const randomish = 2.5;

function walk(i, t = 0) {
    if (i==0) rseed = seed;
    
    for(let j =-8;j<8; j++) {
        const size = 15.9 + (i*1.5);
        const sizeInner = 14 + (i*1.5);
        const sizeDiff = size-sizeInner;
        const h = (j-t) * (size*4)
        for (let c=0; c<2; c++) {
            const z = (-size + c) * size;
            const plane = [
                [-size,-size+h, z],
                [-size, size+h, z],
                [ size, size+h, z],
                [ size,-size+h, z],
            ];
            drawPlane(plane);
        }
        for (let c=0; c<2; c++) {
            const x = c == 0 ? - size : size;
            const zfront = (-size) * size + sizeDiff;
            const zback = (-size + 1) * size - sizeDiff;
            const plane = [
                [x,  size+h, zfront],
                [x,  size+h, zback ],
                [x, -size+h, zback ],
                [x, -size+h, zfront],
            ];
            drawPlane(plane);
        }
        for (let c=0; c<2; c++) {
            const y = c == 0 ? - size : size;
            const zfront = (-size) * size + sizeDiff;
            const zback = (-size + 1) * size - sizeDiff;
            const plane = [
                [sizeInner , y+h, zfront],
                [sizeInner , y+h, zback ],
                [-sizeInner, y+h, zback ],
                [-sizeInner, y+h, zfront],
            ];
            drawPlane(plane);
        }
    }
    return i < 10;
}

function drawPlane(segments) {
    turtle.penup();
    for(let s=0, l=segments.length; s<=l; s++) {
        if (s==1) turtle.pendown();
        let segment = segments[s % l];
        segment = randomize(segment, randomish);
        segment = add(segment, camera.pos);
        segment = project(segment);
        segment = add(segment, camera.pan);
        turtle.goto(segment);
    }
}

/**************************************************/

// some functions from https://turtletoy.net/turtle/2dc4806767
function project(p) {
    p[2] += camera.f;
    return [p[0]/p[2]*camera.f, p[1]/p[2]*camera.f, p[2]];
}
const randomize = (p, v, s) => {
    p[0] += -v * 0.5 + hash(123+p[1]/10|0) * v;
    p[1] += -v * 0.5 + hash(3*p[1]/10|0) * v;
    p[2] += -v * 0.5 + hash(-444+2*p[1]/10|0) * v;
    return p;
};
const cos = Math.cos;
const sin = Math.sin;
const scale = (p,scale) => [p[0]*scale, p[1]*scale, p[2]*scale];
const add = (a,b) => [a[0]+b[0], a[1]+b[1], a[2]+b[2]];
const sub = (a,b) => [a[0]-b[0], a[1]-b[1], a[2]-b[2]];
const cross = (a,b) => [
    a[1]*b[2]-b[1]*a[2],
    a[2]*b[0]-b[2]*a[0],
    a[0]*b[1]-b[0]*a[1]
];

const seed = 1234;

// pseudo random methods
function hash(p) {
    p += seed;
    p = 1103515245 * (((p) >> 1) ^ (p));
    p = 1103515245 * (p ^ (p>>3));
    p = p ^ (p >> 16);
    return p / 1103515245 % 1;	
}

let rseed = seed;
function rand() {
    let r = 1103515245 * (((rseed) >> 1) ^ (rseed++));
    r = 1103515245 * (r ^ (r>>3));
    r = r ^ (r >> 16);
    return r / 1103515245 % 1;	
}