for example the .uv1 file should be:
size,1000,1000
front,452.000,139.000,568.000,752.000
back,1034.000,161.000,252.000,572.000
right,286.000,165.000,205.000,426.000
left,338.000,263.000,156.000,355.000
top,470.000,310.000,631.000,599.000
bottom,218.000,652.000,597.000,196.000
<script>
// JavaScript code
// Get references to DOM elements
const importImageButton = document.getElementById('importImageButton');
const imageLoader = document.getElementById('imageLoader');
const importUVButton = document.getElementById('importUVButton');
const uvLoader = document.getElementById('uvLoader');
const canvas = document.getElementById('imageCanvas');
const faceList = document.getElementById('faceList');
const canvasContainer = document.getElementById('canvasContainer');
const ctx = canvas.getContext('2d');
// Variables for canvas manipulation
let image = new Image();
let imgScale = 1;
let imgPos = { x: 0, y: 0 };
let isPanning = false;
let panStart = { x: 0, y: 0 };
let mouseStart = { x: 0, y: 0 };
// Variables for selection
let faces = ['front', 'back', 'right', 'left', 'top', 'bottom'];
let faceColors = {
front: 'red',
back: 'green',
right: 'blue',
left: 'yellow',
top: 'cyan',
bottom: 'magenta'
};
let selections = {};
let currentFace = null;
let isDrawing = false;
let isEditing = false;
let editHandle = null;
let hoverHandle = null;
let handles = [];
const handleSize = 10;
// Added variables for UV mapping
let uvData = {};
let outputImageSize = { width: 1000, height: 1000 };
// Initialize canvas size
function resizeCanvas() {
canvas.width = canvasContainer.clientWidth;
canvas.height = canvasContainer.clientHeight;
draw();
}
window.addEventListener('resize', resizeCanvas);
resizeCanvas();
// Import image button click
importImageButton.addEventListener('click', () => {
imageLoader.click();
});
// Import UV button click
importUVButton.addEventListener('click', () => {
uvLoader.click();
});
// Load selected image
imageLoader.addEventListener('change', (e) => {
const file = e.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = (event) => {
image.src = event.target.result;
image.onload = () => {
imgScale = 1;
imgPos = { x: (canvas.width - image.width) / 2, y: (canvas.height - image.height) / 2 };
draw();
};
};
reader.readAsDataURL(file);
});
// Load UV file
uvLoader.addEventListener('change', (e) => {
const file = e.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = (event) => {
parseUVData(event.target.result);
alert('UV data loaded successfully!');
};
reader.readAsText(file);
});
// Parse UV data
function parseUVData(data) {
const lines = data.split('\n').map(line => line.trim()).filter(line => line);
uvData = {};
lines.forEach((line, index) => {
const parts = line.split(',');
if (index === 0 && parts[0] === 'size') {
outputImageSize = {
width: parseFloat(parts[1]),
height: parseFloat(parts[2])
};
} else {
const faceName = parts[0];
uvData[faceName] = {
x: parseFloat(parts[1]),
y: parseFloat(parts[2]),
width: parseFloat(parts[3]),
height: parseFloat(parts[4]),
rotation: 0, // Default rotation
flipH: false,
flipV: false
};
}
});
}
// Set up faces list
faces.forEach(faceName => {
// Create face item
const faceItem = document.createElement('div');
faceItem.className = 'face-item';
// Add elements to face item
const faceLabel = document.createElement('span');
faceLabel.textContent = faceName;
faceLabel.className = 'face-label';
const addButton = document.createElement('button');
addButton.textContent = 'Add';
addButton.disabled = false;
const deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
deleteButton.disabled = true;
const editButton = document.createElement('button');
editButton.textContent = 'Edit';
editButton.disabled = true;
// Rotation and flip options
const faceOptions = document.createElement('div');
faceOptions.className = 'face-options';
const rotationSelect = document.createElement('select');
['0°', '90°', '-90°', '180°'].forEach(angle => {
const option = document.createElement('option');
option.value = angle;
option.textContent = angle;
rotationSelect.appendChild(option);
});
const flipHCheckbox = document.createElement('input');
flipHCheckbox.type = 'checkbox';
const flipHLabel = document.createElement('label');
flipHLabel.textContent = 'Flip H';
flipHLabel.style.color = '#fff';
const flipVCheckbox = document.createElement('input');
flipVCheckbox.type = 'checkbox';
const flipVLabel = document.createElement('label');
flipVLabel.textContent = 'Flip V';
flipVLabel.style.color = '#fff';
faceOptions.appendChild(rotationSelect);
faceOptions.appendChild(flipHCheckbox);
faceOptions.appendChild(flipHLabel);
faceOptions.appendChild(flipVCheckbox);
faceOptions.appendChild(flipVLabel);
faceItem.appendChild(faceLabel);
faceItem.appendChild(addButton);
faceItem.appendChild(deleteButton);
faceItem.appendChild(editButton);
faceItem.appendChild(faceOptions);
faceList.appendChild(faceItem);
// Event listeners for buttons
addButton.addEventListener('click', () => {
currentFace = faceName;
isDrawing = true;
addButton.disabled = true;
deleteButton.disabled = false;
editButton.disabled = false;
selections[faceName] = selections[faceName] || {};
selections[faceName].rotation = rotationSelect.value;
selections[faceName].flipH = flipHCheckbox.checked;
selections[faceName].flipV = flipVCheckbox.checked;
});
deleteButton.addEventListener('click', () => {
delete selections[faceName];
addButton.disabled = false;
deleteButton.disabled = true;
editButton.disabled = true;
draw();
});
editButton.addEventListener('click', () => {
currentFace = faceName;
isEditing = !isEditing;
editButton.textContent = isEditing ? 'Done' : 'Edit';
draw();
});
// Event listeners for rotation and flip options
rotationSelect.addEventListener('change', () => {
if (selections[faceName]) {
selections[faceName].rotation = rotationSelect.value;
draw();
}
});
flipHCheckbox.addEventListener('change', () => {
if (selections[faceName]) {
selections[faceName].flipH = flipHCheckbox.checked;
draw();
}
});
flipVCheckbox.addEventListener('change', () => {
if (selections[faceName]) {
selections[faceName].flipV = flipVCheckbox.checked;
draw();
}
});
});
// Canvas event handlers
canvas.addEventListener('mousedown', (e) => {
const rect = canvas.getBoundingClientRect();
const mouseX = (e.clientX - rect.left);
const mouseY = (e.clientY - rect.top);
const imgMouseX = (mouseX - imgPos.x) / imgScale;
const imgMouseY = (mouseY - imgPos.y) / imgScale;
if (e.button === 1 || (e.button === 0 && e.ctrlKey)) {
// Middle mouse button or Ctrl+Left click for panning
isPanning = true;
panStart = { x: imgPos.x, y: imgPos.y };
mouseStart = { x: e.clientX, y: e.clientY };
} else if (isDrawing && currentFace) {
// Start drawing selection rectangle
selections[currentFace] = selections[currentFace] || {};
selections[currentFace].x = imgMouseX;
selections[currentFace].y = imgMouseY;
selections[currentFace].width = 0;
selections[currentFace].height = 0;
isDrawing = true;
} else if (isEditing && selections[currentFace]) {
// Check if clicked on handle
const handle = getHandleUnderMouse(mouseX, mouseY);
if (handle) {
editHandle = handle;
} else if (isPointInSelection(mouseX, mouseY, selections[currentFace])) {
// Start moving the selection
editHandle = 'move';
panStart = { x: selections[currentFace].x, y: selections[currentFace].y };
mouseStart = { x: imgMouseX, y: imgMouseY };
}
}
});
canvas.addEventListener('mousemove', (e) => {
const rect = canvas.getBoundingClientRect();
const mouseX = (e.clientX - rect.left);
const mouseY = (e.clientY - rect.top);
const imgMouseX = (mouseX - imgPos.x) / imgScale;
const imgMouseY = (mouseY - imgPos.y) / imgScale;
if (isPanning) {
// Panning
imgPos.x = panStart.x + (e.clientX - mouseStart.x);
imgPos.y = panStart.y + (e.clientY - mouseStart.y);
draw();
} else if (isDrawing && selections[currentFace]) {
// Update selection rectangle size
const sel = selections[currentFace];
sel.width = imgMouseX - sel.x;
sel.height = imgMouseY - sel.y;
draw();
} else if (isEditing && editHandle && selections[currentFace]) {
// Edit selection rectangle
const sel = selections[currentFace];
if (editHandle === 'move') {
sel.x = panStart.x + (imgMouseX - mouseStart.x);
sel.y = panStart.y + (imgMouseY - mouseStart.y);
} else {
// Adjust selection based on handle
adjustSelection(sel, editHandle, imgMouseX, imgMouseY);
}
draw();
} else {
// Update hover state for handles
hoverHandle = getHandleUnderMouse(mouseX, mouseY);
canvas.style.cursor = hoverHandle ? 'pointer' : 'default';
}
});
canvas.addEventListener('mouseup', (e) => {
isPanning = false;
isDrawing = false;
editHandle = null;
});
canvas.addEventListener('wheel', (e) => {
// Zooming
e.preventDefault();
const delta = e.deltaY < 0 ? 1.1 : 0.9;
const rect = canvas.getBoundingClientRect();
const mouseX = (e.clientX - rect.left);
const mouseY = (e.clientY - rect.top);
const imgMouseX = (mouseX - imgPos.x) / imgScale;
const imgMouseY = (mouseY - imgPos.y) / imgScale;
imgScale *= delta;
imgPos.x = mouseX - imgMouseX * imgScale;
imgPos.y = mouseY - imgMouseY * imgScale;
draw();
});
canvas.addEventListener('contextmenu', (e) => {
e.preventDefault();
});
function draw() {
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw image
if (image.src) {
ctx.drawImage(image, imgPos.x, imgPos.y, image.width * imgScale, image.height * imgScale);
}
// Draw selections
for (const faceName in selections) {
const sel = selections[faceName];
drawSelection(sel, faceName, faceName === currentFace && isEditing);
}
}
function drawSelection(sel, label, showHandles) {
ctx.save();
ctx.strokeStyle = faceColors[label] || 'red';
ctx.lineWidth = 2;
ctx.strokeRect(
sel.x * imgScale + imgPos.x,
sel.y * imgScale + imgPos.y,
sel.width * imgScale,
sel.height * imgScale
);
// Draw label
ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
ctx.font = 'bold 14px Arial';
const textWidth = ctx.measureText(label).width;
ctx.fillRect(
(sel.x + sel.width / 2) * imgScale + imgPos.x - textWidth / 2 - 5,
(sel.y + sel.height / 2) * imgScale + imgPos.y - 15,
textWidth + 10,
20
);
ctx.fillStyle = 'white';
ctx.fillText(
label,
(sel.x + sel.width / 2) * imgScale + imgPos.x - textWidth / 2,
(sel.y + sel.height / 2) * imgScale + imgPos.y
);
// Draw handles if editing
if (showHandles) {
updateHandles(sel);
handles.forEach(handle => {
ctx.fillStyle = 'red';
ctx.fillRect(handle.x - handleSize / 2, handle.y - handleSize / 2, handleSize, handleSize);
});
}
ctx.restore();
}
function updateHandles(sel) {
handles = [];
const points = [
{ x: sel.x, y: sel.y }, // Top-left
{ x: sel.x + sel.width, y: sel.y }, // Top-right
{ x: sel.x, y: sel.y + sel.height }, // Bottom-left
{ x: sel.x + sel.width, y: sel.y + sel.height } // Bottom-right
];
points.forEach((pt, idx) => {
handles.push({
x: pt.x * imgScale + imgPos.x,
y: pt.y * imgScale + imgPos.y,
pos: ['nw', 'ne', 'sw', 'se'][idx]
});
});
}
function getHandleUnderMouse(mouseX, mouseY) {
return handles.find(handle => {
return (
mouseX >= handle.x - handleSize / 2 &&
mouseX <= handle.x + handleSize / 2 &&
mouseY >= handle.y - handleSize / 2 &&
mouseY <= handle.y + handleSize / 2
);
})?.pos;
}
function isPointInSelection(mouseX, mouseY, sel) {
const x = sel.x * imgScale + imgPos.x;
const y = sel.y * imgScale + imgPos.y;
const width = sel.width * imgScale;
const height = sel.height * imgScale;
return (
mouseX >= x && mouseX <= x + width &&
mouseY >= y && mouseY <= y + height
);
}
function adjustSelection(sel, handlePos, imgMouseX, imgMouseY) {
switch (handlePos) {
case 'nw':
sel.width += sel.x - imgMouseX;
sel.height += sel.y - imgMouseY;
sel.x = imgMouseX;
sel.y = imgMouseY;
break;
case 'ne':
sel.width = imgMouseX - sel.x;
sel.height += sel.y - imgMouseY;
sel.y = imgMouseY;
break;
case 'sw':
sel.width += sel.x - imgMouseX;
sel.x = imgMouseX;
sel.height = imgMouseY - sel.y;
break;
case 'se':
sel.width = imgMouseX - sel.x;
sel.height = imgMouseY - sel.y;
break;
}
}
// Additional functionality can be implemented below
// For example, exporting the final texture image combining selections
</script>
const generateTextureButton = document.getElementById('generateTextureButton');
generateTextureButton.addEventListener('click', () => {
if (!image.src) {
alert('Please load an image first.');
return;
}
if (!uvData || Object.keys(uvData).length === 0) {
alert('Please load a .uv1 file first.');
return;
}
generateFinalTexture();
});
function generateFinalTexture() {
// Create off-screen canvas
const outputCanvas = document.createElement('canvas');
outputCanvas.width = outputImageSize.width;
outputCanvas.height = outputImageSize.height;
const outputCtx = outputCanvas.getContext('2d');
for (const faceName in uvData) {
const uv = uvData[faceName];
const sel = selections[faceName];
if (!sel) {
continue; // Skip if no selection is made for this face
}
// Get the selected area from the original image
const sx = sel.x;
const sy = sel.y;
const sw = sel.width;
const sh = sel.height;
// Create an off-screen canvas to apply transformations
const faceCanvas = document.createElement('canvas');
faceCanvas.width = sw;
faceCanvas.height = sh;
const faceCtx = faceCanvas.getContext('2d');
// Draw the selected area onto the face canvas
faceCtx.drawImage(
image,
sx,
sy,
sw,
sh,
0,
0,
sw,
sh
);
// Apply flips
if (sel.flipH || sel.flipV) {
faceCtx.translate(sel.flipH ? sw : 0, sel.flipV ? sh : 0);
faceCtx.scale(sel.flipH ? -1 : 1, sel.flipV ? -1 : 1);
faceCtx.drawImage(faceCanvas, 0, 0);
}
// Apply rotation
let rotationAngle = 0;
switch (sel.rotation) {
case '90°':
rotationAngle = 90;
break;
case '-90°':
rotationAngle = -90;
break;
case '180°':
rotationAngle = 180;
break;
default:
rotationAngle = 0;
}
if (rotationAngle !== 0) {
const rotatedCanvas = document.createElement('canvas');
if (rotationAngle % 180 === 0) {
rotatedCanvas.width = sw;
rotatedCanvas.height = sh;
} else {
rotatedCanvas.width = sh;
rotatedCanvas.height = sw;
}
const rotatedCtx = rotatedCanvas.getContext('2d');
rotatedCtx.translate(rotatedCanvas.width / 2, rotatedCanvas.height / 2);
rotatedCtx.rotate((rotationAngle * Math.PI) / 180);
rotatedCtx.drawImage(faceCanvas, -sw / 2, -sh / 2);
faceCanvas.width = rotatedCanvas.width;
faceCanvas.height = rotatedCanvas.height;
faceCtx.clearRect(0, 0, faceCanvas.width, faceCanvas.height);
faceCtx.drawImage(rotatedCanvas, 0, 0);
}
// Draw the transformed face onto the output canvas at the UV-specified position
outputCtx.drawImage(
faceCanvas,
0,
0,
faceCanvas.width,
faceCanvas.height,
uv.x,
uv.y,
uv.width,
uv.height
);
}
// Open a new window to display the final texture
const dataURL = outputCanvas.toDataURL('image/png');
const newWindow = window.open();
if (newWindow) {
newWindow.document.write(`<img src="${dataURL}" alt="Final Texture"/>`);
}
}