softParticleFade function

double softParticleFade(
  1. ShaderBindings b,
  2. FragmentContext c,
  3. double x,
  4. double y,
  5. double z,
)

lib/particle_soft.glsl's SoftParticleFade: how much of a particle fragment at world (x, y, z) is left once it nears the scene the surface buffer holds at c's pixel — one a softness in front or further, nought at it, and one where nothing was drawn.

One as well while no depth is bound, which is never how a soft stage is drawn and is how a test that leaves the slot empty reads the stage.

Implementation

double softParticleFade(
  ShaderBindings b,
  FragmentContext c,
  double x,
  double y,
  double z,
) {
  final surface = b.textures['scene_depth_texture'];
  if (surface == null) return 1.0;
  const block = 'SoftParticleInfo';
  final target = b.vec4(block, 'target', Vector4.zero());
  final eye = b.vec4(block, 'eye', Vector4.zero());
  final forward = b.vec4(block, 'forward', Vector4.zero());
  // `FragCoordFromTop`.
  final row = target.z > 0.0 ? target.z - c.coord.y : c.coord.y;
  final stored = surface.sample(c.coord.x * target.x, row * target.y).w;
  final depth =
      (x - eye.x) * forward.x +
      (y - eye.y) * forward.y +
      (z - eye.z) * forward.z;
  final fade = ((stored - depth) * target.w).clamp(0.0, 1.0);
  return stored > 0.0 ? fade : 1.0;
}