Attempting to upload an item using the OBJLoader tool on my own computer | Three.js

I've been experimenting with integrating 3D objects into a web page using three.js and a .obj file. Despite my efforts to use OBJLoader to load the file onto the HTML view, I've encountered some difficulties. I even tried different loaders, but none seem to be working for me.

To set up my local environment, I'm utilizing Visual Studio Code along with an extension called Live Server to host my files. For this project, I converted a .stl file to a .obj file using Blender.

While following the documentation provided by three.js, I realized that I haven't imported the objloader.js file yet due to confusion on how to properly import it. Here is the link to the documentation:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Three.js APP</title>
        <link rel="stylesheet" href="css/styles.css">
    </head>
    <body>
        <script src="js/three.js"></script>
        <script>
            const loader = new OBJLoader();
            const scene = new THREE.Scene();
            const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );

            const renderer = new THREE.WebGLRenderer();
            renderer.setSize( window.innerWidth, window.innerHeight );
            document.body.appendChild( renderer.domElement );

            loader.load(
                'models/car.obj',
                function ( object ) {
                    scene.add( object );
                },
                function ( xhr ) {
                    console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
                },
                function ( error ) {
                    console.log( 'An error happened' );
                }
            );
        </script>
    </body>
</html>

Answer №1

Check out the OBJLoader demo for a hands-on look at how it works. Instead of using script tags to reference JS files, it utilizes imports while maintaining the same code structure.

Don't forget to include a light source in your scene to ensure proper lighting on the object. Without it, the object will appear black against a dark background.

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

Is there a way to verify if the $compile process has finished?

I am currently developing a function that can dynamically create an email template from an HTML template and some provided data. To accomplish this, I am utilizing Angular's $compile function. However, I have encountered a challenge that I seem unabl ...

"Unlocking the potential of Vue.js: Grabbing the object ID with a

I am encountering an issue with accessing the object ID when I click on the edit or delete buttons in my CRUD operation. The event.target is returning the values of the edit and delete buttons instead. This is what my HTML template looks like: <div ...

Create a file object using content with the help of JavaScript

I am working with a file containing specific data const ics = 'BEGIN:VCALENDAR\n' + 'VERSION:2.0\n' + 'CALSCALE:GREGORIAN\n' + 'METHOD:PUBLISH\n' + 'END:VCALENDAR\n'; I am trying t ...

Creating a network of communication between API routes in NextJS

Can the fetch API be used within an API route in NextJs? I have a large handler and instead of having everything in one handler, I'd like to modularize it so that after completing a specific task (e.g., writing to a database), I can call another handl ...

Fixing Half Screen Sidebars

I have a query regarding my coding problem. I am trying to create two pop-ups that occupy half of each screen. As I am new to JavaScript and jQuery, I want to ensure that I am doing it correctly. Is there a way for the left side to slide out from the left ...

Tips for hiding a popover in Angular when clicking outside of it

In a demo I created, rows are dynamically added when the user presses an "Add" button and fills in a name in an input field. Upon clicking "OK," a row is generated with a Star icon that should display a popover when clicked. While I have successfully imple ...

Tips for setting limitations on a date picker's minimum and maximum values on an iPhone using JavaScript

I have encountered an issue with my JavaScript function that sets min and max values for the input type date. While it works perfectly on Android devices, I am facing a problem on iPhone where I am unable to restrict the calendar with the specified min and ...

Troubleshooting Problem with Scrolling to the Bottom of a DIV Using J

I've been experimenting with creating a chat box in jQuery that automatically scrolls to the bottom when the page loads. I've tried various methods, but most of them haven't worked as expected. One method I found animates a scroll down, but ...

Show or hide comment sections using jQuery on a single webpage

Currently, I have implemented a script to toggle the visibility of comments on several WordPress-based websites. The script works well on most sites, but I am facing an issue with a site using the Hero theme. On the index pages, the theme displays content ...

combine elements from a different document using a local field as a key

I'm currently working on a project involving a competition document with a field teams array of objects containing the _id of each team, as well as a score document with a teamId field. The competitions.teams array looks like this: [{_id: 100,..}, {.. ...

A method to transfer a floating point number from Python to JavaScript without using cgi

Running an Apache server from a Raspberry Pi, I have a Python script that prints sensor input to the console. A PHP script then processes this output and controls a light based on the sensor reading before printing it again. Everything works fine when exec ...

The React onChange event fails to trigger

Why isn't the onChange event firing in the input tag? I used LinkedStateMixin to track the input value before, but now I want to add an onChange event to run a function. After removing LinkedStateMixin, the onChange event still doesn't fire. I ev ...

Divide the string into segments and set up pagination

<div><p class="pagi">page 1 content</p><p class="pagi">page 2 content</p><p class="pagi">page 3 content</p></div> I am currently facing a challenge with the string provided above. I need to create pagination ...

When using a function linked to an API request, an uncaught TypeError is thrown: Unable to access the 'includes' property of an undefined value

Utilizing the movie DB API (), I am displaying the results of my call on my page using Vue. The API supplies all the necessary data for me to achieve my objective, as demonstrated in this image https://i.stack.imgur.com/vP4I2.jpg Beneath each show's ...

Is it possible to substitute the variables with the function name?

Is there a way to duplicate the variable name? Take a look at the example below (note the name a1) $(document).ready(function() { name = "a1"; // This is "NAME" as a1 }) function name() { // USING "NAME" $("#test" + name).keydown(function(a) ...

Issue when Updating Component State: React child cannot be an object

I'm currently in the process of familiarizing myself with React and how to manage component state. My goal is to build a component, set up the initial state using a constructor, make a GET request to the server to fetch an array of objects, and then ...

Concealing jQueryUI tooltip upon clicking a "target=_blank" link

I have implemented jQuery to display tooltips for all links on my webpage that include a 'details' attribute. $(function() { $tooltip = $(document).tooltip({ show: false, track: true, ...

The Camera component in React Native is not currently supporting foreground service

Trying to capture images in a foreground service, such as taking pictures while the user is using another app. Everything works fine within the app, but when the app is closed with the foreground service active, the camera stops working and shows this erro ...

Dynamic menu item created using Material UI state management

I am facing a challenge in displaying different menu items based on the gender state. Below are the constant values I am working with. const maleArray = ["A", "B", "C", "D"] const femaleArray = ["E", " ...

Combining geometries/meshes in Three.js by eliminating shared regions

My goal is to merge two geometries/meshes (red and blue) into a single unified entity. However, when I create a new geometry and use geometry.merge(), I notice that the inner vertices and faces remain (the green area). I want to eliminate this extra infor ...