我正在查看 Three.js 中的代码,特别是创建球体的 THREE.SphereGeometry 方法: https://github.com/mrdoob/three.js/blob/master/src/extras/geometries/SphereGeometry.js
有 2 组循环 - 我正在查看第二组。
我的问题是这样的: 创建了一个数组数组,称为顶点。向该数组中添加了对象数组。
稍后,使用索引检索单个对象,具体来说:
var v1 = vertices[ y ][ x + 1 ];
然后,就在其下方,该对象似乎再次被引用,但语法如下:
var n1 = this.vertices[ v1 ].clone().normalize();
尽我所能,这对我来说似乎是一个错误.. this.vertices[v1]
不会返回未定义吗?
最佳答案
我认为造成这种困惑的是this.vertices
与vertices
。它们实际上是两种不同的结构。
// first loop
for (...) {
/* ... */
// this.verticies will have every vertex
this.vertices.push( vertex );
verticesRow.push( this.vertices.length - 1 );
/* ... */
}
// notice we pushed a row of vertices to `vertices` not `this.verticies`
vertices.push( verticesRow );
// second loop
for (...) for (...) {
// grab the vertex from the local list
var v1 = vertices[ y ][ x + 1 ];
// use it to grab something from the object's list
var n1 = this.vertices[ v1 ].clone().normalize();
}
关于javascript - 关联数组查找(来自 Three.js 中的代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13202468/