为什么JavaScript控制台中没有定义对象'在渲染函数中的意见?(three.js)

Why is the object not defined in the JavaScript console's opinion in the rendering function? (three.js)

本文关键字:函数 js three 控制台 JavaScript 定义 对象 为什么      更新时间:2023-09-26

这是代码,它正在我的浏览器中运行。看起来具有纹理的对象已经正确渲染,但JavaScript控制台写道,mycylinder没有定义。为什么?我该如何解决这个问题?

var texture,material,mycylinder;
var WIDTH = 400, HEIGHT = 300;
var VIEW_ANGLE = 45, ASPECT =WIDTH/HEIGHT, NEAR=0.1, FAR =10000;
var renderer= new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
var camera= new THREE.PerspectiveCamera(VIEW_ANGLE, window.innerWidth / window.innerHeight, NEAR, FAR);
var scene= new THREE.Scene();
scene.add(camera);
camera.position.set(0,150,400);
camera.lookAt(scene.position);
document.body.appendChild( renderer.domElement );
var light = new THREE.PointLight(0xffffff);
light.position.set(0,250,0);
scene.add(light);
var ambientLight = new THREE.AmbientLight(0x444444);
scene.add(ambientLight);
var cylinderLoader = new THREE.JSONLoader();
cylinderLoader.load( "models/probahenger.js", addModel );
function addModel( geometry, materials ) {
            texture= new THREE.ImageUtils.loadTexture("images/Henger_anyag3.png");
            material= new THREE.MeshLambertMaterial({map: texture});
            mycylinder = new THREE.Mesh( geometry,material);                
            mycylinder.scale.set(30,30,30);
            mycylinder.position.y=0;
            scene.add( mycylinder );
        };
function render() {
        requestAnimationFrame(render);
        mycylinder.rotation.x += 0.01;
        mycylinder.rotation.y += 0.01;
        renderer.render(scene, camera); 
}
render();

很可能在你试图旋转对象时,对象还没有加载

if ( mycylinder !== undefined ) {
    mycylinder.rotation.x += 0.01;
    mycylinder.rotation.y += 0.01;
}