我正在尝试保存Geometry对象的顶点的原始坐标的副本。我试图通过在stackoverflow上找到很多答案的方式来复制对象。



// create a Geometry object from a BufferGeometry
cityGeometry = new THREE.Geometry().fromBufferGeometry( child.geometry );

// properly copy the cityGeometry.vertices array
var originalPositions = cityGeometry.vertices.slice(0);

// change an element of the copy
originalPositions[0].z = 1000;
console.log(originalPositions[0]);
console.log(cityGeometry.vertices[0]);

// now change the original object
cityGeometry.vertices[0].z = 123;
console.log(originalPositions[0]);
console.log(cityGeometry.vertices[0]);




但是,如果执行此操作,则两个对象总是同时更改。控制台输出为:



Vector3 {x: 0.8206260204315186, y: -0.4336470067501068, z: 1000}
Vector3 {x: 0.8206260204315186, y: -0.4336470067501068, z: 1000}
Vector3 {x: 0.8206260204315186, y: -0.4336470067501068, z: 123}
Vector3 {x: 0.8206260204315186, y: -0.4336470067501068, z: 123}




我在这里想念什么吗?谢谢!

最佳答案

您可以使用:

originalPositions = JSON.parse(JSON.stringify( cityGeometry.vertices ));


如array.slice()只做一个浅表副本。

09-17 10:21