admin管理员组文章数量:1425868
I am having trouble reading in data from a JSON file. I have gone through the other reading JSON questions on Stack Overflow and the JSON docs to no avail.
I am trying to read in data to be displayed in Three.js. The following snippet works:
var obj = { "points" : [
{ "vertex":[0.0,0.0,0.0] },
{ "vertex":[200.0,0.0,0.0] },
{ "vertex":[-200.0,0.0,0.0] },
{ "vertex":[0.0,100.0,0.0] },
{ "vertex":[0.0,-100.0,0.0] },
{ "vertex":[0.0,0.0,300.0] },
{ "vertex":[0.0,0.0,-300.0] },
{ "vertex":[75.0,75.0,75.0] },
{ "vertex":[50.0,50.0,50.0] },
{ "vertex":[25.0,25.0,25.0] } ]};
var geometry = new THREE.Geometry();
for (var i=0;i<10;i++)
{
var vertex = new THREE.Vector3();
vertex.x = obj.points[i].vertex[0];
vertex.y = obj.points[i].vertex[1];
vertex.z = obj.points[i].vertex[2];
geometry.vertices.push( vertex );
}
It reads in the points and the points are rendered further down the line. I have a file next to the index.html named
test.json
it contains the following:
{ "points" : [
{ "vertex":[0.0,0.0,0.0] },
{ "vertex":[200.0,0.0,0.0] },
{ "vertex":[-200.0,0.0,0.0] },
{ "vertex":[0.0,100.0,0.0] },
{ "vertex":[0.0,-100.0,0.0] },
{ "vertex":[0.0,0.0,300.0] },
{ "vertex":[0.0,0.0,-300.0] },
{ "vertex":[75.0,75.0,75.0] },
{ "vertex":[50.0,50.0,50.0] },
{ "vertex":[25.0,25.0,25.0] }
]}
My problem is that the following doesn't work (i.e. doesn't display the points):
var geometry = new THREE.Geometry();
var obj2 = jQuery.getJSON('test.json');
for (var i=0;i<10;i++)
{
var vertex = new THREE.Vector3();
vertex.x = obj2.points[i].vertex[0];
vertex.y = obj2.points[i].vertex[1];
vertex.z = obj2.points[i].vertex[2];
geometry.vertices.push( vertex );
}
The files are in the public folder of my dropbox and viewed from the public link, even so, just in case i'm running chrome with the --allow-files-access-from-files flag.
UPDATE
My key mistake was not processing the vertices in the callback of the getJSON function. My secondary mistake was assuming that adding the vertices to the geometry stack in the callback was enough. In fact I had to create the object from the geometry and add it to the scene for it to work. Many thanks for everyones help.
$.getJSON('test.json', function(obj2) {
var geometry = new THREE.Geometry();
for (var i=0;i<10;i++)
{
var vertex = new THREE.Vector3();
vertex.x = obj2.points[i].vertex[0];
vertex.y = obj2.points[i].vertex[1];
vertex.z = obj2.points[i].vertex[2];
geometry.vertices.push( vertex );
}
vc1 = geometry.vertices.length;
object = new THREE.ParticleSystem( geometry, shaderMaterial );
object.dynamic = true;
var vertices = object.geometry.vertices;
var values_size = attributes.size.value;
var values_color = attributes.ca.value;
for( var v = 0; v < vertices.length; v ++ ) {
values_size[ v ] = 50;
values_color[ v ] = new THREE.Color( 0xffffff );
}
scene.add( object );
});
I am having trouble reading in data from a JSON file. I have gone through the other reading JSON questions on Stack Overflow and the JSON docs to no avail.
I am trying to read in data to be displayed in Three.js. The following snippet works:
var obj = { "points" : [
{ "vertex":[0.0,0.0,0.0] },
{ "vertex":[200.0,0.0,0.0] },
{ "vertex":[-200.0,0.0,0.0] },
{ "vertex":[0.0,100.0,0.0] },
{ "vertex":[0.0,-100.0,0.0] },
{ "vertex":[0.0,0.0,300.0] },
{ "vertex":[0.0,0.0,-300.0] },
{ "vertex":[75.0,75.0,75.0] },
{ "vertex":[50.0,50.0,50.0] },
{ "vertex":[25.0,25.0,25.0] } ]};
var geometry = new THREE.Geometry();
for (var i=0;i<10;i++)
{
var vertex = new THREE.Vector3();
vertex.x = obj.points[i].vertex[0];
vertex.y = obj.points[i].vertex[1];
vertex.z = obj.points[i].vertex[2];
geometry.vertices.push( vertex );
}
It reads in the points and the points are rendered further down the line. I have a file next to the index.html named
test.json
it contains the following:
{ "points" : [
{ "vertex":[0.0,0.0,0.0] },
{ "vertex":[200.0,0.0,0.0] },
{ "vertex":[-200.0,0.0,0.0] },
{ "vertex":[0.0,100.0,0.0] },
{ "vertex":[0.0,-100.0,0.0] },
{ "vertex":[0.0,0.0,300.0] },
{ "vertex":[0.0,0.0,-300.0] },
{ "vertex":[75.0,75.0,75.0] },
{ "vertex":[50.0,50.0,50.0] },
{ "vertex":[25.0,25.0,25.0] }
]}
My problem is that the following doesn't work (i.e. doesn't display the points):
var geometry = new THREE.Geometry();
var obj2 = jQuery.getJSON('test.json');
for (var i=0;i<10;i++)
{
var vertex = new THREE.Vector3();
vertex.x = obj2.points[i].vertex[0];
vertex.y = obj2.points[i].vertex[1];
vertex.z = obj2.points[i].vertex[2];
geometry.vertices.push( vertex );
}
The files are in the public folder of my dropbox and viewed from the public link, even so, just in case i'm running chrome with the --allow-files-access-from-files flag.
UPDATE
My key mistake was not processing the vertices in the callback of the getJSON function. My secondary mistake was assuming that adding the vertices to the geometry stack in the callback was enough. In fact I had to create the object from the geometry and add it to the scene for it to work. Many thanks for everyones help.
$.getJSON('test.json', function(obj2) {
var geometry = new THREE.Geometry();
for (var i=0;i<10;i++)
{
var vertex = new THREE.Vector3();
vertex.x = obj2.points[i].vertex[0];
vertex.y = obj2.points[i].vertex[1];
vertex.z = obj2.points[i].vertex[2];
geometry.vertices.push( vertex );
}
vc1 = geometry.vertices.length;
object = new THREE.ParticleSystem( geometry, shaderMaterial );
object.dynamic = true;
var vertices = object.geometry.vertices;
var values_size = attributes.size.value;
var values_color = attributes.ca.value;
for( var v = 0; v < vertices.length; v ++ ) {
values_size[ v ] = 50;
values_color[ v ] = new THREE.Color( 0xffffff );
}
scene.add( object );
});
Share
Improve this question
edited Mar 12, 2013 at 14:02
StuGrey
asked Mar 12, 2013 at 13:09
StuGreyStuGrey
1,4779 silver badges20 bronze badges
1
- Sorry, it doesn't render any of the points. Updated the question. – StuGrey Commented Mar 12, 2013 at 13:12
3 Answers
Reset to default 5Read the docs for getJSON, it is an asynchronous call!
You need to put the for loop in in the callback.
I think you should do it like this
$.getJSON('test.json', function(obj2) {
for (var i=0;i<10;i++)
{
var vertex = new THREE.Vector3();
vertex.x = obj2.points[i].vertex[0];
vertex.y = obj2.points[i].vertex[1];
vertex.z = obj2.points[i].vertex[2];
geometry.vertices.push( vertex );
}
});
Edit1 typo
Seems like tiny mistake. You can do following:
1) Check whether test.json is referenced in your html page or not?
2) Use firebug (console tab) to check exactly what is returned by getJson method
本文标签: javascriptReading an array of points from a JSON fileStack Overflow
版权声明:本文标题:javascript - Reading an array of points from a JSON file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745459510a2659257.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论