fromFlatbuffer static method

Geometry fromFlatbuffer(
  1. MeshPrimitive fbPrimitive
)

Implementation

static Geometry fromFlatbuffer(fb.MeshPrimitive fbPrimitive) {
  Uint8List vertices;
  bool isSkinned =
      fbPrimitive.vertices!.runtimeType == fb.SkinnedVertexBuffer;
  int perVertexBytes =
      isSkinned ? kSkinnedPerVertexSize : kUnskinnedPerVertexSize;

  switch (fbPrimitive.vertices!.runtimeType) {
    case const (fb.UnskinnedVertexBuffer):
      fb.UnskinnedVertexBuffer unskinned =
          (fbPrimitive.vertices as fb.UnskinnedVertexBuffer?)!;
      vertices = unskinned.vertices! as Uint8List;
    case const (fb.SkinnedVertexBuffer):
      fb.SkinnedVertexBuffer skinned =
          (fbPrimitive.vertices as fb.SkinnedVertexBuffer?)!;
      vertices = skinned.vertices! as Uint8List;
    default:
      throw Exception('Unknown vertex buffer type');
  }

  if (vertices.length % perVertexBytes != 0) {
    debugPrint('OH NO: Encountered an vertex buffer of size '
        '${vertices.lengthInBytes} bytes, which doesn\'t match the '
        'expected multiple of $perVertexBytes bytes. Possible data corruption! '
        'Attempting to use a vertex count of ${vertices.length ~/ perVertexBytes}. '
        'The last ${vertices.length % perVertexBytes} bytes will be ignored.');
  }
  int vertexCount = vertices.length ~/ perVertexBytes;

  gpu.IndexType indexType = fbPrimitive.indices!.type.toIndexType();
  Uint8List indices = fbPrimitive.indices!.data! as Uint8List;

  Geometry geometry;
  switch (fbPrimitive.vertices!.runtimeType) {
    case const (fb.UnskinnedVertexBuffer):
      geometry = UnskinnedGeometry();
    case const (fb.SkinnedVertexBuffer):
      geometry = SkinnedGeometry();
    default:
      throw Exception('Unknown vertex buffer type');
  }

  geometry.uploadVertexData(ByteData.sublistView(vertices), vertexCount,
      ByteData.sublistView(indices),
      indexType: indexType);
  return geometry;
}