Three.js + OrbitControls - 未捕获的类型错误:无法读取未定义的属性

我正在尝试OrbitControls在 THREE.js 中使用。如果我let cameraControl = new OrbitControls(camera)从下面删除该行,将不会出现错误。但是现在,我有“Uncaught TypeError: Cannot read property 'addEventListener' of undefined”


我尝试更改OrbitControls(camera)为THREE.OrbitControls(camera),然后出现“Uncaught TypeError: THREE.OrbitControls is not a constructor”。


我尝试OrbitControls.js使用<script src=...></script>outside导入"module",而不是import {OrbitControls} from ...;,但它不起作用,我也尝试移动let cameraControl = new OrbitControls(camera)到其他行,但也不起作用。


任何想法如何解决?



<body>


<script type="module">

import * as THREE from 'https://threejsfundamentals.org/threejs/resources/threejs/r115/build/three.module.js';

import {OrbitControls} from 'https://threejsfundamentals.org/threejs/resources/threejs/r115/examples/jsm/controls/OrbitControls.js';



let scene, renderer, camera

let cube


function init() {


  scene = new THREE.Scene()

  renderer = new THREE.WebGLRenderer()

  renderer.setSize(window.innerWidth, window.innerHeight)

  document.body.appendChild(renderer.domElement)


  camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 100)


  let cameraControl = new OrbitControls(camera)


  camera.position.set(10, 10, 10)

  camera.lookAt(scene.position)


  // cube

  cube = new THREE.Mesh(new THREE.BoxGeometry(1, 1, 1))

  scene.add(cube)


}


function render() {

  requestAnimationFrame(render)

  renderer.render(scene, camera)

}


init()

render()


</script>

</body>


一只甜甜圈
浏览 303回答 1
1回答

慕虎7371278

让 cameraControl = new OrbitControls(camera)始终像这样创建控件:let&nbsp;cameraControl&nbsp;=&nbsp;new&nbsp;OrbitControls(camera,&nbsp;renderer.domElement);第二个参数现在是强制性的。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript