简体   繁体   English

导入 OrbitControl.js 会出现未捕获的类型错误

[英]Importing OrbitControl.js gives uncaught type error

I'm writing some Javascript that loads a glb 3d model into a webpage.我正在编写一些将 glb 3d 模型加载到网页中的 Javascript。 Everything is working.一切正常。

But as soon as I import the OrbitControl.js (either from my local folder or online) the browser gives me this error:但是,一旦我导入OrbitControl.js (从我的本地文件夹或在线),浏览器就会给我这个错误:

Uncaught TypeError: Failed to resolve module specifier "three".未捕获的类型错误:无法解析模块说明符“三”。 Relative references must start with either "/", "./", or "../".*相对引用必须以“/”、“./”或“../”开头。*

Here's the full code:这是完整的代码:

import * as THREE from './three.js-master/three.js-master/build/three.module.js'
import {GLTFLoader} from './three.js-master/three.js-master/examples/jsm/loaders/GLTFLoader.js'
import { OrbitControls } from "https://threejs.org/examples/jsm/controls/OrbitControls.js"


const canvas = document.querySelector('.webgl')
const scene =  new THREE.Scene()

const loader = new GLTFLoader()
loader.load('assets/Prova.glb', function(glb){
    const root = glb.scene;
    root.scale.set(1,1,1)
    scene.add(root);
},function(xhr){
   console.log((xhr.loaded/xhr.total * 100) + "% loaded")
}, function(error){
    console.log('An error occured loading gltf')
})

const light = new THREE.DirectionalLight(0xffffff, 1)
light.position.set(2,2,5)
scene.add(light)


const sizes = {
    width: window.innerWidth,
    height: window.innerHeight
}

const camera = new THREE.PerspectiveCamera(75, sizes.width/sizes.height, 0.1, 100)
camera.position.set(0,1,2)
scene.add(camera)




const renderer = new THREE.WebGL1Renderer

({
    canvas: canvas
})

renderer.setSize(sizes.width, sizes.height)
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
renderer.shadowMap.enabled = true
renderer.outputEncoding = THREE.sRGBEncoding

controls = new OrbitControls(camera, canvas)

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

animate()

OrbitControls should be imported from the examples directory, just like you've imported GLTFLoader . OrbitControls应该从examples目录中导入,就像您导入GLTFLoader

So you'll need something like:所以你需要类似的东西:

import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";

Or, since you're using three.js-master in the path:或者,由于您在路径中使用three.js-master

import { OrbitControls } from './three.js-master/three.js-master/examples/jsm/controls/OrbitControls";

I am not sure if u have notice the code in OrbitControls.js .我不确定您是否注意到 OrbitControls.js 中的代码。 Cuz it shows因为它显示

 import { EventDispatcher, MOUSE, Quaternion, Spherical, TOUCH, Vector2, Vector3 } from 'three';

So which means this was import from 'three', and to solve this problem you may need change the 'three' into the address of your 'three.module.js'.所以这意味着这是从'three'导入的,为了解决这个问题,你可能需要将'three'更改为'three.module.js'的地址。 Because the original file of OrbitControls.js was for npm install.因为 OrbitControls.js 的原始文件是用于 npm 安装的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM