LizzyFox-code / BrgContainer

The Unity BRG Container
MIT License
63 stars 6 forks source link

Can I remove a random cube in real time? #9

Closed wearplugins closed 3 months ago

wearplugins commented 3 months ago

How do I delete a random cube in real time?

LizzyFox-code commented 3 months ago

Yes, you can do it by Remove method:

var dataBuffer = batchHandle.AsInstanceDataBuffer();
dataBuffer.Remove(index, count);
wearplugins commented 3 months ago

Thank you. However, I modified the Update function a bit, in SampleBrgContainer.cs. I use dataBuffer.InstanceCount instead of m_CubeCount.

private void Update() { m_JobHandle.Complete();

var dataBuffer = m_BatchHandle.AsInstanceDataBuffer();
m_Time += Time.deltaTime;
if (m_Time > 2f)
{
    var index = (int)(Random.value * (dataBuffer.InstanceCount - 1));
    dataBuffer.Remove(index, 1);
    m_Time = 0;
}

transform.position = m_CenterFlock.Value;
m_BatchHandle.Upload();

var averageCenterJob = new AverageCenterJob
{
    InstanceDataBuffer = dataBuffer,
    Center = m_CenterFlock,
    Size = dataBuffer.InstanceCount,
    ObjectToWorldPropertyId = m_ObjectToWorldPropertyId
};
var averageCenterHandle = averageCenterJob.ScheduleByRef();

var sampleJob = new SampleJob
{
    Weights = m_Weights,
    Goal = m_Destination.position,
    NoiseOffsets = m_NoiseOffsets,
    Time = Time.time,
    DeltaTime = Time.deltaTime,
    MaxDistance = m_SeparationDistance,
    Speed = m_MaxSpeed,
    RotationSpeed = m_RotationSpeed,
    Size = dataBuffer.InstanceCount,
    InstanceDataBuffer = dataBuffer,
    ObjectToWorldPropertyId = m_ObjectToWorldPropertyId
};
m_JobHandle = sampleJob.ScheduleParallelByRef(dataBuffer.InstanceCount, 32, averageCenterHandle);

}