无法渲染在jQuery $ .get请求函数中添加到场景中的三个js对象

时间:2015-01-03 00:55:37

标签: javascript jquery three.js

我是一名三人初学者。我尝试使用JavaScript get请求返回的位置坐标向场景添加球体。

在正确渲染get请求之前创建的球体,但是不会渲染在回调函数中创建并添加到场景中的球体 - 尽管如果我调试并检查场景,则两个球体都作为其子节点存在。

我的代码:

var sphere = new THREE.Mesh(
  new THREE.SphereGeometry(radius),
  sphereMaterial);
sphere.position.set(-20,-20,-20);
scene.add(sphere); // this sphere shows

sphere2 = sphere.clone();
sphere2.position.set(50,50,50); // testing initializing outside 

$.get("{% /graph %}",function(data,status){
  scene.add(sphere2); // this sphere does not show
});

renderer.render(scene, camera);

我尝试初始化回调内外的第二个球体,我尝试创建新球体而不是克隆,我不知道还有什么可以尝试而且我不知道我是什么&我#39;我失踪了。

1 个答案:

答案 0 :(得分:2)

jQuery get请求是异步请求。直到渲染调用之后才会调用回调函数。这意味着该对象将被添加到场景中,但从未被绘制。

您需要在get处理程序中调用render函数。

$.get("{% /graph %}",function(data,status){
  scene.add(sphere2);
  renderer.render(scene, camera);
});

或者,如果为动画之类的东西创建渲染循环,则不需要这样做。