数组中的值被覆盖

时间:2018-08-09 04:21:36

标签: javascript arrays

meshEdge中的值被gemoetry的最后一个值覆盖。我已经尝试过了,但问题仍然存在。

var geometry = new THREE.BufferGeometry();
var meshEdges = new Array();
var firstPoint = [];
for (i = 0; i < json.edges.length; i++) {
    firstPoint[i] = json.edges[i].edeVertices;
    positions = new Float32Array([firstPoint[i][0] , firstPoint[i][1], firstPoint[i][2], firstPoint[i][3], firstPoint[i][4], firstPoint[i][5]]);
    geometry.addAttribute('position', new THREE.BufferAttribute(positions,3));
    meshEdges.push(geometry);

    }

任何想法我在做什么错。 预先感谢。

1 个答案:

答案 0 :(得分:1)

您一次又一次地推动相同的geometry。数组对此具有引用,并且当几何图形更新时,数组值也会更新。 因此,每次创建一个新的几何图形并将其推入。

 var meshEdges = new Array();
 var firstPoint = [];
 for (i = 0; i < json.edges.length; i++) {
   var geometry = new THREE.BufferGeometry();
   firstPoint[i] = json.edges[i].edeVertices;
   positions = new Float32Array([firstPoint[i][0] , firstPoint[i][1], firstPoint[i][2], firstPoint[i][3], firstPoint[i][4], firstPoint[i][5]]);
   geometry.addAttribute('position', new THREE.BufferAttribute(positions,3));
   meshEdges.push(geometry); 
 }