简体   繁体   English

Three.js和colladaloader

[英]Three.js and colladaloader

I am new to Three.JS and am trying to load a load a very simple (single cube) Sketchup model into Three.JS via the ColladaLoader, I get no errors but nothing is displayed: 我是Three.JS的新手,我试图通过ColladaLoader将一个非常简单的(单一立方体)Sketchup模型加载到Three.JS中,我没有得到任何错误,但没有显示任何内容:

var renderer = new THREE.WebGLRenderer();
var loader = new THREE.ColladaLoader();
var scene = new THREE.Scene();
var camera = new THREE.Camera();
loader.load('Test.dae', function (result) {
  scene.add(result.scene);
});
camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.set(0, 0, 5);
scene.add(camera);
renderer.render(scene,camera);

Can anyone spot any immediate errors? 有人能发现任何即时错误吗? Thanks 谢谢

Fixed. 固定。 Whilst I had declared the renderer, I hadn't attached it to the document. 虽然我已经宣布了渲染器,但我没有将它附加到文档中。 The following code works: 以下代码有效:

var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(100, window.innerWidth / window.innerHeight, 0.1, 1000);
var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
camera.position.set(0, 0, 4);
var loader = new THREE.ColladaLoader();
loader.load("test.dae", function (result) {
    scene.add(result.scene);
});

function render() {
    requestAnimationFrame(render);
    renderer.render(scene, camera);
}
render();

You need to rerender the scene after the collada is loaded. 加载collada后需要重新渲染场景。 Something like 就像是

loader.load('Test.dae', function (result) {
  scene.add(result.scene);
  renderer.render(scene,camera);
});

Additionally: 另外:

  • The camera might be inside your geometry. 相机可能在您的几何体内。 Faces are one-sided by default, they are invisible when looking from back/inside. 默认情况下,面部是单侧的,从后面/内部看时它们是不可见的。 You could try changing the camera or object location and/or set material.side = THREE.DoubleSide so the faces are visible from both front and back. 您可以尝试更改相机或对象位置和/或设置material.side = THREE.DoubleSide,以便从正面和背面都可以看到面部。
  • The scale of the model can be way off, so it can be too small or large to show. 模型的比例可以偏离,因此它可能太小或太大而无法显示。 Try different camera positions (eg. z= -1000, -100, -10, -1, 1, 10, 100, 1000) and use lookAt() to point it to 0,0,0 or, I think colladaloader has a scale setting nowadays, not sure. 尝试不同的相机位置(例如,z = -1000,-100,-10,-1,1,10,100,1000)并使用lookAt()将其指向0,0,0或者,我认为colladaloader有一个现在规模设定,不确定。
  • Loader defaults to position 0,0,0, so center of the world/scene. Loader默认为位置0,0,0,因此是世界/场景的中心。 That doesn't necessarily mean center of screen, depends on camera. 这并不一定意味着屏幕的中心,取决于相机。 In some cases the Collada model itself can be such that the center is away from visible objects, so when it's placed on the scene it can be effectively "off-center". 在某些情况下,Collada模型本身可以使得中心远离可见物体,因此当它放置在场景上时,它可以有效地“偏离中心”。 That's quite unlikely though. 但这不太可能。
  • You dont have any lights in the scene. 你现场没有任何灯光。
    loader.load('test.dae', function colladaReady(collada) {

    localObject = collada.scene;

    localObject.scale.x = localObject.scale.y = localObject.scale.z = 2;
            localObject.updateMatrix();
scene.add(localObject);

I think you need to add the object in the collada scene, or there might be problem with the scaling of the object that you are adding, scale it and than update the matrix of the object. 我认为您需要在collada场景中添加对象,或者您正在添加的对象的缩放可能存在问题,缩放它并更新对象的矩阵。

如果您仍然遇到问题,可能是某些版本的IE停止下载本地文件(您的Test.dae),因此可能值得使用firefox或将您的代码放在服务器上进行尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM