blendNormals method

Float32List blendNormals(
  1. Float32List baseNormals,
  2. Float32List weights, {
  3. Float32List? out,
})

Blends weights worth of normal deltas onto baseNormals and renormalizes each result, keeping the base normal where the weighted sum collapses to near zero. Returns the base values unchanged when the targets carry no normal deltas.

Implementation

Float32List blendNormals(
  Float32List baseNormals,
  Float32List weights, {
  Float32List? out,
}) {
  final deltas = normalDeltas;
  if (deltas == null) {
    if (out == null) return Float32List.fromList(baseNormals);
    out.setAll(0, baseNormals);
    return out;
  }
  final result = _blendAdditive(baseNormals, weights, deltas, out);
  for (var v = 0; v < vertexCount; v++) {
    final x = result[v * 3], y = result[v * 3 + 1], z = result[v * 3 + 2];
    final lengthSquared = x * x + y * y + z * z;
    if (lengthSquared > 1e-12) {
      final inverseLength = 1.0 / sqrt(lengthSquared);
      result[v * 3] = x * inverseLength;
      result[v * 3 + 1] = y * inverseLength;
      result[v * 3 + 2] = z * inverseLength;
    } else {
      result[v * 3] = baseNormals[v * 3];
      result[v * 3 + 1] = baseNormals[v * 3 + 1];
      result[v * 3 + 2] = baseNormals[v * 3 + 2];
    }
  }
  return result;
}