Creating a material texture with file api in three.js is a straightforward process

I have a vision to create a cutting-edge model browser that allows users to handpick models and textures themselves. In order to achieve this, I am utilizing the File API for smooth file handling. My approach involves using two separate file inputs for receiving both the model and texture files.

<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="file" id="file" accept=".obj" />
    <input type="file" name="texture" id="texture" accept=".jpg" />
</form>

Furthermore, I have incorporated two key events:

document.getElementById('file').addEventListener('change', readFile, false);
document.getElementById('texture').addEventListener('change', readTexture, false);

The following functions are crucial in achieving this innovative browser:

function readFile(evt) {
if(window.object) {                     
        scene.remove(object);
        delete object;
}
var fileObject = evt.target.files[0];
var reader = new FileReader();
reader.readAsText(fileObject);

reader.onload = function() {
loader = new THREE.OBJLoader();
object = loader.parse(reader.rresult);
scene.add(object);
};
}//Method to add model to the scene

function readTexture(evt) {
  if(window.mat){
   scene.remove(mat);
   delete mat;
  }
  var fileObject = evt.target.files[0];
  var reader = new FileReader();
  reader.readAsDataURL(fileObject);
  var textureLoader = new THREE.TextureLoader();
  mat = new THREE.MeshLambertMaterial();
  mat.map=reader.result;//Though it doesn’t work correctly.

  reader.onload = function() {
  object.children[0].material = mat;
};
}//Method to apply texture to model

I've encountered an issue with 'mat.map=reader.resul' not functioning as intended. Seeking guidance from anyone who can assist me!

Answer №1

Consider attempting this:

let loader = new THREE.TextureLoader();
let texture = loader.load(reader.readAsDataURL(fileObject));
let material = new THREE.MeshLambertMaterial({map:texture});

Note that this code is untested.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Passing information from a directive to a controller using AngularJS

I am looking to display the progress of a file upload using a file reader. Here is the HTML code snippet I have: <md-progress-linear id="file-progress-indicator" md-mode="determinate" value="{{progress}}"></md-progress-linear> and below is m ...

A versatile jQuery function for extracting values from a :input selector

Is there a universal method in jQuery to retrieve the value of any :input element? I pose this question because I have a webpage containing select and checkbox inputs, as seen in the code below: for (var i = 0; i < arguments.length; i++) { ...

Tips for keeping the options menu visible even when the video is paused

As I was creating my own customized Video player, I encountered an issue where I wanted the options bar/menu to disappear when the user became inactive. While I have managed to achieve this functionality, I still require the option bar to remain visible ...

Deciphering HTML with AngularJS

We are currently working with AngularJS version 1.5.6 and facing an issue with HTML characters not being displayed correctly in our text. Despite trying various solutions, we have been unsuccessful in resolving this issue. We have explored numerous discuss ...

Instructions for making dropdown menus that dynamically reduce their options as you make selections:

I have an HTML form structured like this: Within the dropdowns, there is a uniform list of choices: Option 1, Option 2, Option 3. Users must select a value for each key, which currently functions correctly: Yet, I am seeking to enhance this functionality ...

Error: The socket.io client script cannot be found when using Express + socket.io

This situation is really getting to me... even though I have a functioning version of Express + Socket.io, I can't replicate it in a new project folder with standard NPM installs. Can someone please help me figure out what I'm doing wrong...? Her ...

Tips for solving the problem of TSX error where 'ReactNode' cannot be assigned with 'void' type

I've been attempting to loop through the Mui component MenuItem using a forEach loop, but I'm encountering an error stating 'Type 'void' is not assignable to type 'ReactNode''. Here's the section of my code caus ...

Issues arise when attempting to transfer strings through ajax to a Node.js server that is utilizing express.js

On my website, I am encountering a problem where certain characters are being lost when I send strings to my Node.js server. // Client: microAjax("/foo?test="+encodeURI("this is ++ a test"), function callback(){}); // Server: app.get('/foo',fun ...

Reactjs Router.push function does not behave as intended

I'm currently working with Reactjs and Next.js. I am experiencing an issue where the correct data is only displayed after refreshing the page instead of upon clicking, as intended. To solve this problem, I have attempted to use "router.push", but unfo ...

Encountering issues when trying to build a Nestjs app with node-crc (rust cargo) in Docker

I am encountering an issue with building my Nest.js app using Docker due to a dependency called "node-crc" version "2.0.13" that fails during the docker build process. Here is my Dockerfile: FROM node:17.3.1-alpine RUN curl https://sh.rustup.rs -sSf | sh ...

When you click, transfer a single item from one array to another and modify the text according to the items in the array

How can I dynamically update a shopping cart feature on my website when a user clicks on the "addtocart" button? I want to create functionality where clicking on the button adds the corresponding product to the cart array, and updates the cart amount displ ...

How come my post isn't being saved to the page after I refresh it?

Utilizing Javascript, NodeJS, MongoDB, Express Within my application, a user is expected to enter text in an input field and upon clicking the submit button, the text should appear on the page. I have succeeded in posting text to the page; however, after ...

Bindings with Angular.js

I have developed an application similar to Pastebin. My goal is to allow users to paste code snippets and display them with syntax highlighting and other visual enhancements, regardless of the programming language used. To achieve this, I utilize Google&ap ...

Error encountered: The EVM has reverted the transaction

After successfully deploying this basic smart contract using Remix, I encountered an issue when trying to interact with it through web3.js in my upcoming app. I used the evm version: paris for deployment and everything worked smoothly on Remix IDE. Here i ...

Node application experiencing issues retrieving SVG files during production

When testing my application locally, the svg files display correctly with the code below (Angular.js variables are used within curly brackets): <img ng-src="img/servant_{{servant.personality}}.svg" draggable="false"> However, once deployed on Herok ...

Maximizing Efficiency: Utilizing a Single Panel across Multiple HTML Files with jQueryMobile

Can a panel defined in index.html be used on another page, such as results.html? Or do I have to define the panel on every page and duplicate the HTML code on each page? Because I want the panel to be consistent across all pages. This is the panel in my ...

Display a smaller image preview in the product photo gallery

Hey there! I'm currently working on a website and looking to showcase my product in a similar way as shown here: I would like my main image to be displayed with all the other variants of the product image underneath, along with the same visual effect ...

Utilizing URL Parameters and Query Strings in React Router: A Comprehensive Guide

Can someone help me retrieve the foo parameter value from http://localhost:3000/params?foo=123? I keep encountering an error message: Error: Params(...): No render output was returned. This typically indicates a missing return statement or returning null ...

Highlight dates in Vue.js that are overdue using a date filter

Currently, I have a Vue filter set up to display dates in a visually appealing way. However, I am looking to enhance the filter by adding a feature that would highlight dates in red if they are overdue (meaning the date is earlier than the current date). I ...

Tips for dynamically incorporating input forms within AngularJS

I am trying to dynamically change the form inputs using ng-bind-html. However, I am only able to display the label and not the text box on the DOM. The content inside ctrl.content will depend on the values received from the server. Important Note: The ...