acquire method
InstanceHandle
acquire({
- Matrix4? transform,
- LinearColor color = LinearColor.white,
- Vector4? data,
Takes a slot for something that comes and goes, and returns the handle that keeps finding it: a shot, a spark, an invader.
For a batch whose members leave in any order. Written by index, a batch whose middle member left had a hole to fill, and filling it moved another member to a slot its owner did not know about. release fills the hole with the last slot and tells the last slot's handle where it went, so an owner holding a handle is never pointed at someone else's.
Grows the buffer when it is full, the way ensureCapacity does: a batch of things that come and go has no size to name in advance.
Implementation
InstanceHandle acquire({
Matrix4? transform,
LinearColor color = LinearColor.white,
Vector4? data,
}) {
ensureCapacity(_count + 1);
final index = _count;
final handle = InstanceHandle._(this, index);
while (_holders.length <= index) {
_holders.add(null);
}
_holders[index] = handle;
count = index + 1;
setTransform(index, transform ?? Matrix4.identity());
setColor(index, color);
// A slot a released member left holds its numbers; the new one starts
// from nought, as a fresh batch does.
setInstanceData(index, data ?? Vector4.zero());
final weights = _weights;
if (weights != null) {
weights.fillRange(
index * maxMorphTargets,
(index + 1) * maxMorphTargets,
0.0,
);
_weightsVersion++;
}
return handle;
}