由于此错误,我无法执行我的代码:
TypeError: THREE.Scene is not a constructor
我使用来自官方 github 存储库的 three.js 文件,这是我的文件:
索引.html
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../style.css">
<title>First three.js</title>
</head>
<body>
<script type="module" src="../frontend/tetrahedrons_render.js"></script>
</body>
</html>
四面体渲染.js
import * as THREE from '../lib/three.js';
// Create the scene
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x2d3436);
// Set up the camera
const camera = new THREE.PerspectiveCamera(100, window.innerWidth / window.innerHeight, 0.1, 1000);
// Set up the render
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
document.body.appendChild(renderer.domElement);
// Create n random tetrahedrons
const n = Math.floor(Math.random() * 7) + 2;
const tetrahedrons = [];
const positions = [];
let x, y;
for (let i = 0; i < n; i++) {
do {
x = Math.floor(Math.random() * 10) - 5;
y = Math.floor(Math.random() * 10) - 5;
} while ([x, y] in positions);
positions.push([x, y]);
tetrahedrons.push(createTetrahedron(x * 2.5, y * 2.5));
}
// Color one random tetrahedron in black and another in white
const i = Math.floor(Math.random() * tetrahedrons.length);
let i2;
do {
i2 = Math.floor(Math.random() * tetrahedrons.length);
} while (i2 === i);
tetrahedrons[i].geometry.faces.forEach((face) => {
face.color = new THREE.Color('black');
});
tetrahedrons[i2].geometry.faces.forEach((face) => {
face.color = new THREE.Color('white');
});
camera.position.z = 15;
function animate() {
requestAnimationFrame(animate);
// Rotation
tetrahedrons.forEach((tetrahedron) => {
tetrahedron.rotation.x += 0.03;
tetrahedron.rotation.y -= 0.03;
});
renderer.render(scene, camera);
}
这是所有代码,但我认为问题必须在第一行导入。
我看到很多帖子都有类似的错误,但我找不到适合我的解决方案。
暮色呼如
相关分类