gpt4 book ai didi

javascript - THREE.js:错误消息 "THREE.OBJLoader is not a constructor"

转载 作者:数据小太阳 更新时间:2023-10-29 03:49:47 29 4
gpt4 key购买 nike

我刚开始学习three.js的使用。看起来很好,但现在我有一个问题,我无法解决。

我想加载一个 OBJ 文件,这是我之前在 blender 中创建的。为此,我正在尝试使用 THREE.OBJloader。我从 http://mamboleoo.be/learnThree/ 复制了代码,但我在第 32 行收到错误消息“THREE.OBJLoader 不是构造函数”。

其他一切正常:添加场景、添加 Material 、添加立方体等。

为了简单起见,这是代码:

var renderer, scene, camera, banana;
var ww = window.innerWidth,
wh = window.innerHeight;

function init(){

renderer = new THREE.WebGLRenderer({canvas : document.getElementById('scene')});
renderer.setSize(ww,wh);

scene = new THREE.Scene();

camera = new THREE.PerspectiveCamera(50,ww/wh, 0.1, 10000 );
camera.position.set(0,0,500);
scene.add(camera);

//Add a light in the scene
directionalLight = new THREE.DirectionalLight( 0xffffff, 0.8 );
directionalLight.position.set( 0, 0, 350 );
directionalLight.lookAt(new THREE.Vector3(0,0,0));
scene.add( directionalLight );

//Load the obj file
loadOBJ();
}

var loadOBJ = function(){

//Manager from ThreeJs to track a loader and its status
var manager = new THREE.LoadingManager();
//Loader for Obj from Three.js
var loader = new THREE.OBJLoader( manager );
//Launch loading of the obj file, addBananaInScene is the callback when it's ready
loader.load( 'http://mamboleoo.be/learnThree/demos/banana.obj', addBananaInScene);

};

var addBananaInScene = function(object){
banana = object;
//Move the banana in the scene
banana.rotation.x = Math.PI/2;
banana.position.y = -200;
banana.position.z = 50;
//Go through all children of the loaded object and search for a Mesh
object.traverse( function ( child ) {
//This allow us to check if the children is an instance of the Mesh constructor
if(child instanceof THREE.Mesh){
child.material.color = new THREE.Color(0X00FF00);
//Sometimes there are some vertex normals missing in the .obj files, ThreeJs will compute them
child.geometry.computeVertexNormals();
}
});
//Add the 3D object in the scene
scene.add(banana);
render();
};


var render = function () {
requestAnimationFrame(render);

//Turn the banana
banana.rotation.z += .01;

renderer.render(scene, camera);
};

init();

我希望有人知道这可能来自哪里。

问候,克里斯蒂安

最佳答案

当您从 Codepen 示例中复制时,请始终转到笔并在“设置”下检查以查看项目中使用的任何其他文件。

在您的例子中,作者使用的是 OBJLoader.js这就是 OBJLoader 构造函数的来源,您没有对它的引用。因此,你得到了错误。包含引用,它应该可以正常工作。

关于javascript - THREE.js:错误消息 "THREE.OBJLoader is not a constructor",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35585353/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com