Skip to content

Commit

Permalink
Editor: load image as Plane
Browse files Browse the repository at this point in the history
  • Loading branch information
linbingquan committed May 13, 2024
1 parent cb1464c commit 7d7fca1
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions editor/js/Loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,15 @@ fileHandlers[ 'zip' ] = function ( editor, manager, reader, file ) {

};

fileHandlers[ 'png' ] = handleImages;
fileHandlers[ 'jpg' ] = handleImages;
fileHandlers[ 'jpeg' ] = handleImages;
fileHandlers[ 'bmp' ] = handleImages;
fileHandlers[ 'webp' ] = handleImages;
fileHandlers[ 'gif' ] = handleImages;
fileHandlers[ 'avif' ] = handleImages;
fileHandlers[ 'png' ] = handleImages;

Loader.getSupportedFileFormats = function () {

return Object.keys( fileHandlers ).sort();
Expand Down Expand Up @@ -784,6 +793,29 @@ function handleJSON( editor, texturePath, data ) {

}

function handleImages( editor, manager, reader, file ) {

const image = new Image();
image.src = URL.createObjectURL( file );

image.onload = function () {

const texture = new THREE.Texture( image );
texture.colorSpace = THREE.SRGBColorSpace;
texture.needsUpdate = true;

const size = 4;
const geometry = new THREE.PlaneGeometry( size * image.width / image.height, size );
const material = new THREE.MeshBasicMaterial( { map: texture } );
const mesh = new THREE.Mesh( geometry, material );
mesh.name = file.name;

editor.execute( new AddObjectCommand( editor, mesh ) );

};

}

async function handleZIP( editor, contents ) {

const zip = unzipSync( new Uint8Array( contents ) );
Expand Down

0 comments on commit 7d7fca1

Please sign in to comment.