toTrianglesDrawMode top-level property

Function toTrianglesDrawMode
getter/setter pair

@param {BufferGeometry} geometry @param {Number} drawMode @return {BufferGeometry}

Implementation

Function toTrianglesDrawMode = (geometry, drawMode) {
  var index = geometry.getIndex();

  // generate index if not present

  if (index == null) {
    var indices = [];

    var position = geometry.getAttribute('position');

    if (position != null) {
      for (var i = 0; i < position.count; i++) {
        indices.add(i);
      }

      geometry.setIndex(indices);
      index = geometry.getIndex();
    } else {
      print('THREE.GLTFLoader.toTrianglesDrawMode(): Undefined position attribute. Processing not possible.');
      return geometry;
    }
  }

  //

  var numberOfTriangles = index.count - 2;
  var newIndices = [];

  if (drawMode == TriangleFanDrawMode) {
    // gl.TRIANGLE_FAN

    for (var i = 1; i <= numberOfTriangles; i++) {
      newIndices.add(index.getX(0));
      newIndices.add(index.getX(i));
      newIndices.add(index.getX(i + 1));
    }
  } else {
    // gl.TRIANGLE_STRIP

    for (var i = 0; i < numberOfTriangles; i++) {
      if (i % 2 == 0) {
        newIndices.add(index.getX(i));
        newIndices.add(index.getX(i + 1));
        newIndices.add(index.getX(i + 2));
      } else {
        newIndices.add(index.getX(i + 2));
        newIndices.add(index.getX(i + 1));
        newIndices.add(index.getX(i));
      }
    }
  }

  if ((newIndices.length / 3) != numberOfTriangles) {
    print('THREE.GLTFLoader.toTrianglesDrawMode(): Unable to generate correct amount of triangles.');
  }

  // build final geometry

  var newGeometry = geometry.clone();
  newGeometry.setIndex(newIndices);

  return newGeometry;
};