xxxxxxxxxx
73
let blockArr = ["stone", "cobblestone", "stone_bricks", "mossy_cobblestone", "sandstone", "cut_sandstone", "chiseled_sandstone", "red_sandstone", "diorite", "polished_diorite", "andesite", "polished_andesite", "granite", "polished_granite", "quartz_block_side", "chiseled_quartz_block", "quartz_pillar", "prismarine", "prismarine_bricks", "dark_prismarine", "bricks", "mossy_stone_bricks", "chiseled_stone_bricks", "nether_bricks"];
let link1 = "https://raw.githubusercontent.com/InventivetalentDev/minecraft-assets/1.17.1/assets/minecraft/textures/block/";
let link2 = ".png";
let blockImgArr = [];
let blockColArr = [];
let blockSmallArr;
let BLOCK = 0;
function preload() {
for (let i = 0; i < blockArr.length; i++) {
blockImgArr.push(loadImage(link1 + blockArr[i] + link2));
}
}
function setup() {
createCanvas(640, 360);
for (let i = 0; i < blockArr.length; i++) {
let currentBlock = blockImgArr[i];
let aveR = 0; let aveG = 0; let aveB = 0;
for (let y = 0; y < 16; y++) {
for (let x = 0; x < 16; x++) {
let col = currentBlock.get(x, y);
aveR += red(col); aveG += green(col); aveB += blue(col);
}
}
aveR /= 256; aveG /= 256; aveB /= 256;
blockColArr.push(color(aveR, aveG, aveB));
}
let mainImg = blockImgArr[BLOCK];
blockSmallArr = create2DArray(16, 16);
for (let y = 0; y < 16; y++) {
for (let x = 0; x < 16; x++) {
let col = mainImg.get(x, y);
let r1 = red(col); let g1 = green(col); let b1 = blue(col);
let bestVal = 1000000;
let bestIndex = -1;
for (let i = 0; i < blockArr.length; i++) {
let bCol = blockColArr[i];
let r2 = red(bCol); let g2 = green(bCol); let b2 = blue(bCol);
let val = (r2 - r1) * (r2 - r1) + (g2 - g1) * (g2 - g1) + (b2 - b1) * (b2 - b1);
if (val < bestVal) {
bestVal = val;
bestIndex = i;
}
}
blockSmallArr[x][y] = bestIndex;
}
}
}
function draw() {
background(255);
let w = 10;
translate(width / 2 - 8 * w, height / 2 - 8 * w)
for (let y = 0; y < 16; y++) {
for (let x = 0; x < 16; x++) {
let index = blockSmallArr[x][y];
image(blockImgArr[index], x * w, y * w, w, w);
}
}
noLoop();
}
function create2DArray(w, h) {
let arr = new Array(w);
for (let i = 0; i < w; i++) {
arr[i] = new Array(h);
}
return arr;
}