Skip to content

Commit

Permalink
Fixed #4
Browse files Browse the repository at this point in the history
  • Loading branch information
LIUKRAST committed Nov 8, 2024
1 parent 1be7f26 commit 7c40c0a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 32 deletions.
62 changes: 32 additions & 30 deletions assets/frozenblock/textures/render/skin.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Should generate a skin 3D render based on the uuid

const express = require('express');
const { createCanvas, loadImage } = require('canvas');
const fetch = require('node-fetch');
Expand All @@ -9,103 +11,103 @@ app.get('/assets/frozenblock/render/skin.png', async (req, res) => {
const uuid = req.query.uuid;

if (!uuid) {
res.status(400).send('UUID non fornito');
res.status(400).send('UUID not found');
return;
}

try {
// Carica la skin di Minecraft usando l'UUID
// Fetch UUID from mojang
const response = await fetch(`https://sessionserver.mojang.com/session/minecraft/profile/${uuid}`);
const data = await response.json();
const textures = data.properties.find(prop => prop.name === "textures");

if (!textures) {
res.status(404).send('Texture non trovata per l\'UUID fornito');
res.status(404).send('Texture not found for given UUID');
return;
}

const textureData = JSON.parse(Buffer.from(textures.value, 'base64').toString('utf-8'));
const skinUrl = textureData['textures']['SKIN']['url'];

// Crea un canvas con dimensioni 360x864
// Create a 360x864 canvas
const width = 360;
const height = 864;
const canvas = createCanvas(width, height);
const ctx = canvas.getContext('2d');

// Imposta lo sfondo trasparente
// Set the background to transparent
ctx.clearRect(0, 0, width, height);

// Carica l'immagine della skin
// Load Skin image
const skinImage = await loadImage(skinUrl);

// Imposta il filtro "nearest neighbor" per un effetto pixelato
// sets Nearest Neighbor
ctx.imageSmoothingEnabled = false;

const scale = 20; // Scala per ingrandire il rombo
const scale = 20; // Scale image

// Centro della testa
// Middle of head
const centerX = width / 2;
const topMargin = 100; // Spostamento verticale
const topMargin = 100; // Vertical translation

// === Prima faccia (frontale) ===
// === First face ===
const frontSx = 8;
const frontSy = 8;
const sWidth = 8;
const sHeight = 8;

// Trasformazione isometrica per la prima faccia (frontale)
// Isometric transform for first face
ctx.setTransform(1, 0.5, 0, 1, centerX - (sWidth * scale) / 2 - 90, topMargin);
// Ombreggiatura per la faccia frontale (nessuna modifica)
// Shadow for first face
ctx.drawImage(skinImage, frontSx, frontSy, sWidth, sHeight, 0, 0, sWidth * scale, sHeight * scale);

// Ripristina la trasformazione originale
// Reset transformations
ctx.setTransform(1, 0, 0, 1, 0, 0);

// === Seconda faccia (laterale destra) ===
// === Second face ===
const rightSx = 16;
const rightSy = 8;

// Trasformazione isometrica per la seconda faccia (laterale destra)
// Isometric transform for second face
ctx.setTransform(1, -0.5, 0, 1, centerX + (sWidth * scale) / 2 - scale - 70, topMargin + 80);
// Ombreggiatura per la faccia laterale (leggermente più scura)
// Shadow for second face
ctx.drawImage(skinImage, rightSx, rightSy, sWidth, sHeight, 0, 0, sWidth * scale, sHeight * scale);
ctx.globalAlpha = 1.0; // Ripristina opacità per le altre facce
ctx.globalAlpha = 1.0; // Resets opacity for other faces

// Ripristina la trasformazione originale
// Reset transformations
ctx.setTransform(1, 0, 0, 1, 0, 0);

// === Terza faccia (superiore) ===
// === Third face ===
const topSx = 8;
const topSy = 0;

// Trasformazione isometrica per la faccia superiore (ruotata e traslata sopra le altre facce)
// Isometric transform for third face
ctx.setTransform(1, 0.5, -1, 0.5, centerX - scale / 2, topMargin - (sHeight * scale) / 2);
// Ombreggiatura per la faccia superiore (ancora più scura)
// Shadow for third face
ctx.drawImage(skinImage, topSx, topSy, sWidth, sHeight, 0, 0, sWidth * scale, sHeight * scale);
ctx.globalAlpha = 1.0; // Ripristina opacità
ctx.globalAlpha = 1.0; // Resets opacity for other faces

// Ripristina la trasformazione originale
// Reset transformations
ctx.setTransform(1, 0, 0, 1, 0, 0);

// Imposta l'intestazione della risposta
// Sets the header
res.setHeader('Content-Type', 'image/png');

// Invia l'immagine generata come risposta
// Sends the image as response
canvas.toBuffer((err, buf) => {
if (err) {
res.status(500).send('Errore nella generazione dell\'immagine');
res.status(500).send('Generating skin image error');
return;
}
res.end(buf);
});

} catch (error) {
console.error('Errore nel caricamento della skin:', error);
res.status(500).send('Errore nel caricamento della skin');
console.error('Loading skin error:', error);
res.status(500).send('Loading skin error');
}
});

app.listen(port, () => {
console.log(`Server in ascolto su http://localhost:${port}`);
//console.log(`Server listening to http://localhost:${port}`);
});
5 changes: 3 additions & 2 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ function increase(index, n) {
}

function setCurrent(index, n) {
func_092018(index, temp[index] = n)
rawSetCurrent(index, temp[index] = n)
}

function func_092018(index, n) {
// Dont use. Use setCurrent instead. This is an internal function.
function rawSetCurrent(index, n) {
let slideshow = document.getElementById(`slideshow-${index}`)
let slides = slideshow.children

Expand Down

0 comments on commit 7c40c0a

Please sign in to comment.