2022-06-12 07:47:30 +08:00
|
|
|
import { ATLASES_DIR, TOOLS_DIR, UV } from '../src/util';
|
2022-04-17 02:29:20 +08:00
|
|
|
import { log, LogStyle } from './logging';
|
|
|
|
import { isDirSetup, ASSERT, getAverageColour, getPermission, getMinecraftDir } from './misc';
|
2022-02-20 05:50:17 +08:00
|
|
|
|
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
|
|
|
import images from 'images';
|
|
|
|
import { PNG } from 'pngjs';
|
|
|
|
import chalk from 'chalk';
|
|
|
|
import prompt from 'prompt';
|
2022-06-12 07:47:30 +08:00
|
|
|
import { RGBA } from '../src/colour';
|
2022-02-20 05:50:17 +08:00
|
|
|
const AdmZip = require('adm-zip');
|
2022-02-20 08:42:30 +08:00
|
|
|
const copydir = require('copy-dir');
|
2021-10-22 22:52:03 +08:00
|
|
|
|
2022-02-20 05:50:17 +08:00
|
|
|
void async function main() {
|
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-03-23 07:06:08 +08:00
|
|
|
fs.rmSync(path.join(TOOLS_DIR, '/blocks'), { recursive: true, force: true });
|
|
|
|
fs.rmSync(path.join(TOOLS_DIR, '/models'), { 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) {
|
|
|
|
log(LogStyle.None, `${i+2}) ${resourcePacks[i]}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { packChoice } = await prompt.get({
|
|
|
|
properties: {
|
|
|
|
packChoice: {
|
|
|
|
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}`,
|
|
|
|
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-03-23 07:06:08 +08:00
|
|
|
zip.extractEntryTo(zipEntry.entryName, path.join(TOOLS_DIR, './blocks'), false, true);
|
2022-02-20 05:50:17 +08:00
|
|
|
} else if (zipEntry.entryName.startsWith('assets/minecraft/models/block')) {
|
2022-03-23 07:06:08 +08:00
|
|
|
zip.extractEntryTo(zipEntry.entryName, path.join(TOOLS_DIR, './models'), 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-03-23 07:06:08 +08:00
|
|
|
const blockTexturesDst = path.join(TOOLS_DIR, './blocks');
|
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`);
|
|
|
|
|
|
|
|
const zip = new AdmZip(resourcePackDir);
|
|
|
|
const zipEntries = zip.getEntries();
|
|
|
|
zipEntries.forEach((zipEntry: any) => {
|
|
|
|
if (zipEntry.entryName.startsWith('assets/minecraft/textures/block')) {
|
2022-03-23 07:06:08 +08:00
|
|
|
zip.extractEntryTo(zipEntry.entryName, path.join(TOOLS_DIR, './blocks'), 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
|
|
|
|
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-02-20 08:42:30 +08:00
|
|
|
log(LogStyle.Success, '/tools/blocks/ setup correctly');
|
2022-01-16 06:39:52 +08:00
|
|
|
|
2022-02-20 05:50:17 +08:00
|
|
|
const modelsDirSetup = isDirSetup('./models', 'assets/minecraft/models/block');
|
|
|
|
ASSERT(modelsDirSetup, '/models is not setup correctly');
|
2022-02-20 08:42:30 +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-03-23 07:06:08 +08:00
|
|
|
const ignoreListPath = path.join(TOOLS_DIR, './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-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-02-20 05:50:17 +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-02-20 05:50:17 +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-03-23 07:06:08 +08:00
|
|
|
fs.readdirSync(path.join(TOOLS_DIR, './models')).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-03-23 07:06:08 +08:00
|
|
|
const filePath = path.join(TOOLS_DIR, './models', 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));
|
|
|
|
const atlasWidth = atlasSize * 16;
|
2021-10-14 06:26:26 +08:00
|
|
|
|
2022-02-20 05:50:17 +08:00
|
|
|
let offsetX = 0;
|
|
|
|
let offsetY = 0;
|
2022-01-16 06:39:52 +08:00
|
|
|
const outputImage = images(atlasWidth * 3, atlasWidth * 3);
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
log(LogStyle.Info, `Building ${atlasName}.png...`);
|
2022-02-20 05:50:17 +08:00
|
|
|
usedTextures.forEach((textureName) => {
|
|
|
|
const shortName = textureName.split('/')[1]; // Eww
|
2022-03-23 07:06:08 +08:00
|
|
|
const absolutePath = path.join(TOOLS_DIR, './blocks', shortName + '.png');
|
2022-01-16 06:39:52 +08:00
|
|
|
const fileData = fs.readFileSync(absolutePath);
|
|
|
|
const pngData = PNG.sync.read(fileData);
|
|
|
|
const image = images(absolutePath);
|
|
|
|
|
|
|
|
for (let x = 0; x < 3; ++x) {
|
|
|
|
for (let y = 0; y < 3; ++y) {
|
|
|
|
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
|
|
|
|
2022-01-16 06:39:52 +08:00
|
|
|
textureDetails[textureName] = {
|
2022-03-19 09:20:47 +08:00
|
|
|
texcoord: new UV(
|
|
|
|
16 * (3 * offsetX + 1) / (atlasWidth * 3),
|
|
|
|
16 * (3 * offsetY + 1) / (atlasWidth * 3),
|
|
|
|
),
|
2022-02-20 05:50:17 +08:00
|
|
|
colour: getAverageColour(pngData),
|
|
|
|
},
|
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-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-03-23 07:06:08 +08:00
|
|
|
const atlasDir = path.join(ATLASES_DIR, `./${atlasName}.png`);
|
2022-02-20 05:50:17 +08:00
|
|
|
outputImage.save(atlasDir);
|
|
|
|
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-03-23 07:06:08 +08:00
|
|
|
fs.writeFileSync(path.join(ATLASES_DIR, `./${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 */
|
|
|
|
}
|