Taric_112
Taric_112

Reputation: 3

TypeError when trying to import OrbitControls. What do I do?

I get this error when trying to import the OrbitControls

JS:

import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.MeshBasicMaterial({color: 0x00ff00});
const cube = new THREE.Mesh(geometry, material);
const controls = new OrbitControls(camera, renderer.domElement);

renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

scene.add(cube);

camera.position.set(0, 20, 100);
camera.position.z = 5;

renderer.render(scene, camera);

controls.update();

function animate(){
    requestAnimationFrame(animate);

    controls.update();

    renderer.render(scene, camera);
}

HTML:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Document</title>
        <style>
            body { margin: 0; }
        </style>
    </head>
    <body>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js" ></script>
        <script type="module" src="task9.js"></script>
    </body>
</html>

I've tried to use different imports; import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; and import { OrbitControls } from 'https://unpkg.com/[email protected]/examples/jsm/controls/OrbitControls.js'; None of them work

Upvotes: 0

Views: 126

Answers (3)

Yaniek
Yaniek

Reputation: 82

The error you get says that the path to OrbitControls has to be absolute and not relative. Here are the differences. In a nutshell - you just have to replace the path to OrbitControls with ./three/examples/jsm/controls/OrbitControls or /home/&USER/path/to/OrbitControls.

Upvotes: 0

Łukasz Daniel Mastalerz
Łukasz Daniel Mastalerz

Reputation: 2217

Try this approach. BTW your code has couple issues…

  1. Don’t duplicate camera settings.
  2. Initialise OrbitControls after renderer.
  3. Add some lights to scene.
  4. Remove unnecessary controls.update();
  5. Call animation loop.

That version works fine:

import * as THREE from "https://esm.sh/three";
import { OrbitControls } from "https://esm.sh/three/examples/jsm/controls/OrbitControls";
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.MeshBasicMaterial({color: 0x00ff00});
const cube = new THREE.Mesh(geometry, material);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

scene.add(cube);

camera.position.z = 5;
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.enabled = true;

function animate() {
    requestAnimationFrame(animate);
    controls.update();
    renderer.render(scene, camera);
}
animate();

Look at these topics as well:

https://discourse.threejs.org/t/cant-find-threejs-and-threejs-min-js-file/65270/3

Three.js breaks when trying to import OrbitControls.js

Upvotes: 0

Kartik Tyagi
Kartik Tyagi

Reputation: 1

import OrbitControls from '../three/examples/jsm/controls/OrbitControls'

Try this it may solve your problem.

Upvotes: 0

Related Questions