javascript - Can't import OrbitControls.js with Three.js - Stack Overflow

I'm going over three.js, and found this example..htmlUnfortunately, I keep getting this error fo

I'm going over three.js, and found this example. .html Unfortunately, I keep getting this error for all three imports when I run it locally using Flask.

import * as THREE from '.module.js';
import {OrbitControls} from '.js';
import {GLTFLoader} from '.js';

Uncaught SyntaxError: Cannot use import statement outside a module

I've looked high and low for solutions, and can't find any.

I've also tried to run the three imports with script tags.

<script src="{{url_for('static',filename='js/three/build/three.js')}}"></script>
<script type="module" src="{{url_for('static',filename='js/three/examples/jsm/controls/OrbitControls.js')}}"></script>
<script type="module" src="{{url_for('static',filename='js/three/examples/jsm/loaders/GLTFLoader.js')}}"></script>

This works, but when I try the code below, I get: Uncaught ReferenceError: OrbitControls is not defined

const controls = new OrbitControls(camera, canvas);
controls.target.set(0, 5, 0);
controls.update();

Also, if I try either for GLTFLoader, I get an error.

const loader = new THREE.GLTFLoader();

or

const loader = new GLTFLoader();

Any idea how I can solve this would be appreciated.

I'm going over three.js, and found this example. https://threejsfundamentals/threejs/lessons/threejs-load-gltf.html Unfortunately, I keep getting this error for all three imports when I run it locally using Flask.

import * as THREE from 'https://threejsfundamentals/threejs/resources/threejs/r122/build/three.module.js';
import {OrbitControls} from 'https://threejsfundamentals/threejs/resources/threejs/r122/examples/jsm/controls/OrbitControls.js';
import {GLTFLoader} from 'https://threejsfundamentals/threejs/resources/threejs/r122/examples/jsm/loaders/GLTFLoader.js';

Uncaught SyntaxError: Cannot use import statement outside a module

I've looked high and low for solutions, and can't find any.

I've also tried to run the three imports with script tags.

<script src="{{url_for('static',filename='js/three/build/three.js')}}"></script>
<script type="module" src="{{url_for('static',filename='js/three/examples/jsm/controls/OrbitControls.js')}}"></script>
<script type="module" src="{{url_for('static',filename='js/three/examples/jsm/loaders/GLTFLoader.js')}}"></script>

This works, but when I try the code below, I get: Uncaught ReferenceError: OrbitControls is not defined

const controls = new OrbitControls(camera, canvas);
controls.target.set(0, 5, 0);
controls.update();

Also, if I try either for GLTFLoader, I get an error.

const loader = new THREE.GLTFLoader();

or

const loader = new GLTFLoader();

Any idea how I can solve this would be appreciated.

Share Improve this question edited Jan 23, 2021 at 22:24 davidism 128k31 gold badges415 silver badges347 bronze badges asked Jan 23, 2021 at 21:25 MichaelRSFMichaelRSF 8966 gold badges21 silver badges42 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You need to say that your script tag is of type module. So what you need is something like:

<html>
  <body>
    <script type="module">
      import * as THREE from 'https://threejsfundamentals/threejs/resources/threejs/r122/build/three.module.js';
      import {OrbitControls} from 'https://threejsfundamentals/threejs/resources/threejs/r122/examples/jsm/controls/OrbitControls.js';
      import {GLTFLoader} from 'https://threejsfundamentals/threejs/resources/threejs/r122/examples/jsm/loaders/GLTFLoader.js';
    </script>
  </body>
</html>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745541428a4632145.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信