0

I am new to both three.js and javascript so this might be a very noob question.

I have a list of json objects in a file which contains data as following:

[
{ "x":x,"y":y,"z":z ,"r":r},
{ "x":x2,"y":y2,"z":z2 ,"r":r2},
{ "x":x3,"y":y3,"z":z 3,"r":r3}
]

And render it on screen.

Its exactly an example here: http://mrdoob.github.com/three.js/examples/webgl_materials2.html

except that sphere values I am reading from a file?

How do i tweak the example above that i read the values from this file instead of directly hardcoding these values in js?

Please help THanks

user2052251
  • 161
  • 2
  • 12

1 Answers1

1

If all you want to do is separate the data from your application logic, then you don't need to create a JSON file and load it via Ajax, you can just create another JavaScript file and put the data in there.

For example:

// myData.js
var myData = [
    { "x":x,"y":y,"z":z ,"r":r},
    { "x":x2,"y":y2,"z":z2 ,"r":r2},
    // ...
];

// --------------

// myApplication.js

// Here goes the three.js code and you can access the data here, for example
alert(myData[0].x);

In your HTML file, you have to include the data file first, so that all following scripts have access to the myData variabe:

<script src="myData.js"></script>
<script src="myApplication.js"></script>
<!-- this works to: -->
<script>
    alert(myData.length);
</script>

If you want to or have to load the file via Ajax, have a look at this question: How do I load a JSON object from a file with ajax?.

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143