4

I was just wondering for a while now what exactly "offsets" and "indices / index" are. Offsets are e.g. mentioned in https://github.com/mrdoob/three.js/blob/dev/src/core/BufferGeometry.js and indices were mentioned in IndexedGeometry, however I can't currently find it anymore in the dev tree. Although indices seem rather obvious and although I could dive into the code to figure out some maybe-correct answer for myself I'd love to hear an "official" statement :)

Thanks!

Wilt
  • 41,477
  • 12
  • 152
  • 203
Doidel
  • 1,743
  • 1
  • 14
  • 22

1 Answers1

8

There are two ways to defining geometries:

Non-Indexed

"vertices": [ 0, 1, 2,  3, 4, 5,  6, 7, 8,  ... ],
"normals":  [ 0, 1, 2,  3, 4, 5,  6, 7, 8,  ... ]

In this mode every triangle position is as defined and you can't reuse data.

triangle 0: [ 0, 1, 2,  3, 4, 5,  6, 7, 8 ]

Indexed

"indices":  [ 0, 1, 2, ... ],
"vertices": [ 0, 1, 2,  3, 4, 5,  6, 7, 8,  ... ],
"normals":  [ 0, 1, 2,  3, 4, 5,  6, 7, 8,  ... ]

In this mode, indices define the order of the data. The first triangle is using indices 0, 1, and 2. These indices will be used to fetch the vertices and normals data:

triangle 0: [ 0, 1, 2,  3, 4, 5,  6, 7, 8 ]

The main benefit of indexed is the possibility of reusing data and uploading less data to the GPU:

"indices":  [ 0, 0, 0, ... ],
"vertices": [ 0, 1, 2,  3, 4, 5,  6, 7, 8,  ... ]

triangle 0: [ 0, 1, 2,  0, 1, 2,  0, 1, 2 ]

As per offsets...

With offsets you can render specific ranges of your geometry. Instead of drawing from triangle 0 to triangle.length, you can draw from triangle 200 to triangle 400.

mrdoob
  • 19,334
  • 4
  • 63
  • 62
  • Awesome, thanks! Do the numbers make sense this way? Let's say we build a flat plane then the non-indexed would be [0,0,0, 0,1,0] [0,0,1, 0,0,1], right? Then how would/could the numbers look when indexed? (Could you maybe edit that into your post..?) Further can I define multiple offsets, e.g. to draw every second "line" of a plane? – Doidel May 30 '14 at 09:52
  • @Doidel I tried to modify the answer using a simple plane geometry but I think it's more confusing. – mrdoob May 31 '14 at 10:06
  • @Doidel you can indeed draw multiple offsets but bear in mind that every offset is a draw call so you don't want to do a ton of those. It tends to be used for multi material stuff. – mrdoob May 31 '14 at 10:07