2021-10-14 06:26:26 +08:00
|
|
|
import fs from "fs";
|
|
|
|
import path from "path";
|
|
|
|
import images from "images";
|
2021-10-22 22:52:03 +08:00
|
|
|
import { RGB, UV } from "../src/util";
|
|
|
|
import { log, LogStyle } from "./logging";
|
|
|
|
import { PNG } from "pngjs";
|
|
|
|
import { isDirSetup, assert, getAverageColour } from "./misc";
|
|
|
|
import chalk from "chalk";
|
|
|
|
|
|
|
|
// Check /blocks and /models is setup correctly
|
|
|
|
log(LogStyle.None, "Checking Minecraft assets are provided...");
|
2021-10-14 06:26:26 +08:00
|
|
|
const blocksDirSetup = isDirSetup("./blocks", "assets/minecraft/textures/block");
|
|
|
|
const modelsDirSetup = isDirSetup("./models", "assets/minecraft/models/block");
|
|
|
|
assert(blocksDirSetup && modelsDirSetup, "Folders not setup correctly");
|
2021-10-22 22:52:03 +08:00
|
|
|
log(LogStyle.Success, "Folders setup correctly\n")
|
2021-10-14 06:26:26 +08:00
|
|
|
|
|
|
|
|
2021-10-22 22:52:03 +08:00
|
|
|
// Load the ignore list
|
|
|
|
log(LogStyle.None, "Loading ignore list...")
|
2021-10-14 06:26:26 +08:00
|
|
|
let ignoreList: Array<string> = [];
|
|
|
|
const ignoreListPath = path.join(__dirname, "./ignore-list.txt");
|
|
|
|
const defaultIgnoreListPath = path.join(__dirname, "./default-ignore-list.txt");
|
|
|
|
if (fs.existsSync(ignoreListPath)) {
|
2021-10-22 22:52:03 +08:00
|
|
|
log(LogStyle.Success, "Found custom ignore list");
|
2021-10-14 06:26:26 +08:00
|
|
|
ignoreList = fs.readFileSync(ignoreListPath, "utf-8").replace(/\r/g, "").split("\n");
|
|
|
|
} else if (fs.existsSync(defaultIgnoreListPath)){
|
2021-10-22 22:52:03 +08:00
|
|
|
log(LogStyle.Success, "Found default ignore list");
|
2021-10-14 06:26:26 +08:00
|
|
|
ignoreList = fs.readFileSync(defaultIgnoreListPath, "utf-8").replace(/\r/g, "").split("\n");
|
|
|
|
} else {
|
2021-10-22 22:52:03 +08:00
|
|
|
log(LogStyle.Warning, "No ignore list found, looked for ignore-list.txt and default-ignore-list.txt");
|
2021-10-14 06:26:26 +08:00
|
|
|
}
|
2021-10-22 22:52:03 +08:00
|
|
|
log(LogStyle.Info, `${ignoreList.length} blocks found in ignore list\n`);
|
2021-10-14 06:26:26 +08:00
|
|
|
|
|
|
|
|
2021-10-22 22:52:03 +08:00
|
|
|
//
|
|
|
|
log(LogStyle.None, "Loading block models...")
|
2021-10-14 06:26:26 +08:00
|
|
|
|
|
|
|
enum parentModel {
|
2021-10-22 22:52:03 +08:00
|
|
|
Cube = "minecraft:block/cube",
|
2021-10-14 06:26:26 +08:00
|
|
|
CubeAll = "minecraft:block/cube_all",
|
|
|
|
CubeColumn = "minecraft:block/cube_column",
|
2021-10-22 22:52:03 +08:00
|
|
|
CubeColumnHorizontal = "minecraft:block/cube_column_horizontal"
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Model {
|
|
|
|
name: string,
|
|
|
|
colour?: RGB,
|
|
|
|
faces: {
|
|
|
|
[face: string]: Texture
|
|
|
|
}
|
2021-10-14 06:26:26 +08:00
|
|
|
}
|
|
|
|
|
2021-10-22 22:52:03 +08:00
|
|
|
interface Texture {
|
|
|
|
name: string,
|
|
|
|
texcoord?: UV,
|
|
|
|
colour?: RGB
|
|
|
|
}
|
2021-10-14 06:26:26 +08:00
|
|
|
|
2021-10-22 22:52:03 +08:00
|
|
|
const faces = ["north", "south", "up", "down", "east", "west"];
|
|
|
|
let allModels: Array<Model> = [];
|
|
|
|
let usedTextures: Set<string> = new Set();
|
2021-10-14 06:26:26 +08:00
|
|
|
fs.readdirSync(path.join(__dirname, "./models")).forEach(filename => {
|
|
|
|
if (path.extname(filename) !== ".json") {
|
|
|
|
return;
|
|
|
|
};
|
2021-10-22 22:52:03 +08:00
|
|
|
|
2021-10-14 06:26:26 +08:00
|
|
|
const filePath = path.join(__dirname, "./models", filename);
|
|
|
|
const fileData = fs.readFileSync(filePath, "utf8");
|
|
|
|
const modelData = JSON.parse(fileData);
|
2021-10-22 22:52:03 +08:00
|
|
|
const parsedPath = path.parse(filePath);
|
|
|
|
const modelName = parsedPath.name;
|
2021-10-14 06:26:26 +08:00
|
|
|
|
2021-10-22 22:52:03 +08:00
|
|
|
if (ignoreList.includes(filename)) {
|
|
|
|
return;
|
2021-10-14 06:26:26 +08:00
|
|
|
}
|
|
|
|
|
2021-10-22 22:52:03 +08:00
|
|
|
let faceData: {[face: string]: Texture} = {};
|
|
|
|
switch (modelData.parent) {
|
|
|
|
case parentModel.CubeAll:
|
|
|
|
faceData = {
|
|
|
|
up: { name: modelData.textures.all },
|
|
|
|
down: { name: modelData.textures.all },
|
|
|
|
north: { name: modelData.textures.all },
|
|
|
|
south: { name: modelData.textures.all },
|
|
|
|
east: { name: modelData.textures.all },
|
|
|
|
west: { name: modelData.textures.all }
|
|
|
|
}
|
2021-10-14 06:26:26 +08:00
|
|
|
break;
|
2021-10-22 22:52:03 +08:00
|
|
|
case parentModel.CubeColumn:
|
|
|
|
faceData = {
|
|
|
|
up: { name: modelData.textures.end },
|
|
|
|
down: { name: modelData.textures.end },
|
|
|
|
north: { name: modelData.textures.side },
|
|
|
|
south: { name: modelData.textures.side },
|
|
|
|
east: { name: modelData.textures.side },
|
|
|
|
west: { name: modelData.textures.side }
|
|
|
|
}
|
2021-10-14 06:26:26 +08:00
|
|
|
break;
|
2021-10-22 22:52:03 +08:00
|
|
|
case parentModel.Cube:
|
|
|
|
faceData = {
|
|
|
|
up: { name: modelData.textures.up },
|
|
|
|
down: { name: modelData.textures.down },
|
|
|
|
north: { name: modelData.textures.north },
|
|
|
|
south: { name: modelData.textures.south },
|
|
|
|
east: { name: modelData.textures.east },
|
|
|
|
west: { name: modelData.textures.west }
|
|
|
|
}
|
2021-10-14 06:26:26 +08:00
|
|
|
break;
|
2021-10-22 22:52:03 +08:00
|
|
|
default:
|
|
|
|
return;
|
2021-10-14 06:26:26 +08:00
|
|
|
}
|
|
|
|
|
2021-10-22 22:52:03 +08:00
|
|
|
for (const face of faces) {
|
|
|
|
usedTextures.add(faceData[face].name);
|
2021-10-14 06:26:26 +08:00
|
|
|
}
|
|
|
|
|
2021-10-22 22:52:03 +08:00
|
|
|
allModels.push({
|
|
|
|
name: modelName,
|
|
|
|
faces: faceData
|
|
|
|
});
|
|
|
|
});
|
|
|
|
log(LogStyle.Success, `${allModels.length} blocks loaded\n`);
|
2021-10-14 06:26:26 +08:00
|
|
|
|
2021-10-22 22:52:03 +08:00
|
|
|
const atlasSize = Math.ceil(Math.sqrt(usedTextures.size));
|
2021-10-14 06:26:26 +08:00
|
|
|
const atlasWidth = atlasSize * 16;
|
|
|
|
|
2021-10-22 22:52:03 +08:00
|
|
|
let offsetX = 0, offsetY = 0;
|
|
|
|
const outputImage = images(atlasWidth * 3, atlasWidth * 3);
|
2021-10-14 06:26:26 +08:00
|
|
|
|
2021-10-22 22:52:03 +08:00
|
|
|
let textureDetails: {[textureName: string]: {texcoord: UV, colour: RGB}} = {};
|
2021-10-14 06:26:26 +08:00
|
|
|
|
2021-10-22 22:52:03 +08:00
|
|
|
log(LogStyle.None, "Building blocks.png...");
|
|
|
|
usedTextures.forEach(textureName => {
|
|
|
|
const shortName = textureName.split("/")[1]; // Eww
|
|
|
|
const absolutePath = path.join(__dirname, "./blocks", shortName + ".png");
|
|
|
|
const fileData = fs.readFileSync(absolutePath);
|
|
|
|
const pngData = PNG.sync.read(fileData);
|
|
|
|
const image = images(absolutePath);
|
|
|
|
|
2021-10-14 06:26:26 +08:00
|
|
|
for (let x = 0; x < 3; ++x) {
|
|
|
|
for (let y = 0; y < 3; ++y) {
|
2021-10-22 22:52:03 +08:00
|
|
|
outputImage.draw(image, 16 * (3 * offsetX + x), 16 * (3 * offsetY + y));
|
2021-10-14 06:26:26 +08:00
|
|
|
}
|
|
|
|
}
|
2021-10-22 22:52:03 +08:00
|
|
|
|
|
|
|
textureDetails[textureName] = {
|
2021-10-14 06:26:26 +08:00
|
|
|
texcoord: {
|
2021-10-22 22:52:03 +08:00
|
|
|
u: 16 * (3 * offsetX + 1) / (atlasWidth * 3),
|
|
|
|
v: 16 * (3 * offsetY + 1) / (atlasWidth * 3)
|
2021-10-14 06:26:26 +08:00
|
|
|
},
|
2021-10-22 22:52:03 +08:00
|
|
|
colour: getAverageColour(pngData)
|
|
|
|
}
|
|
|
|
|
|
|
|
++offsetX;
|
|
|
|
if (offsetX >= atlasSize) {
|
|
|
|
++offsetY;
|
|
|
|
offsetX = 0;
|
|
|
|
}
|
|
|
|
});
|
2021-10-14 06:26:26 +08:00
|
|
|
|
|
|
|
|
2021-10-22 22:52:03 +08:00
|
|
|
// Build up the output JSON
|
|
|
|
log(LogStyle.None, "Building blocks.json...\n");
|
|
|
|
for (const model of allModels) {
|
|
|
|
let blockColour = {r: 0, g: 0, b: 0};
|
|
|
|
for (const face of faces) {
|
|
|
|
const faceTexture = textureDetails[model.faces[face].name];
|
|
|
|
const faceColour = faceTexture.colour;
|
|
|
|
blockColour.r += faceColour.r;
|
|
|
|
blockColour.g += faceColour.g;
|
|
|
|
blockColour.b += faceColour.b;
|
|
|
|
model.faces[face].texcoord = faceTexture.texcoord;
|
|
|
|
}
|
|
|
|
blockColour.r /= 6;
|
|
|
|
blockColour.g /= 6;
|
|
|
|
blockColour.b /= 6;
|
|
|
|
model.colour = blockColour;
|
|
|
|
}
|
2021-10-14 06:26:26 +08:00
|
|
|
|
2021-10-22 22:52:03 +08:00
|
|
|
|
|
|
|
log(LogStyle.None, "Exporting...");
|
2021-10-14 06:26:26 +08:00
|
|
|
outputImage.save(path.join(__dirname, "../resources/blocks.png"));
|
2021-10-22 22:52:03 +08:00
|
|
|
log(LogStyle.Success, "blocks.png exported to /resources");
|
|
|
|
let outputJSON = { atlasSize: atlasSize, blocks: allModels };
|
|
|
|
fs.writeFileSync(path.join(__dirname, "../resources/blocks.json"), JSON.stringify(outputJSON, null, 4));
|
|
|
|
log(LogStyle.Success, "blocks.json exported to /resources\n");
|
|
|
|
|
|
|
|
console.log(chalk.cyanBright(chalk.inverse("DONE") + " Now run " + chalk.inverse("npm start") + " and the new blocks will be used"));
|