fromBufferGeometry method

dynamic fromBufferGeometry(
  1. dynamic geometry
)

Implementation

fromBufferGeometry(geometry) {
  var scope = this;

  var index = geometry.index;
  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) faceVertexUvs[1] = [];

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

    if (color != null) {
      scope.colors.add(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)
        ? []
        : [
            three.Vector3.init().fromBufferAttribute(normal, a),
            three.Vector3.init().fromBufferAttribute(normal, b),
            three.Vector3.init().fromBufferAttribute(normal, c)
          ];

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

    scope.faces.add(face);

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

    if (uv2 != null) {
      scope.faceVertexUvs[1]?.add([
        three.Vector2(null, null).fromBufferAttribute(uv2, a),
        three.Vector2(null, null).fromBufferAttribute(uv2, b),
        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);
      }
    }
  }

  computeFaceNormals();

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

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

  return this;
}