3dQR
startGridendGridcellsnCellsbSizeqrCodeGeometryinitializeQRcol1cnt2populateGrid
let qr;
let t, d, startGrid, endGrid;
let cells;
let nCells;
let bSize = 100;
let qrCodeGeometry;
function preload() {
qr = loadImage("/assets/qr2_1.jpg");
}
function setup() {
let c = min(windowWidth, windowHeight) * 0.7;
createCanvas(c, c, WEBGL);
initializeQR();
qrCodeGeometry = buildGeometry(() => {
for (let i = 0; i < nCells - int((startGrid * 2) / d); i++) {
for (let j = 0; j < nCells - int((startGrid * 2) / d); j++) {
if (cells[i][j] === 1) {
push();
translate(-width / 2 + d * i, -height / 2 + d * j, bSize / 2);
box(d, d, bSize * cells[i][j]);
pop();
} else {
push();
translate(-width / 2 + d * i, -height / 2 + d * j, 5);
box(d, d, 10);
pop();
}
}
}
});
}
function draw() {
t = frameCount / 100;
scale(0.6);
orbitControl();
rotateY((PI / 4) * sin(t / 2));
rotateX((PI / 4) * sin(t / 5));
rotateZ((PI / 4) * sin(t / 3));
background(255);
pointLight(255, 255, 255, 600, 600, 600);
specularMaterial(255);
model(qrCodeGeometry);
}
function initializeQR() {
cells = [];
let cnt = 0;
startGrid = cnt;
let col1 = [255, 255, 255, 255];
noStroke();
while (col1[0] > 200 && cnt < width / 3) {
x = cnt;
y = 5;
col1 = qr.get(qr.width / (width / cnt), qr.height / (height / cnt));
cnt++;
startGrid = cnt;
}
let cnt2 = width;
endGrid = cnt2;
col1 = [255, 255, 255, 255];
while (col1[0] > 200 && cnt > width / 3) {
x = cnt2;
y = 5;
col1 = qr.get(qr.width / (width / cnt2), qr.height / (height / cnt2));
cnt2--;
endGrid = cnt2;
}
col1 = [0, 0, 0, 2055];
while (col1[0] < 200 && cnt < width / 3) {
x = cnt;
y = startGrid;
col1 = qr.get(qr.width / (width / x), qr.height / (height / y));
cnt++;
}
d = (cnt - startGrid) / 7;
nCells = int(width / d) + 1;
populateGrid();
}
function populateGrid() {
for (let i = 0; i < nCells; i++) {
cells[i] = [];
for (let j = 0; j < nCells; j++) {
x = i * d + startGrid;
y = j * d + startGrid;
col = qr.get(qr.width / (width / x), qr.height / (height / y));
if (col[0] > 100) {
a = 0;
} else {
a = 1;
}
cells[i][j] = a;
}
}
}
function keyPressed() {
if (keyCode === 83) {
qrCodeGeometry.saveStl("model.stl", { binary: true });
}
}