2017-07-03 3 views
0

時々、頂点や色を変更するBufferGeometryがあります。私は、頂点にもっと多くのスペースを割り当てる必要がある場合、新しいバッファーメトリーを作成することでこれを行います。そうでない場合、私はすでに持っているものを再利用しますが、drawRangeを変更します。Geometry.center()が適用されていません

私の問題は、私のジオメトリにはない以下のコードの私は最初の「this.mesh!=未定義& & this.prev(あれば...」のように新しい頂点とBufferGeometryを更新一環ということです私は明示的にそれを適用していますが、幾何学がちょうど後で中心合わせされる必要がある場合、これを解決するにはどうすればいいですか?(以下のコードを使用すると、オブジェクトはcenter()位置)。そのmesh.positionに注意してください。これでは何の関係もありません、それはすべての時間と同じです。

if(this.mesh != undefined && this.prev_len > vertices.length) { 
     for (var i = 0; i < vertices.length; i++) { 
      this.v.setXYZ(i, vertices[i][0], vertices[i][1], vertices[i][2]); 
      this.c.setXYZW(i, colors[i][0], colors[i][1], colors[i][2], 1); 
     } 
     this.geometry.setDrawRange(0, vertices.length); 
     this.geometry.attributes.position.needsUpdate = true; 
     this.geometry.attributes.color.needsUpdate = true; 
     this.geometry.computeVertexNormals(); 

     // Here seems to be the problem. center() is not actually applied? 
     // I've debuged three.js and it seems to perform the translate() in center() function. But it seems to be changed afterwards? 
     this.mesh.geometry.center(); 

    } else { 
     this.v = new THREE.BufferAttribute(new Float32Array(vertices.length * 3), 3); 
     this.c = new THREE.BufferAttribute(new Float32Array(colors.length * 3), 3); 
     for (var i = 0; i < vertices.length; i++) { 
      this.v.setXYZ(i, vertices[i][0], vertices[i][1], vertices[i][2]); 
      this.c.setXYZW(i, colors[i][0], colors[i][1], colors[i][2], 1); 
     } 
     this.geometry = new THREE.BufferGeometry(); 
     this.geometry.dynamic = true; 
     this.geometry.addAttribute('position', this.v); 
     this.geometry.addAttribute('color', this.c); 
     this.geometry.attributes.position.dynamic = true; 
     this.geometry.attributes.color.dynamic = true; 
     this.geometry.computeVertexNormals(); 
     this.prev_len = vertices.length; 

     if(this.mesh == undefined) { 
      this.mesh = new THREE.Mesh(this.geometry, this.material); 
      this.mesh.position.set(
            this.from_x, 
            this.from_y, 
            this.from_z 
      ); 
      scene.add(this.mesh); 
      this.geometry.center(); 

     } else { 
      // This works fine, the geometry becomes centered. 
      this.mesh.geometry = this.geometry; 
      this.geometry.center(); 
     } 

    } 

答えて

0

新しい位置で更新されたジオメトリにはcenter()が適用されないという問題があるようです。更新されたBufferGeometryの新しいcenter()を計算すると、position属性の変更によって反映されるfalse offsetが作成されます。

私の解決策は、geometry.center()から返されたオフセットを保存し、更新されたジオメトリに適用することです。これが「正しい」溶液または単なる回避策です

this.geometry.translate(this.offset.x, this.offset.y, this.offset.z); 

わからない場合:ちょうどこのように、位置を更新するとき

this.offset = this.geometry.center(); 

は、次にそれを手動で適用します。しかし、私のために働く。

0

あなたは「this.geometry」を変更して、あなたが適用されます「(中央)」「this.meshへ。ジオメトリ "かもしれない。 BufferGeometryと "this"オブジェクトの他の属性との間の参照が壊れてしまう問題。

+0

いいえ、それは同じです。しかし、確かに、私はthis.geometryにcenter()を試みました。しかし問題は残っている。 – nergal

関連する問題