这是代码,它在我的浏览器中运行。似乎具有纹理的对象正确呈现,但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();
答案 0 :(得分:1)
当您尝试旋转对象时,很可能尚未加载对象。这应该可以解决问题:
if ( mycylinder !== undefined ) {
mycylinder.rotation.x += 0.01;
mycylinder.rotation.y += 0.01;
}