Loading a 3D model dynamically in three.js from a local file

Attempting to upload a 3D model from a local file during runtime is resulting in a CORS error message. While utilizing http-server for uploading predetermined models at the beginning works fine, the issue arises when trying to upload random objects from local files to the scene. What is the recommended approach to circumvent the CORS error in this scenario?

When it comes to loading, the "OBJLoader" from has been utilized.

Answer №1

If you want to import files from your computer into an app, consider using a drag-and-drop method similar to the one employed in the three.js editor. By attaching an event listener to the drop event, extracting files from the event object, and utilizing FileReader to read the content, you can streamline the loading process. Once the files are loaded, you can employ OBJLoader.parse() to generate 3D objects.

Take a look at the code snippets in the editor to gain a clearer understanding of this process.

  • Manage the drop event.

https://github.com/mrdoob/three.js/blob/b524f4bca95169a2b197f8a68058e6c28abf416d/editor/index.html#L284-L290

  • Handle the file list to accommodate dragging and dropping multiple objects simultaneously.

https://github.com/mrdoob/three.js/blob/b524f4bca95169a2b197f8a68058e6c28abf416d/editor/js/Loader.js#L12-L43

  • Utilize FileReader and the appropriate loader (such as OBJLoader) to load and parse the file. The method linked below supports various 3D formats, including OBJ.

https://github.com/mrdoob/three.js/blob/b524f4bca95169a2b197f8a68058e6c28abf416d/editor/js/Loader.js#L45

three.js R104

Answer №2

Implementing the suggested solution, I have included the following code snippet to enable the system to function properly:

document.addEventListener("dragover", function (event) {
   event.preventDefault();
   event.dataTransfer.dropEffect = "copy";
});

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

Switching between vertical and horizontal div layouts while reorganizing text fields within the top div

function toggleDivs() { var container = document.querySelector(".container"); var top = document.querySelector(".top"); var bottom = document.querySelector(".bottom"); if (container.style.flexDirection === "column") { container.style.flexDirec ...

Float two DIVs horizontally with the same height using jQuery

Having a strange issue with the website I'm currently working on and can't seem to figure out what's causing it. Something is definitely off with my javascript, but I just can't pinpoint the exact problem. Here's the situation: I ...

Execute a function upon pressing the enter key

Currently, I am working on coding a webpage with a form that includes one field where users input a set of numbers. After typing in the numbers, they should then press a button labeled 'Run' to execute the code. However, the issue arises when use ...

Check input validations in Vue.js when a field loses focus

So I've created a table with multiple tr elements generated using a v-for loop The code snippet looks like this: <tr v-for="(item, index) in documentItems" :key="item.id" class="border-b border-l border-r border-black text ...

Is there a node.js equivalent to Turbolinks available?

One of my favorite features in Rails is Turbolinks, as it enhances the user experience by making pages appear to load faster. Is there any alternative or similar functionality for node.js? ...

How to use jQuery to highlight the parent element when clicking on a child element?

I'm struggling with some HTML code that looks like the following: <ul> <li class="curent"><a href="home.html">Home</a></li> <li> <a href="javascript:void(0)">Products</a> <ul ...

How to effectively replace deprecated UV and Vertex with Vector2 and Vector3 in your projects?

While developing my custom geometry, I encountered the following issue: THREE.Vertex has been DEPRECATED. Use THREE.Vector3 instead. THREE.UV has been DEPRECATED. Use THREE.Vector2 instead. I'm uncertain about the correct way to update my code below ...

Uploading files in javascript and PHP

I am currently utilizing an audio recorder provided by this source , However, instead of storing the file locally, I am interested in uploading it back to the server. My attempt involved adjusting the Recorder.setupDownload function within the recording. ...

VueJS File Upload Field Failing to Reset Post Successful Submission

I am facing an issue in my VueJS application where the form does not clear all field values after submission, especially the file upload field. After a successful submission, the uploaded file name still remains displayed on the screen. Below is the code ...

When employing UI-Router, custom directives may not function properly within nested views

I was developing an angular application using phonegap, ionic, and angular. I had created a custom directive that registered an event listener for the element to activate iScroll upon loading. Initially, the directive worked perfectly when all the views we ...

Gatsby Dazzling Graphic

I'm currently facing an issue with my Heroes component. const UniqueHero = styled.div` display: flex; flex-direction: column; justify-content: flex-end; background: linear-gradient(to top, #1f1f21 1%, #1f1f21 1%,rgba(25, 26, 27, 0) 100%) , url(${prop ...

Organizing JSON keys based on their values using Typescript

In the context of a main JSON structure represented below, I am interested in creating two separate JSONs based on the ID and Hobby values. x = [ {id: "1", hobby: "videogames"}, {id: "1", hobby: "chess"}, {id: "2", hobby: "chess ...

Guide on positioning the camera to track the model in a third-person view

Currently, I am tackling a project in Three.js where I am in need of setting up a third-person camera that tracks a 3D model as it navigates through the scene. Despite having a basic framework with animations and controls, I am encountering difficulties in ...

Concealing tab bars on Ionic 2 secondary pages

In my Ionic Bootstrap configuration, I have the following setup: { mode: 'md', tabsHideOnSubPages: true } However, despite having this setting in place, the tabs still appear on some sub-pages. It seems to be happening randomly. Is there ...

Encountering "net::ERR_EMPTY_RESPONSE" error when making a HTTP PUT request using the HUE API in JavaScript

GET requests are functioning properly. PUT requests made from the API Debug tool are also working correctly. However, both PUT and POST requests, regardless of the data or API URL used, are resulting in the following error: example: OPTIONS net::ERR_ ...

Flag is activated to retrieve the data from the @Input source

@Input() config= []; flag = false; I need to change the flag to true only when I receive data in the config from the @input. Where should I do this? The data in the config is delayed and I am unable to access it in ngOnInit but can get it in ngOnChanges. ...

Error message "Uncaught TypeError: Unable to read property 'defaultView' of undefined" was encountered while using Google Maps

I am encountering an issue when attempting to load a Google map in React: "Uncaught TypeError: Cannot read property 'defaultView' of undefined" The problem seems to be isolated within this particular component, as the rest of the app renders su ...

Encountering a client component error with the app router in Next.js version 13.4.9

Encountering an error in Nextjs that persists until the 'use client' directive is removed. Warning: Rendering <Context.Consumer.Consumer> is not supported and will be removed in a future major release. Did you mean to render <Context.Con ...

Failure to execute after Ajax success

Currently working on a login page using Spring MVC on the server side and JS/Ajax on the client side. I'm facing an issue where the server code executes but does not return anything. <script type="text/javascript"> $(function() { $( ...

Implementing Pagination in Vue: How to Make it Work with Response Data

I am looking to integrate pagination from the response data into my existing code, while also incorporating filters. JavaScript var entriesList = new Vue({ el: "#post-list-template", data: { posts: [], categories: [], cu ...