javascript - How can I add faces to an indexed THREE.BufferGeometry? -


say had generated three.buffergeometry three.geometry named oldgeom so:

// using webglrenderer var geometry = new three.buffergeometry(); var indices = new uint16array(oldgeom.vertices.length); var vertices = new float32array(oldgeom.vertices.length * 3); (var = 0; < oldgeom.vertices.length; i++) {     indices[i] = i;     vertices[i * 3 + 0] = oldgeom.vertices[i].x;     vertices[i * 3 + 1] = oldgeom.vertices[i].y;     vertices[i * 3 + 2] = oldgeom.vertices[i].z; } geometry.addattribute('position', new three.bufferattribute(vertices, 3)); geometry.setindex(new three.bufferattribute(indices, 1)); 

hopefully have indexing right. @ point, how add face using indices? i'm planning loop through faces of oldgeom add them here, can't find documentation on this. thanks!

similar this question, indexed geometry.

from the documentation buffergeometry:

index (itemsize: 3)

allows vertices re-used across multiple triangles; called using "indexed triangles," , works same in geometry: each triangle associated index of 3 vertices. attribute therefore stores index of each vertex each triangular face. if attribute not set, renderer assumes each 3 contiguous positions represent single triangle.

the way "indexed triangles" work "position" array of numbers, every consecutive set of 3 numbers representing 1 vertex (x, y, z). "index" array of numbers, every consecutive set of 3 numbers represents 1 face, referring indices of vertices in "position" array.

you might have array of vertices this:

var vertices = [0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0]; 

you can think of array sets of xyz coordinates this:

var vertices = [     0, 0, 0, // vertex index 0     1, 0, 0, // vertex index 1     1, 1, 0, // vertex index 2     0, 1, 0  // vertex index 3 ]; 

now if have index array this:

var indices = [0, 1, 2, 1, 2, 3]; 

it represents 2 triangles:

var indices = [     0, 1, 2, // face vertices @ indices 0, 1, 2     1, 2, 3  // face vertices @ indices 1, 2, 3 ]; 

so triangle #1 has vertices @ xyz (0, 0, 0), (1, 0, 0), (1, 1, 0) while triangle #2 has vertices @ xyz (1, 0, 0), (1, 1, 0), (0, 1, 0).

on other hand can define vertices without using index. power of indexing lets reuse vertices defined in array instead of listing them redundantly every time appear in triangle. if have single array, vertices, quite simply, every set of 9 numbers in array 1 triangle (three sets of consecutive vertices, each 3 consecutive xyz values).

going original question, if want add triangles bufferedgeometry, see 2 basic options:

  1. add triangles original oldgeom object, , convert it. it's lot easier add triangles geometry buffergeometry. remember whole point of buffergeometry it's not supposed change! able take advantage of .fromgeometry() because new faces defined in oldgeom.
  2. make indices array that's larger necessary original indices , manually define triangles there. if you're defining new vertices don't exist in vertices array you'd have add them in there too. pain in butt.

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -