Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added possibility to load MTL files via File object #12591

Closed
wants to merge 1 commit into from

Conversation

cnspaha
Copy link
Contributor

@cnspaha cnspaha commented Nov 6, 2017

Use case:
The user has the possibility to load a obj + mtl file via an input file tag (multiple files allowed) into a viewer.
So before I use the MTLLoader I seperate the obj file from the mtl and map (.png, .jpg) files.
Afterwards I wanna load the mtl file but I only have the File object at hand no URL.
So I first load the map files as data urls and then load the mtl file and create the textures from the map files if needed.

Any suggestions? Feel free to criticize ;)

PS: If this change is approved I also will open another PR for the documentation of the function "loadByFiles".

Use case:
The user has the possibility to load a obj + mtl file via an input file tag (multiple files allowed) into a viewer.
So before I use the MTLLoader I seperate the obj file from the mtl and map (.png, .jpg) files. 
Afterwards I wanna load the mtl file but I only have the File object at hand no URL. 
So I first load the map files as data urls and then load the mtl file and create the textures from the map files if needed.

Any suggestions? Feel free to criticize ;)

PS: If this change is approved I also will open another PR for the documentation of the function "loadByFiles".
@cnspaha cnspaha changed the title Added possibility to load MTL files via File API Added possibility to load MTL files via File object Nov 6, 2017
@donmccurdy
Copy link
Collaborator

@cnspaha you can achieve this more easily with a recent addition in three.js r88, LoadingManager.setURLModifier.

Here's an example. The important parts are:

var extraFiles = {
  'foo.mtl': file1,
  'foo.png': file2
};

const manager = new THREE.LoadingManager();
manager.setURLModifier((url, path) => {
  if (files[url] !== undefined) {
    return URL.createObjectURL( files[url] );
  }
  return url;
});

const loader = new THREE.OBJLoader( manager );
loader.load( 'foo.obj', (model) => { ... });

Practically, you'd need to use the input tag to determine the names of the files and create the extraFiles object from that.

@cnspaha
Copy link
Contributor Author

cnspaha commented Nov 6, 2017

@donmccurdy
Wow... that's awesome...
worked with r85 so far, but updated today. really saves quite some work, wished I had known before I made so many changes :/

for these who are interested, here's an working example code (for r88):

    $('.inputfile').change(function (e) {
        var files = e.currentTarget.files;
        var extraFiles = {},
          file;
        for (var i = 0; i < files.length; i++) {
          file = files[i];
          extraFiles[file.name] = file;
        }

        const manager = new THREE.LoadingManager();
        manager.setURLModifier((url, path) => {
          if (extraFiles[url] !== undefined) {
            return URL.createObjectURL(extraFiles[url]);
          }
          return url;
        });

        var mtlloader = new THREE.MTLLoader(manager);
        mtlloader.load('D3D-P-LOW-001.mtl', (materials) => {
          const loader = new THREE.OBJLoader(manager);
          loader.setMaterials(materials);
          loader.load('D3D-P-LOW-001.obj', (model) => {
            console.log(model);
          });
        });


      });

@cnspaha cnspaha closed this Nov 6, 2017
@cnspaha cnspaha deleted the patch-5 branch November 6, 2017 15:56
@donmccurdy
Copy link
Collaborator

Good to hear it's working! One thing I forgot to mention when using URL.createObjectURL is that you may want to save the URL and revoke it after the model has loaded, like URL.revokeObjectURL(url), to free up that memory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants