2022-10-05 01:06:53 +08:00
|
|
|
import AdmZip from 'adm-zip';
|
2022-09-12 08:07:42 +08:00
|
|
|
import chalk from 'chalk';
|
2022-02-20 05:50:17 +08:00
|
|
|
import fs from 'fs';
|
2022-09-12 08:07:42 +08:00
|
|
|
import path from 'path';
|
2022-02-20 05:50:17 +08:00
|
|
|
import { PNG } from 'pngjs';
|
|
|
|
import prompt from 'prompt';
|
2022-10-05 01:06:53 +08:00
|
|
|
import * as sharp from 'sharp';
|
|
|
|
const copydir = require('copy-dir');
|
2022-09-12 08:07:42 +08:00
|
|
|
|
2022-06-12 07:47:30 +08:00
|
|
|
import { RGBA } from '../src/colour';
|
2022-09-12 08:07:42 +08:00
|
|
|
import { UV } from '../src/util';
|
2022-09-12 02:21:33 +08:00
|
|
|
import { AppPaths, PathUtil } from '../src/util/path_util';
|
2022-09-12 08:07:42 +08:00
|
|
|
import { log, LogStyle } from './logging';
|
2022-10-05 01:06:53 +08:00
|
|
|
import { ASSERT, getAverageColour, getMinecraftDir, getPermission, isDirSetup } from './misc'; ;
|
2021-10-22 22:52:03 +08:00
|
|
|
|
2022-09-12 02:21:33 +08:00
|
|
|
const BLOCKS_DIR = PathUtil.join(AppPaths.Get.tools, '/blocks');
|
|
|
|
const MODELS_DIR = PathUtil.join(AppPaths.Get.tools, '/models');
|
|
|
|
|
2022-02-20 05:50:17 +08:00
|
|
|
void async function main() {
|
2022-09-12 02:21:33 +08:00
|
|
|
AppPaths.Get.setBaseDir(PathUtil.join(__dirname, '../..'));
|
|
|
|
|
2022-02-20 08:42:30 +08:00
|
|
|
await getPermission();
|
2022-04-17 02:29:20 +08:00
|
|
|
checkMinecraftInstallation();
|
2022-02-21 06:57:14 +08:00
|
|
|
cleanupDirectories();
|
2022-02-20 08:42:30 +08:00
|
|
|
await fetchModelsAndTextures();
|
|
|
|
await buildAtlas();
|
2022-02-21 06:57:14 +08:00
|
|
|
cleanupDirectories();
|
2022-02-20 05:50:17 +08:00
|
|
|
}();
|
2021-10-14 06:26:26 +08:00
|
|
|
|
2022-04-17 02:29:20 +08:00
|
|
|
function checkMinecraftInstallation() {
|
|
|
|
const dir = getMinecraftDir();
|
|
|
|
if (!fs.existsSync(dir)) {
|
|
|
|
log(LogStyle.Failure, `Could not find ${dir}`);
|
|
|
|
log(LogStyle.Failure, 'To use this tool you need to install Minecraft Java Edition');
|
|
|
|
process.exit(1);
|
|
|
|
} else {
|
|
|
|
log(LogStyle.Success, `Found Minecraft Java Edition installation at ${dir}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 06:57:14 +08:00
|
|
|
function cleanupDirectories() {
|
2022-09-12 02:21:33 +08:00
|
|
|
fs.rmSync(BLOCKS_DIR, { recursive: true, force: true });
|
|
|
|
fs.rmSync(MODELS_DIR, { recursive: true, force: true });
|
2022-02-21 06:57:14 +08:00
|
|
|
}
|
|
|
|
|
2022-02-20 08:42:30 +08:00
|
|
|
async function getResourcePack() {
|
2022-04-17 02:29:20 +08:00
|
|
|
const resourcePacksDir = path.join(getMinecraftDir(), './resourcepacks');
|
2022-02-20 08:42:30 +08:00
|
|
|
if (!fs.existsSync(resourcePacksDir)) {
|
|
|
|
log(LogStyle.Failure, 'Could not find .minecraft/resourcepacks\n');
|
2022-01-16 06:39:52 +08:00
|
|
|
process.exit(1);
|
2021-10-14 06:26:26 +08:00
|
|
|
}
|
2022-02-20 08:42:30 +08:00
|
|
|
|
|
|
|
log(LogStyle.Info, 'Looking for resource packs...');
|
|
|
|
const resourcePacks = fs.readdirSync(resourcePacksDir);
|
|
|
|
log(LogStyle.None, `1) Vanilla`);
|
|
|
|
for (let i = 0; i < resourcePacks.length; ++i) {
|
2022-09-12 02:21:33 +08:00
|
|
|
log(LogStyle.None, `${i + 2}) ${resourcePacks[i]}`);
|
2022-02-20 08:42:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const { packChoice } = await prompt.get({
|
|
|
|
properties: {
|
|
|
|
packChoice: {
|
2022-09-12 02:21:33 +08:00
|
|
|
description: `Which resource pack do you want to build an atlas for? (1-${resourcePacks.length + 1})`,
|
|
|
|
message: `Response must be between 1 and ${resourcePacks.length + 1}`,
|
2022-02-20 08:42:30 +08:00
|
|
|
required: true,
|
|
|
|
conform: (value) => {
|
|
|
|
return value >= 1 && value <= resourcePacks.length + 1;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
if (<number>packChoice == 1) {
|
|
|
|
return 'Vanilla';
|
|
|
|
}
|
|
|
|
return resourcePacks[(<number>packChoice) - 2];
|
|
|
|
}
|
|
|
|
|
|
|
|
function fetchVanillModelsAndTextures(fetchTextures: boolean) {
|
2022-04-17 02:29:20 +08:00
|
|
|
const versionsDir = path.join(getMinecraftDir(), './versions');
|
2022-02-20 08:42:30 +08:00
|
|
|
ASSERT(fs.existsSync(versionsDir), 'Could not find .minecraft/versions');
|
|
|
|
log(LogStyle.Info, '.minecraft/versions found successfully');
|
2021-10-14 06:26:26 +08:00
|
|
|
|
2022-01-16 06:39:52 +08:00
|
|
|
const versions = fs.readdirSync(versionsDir)
|
|
|
|
.filter((file) => fs.lstatSync(path.join(versionsDir, file)).isDirectory())
|
|
|
|
.map((file) => ({ file, birthtime: fs.lstatSync(path.join(versionsDir, file)).birthtime }))
|
|
|
|
.sort((a, b) => b.birthtime.getTime() - a.birthtime.getTime());
|
|
|
|
|
|
|
|
for (let i = 0; i < versions.length; ++i) {
|
2022-02-20 05:50:17 +08:00
|
|
|
const versionName = versions[i].file;
|
2022-01-16 06:39:52 +08:00
|
|
|
log(LogStyle.Info, `Searching in ${versionName} for ${versionName}.jar`);
|
|
|
|
|
|
|
|
const versionDir = path.join(versionsDir, versionName);
|
|
|
|
const versionFiles = fs.readdirSync(versionDir);
|
2022-02-20 05:50:17 +08:00
|
|
|
if (!versionFiles.includes(versionName + '.jar')) {
|
2022-01-16 06:39:52 +08:00
|
|
|
continue;
|
|
|
|
}
|
2022-02-20 05:50:17 +08:00
|
|
|
log(LogStyle.Success, `Found ${versionName}.jar successfully\n`);
|
2022-01-16 06:39:52 +08:00
|
|
|
|
|
|
|
const versionJarPath = path.join(versionDir, `${versionName}.jar`);
|
|
|
|
|
|
|
|
log(LogStyle.Info, `Upzipping ${versionName}.jar...`);
|
2022-02-20 05:50:17 +08:00
|
|
|
const zip = new AdmZip(versionJarPath);
|
2022-01-16 06:39:52 +08:00
|
|
|
const zipEntries = zip.getEntries();
|
|
|
|
zipEntries.forEach((zipEntry: any) => {
|
2022-02-20 08:42:30 +08:00
|
|
|
if (fetchTextures && zipEntry.entryName.startsWith('assets/minecraft/textures/block')) {
|
2022-09-12 02:21:33 +08:00
|
|
|
zip.extractEntryTo(zipEntry.entryName, BLOCKS_DIR, false, true);
|
2022-02-20 05:50:17 +08:00
|
|
|
} else if (zipEntry.entryName.startsWith('assets/minecraft/models/block')) {
|
2022-09-12 02:21:33 +08:00
|
|
|
zip.extractEntryTo(zipEntry.entryName, MODELS_DIR, false, true);
|
2021-11-07 04:32:18 +08:00
|
|
|
}
|
2022-01-16 06:39:52 +08:00
|
|
|
});
|
|
|
|
log(LogStyle.Success, `Extracted textures and models successfully\n`);
|
|
|
|
return;
|
|
|
|
}
|
2022-02-20 05:50:17 +08:00
|
|
|
}
|
2022-01-16 06:39:52 +08:00
|
|
|
|
2022-02-20 08:42:30 +08:00
|
|
|
async function fetchModelsAndTextures() {
|
|
|
|
const resourcePack = await getResourcePack();
|
2022-02-21 06:57:14 +08:00
|
|
|
await fetchVanillModelsAndTextures(true);
|
2022-02-20 08:42:30 +08:00
|
|
|
if (resourcePack === 'Vanilla') {
|
|
|
|
return;
|
|
|
|
}
|
2022-01-16 06:39:52 +08:00
|
|
|
|
2022-02-20 08:42:30 +08:00
|
|
|
log(LogStyle.Warning, 'Non-16x16 texture packs are not supported');
|
|
|
|
|
2022-04-17 02:29:20 +08:00
|
|
|
const resourcePackDir = path.join(getMinecraftDir(), './resourcepacks', resourcePack);
|
2022-02-20 08:42:30 +08:00
|
|
|
if (fs.lstatSync(resourcePackDir).isDirectory()) {
|
|
|
|
log(LogStyle.Info, `Resource pack '${resourcePack}' is a directory`);
|
|
|
|
const blockTexturesSrc = path.join(resourcePackDir, 'assets/minecraft/textures/block');
|
2022-09-12 02:21:33 +08:00
|
|
|
const blockTexturesDst = BLOCKS_DIR;
|
2022-02-20 08:42:30 +08:00
|
|
|
log(LogStyle.Info, `Copying ${blockTexturesSrc} to ${blockTexturesDst}`);
|
|
|
|
copydir(blockTexturesSrc, blockTexturesDst, {
|
|
|
|
utimes: true,
|
|
|
|
mode: true,
|
|
|
|
cover: true,
|
|
|
|
});
|
|
|
|
log(LogStyle.Success, `Copied block textures successfully`);
|
|
|
|
} else {
|
|
|
|
log(LogStyle.Info, `Resource pack '${resourcePack}' is not a directory, expecting to be a .zip`);
|
2022-09-12 02:21:33 +08:00
|
|
|
|
2022-02-20 08:42:30 +08:00
|
|
|
const zip = new AdmZip(resourcePackDir);
|
|
|
|
const zipEntries = zip.getEntries();
|
|
|
|
zipEntries.forEach((zipEntry: any) => {
|
|
|
|
if (zipEntry.entryName.startsWith('assets/minecraft/textures/block')) {
|
2022-09-12 02:21:33 +08:00
|
|
|
zip.extractEntryTo(zipEntry.entryName, BLOCKS_DIR, false, true);
|
2022-02-20 08:42:30 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
log(LogStyle.Success, `Copied block textures successfully`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function buildAtlas() {
|
|
|
|
// Check /blocks and /models is setup correctly
|
2022-09-12 02:21:33 +08:00
|
|
|
log(LogStyle.Info, 'Checking assets are provided...');
|
|
|
|
|
2022-04-01 04:26:13 +08:00
|
|
|
const texturesDirSetup = isDirSetup('./blocks', 'assets/minecraft/textures/block');
|
2022-02-20 05:50:17 +08:00
|
|
|
ASSERT(texturesDirSetup, '/blocks is not setup correctly');
|
2022-09-12 02:21:33 +08:00
|
|
|
log(LogStyle.Success, '/tools/blocks/ setup correctly');
|
|
|
|
|
2022-02-20 05:50:17 +08:00
|
|
|
const modelsDirSetup = isDirSetup('./models', 'assets/minecraft/models/block');
|
|
|
|
ASSERT(modelsDirSetup, '/models is not setup correctly');
|
2022-09-12 02:21:33 +08:00
|
|
|
log(LogStyle.Success, '/tools/models/ setup correctly');
|
2022-01-16 06:39:52 +08:00
|
|
|
|
|
|
|
// Load the ignore list
|
2022-02-20 08:42:30 +08:00
|
|
|
log(LogStyle.Info, 'Loading ignore list...');
|
2022-01-16 06:39:52 +08:00
|
|
|
let ignoreList: Array<string> = [];
|
2022-09-12 02:21:33 +08:00
|
|
|
const ignoreListPath = path.join(AppPaths.Get.tools, './ignore-list.txt');
|
2022-01-16 06:39:52 +08:00
|
|
|
if (fs.existsSync(ignoreListPath)) {
|
2022-02-20 08:42:30 +08:00
|
|
|
log(LogStyle.Success, 'Found ignore list');
|
2022-02-20 05:50:17 +08:00
|
|
|
ignoreList = fs.readFileSync(ignoreListPath, 'utf-8').replace(/\r/g, '').split('\n');
|
2022-01-16 06:39:52 +08:00
|
|
|
} else {
|
2022-02-20 05:50:17 +08:00
|
|
|
log(LogStyle.Warning, 'No ignore list found, looked for ignore-list.txt');
|
2022-01-16 06:39:52 +08:00
|
|
|
}
|
2022-02-20 08:42:30 +08:00
|
|
|
log(LogStyle.Success, `${ignoreList.length} blocks found in ignore list\n`);
|
2022-01-16 06:39:52 +08:00
|
|
|
|
2022-02-20 05:50:17 +08:00
|
|
|
/* eslint-disable */
|
2022-01-16 06:39:52 +08:00
|
|
|
enum parentModel {
|
2022-02-20 05:50:17 +08:00
|
|
|
Cube = 'minecraft:block/cube',
|
|
|
|
CubeAll = 'minecraft:block/cube_all',
|
|
|
|
CubeColumn = 'minecraft:block/cube_column',
|
|
|
|
CubeColumnHorizontal = 'minecraft:block/cube_column_horizontal',
|
2022-05-15 02:52:11 +08:00
|
|
|
CubeBottomTop = 'minecraft:block/cube_bottom_top',
|
2022-02-20 05:50:17 +08:00
|
|
|
TemplateSingleFace = 'minecraft:block/template_single_face',
|
|
|
|
TemplateGlazedTerracotta = 'minecraft:block/template_glazed_terracotta',
|
2022-06-12 07:47:30 +08:00
|
|
|
Leaves = 'minecraft:block/leaves',
|
2022-01-16 06:39:52 +08:00
|
|
|
}
|
2022-02-20 05:50:17 +08:00
|
|
|
/* eslint-enable */
|
2022-09-12 02:21:33 +08:00
|
|
|
|
2022-01-16 06:39:52 +08:00
|
|
|
interface Model {
|
|
|
|
name: string,
|
2022-06-12 07:47:30 +08:00
|
|
|
colour?: RGBA,
|
2022-01-16 06:39:52 +08:00
|
|
|
faces: {
|
|
|
|
[face: string]: Texture
|
|
|
|
}
|
|
|
|
}
|
2022-09-12 02:21:33 +08:00
|
|
|
|
2022-01-16 06:39:52 +08:00
|
|
|
interface Texture {
|
|
|
|
name: string,
|
|
|
|
texcoord?: UV,
|
2022-06-12 07:47:30 +08:00
|
|
|
colour?: RGBA
|
2022-01-16 06:39:52 +08:00
|
|
|
}
|
2022-09-12 02:21:33 +08:00
|
|
|
|
2022-02-20 08:42:30 +08:00
|
|
|
log(LogStyle.Info, 'Loading block models...');
|
2022-02-20 05:50:17 +08:00
|
|
|
const faces = ['north', 'south', 'up', 'down', 'east', 'west'];
|
|
|
|
const allModels: Array<Model> = [];
|
2022-03-23 07:06:08 +08:00
|
|
|
const allBlockNames: Set<string> = new Set();
|
2022-02-20 05:50:17 +08:00
|
|
|
const usedTextures: Set<string> = new Set();
|
2022-09-12 02:21:33 +08:00
|
|
|
fs.readdirSync(MODELS_DIR).forEach((filename) => {
|
2022-02-20 05:50:17 +08:00
|
|
|
if (path.extname(filename) !== '.json') {
|
2022-01-16 06:39:52 +08:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
2022-09-12 02:21:33 +08:00
|
|
|
const filePath = path.join(MODELS_DIR, filename);
|
2022-02-20 05:50:17 +08:00
|
|
|
const fileData = fs.readFileSync(filePath, 'utf8');
|
2022-01-16 06:39:52 +08:00
|
|
|
const modelData = JSON.parse(fileData);
|
|
|
|
const parsedPath = path.parse(filePath);
|
|
|
|
const modelName = parsedPath.name;
|
|
|
|
|
|
|
|
if (ignoreList.includes(filename)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let faceData: { [face: string]: Texture } = {};
|
|
|
|
switch (modelData.parent) {
|
2022-04-14 03:04:19 +08:00
|
|
|
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 },
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
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 },
|
2022-05-15 02:52:11 +08:00
|
|
|
west: { name: modelData.textures.side },
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case parentModel.CubeBottomTop:
|
|
|
|
faceData = {
|
|
|
|
up: { name: modelData.textures.top },
|
|
|
|
down: { name: modelData.textures.bottom },
|
|
|
|
north: { name: modelData.textures.side },
|
|
|
|
south: { name: modelData.textures.side },
|
|
|
|
east: { name: modelData.textures.side },
|
2022-04-14 03:04:19 +08:00
|
|
|
west: { name: modelData.textures.side },
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
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 },
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case parentModel.TemplateSingleFace:
|
|
|
|
faceData = {
|
|
|
|
up: { name: modelData.textures.texture },
|
|
|
|
down: { name: modelData.textures.texture },
|
|
|
|
north: { name: modelData.textures.texture },
|
|
|
|
south: { name: modelData.textures.texture },
|
|
|
|
east: { name: modelData.textures.texture },
|
|
|
|
west: { name: modelData.textures.texture },
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case parentModel.TemplateGlazedTerracotta:
|
|
|
|
faceData = {
|
|
|
|
up: { name: modelData.textures.pattern },
|
|
|
|
down: { name: modelData.textures.pattern },
|
|
|
|
north: { name: modelData.textures.pattern },
|
|
|
|
south: { name: modelData.textures.pattern },
|
|
|
|
east: { name: modelData.textures.pattern },
|
|
|
|
west: { name: modelData.textures.pattern },
|
|
|
|
};
|
|
|
|
break;
|
2022-06-12 07:47:30 +08:00
|
|
|
case parentModel.Leaves:
|
|
|
|
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 },
|
|
|
|
};
|
|
|
|
break;
|
2022-04-14 03:04:19 +08:00
|
|
|
default:
|
|
|
|
return;
|
2022-01-16 06:39:52 +08:00
|
|
|
}
|
2021-10-14 06:26:26 +08:00
|
|
|
|
2022-01-16 06:39:52 +08:00
|
|
|
for (const face of faces) {
|
|
|
|
usedTextures.add(faceData[face].name);
|
|
|
|
}
|
2021-10-14 06:26:26 +08:00
|
|
|
|
2022-01-16 06:39:52 +08:00
|
|
|
allModels.push({
|
|
|
|
name: modelName,
|
2022-02-20 05:50:17 +08:00
|
|
|
faces: faceData,
|
2022-01-16 06:39:52 +08:00
|
|
|
});
|
2022-03-23 07:06:08 +08:00
|
|
|
allBlockNames.add(modelName);
|
2021-10-22 22:52:03 +08:00
|
|
|
});
|
2022-02-20 08:42:30 +08:00
|
|
|
if (allModels.length === 0) {
|
|
|
|
log(LogStyle.Failure, 'No blocks loaded');
|
|
|
|
process.exit(0);
|
|
|
|
}
|
2022-01-16 06:39:52 +08:00
|
|
|
log(LogStyle.Success, `${allModels.length} blocks loaded\n`);
|
2021-10-14 06:26:26 +08:00
|
|
|
|
2022-01-16 06:39:52 +08:00
|
|
|
const atlasSize = Math.ceil(Math.sqrt(usedTextures.size));
|
2022-10-05 01:06:53 +08:00
|
|
|
const atlasWidthPixels = atlasSize * 16 * 3;
|
2021-10-14 06:26:26 +08:00
|
|
|
|
2022-02-20 05:50:17 +08:00
|
|
|
let offsetX = 0;
|
|
|
|
let offsetY = 0;
|
2021-10-14 06:26:26 +08:00
|
|
|
|
2022-06-12 07:47:30 +08:00
|
|
|
const textureDetails: { [textureName: string]: { texcoord: UV, colour: RGBA } } = {};
|
2021-10-14 06:26:26 +08:00
|
|
|
|
2022-02-20 08:42:30 +08:00
|
|
|
const { atlasName } = await prompt.get({
|
|
|
|
properties: {
|
|
|
|
atlasName: {
|
|
|
|
pattern: /^[a-zA-Z\-]+$/,
|
|
|
|
description: 'What do you want to call this texture atlas?',
|
|
|
|
message: 'Name must only be letters or dash',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-10-05 01:06:53 +08:00
|
|
|
//const tiles: mergeImages.ImageSource[] = [];
|
|
|
|
let outputImage = sharp.default({
|
|
|
|
create: {
|
|
|
|
width: atlasWidthPixels,
|
|
|
|
height: atlasWidthPixels,
|
|
|
|
channels: 4,
|
|
|
|
background: { r: 255, g: 255, b: 255, alpha: 0.0 },
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const composites: sharp.OverlayOptions[] = [];
|
|
|
|
|
2022-02-20 08:42:30 +08:00
|
|
|
log(LogStyle.Info, `Building ${atlasName}.png...`);
|
2022-02-20 05:50:17 +08:00
|
|
|
usedTextures.forEach((textureName) => {
|
|
|
|
const shortName = textureName.split('/')[1]; // Eww
|
2022-09-12 02:21:33 +08:00
|
|
|
const absolutePath = path.join(BLOCKS_DIR, shortName + '.png');
|
2022-01-16 06:39:52 +08:00
|
|
|
|
|
|
|
for (let x = 0; x < 3; ++x) {
|
|
|
|
for (let y = 0; y < 3; ++y) {
|
2022-10-05 01:06:53 +08:00
|
|
|
composites.push({
|
|
|
|
input: absolutePath,
|
|
|
|
left: 16 * (3 * offsetX + x),
|
|
|
|
top: 16 * (3 * offsetY + y),
|
|
|
|
});
|
2022-01-16 06:39:52 +08:00
|
|
|
}
|
2021-10-14 06:26:26 +08:00
|
|
|
}
|
2021-10-22 22:52:03 +08:00
|
|
|
|
2022-10-05 01:06:53 +08:00
|
|
|
const fileData = fs.readFileSync(absolutePath);
|
|
|
|
const pngData = PNG.sync.read(fileData);
|
|
|
|
|
2022-01-16 06:39:52 +08:00
|
|
|
textureDetails[textureName] = {
|
2022-03-19 09:20:47 +08:00
|
|
|
texcoord: new UV(
|
2022-10-05 01:06:53 +08:00
|
|
|
16 * (3 * offsetX + 1) / atlasWidthPixels,
|
|
|
|
16 * (3 * offsetY + 1) / atlasWidthPixels,
|
2022-03-19 09:20:47 +08:00
|
|
|
),
|
2022-02-20 05:50:17 +08:00
|
|
|
colour: getAverageColour(pngData),
|
2022-09-12 02:21:33 +08:00
|
|
|
};
|
2021-10-14 06:26:26 +08:00
|
|
|
|
2022-01-16 06:39:52 +08:00
|
|
|
++offsetX;
|
|
|
|
if (offsetX >= atlasSize) {
|
|
|
|
++offsetY;
|
|
|
|
offsetX = 0;
|
|
|
|
}
|
|
|
|
});
|
2021-10-14 06:26:26 +08:00
|
|
|
|
2022-10-05 01:06:53 +08:00
|
|
|
outputImage = outputImage.composite(composites);
|
|
|
|
|
2022-01-16 06:39:52 +08:00
|
|
|
|
|
|
|
// Build up the output JSON
|
2022-02-20 08:42:30 +08:00
|
|
|
log(LogStyle.Info, `Building ${atlasName}.atlas...\n`);
|
2022-01-16 06:39:52 +08:00
|
|
|
for (const model of allModels) {
|
2022-06-12 07:47:30 +08:00
|
|
|
const blockColour: RGBA = {
|
|
|
|
r: 0.0, g: 0.0, b: 0.0, a: 0.0,
|
|
|
|
};
|
2022-01-16 06:39:52 +08:00
|
|
|
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;
|
2022-06-12 07:47:30 +08:00
|
|
|
blockColour.a += faceColour.a;
|
2022-01-16 06:39:52 +08:00
|
|
|
model.faces[face].texcoord = faceTexture.texcoord;
|
|
|
|
}
|
|
|
|
blockColour.r /= 6;
|
|
|
|
blockColour.g /= 6;
|
|
|
|
blockColour.b /= 6;
|
2022-06-12 07:47:30 +08:00
|
|
|
blockColour.a /= 6;
|
2022-01-16 06:39:52 +08:00
|
|
|
model.colour = blockColour;
|
2021-10-22 22:52:03 +08:00
|
|
|
}
|
2021-10-14 06:26:26 +08:00
|
|
|
|
2021-10-22 22:52:03 +08:00
|
|
|
|
2022-02-20 08:42:30 +08:00
|
|
|
log(LogStyle.Info, 'Exporting...');
|
2022-10-05 01:06:53 +08:00
|
|
|
const atlasTextureDir = path.join(AppPaths.Get.atlases, `./${atlasName}.png`);
|
|
|
|
await outputImage.toFile(atlasTextureDir);
|
|
|
|
//ASSERT(success, 'Unsuccess save');
|
|
|
|
|
2022-02-20 05:50:17 +08:00
|
|
|
log(LogStyle.Success, `${atlasName}.png exported to /resources/atlases/`);
|
|
|
|
const outputJSON = {
|
|
|
|
atlasSize: atlasSize,
|
|
|
|
blocks: allModels,
|
2022-03-23 07:06:08 +08:00
|
|
|
supportedBlockNames: Array.from(allBlockNames),
|
2022-02-20 05:50:17 +08:00
|
|
|
};
|
2022-09-12 02:21:33 +08:00
|
|
|
fs.writeFileSync(path.join(AppPaths.Get.atlases, `./${atlasName}.atlas`), JSON.stringify(outputJSON, null, 4));
|
2022-02-20 05:50:17 +08:00
|
|
|
log(LogStyle.Success, `${atlasName}.atlas exported to /resources/atlases/\n`);
|
|
|
|
|
|
|
|
/* eslint-disable */
|
2022-02-20 08:42:30 +08:00
|
|
|
console.log(chalk.cyanBright(chalk.inverse('DONE') + ' Now run ' + chalk.inverse(' npm start ') + ' and the new texture atlas can be used'));
|
2022-02-20 05:50:17 +08:00
|
|
|
/* eslint-enable */
|
|
|
|
}
|