1

我正在尝试使用 three.js 库,它是一个 javascript 3d 库...

所以代码片段是这样的:

var segments = 16, rings = 16;
//var radius = 50;  <-- original

// create a new mesh with sphere geometry -
// we will cover the sphereMaterial next!
var scene = new THREE.Scene();
// My read from file snippet.
$.getJSON('sample.json', function(data) {  
     var radius = data.circles.r;
     console.log(radius);


    var sphere = new THREE.Mesh(
       new THREE.SphereGeometry(radius, segments, rings),
       sphereMaterial);

    // add the sphere to the scene
    scene.add(sphere);
});
// and the camera
scene.add(camera);

所以基本上早先这段代码..半径是硬编码的(第二行),而不是硬编码半径..我是从下面的json文件中读取它的?

{
 "circles":

{"x":14,"y":2,"z":4,"r":20}
}

但它没有显示任何东西?而且我在萤火虫中也没有看到任何错误有什么建议吗?谢谢

4

1 回答 1

1

调用$.getJSON(...)是异步的。所以语句的顺序最有可能

  1. var 场景 = new THREE.Scene();
  2. 场景。添加(相机);
  3. var sphere = new THREE.Mesh(...);
  4. 场景.添加(球体);

如果你scene.add(camera);进入你的成功功能,它应该像以前一样工作。

于 2013-02-23T21:31:59.150 回答