如何从three.js文件中最好地导出/导入THREE.Vector3数组?

时间:2015-09-10 21:01:55

标签: javascript arrays json three.js

我有一大堆THREE.Vector3()我希望保存到JSON文件以便稍后阅读。

我可以把它写成JSON.stringify(Points),它输出一个对象数组,如下所示:

  "Points": [
        {"x":115,"y":0,"z":-250},
        {"x":115,"y":0,"z":-220},
        {"x":115,"y":0,"z":-190},
        ...
  ]

现在,如果我转过身来读回来的话,我会松开我的三个工作人员,并且必须按照以下方式单独重新分配:

   newPoint[nn] = new THREE.Vector3(jsonData.Points[nn].x,jsonData.Points[nn].y,jsonData.Points[nn].z);

也许我只是希望获得某种魔力,但我觉得这样可以更有效率。有没有更好的方法来读取/写入JSON中的three.js向量数组?

2 个答案:

答案 0 :(得分:1)

我的项目中有类似的任务。我只是用浮点数来保持点数。它允许减小文件大小。例如:

{
    points: [x0, y0, z0, x1, y1, z1, ..., xn, yn, zn]
}

也可以将坐标enter image description here

分组

要保留THREE.Vector3对象,您可以使用js文件:

var pointList = [new THREE.Vector3(x0, y0, z0), new THREE.Vector3(x1, y1, z1), ..., new THREE.Vector3(xn, yn, zn)];

答案 1 :(得分:0)

示例PART1。您可以使用for循环重新创建您的three.Vector3;

//Let's assume, at a given time you have this arrays;

let x_array=[-10,-5,20,10];
let y_array=[0,15,15,0];
let z_array=[0,0,0,0];


//This loop re_create yours three.Vector3;
let array_of_threejs_points=[];
let i=0;
for(i=0; i<x_array.length; i++)
{
    array_of_threejs_points.push(new THREE.Vector3(x_array[i],y_array[i],z_array[i]));
}


console.log('array_of_threejs_points'); 
console.log(array_of_threejs_points);

示例第2部分。 将这三个Vector3传递到一条曲线,在此示例中为BezierCurve。

let curve = new THREE.CubicBezierCurve3(array_of_threejs_points[0], array_of_threejs_points[1], array_of_threejs_points[2], array_of_threejs_points[3]); 

是的。我知道。 PART2应该得到增强;

PS1。在此示例中,threejs_point是three.Vector3对象。

PS2。在此示例中,最重要的说明是:

array_of_threejs_points.push(new THREE.Vector3(x_array[i],y_array[i],z_array[i]));