three。js报错TypeError: Cannot read properties of undefined (reading 'SphereGeometry')
时间: 2023-11-27 22:03:39 浏览: 123
Vue打包Cannot read property 'tapPromise' of undefined报错处理.docx
5星 · 资源好评率100%
在Three.js中,当出现“TypeError: Cannot read properties of undefined (reading 'SphereGeometry')”这样的错误时,通常是因为没有正确导入所需的Three.js库或模块。请确保已正确导入所需的Three.js库或模块,并检查代码中是否存在拼写错误或语法错误。
以下是一个创建球体的Three.js示例代码,可以作为参考:
```javascript
import * as THREE from 'three';
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const geometry = new THREE.SphereGeometry(5, 32, 32);
const material = new THREE.MeshBasicMaterial({ color: 0xffff00 });
const sphere = new THREE.Mesh(geometry, material);
scene.add(sphere);
camera.position.z = 10;
function animate() {
requestAnimationFrame(animate);
sphere.rotation.x += 0.01;
sphere.rotation.y += 0.01;
renderer.render(scene, camera);
}
animate();
```
阅读全文