1

I am trying to use three.js library which is a javascript 3d library...

So the code snippet is this:

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);

So basically earlier this code .. the radius was hardcoded (second line) but instead of hardcoding radius.. I am reading it from json file which is following?

{
 "circles":

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

But it doesnt show anything? And I dont see any error in firebug as well Any suggestions? Thanks

1
  • is it working for you directly, without the JSON call? Commented Feb 23, 2013 at 22:14

1 Answer 1

1

The call to $.getJSON(...) is asynchronous. So the order of statements is most likely

  1. var scene = new THREE.Scene();
  2. scene.add(camera);
  3. var sphere = new THREE.Mesh(...);
  4. scene.add(sphere);

If you move scene.add(camera); into your success function, it should work as before.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks.. It doesnt work. I am getting a warning "DEPRECATED: Camera hasn't been added to a Scene. Adding it..." What is the right way to get this thing read the data from and pass it as a parameter?
@user2052251 What was the order before your change to getJSON?
Please take a look at above link.
@user2052251 Then you must move all the code up to render(...) into your success function, of course.
Hi.. Now I updated the json that instead of one circle there is a list of circles and it fails again... can you please take a look at it jsfiddle.net/AWDg7/2
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.