我在javascript中有三个函数,parsing
,init
和animate
。必须首先调用Parsing
因为没有它,positions
数组不会被修改,函数init
需要它。奇怪的是,动画需要由controls
创建的init
变量,但它们曾经同时被调用并且之前有效。我已经尝试了调用函数的所有组合,但controls
未定义或positions
。这是我代码的最后一次迭代。
var positions = [];
parsing();
function parsing() {
fetch('Petit_film_256_atomes.txt').then(response => response.text()).then(text => {
const arr = text.split('\n')
.map(line => line.trim())
.map((arr) => arr.split(' '))
.map(([size, x, y, z]) => ({
size: Number(size),
x: Number(x),
y: Number(y),
z: Number(z)
}));
while (arr.length > 0) {
positions.push(arr.splice(0, size).slice(2));
}
init();
animate();
})
}
...
function init() {
renderer = new THREE.WebGLRenderer({antialias:true});
renderer.setSize(WIDTH, HEIGHT);
document.body.appendChild(renderer.domElement);
...
console.log(positions);
for (var i = 0; i < 256; i++) {
atom = sphere.clone();
atom.position.set( positions[0][i].x, positions[0][i].y, positions[0][i].z );
scene.add( atom );
group.push( atom );
}
...
controls = new THREE.OrbitControls(camera, renderer.domElement);
}
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
controls.update();
}
此外,console.log(positions)
显示非空数组,但显示的错误仍然是positions
未定义。任何人都知道为什么?