Load default texture into UV editor and auto-scale to fit canvas

This commit is contained in:
AI Bot
2026-07-30 16:49:54 +05:30
parent 5e5262c0b8
commit 55465099b2
+31 -2
View File
@@ -47,7 +47,7 @@ export function UVEditor({ onClose, onSave, targetObject, loadedModel, sceneObje
useEffect(() => { useEffect(() => {
const saved = targetObject?.materialProps?.uvLayers; const saved = targetObject?.materialProps?.uvLayers;
if (!saved?.length) return; if (saved?.length) {
let n = 0; let n = 0;
const init: Layer[] = saved.map((l: any) => ({ ...l })); const init: Layer[] = saved.map((l: any) => ({ ...l }));
init.forEach(layer => { init.forEach(layer => {
@@ -55,7 +55,36 @@ export function UVEditor({ onClose, onSave, targetObject, loadedModel, sceneObje
img.onload = () => { layer.img = img; n++; if (n === init.length) setLayers([...init]); }; img.onload = () => { layer.img = img; n++; if (n === init.length) setLayers([...init]); };
img.src = layer.url; img.src = layer.url;
}); });
}, []); } else if (targetObject?.materialProps?.map) {
// If no saved layers but there's a base map texture, load it as the initial layer
const url = targetObject.materialProps.map;
const img = new Image();
img.onload = () => {
// Automatically scale the image so it fits the 1024x1024 canvas exactly
const scaleX = CANVAS_SIZE / img.width;
const scaleY = CANVAS_SIZE / img.height;
const layer: Layer = {
id: `l-base`,
url,
img,
x: CANVAS_SIZE / 2,
y: CANVAS_SIZE / 2,
scaleX,
scaleY,
rotation: 0,
cropL: 0,
cropT: 0,
cropR: 0,
cropB: 0,
visible: true
};
setLayers([layer]);
setSelectedId(layer.id);
};
img.src = url;
}
}, [targetObject]);
const renderCanvas = useCallback((lrs: Layer[], transparent: boolean, bg: string): string => { const renderCanvas = useCallback((lrs: Layer[], transparent: boolean, bg: string): string => {
const c = document.createElement('canvas'); const c = document.createElement('canvas');