2

我正在查看three.js中的代码,特别是创建球体的THREE.SphereGeometry方法: https ://github.com/mrdoob/three.js/blob/master/src/extras/geometries/SphereGeometry.js

有两组循环 - 我正在看第二组。

我的问题是:创建了一个数组数组,称为顶点。在这个数组中添加了对象数组。

稍后,使用索引检索单个对象,具体而言:

var v1 = vertices[ y ][ x + 1 ];

然后,就在此之下,似乎再次引用了该对象,但语法如下:

var n1 = this.vertices[ v1 ].clone().normalize();

尽我所能,这对我来说似乎是一个错误..不会this.vertices[v1]返回未定义?

4

2 回答 2

3

认为让这令人困惑的是this.verticesvs 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();
}
于 2012-11-02T20:31:45.750 回答
0

我不知道那个具体的例子,但这是一个人为的数据结构,它是有效的,不一定是错误。

x = 1
y = 2

vertices = ["","",["","x",3],"the third element"]

var v1 = vertices[y][x+1] // v1 is `3`

var n1 = this.vertices[v1]

alert(n1)
// alerts `the third element`
于 2012-11-02T20:25:59.703 回答