sample method

  1. @override
void sample(
  1. ParticleStorage storage,
  2. int index
)
override

Writes the spawn position and unit emission direction for particle index into storage.

Implementation

@override
void sample(ParticleStorage storage, int index) {
  final u = storage.randomFor(index, _saltA);
  final v = storage.randomFor(index, _saltB);
  // A uniform direction on the unit sphere (or +Y hemisphere).
  final y = hemisphere ? u : (2.0 * u - 1.0);
  final ring = math.sqrt(math.max(0.0, 1.0 - y * y));
  final phi = 2.0 * math.pi * v;
  final dx = ring * math.cos(phi);
  final dy = y;
  final dz = ring * math.sin(phi);

  var magnitude = radius;
  if (!surfaceOnly && radius > 0.0) {
    // Cube-root keeps the volume density uniform rather than centre-heavy.
    final w = storage.randomFor(index, _saltC);
    magnitude = radius * math.pow(w, 1.0 / 3.0).toDouble();
  }

  storage.posX[index] = dx * magnitude;
  storage.posY[index] = dy * magnitude;
  storage.posZ[index] = dz * magnitude;
  storage.velX[index] = dx;
  storage.velY[index] = dy;
  storage.velZ[index] = dz;
}