Challenges arise when trying to import GLTFLoaders in THREE.js

I'm facing a couple of issues with implementing three.js on my WordPress page.

First: I'm struggling to import GLTFLoader. When I try, I receive the error message: "Uncaught TypeError: Failed to resolve module specifier "THREE/examples/jsm/loaders/GLTFLoader.js". Relative references must start with either "/", "./", or "../"." Even when I use "/" it attempts to import from my server and "//" looks for https://three/examples/, both of which are not working as intended.

Here's an excerpt of my code:

<script type="module">
// Find the latest version by visiting https://cdn.skypack.dev/three.
 import * as THREE from '//cdn.skypack.dev/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d6a2bea4b3b396e6f8e7e4eff8e6">[email protected]</a>';
 import {GLTFLoader} from 'THREE/examples/jsm/loaders/GLTFLoader.js'
</script>

<script>
 //load 3d model
    const loader = new GLTFLoader();
    loader.load( 'https://www.historia3d.pl/wp-content/uploads/2021/05/mini_3D02.glb', function ( gltf ) {
        model = gltf.scene.children[0];
        model.scale.set(0.5,0.5,0.5);
        scene.add( gltf.scene );
    },
    undefined, function ( error ) {
        console.error( error );
    } );
</script>

Second: I would like to utilize OrbitControls, but specifically track mouse position only when the cursor is hovering over the canvas.

The code snippet:

controls = new THREE.OrbitControls(camera);
controls.addEventListener('change', renderer);

Answer №1

import * as THREE from '//cdn.skypack.dev/[email protected]'; import {GLTFLoader} from 'THREE/examples/jsm/loaders/GLTFLoader.js'

Avoid importing modules from different locations. For now, follow this format:

import * as THREE from 'https://cdn.skypack.dev/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cabea2b8afaf8afae4fbf8f3e4fa">[email protected]</a>/build/three.module.js';
import { OrbitControls } from 'https://cdn.skypack.dev/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ff8b978d9a9abfcfd1cecdc6d1cf">[email protected]</a>/examples/jsm/controls/OrbitControls.js';
import { GLTFLoader } from 'https://cdn.skypack.dev/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a9ddc1dbcccce99987989b908799">[email protected]</a>/examples/jsm/loaders/GLTFLoader.js';

Your code should be placed within the same script tag. Remember that you are using ES6 modules with module scope, not global scripts. Example format:

<script type="module">
import * as THREE from 'https://cdn.skypack.dev/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="681c001a0d0d285846595a514658">[email protected]</a>/build/three.module.js';
import { OrbitControls } from 'https://cdn.skypack.dev/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="becad6ccdbdbfe8e908f8c87908e">[email protected]</a>/examples/jsm/controls/OrbitControls.js';
import { GLTFLoader } from 'https://cdn.skypack.dev/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3a4e52485f5f7a0a140b0803140a">[email protected]</a>/examples/jsm/loaders/GLTFLoader.js';

 //load 3d model
    const loader = new GLTFLoader();
    loader.load( 'https://www.historia3d.pl/wp-content/uploads/2021/05/mini_3D02.glb', function ( gltf ) {
        model = gltf.scene.children[0];
        model.scale.set(0.5,0.5,0.5);
        scene.add( gltf.scene );
    },
    undefined, function ( error ) {
        console.error( error );
    } );
</script>

I want to use OrbitControls. But I want to check mouse position only while the cursor hover over canvas.

You seem to be initializing OrbitContorls incorrectly. The second parameter is required and currently missing. Use the following code snippet instead:

const controls = new OrbitControls(camera, renderer.domElement);
controls.addEventListener('change', renderer);

renderer.domElement represents your canvas element.

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

AngularJS Upgrading the Scope with $q

Lately, I've been working on updating code that resembles the following: $scope.arrayOfThings = []; function setArrayOfThings() { thingsService.get().then(function (things) { $scope.arrayOfThings = things; }); } $scope.$on(' ...

Can you show me how to design a website with an embedded browsing window and a customized right-click menu?

I am looking to develop a website that includes a window or frame where users can browse other websites. Additionally, I am interested in implementing a custom right-click menu within this browsing window. While I know that using iframes can help with th ...

Having trouble with Grunt and Autoprefixer integration not functioning properly

Joining a non-profit open source project, I wanted to contribute by helping out, but I'm struggling with Grunt configuration. Despite my research, I can't seem to figure out why it's not working. I am trying to integrate a plugin that allow ...

Having trouble retrieving information from the server using ajax, javascript, jquery, and php

I am currently facing an issue with sending data retrieved from a jQuery call and attempting to save it to a server file using PHP. function getSVG(){ svghead = svghead + $('#test').html(); $.ajax({ type:"POST", da ...

How can I fix the issue of a black JPG being returned when converting a ThreeJS scene with textures to

I am encountering an issue with displaying 2 textures in a scene that are loaded from jpg files. After using the code imgData = renderer.domElement.toDataURL(strMime), I am seeing a black image. In my search for a solution, I came across this question whi ...

Divide a SINGLE BACKGROUND IMAGE in HTML into two separate links of equal size, one at the top and

As a beginner in HTML, I am trying to find a way to divide a background image into two equal sections without using image mapping. I attempted to split the links by setting the style to 0% and 50% to designate the top and bottom halves, but unfortunately, ...

The mystery of the unassigned value in $(this).data(value) when using the jQuery click() method

In my Rails 5 application, I am working on creating a dynamic menu that will guide users to different parts of the site based on their location. The idea is that when they click on a specific menu item from the home page, a modal will appear allowing them ...

Issue with JQuery Promise: fail() being invoked before promise resolution

In my TypeScript code, I encountered a peculiar issue with a jQuery promise. The fail() function is being executed immediately, logging an error message to the console, despite the promise resolving successfully afterwards. Here is the code snippet: ...

Instructions for embedding a URL within a div

I'm trying to load an external URL into a div using jQuery's load function. I've included the jQuery JS file with correct syntax and I want to avoid using an iframe. Here's the code snippet: <div id="content"><div> jQuery ...

Tips for updating the state in React once all the fetch requests inside a loop have been successfully completed

Currently, I am working on a React application where I am facing an issue with setState in my App. I want to set the state only after all the AJAX fetches have been resolved to prevent multiple unnecessary re-rendering. Here is what I have attempted so fa ...

Is there a way to ensure that the code only runs if the promise has been successfully fulfilled

In my NodeJS project, I am leveraging promises to ensure that the server stops running if certain functions do not meet the required conditions. Currently, the server halts as intended, but I also want to include a console log message when the promises are ...

Execute another Ajax request using jQuery after the first Ajax callback has been completed

Looking for a solution to ensure the correct order of appended contents loaded via ajax, I have the following script: $.get('http://mydomain.com/ajax1.php/', function(data){ var data_one = $(data).find('#ajax_editor_suggestion_c ...

Angular 6 throws an error stating: "The property 'push' cannot be read of null."

The Cart model is defined as follows: //declaring a cart model for products to add export class Cart{ id:string; name:string; quantity:number; picture:string; } Below is the code for my app.service.ts: import { Injectable, Inject } fro ...

How to retrieve the name of a dynamically generated div using JavaScript?

My JavaScript .click() method isn't working with dynamically created divs. I have divs under the class name "class1" but the click method doesn't detect them. Here's my code: <link href="../../Content/Site.css" rel="stylesheet" type= ...

Insects featuring a button and tooltip duo creating a captivating "pull-effect"

Why is the button pulling when I move the cursor over a camera? Is there a way to solve this issue? <div class="input-group-btn advanced-group"> <button class="btn btn-default" data-toggle="tooltip" data-placement="bottom" title="Send Imag ...

Is it advisable to prevent Set and Map from having unspecified generics in TypeScript?

Upon reviewing some old code that I wrote, I realized that I had neglected to specify a generic type for a Set. The type was set as Set<unknown>. Strangely, despite this, I found that I could still utilize all the methods of the Set without encounter ...

Working on the image for presentation

I am looking to determine the size of an image and then display it as a cropped version with a set width and height. What is the best way for me to accomplish this task? Here's the code that I have attempted: var filesSelected = document.getElemen ...

What are some effective strategies for sharing information in JavaScript?

I have an ASP.NET website and I need to pass information to JavaScript for some tasks. Currently, I am using the following method: <script type="text/javascript"> var userHasMicrositePhoto = '<%=hasMicrositePhoto%>'; </script& ...

The process of deleting lines from an image using Javascript

If I have an image of a data-table and I want to eliminate all the grid lines (defined as continuous vertical or horizontal pixels), is there a way to achieve this using Javascript's image data manipulation? I envision looping through a 2D array conta ...

Why is npm attempting to compile a previous version of my code?

Being a complete newbie to npm and node.js, I hope you can bear with me if I'm lacking in providing the right details. I am working on developing a plugin for a website that utilizes an out-of-the-box framework within npm. Initially, everything was ru ...