简体   繁体   English

Three.js-无法从函数对象检索对象

[英]Three.js - Can't retrieve objects from function objects

Here's my function. 这是我的职能。

function Planet(radius, c) {

    var sphere = new THREE.Mesh(
        new THREE.SphereGeometry(radius, 64, 64),
        new THREE.MeshLambertMaterial({ color: c })
    );

}

I then create an object out of the function. 然后,我从函数中创建一个对象。

var planet = new Planet(1, 0xffffff);

Finally I attempt to add the planet object's sphere mesh to the three.js scene. 最后,我尝试将行星对象的球体网格添加到three.js场景。

scene.add(planet.sphere);

I get no errors in the Chrome JavaScript console, but the sphere isn't showing up. 我在Chrome JavaScript控制台中没有收到任何错误,但没有显示。 If I create a sphere mesh from outside the Planet function and add it to the scene, it works. 如果我从Planet函数外部创建一个球体网格并将其添加到场景中,那么它将起作用。 It wouldn't really make sense to have everything outside functions in the long run, though. 从长远来看,让所有外部功能都没有真正意义。 I'm eventually going to have to create multiple arrays to store a single planet. 我最终将不得不创建多个数组来存储一个星球。

try 尝试

function Planet(radius, c) {

    this.sphere = new THREE.Mesh(
        new THREE.SphereGeometry(radius, 64, 64),
        new THREE.MeshLambertMaterial({ color: c })
    );

}

var planet = new Planet(1, 0xffffff );
scene.add(planet.sphere);

or 要么

function Planet(radius, c) {

    return new THREE.Mesh(
        new THREE.SphereGeometry(radius, 64, 64),
        new THREE.MeshLambertMaterial({ color: c })
    );

}

var planet = new Planet(1, 0xffffff);
scene.add(planet);

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

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