@@ -371,6 +371,11 @@ export default class TextureViewer extends EditorPlugin {
371
371
const texture = new Texture ( 'file:' + file . name , this . editor . core . scene ) ;
372
372
texture . name = texture . url = texture . name . replace ( 'file:' , '' ) ;
373
373
}
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 ) ) ;
374
379
}
375
380
else {
376
381
const data = await Tools . ReadFileAsBase64 ( file ) ;
@@ -391,6 +396,11 @@ export default class TextureViewer extends EditorPlugin {
391
396
const texture = new Texture ( 'file:' + file . name , this . editor . core . scene ) ;
392
397
texture . name = texture . url = texture . name . replace ( 'file:' , '' ) ;
393
398
}
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 ) ) ;
394
404
}
395
405
396
406
// Add text
@@ -409,6 +419,40 @@ export default class TextureViewer extends EditorPlugin {
409
419
parent . appendChild ( text ) ;
410
420
}
411
421
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
+
412
456
/**
413
457
* Convets a cube texture to
414
458
*/
0 commit comments