fromBufferGeometry method

dynamic fromBufferGeometry(
  1. dynamic geometry
)

Implementation

fromBufferGeometry(geometry) {
  var scope = this;

  var index = geometry.index != null ? geometry.index : null;
  var attributes = geometry.attributes;

  if (attributes["position"] == null) {
    print(
        'THREE.Geometry.fromBufferGeometry(): Position attribute required for conversion.');
    return this;
  }

  var position = attributes["position"];
  var normal = attributes["normal"];
  var color = attributes["color"];
  var uv = attributes["uv"];
  var uv2 = attributes["uv2"];

  if (uv2 != null) this.faceVertexUvs[1] = [];

  for (var i = 0; i < position.count; i++) {
    scope.vertices
        .add(new THREE.Vector3().fromBufferAttribute(position, i));

    if (color != null) {
      scope.colors
          .add(new THREE.Color(0, 0, 0).fromBufferAttribute(color, i));
    }
  }

  addFace(int a, int b, int c, materialIndex) {
    List<THREE.Color> vertexColors = (color == null)
        ? []
        : [
            scope.colors[a].clone(),
            scope.colors[b].clone(),
            scope.colors[c].clone()
          ];

    List<THREE.Vector3> vertexNormals = (normal == null)
        ? []
        : [
            new THREE.Vector3().fromBufferAttribute(normal, a),
            new THREE.Vector3().fromBufferAttribute(normal, b),
            new THREE.Vector3().fromBufferAttribute(normal, c)
          ];

    var face = new Face3(a, b, c, vertexNormals, vertexColors,
        materialIndex: materialIndex ?? 0);

    scope.faces.add(face);

    if (uv != null) {
      scope.faceVertexUvs[0].add([
        new THREE.Vector2(null, null).fromBufferAttribute(uv, a),
        new THREE.Vector2(null, null).fromBufferAttribute(uv, b),
        new THREE.Vector2(null, null).fromBufferAttribute(uv, c)
      ]);
    }

    if (uv2 != null) {
      scope.faceVertexUvs[1].add([
        new THREE.Vector2(null, null).fromBufferAttribute(uv2, a),
        new THREE.Vector2(null, null).fromBufferAttribute(uv2, b),
        new THREE.Vector2(null, null).fromBufferAttribute(uv2, c)
      ]);
    }
  }

  var groups = geometry.groups;

  if (groups.length > 0) {
    for (var i = 0; i < groups.length; i++) {
      var group = groups[i];

      int start = group["start"];
      int count = group["count"];

      for (int j = start, jl = start + count; j < jl; j += 3) {
        if (index != null) {
          addFace(index.getX(j).toInt(), index.getX(j + 1).toInt(),
              index.getX(j + 2).toInt(), group["materialIndex"]);
        } else {
          addFace(j, j + 1, j + 2, group["materialIndex"]);
        }
      }
    }
  } else {
    if (index != null) {
      for (var i = 0; i < index.count; i += 3) {
        addFace(index.getX(i), index.getX(i + 1), index.getX(i + 2), null);
      }
    } else {
      for (var i = 0; i < position.count; i += 3) {
        addFace(i, i + 1, i + 2, null);
      }
    }
  }

  this.computeFaceNormals();

  if (geometry.boundingBox != null) {
    this.boundingBox = geometry.boundingBox.clone();
  }

  if (geometry.boundingSphere != null) {
    this.boundingSphere = geometry.boundingSphere.clone();
  }

  return this;
}