Having difficulty uploading custom 3D model with three.js

After spending two full days attempting to follow tutorials on how to load my own 3D model using JavaScript, I'm still unable to get the model to display in my browser. Everything appears to be set up correctly from my perspective, yet nothing is showing up.

I have double-checked that the model loads properly in other environments.

import {
  GLTFLoader
} from "./GLTFLoader.js"

const dnaContainer = document.querySelector('#dnaContainer')

var dnaScene = new THREE.Scene()
var camera = new THREE.PerspectiveCamera(75, dnaContainer.clientWidth / dnaContainer.clientHeight, 0.1, 10)

camera.position.z = 3

var renderer = new THREE.WebGLRenderer({
  antialias: true
})
renderer.setClearColor('#0D1033')
renderer.setSize(dnaContainer.clientWidth, dnaContainer.clientHeight)

dnaContainer.appendChild(renderer.domElement)

dnaContainer.addEventListener('resize', () => {
  renderer.setSize(dnaContainer.clientWidth, dnaContainer.clientHeight)
  camera.aspect = dnaContainer.clientWidth / dnaContainer.clientHeight
})

var dnaLoader = new GLTFLoader()
var dna =
  dnaLoader.load('dna.gltf', function(gltf) {
    console.log(dnaLoader)
    dna = gltf.scene
    dnascene.add(gltf.scene)
  })

var light = new THREE.HemisphereLight(0xffffff, 5, 10)
dnaScene.add(light)
var render = function() {
  requestAnimationFrame(render)
  renderer.render(dnaScene, camera)
}

render()
* {
  box-sizing: border-box;
}

body {
  padding: 0;
  margin: 0;
  height: 100vh;
  display: flex;
}

canvas {
  display: block;
  height: 100%;
  width: 100%;
}

div {
  background-color: blueviolet;
}

#dnaContainer {
  background-color: black;
  height: 100vh;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8>
  <meta name="viewport" content="width=device-width, initial-scale=1.0>
  <link rel="stylesheet" href="styles.css>
  <title>Document</title>
</head>

<body>
  <div id="dnaContainer>

  </div>
  <script src="three.js></script>
  <script type="module" src="GLTFLoader.js></script>
  <script type="module" src="main.js></script>
</body>

</html>

This situation feels overwhelming and confusing, as if there's a crucial piece of information missing to fully comprehend how everything works within three.js.

Answer №1

Give this a shot

gltfLoader.load("theModel.gltf", (gltf) => {
  gltf.scene.scale.set(0.5, 0.5, 0.5);
  scene.add(gltf.scene);
}

Occasionally, the model may not display correctly due to its scale. It could either be too large or too small.

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

A warning message has been triggered: 'Script Alert Error - Object

I've been putting in hours on a SUP project that requires some tweaking and I keep running into the issue Script Alert Error: Object expected The code I've added is: $('#bottomOfStart_ScreenForm').before('<div style="display: ...

The condition to break when a value is found within a specific range on sheet1 if it is present in the range on

Essentially, the user will input data for a new item on the NovoItem sheet. Upon pressing the Save Button (yet to be added), the code should check the ArquivoItens sheet to see if the item already exists. If it does, the code should halt its operation. Ho ...

Item floating in a list

I'm facing a challenging issue that I need help with. It's a bit complicated to explain, but I'll do my best. There's also a picture included to help illustrate the problem. The problem I have is with an ordered list. When the next row ...

Tabulator - Using AJAX to retrieve a JSON document

I am currently working on importing a json file from an Azure blob container using Azure Data Factory. Despite closely following the documentation and researching various resources on Stack Overflow, I am facing challenges with making the ajax request fun ...

Managing concurrent users updating the same form on a web application

Imagine a scenario where user A opens a form with pre-filled data. While user A makes changes to the form data, user B also opens the same form with the data intended for user A. Just as user B begins modifying the data, user A clicks on the submit butto ...

Executing command prompt using Javascript

As a newcomer to stackoverflow, I am facing an issue with my asp.net web application. In one of the web forms, I need to execute cmd.exe from JavaScript on the server side after clicking a button. function onRec(){ try{ var commandtoRun = "c:\\W ...

"Troubutes aren't being set properly for vec3 in multi-attribute components

I'm having trouble adding a multi-attribute component to an entity using the following code - inside my a-frame scene - <a-entity id="hornets"></a-entity> in my controller - var hornets = document.querySelector('#horne ...

A Guide to Handling Errors while Converting a Map to a Promise in Typescript

I am attempting to capture errors if any of the asynchronous code within my map function fails. It seems to enter the error block but does not log anything, as the error variable remains null. Is there an alternative method for handling errors within map ...

Inverting Arrays Recursively with JavaScript

Currently, we are delving into functional JavaScript in my programming course with a particular assignment. However, I seem to be struggling to make the code work as intended. I would greatly appreciate any advice on how to improve this piece of code. Ever ...

Concealing Images in HTML Without Reserving Space for Them

Our task is to create a website here: I tried hiding elements using display: none; However, I ended up with a large blank space. https://i.sstatic.net/B2E8Y.jpg This is the HTML code: <h1>OUR PORTFOLIO</h1><div class="subtext-top"& ...

Display the tooltip only when the checkbox is disabled in AngularJS

My current setup includes a checkbox that is disabled based on a scope variable in Angular. If the scope variable is true, the checkbox stays disabled. However, if the scope variable is false, the checkbox becomes enabled. I am looking to implement a too ...

jQuery.ajax encountering failure to read success function

Hey everyone, I need some assistance with web programming. I'm facing an issue with ajax while trying to read a JSON response. The catch is, I have to use Internet Explorer for this task, so switching browsers is not an option. I'm stuck because ...

Troubleshooting problems with dataTransfer and the file input type in JavaScript

Working on a project, I'm currently exploring the idea of implementing a JavaScript drag-and-drop feature. Typically, when using drag and drop, one would pass the event.dataTransfer.files to an ajax call for uploading the file to an external PHP file ...

The deletion was not successfully carried out in the ajax request

Can anyone help with an issue I'm having while trying to remove a row from a table using the closest function? The function works fine outside of the $.post request, but it doesn't work properly when used within the post request. Here is my code: ...

Retrieving a single post from a JSON file using AngularJS

I'm currently delving into AngularJS, but I seem to be stuck on what might be a simple issue. At the moment, I have some hardcoded JSON files with a few persons in them and no actual backend set up yet. In my form, I aim to display a single person ea ...

Utilize Page.evaluate() to send multiple arguments

I am facing an issue with the Playwright-TS code below. I need to pass the email id dynamically to the tester method and have it inside page.evaluate, but using email:emailId in the code throws an undefined error. apiData = { name: faker.name.firstNa ...

The full screen menu is exclusively displayed in the navigation bar

My HTML and CSS code is causing an issue where the full-screen menu only appears in the nav bar. This problem seems to be specific to Safari. To solve the issue, I need to remove the following code: .nav { position: fixed; } However, I still ...

Upon clicking, Vue triggers form validation in Laravel

I have encountered an issue that I am unable to solve by myself: Within my Laravel view, I have included a form in the following manner {!! Form::open(array('route' => ['reports.store', $project->id], 'data-parsley-validate& ...

The function of JQuery Validation successfully executes, but unfortunately, the page quickly refreshes and as a result, the submit function

<script type="text/javascript"> $(document).ready(function(){ //$('.this_btn').click(function(){ //$('#myModal').modal('show'); //setTimeout(function() {enter code here //$('#myModal').modal(& ...

Is it possible to convert MUI components into strings on the client side?

I have incorporated the Material UI (MUI) library into my React application and am currently attempting to display certain components as PDF files directly in the browser. The approach I am taking involves: Creating a React element Rendering the React el ...