parseDiffuseShSidecar function Lighting and environment

List<Vector3> parseDiffuseShSidecar(
  1. Uint8List bytes
)

Decodes a diffuse spherical-harmonic sidecar written by encodeDiffuseShSidecar. Throws FormatException on a wrong-sized or non-finite payload.

Implementation

List<Vector3> parseDiffuseShSidecar(Uint8List bytes) {
  if (bytes.length != kDiffuseShSidecarByteLength) {
    throw FormatException(
      'Diffuse SH sidecar must be exactly $kDiffuseShSidecarByteLength bytes '
      '($kDiffuseShCoefficientCount RGB float32 coefficients), got '
      '${bytes.length}',
    );
  }
  final data = ByteData.sublistView(bytes);
  final sh = <Vector3>[];
  for (var i = 0; i < kDiffuseShCoefficientCount; i++) {
    final coefficient = Vector3(
      data.getFloat32(i * 12, Endian.little),
      data.getFloat32(i * 12 + 4, Endian.little),
      data.getFloat32(i * 12 + 8, Endian.little),
    );
    if (!coefficient.x.isFinite ||
        !coefficient.y.isFinite ||
        !coefficient.z.isFinite) {
      throw FormatException('Diffuse SH coefficient $i is not finite');
    }
    sh.add(coefficient);
  }
  return sh;
}