Skip to content

Commit 02bbb23

Browse files
Julien Moreau-MathisJulien Moreau-Mathis
authored andcommitted
Now drag'n'drop textures from texture viewer
1 parent 160e23f commit 02bbb23

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/tools/textures/viewer.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,11 @@ export default class TextureViewer extends EditorPlugin {
371371
const texture = new Texture('file:' + file.name, this.editor.core.scene);
372372
texture.name = texture.url = texture.name.replace('file:', '');
373373
}
374+
375+
// Drag'n'drop
376+
const dropListener = this.dragEnd(originalTexture, true);
377+
img.addEventListener('dragstart', () => this.editor.core.engine.getRenderingCanvas().addEventListener('drop', dropListener));
378+
img.addEventListener('dragend', () => this.editor.core.engine.getRenderingCanvas().removeEventListener('drop', dropListener));
374379
}
375380
else {
376381
const data = await Tools.ReadFileAsBase64(file);
@@ -391,6 +396,11 @@ export default class TextureViewer extends EditorPlugin {
391396
const texture = new Texture('file:' + file.name, this.editor.core.scene);
392397
texture.name = texture.url = texture.name.replace('file:', '');
393398
}
399+
400+
// Drag'n'drop
401+
const dropListener = this.dragEnd(originalTexture, false);
402+
img.addEventListener('dragstart', () => this.editor.core.engine.getRenderingCanvas().addEventListener('drop', dropListener));
403+
img.addEventListener('dragend', () => this.editor.core.engine.getRenderingCanvas().removeEventListener('drop', dropListener));
394404
}
395405

396406
// Add text
@@ -409,6 +419,40 @@ export default class TextureViewer extends EditorPlugin {
409419
parent.appendChild(text);
410420
}
411421

422+
/**
423+
* Returns an event called when the user drops a texture on the preview canvas
424+
* @param texture: the texture to drop on a mesh/instanced-mesh
425+
*/
426+
protected dragEnd (texture: BaseTexture, isCube: boolean): (ev: DragEvent) => void {
427+
return (ev: DragEvent) => {
428+
const scene = this.editor.core.scene;
429+
const pick = scene.pick(ev.offsetX, ev.offsetY);
430+
431+
if (!pick.pickedMesh || !pick.pickedMesh.material)
432+
return;
433+
434+
// Apply
435+
const material = pick.pickedMesh.material;
436+
437+
if (isCube) {
438+
UndoRedo.Push({ baseObject: material, object: material, property: 'reflectionTexture', from: material['reflectionTexture'], to: texture });
439+
material['reflectionTexture'] = texture;
440+
}
441+
else {
442+
if (material instanceof PBRMaterial) {
443+
UndoRedo.Push({ baseObject: material, object: material, property: 'albedoTexture', from: material.albedoTexture, to: texture });
444+
material.albedoTexture = texture;
445+
}
446+
else {
447+
UndoRedo.Push({ baseObject: material, object: material, property: 'diffuseTexture', from: material['diffuseTexture'], to: texture });
448+
material['diffuseTexture'] = texture;
449+
}
450+
}
451+
452+
this.editor.core.onSelectObject.notifyObservers(pick.pickedMesh);
453+
};
454+
}
455+
412456
/**
413457
* Convets a cube texture to
414458
*/

0 commit comments

Comments
 (0)