Batch update UInstancedStaticMeshComponent instances by index

Just a feature I’d like to request.
https://docs.unrealengine.com/5.3/en-US/API/Runtime/Engine/Components/UInstancedStaticMeshComponent/

I’m making a game where I constantly add and remove static mesh components. To be more efficient I’d like to instead reuse ‘deleted’ instances by updating the transform. Currently the BatchUpdateInstancesTransforms method seems like it only updates consecutive indices. I’d like to be able to update the transforms of specific indices, say indices 1, 5, 12.

A method overload could be something like this:

virtual bool BatchUpdateInstancesTransforms
(
    TArray<int32> instanceIndices,
    const TArray< FTransform > & NewInstancesTransforms,
    bool bWorldSpace,
    bool bMarkRenderStateDirty,
    bool bTeleport
) 

Anyway, that would be handy for me a least. Either way, love unreal engine and I think you guys are awesome!

Hi Tep Daddy,

You can access the array of instance transforms directly - a routine like you want should be easy to write - it just won’t be quite as fast as a tight loop through consecutive indexes (you’re reading memory to get the index and then the index transform as well so two memory lookups - plus they’re not guaranteed to be cached in the CPU if they’re not consecutive so there may be multiple cache invalidations). If you’re only moving a small number of instances it won’t be noticable though…

Oh cool, I didn’t we could access that array directly! Thanks!

I also found this method I think might for anyone interested:

virtual bool UpdateInstances
(
    const TArray< int32 > & UpdateInstanceIds,
    const TArray< FTransform > & UpdateInstanceTransforms,
    const TArray< FTransform > & UpdateInstancePreviousTransforms,
    int32 NumCustomFloats,
    const TArray< float > & CustomFloatData
) 

https://docs.unrealengine.com/5.3/en-US/API/Runtime/Engine/Components/UInstancedStaticMeshComponent/UpdateInstances/

Thanks Recourse

1 Like

If you’re looping somewhere to create those update lists, it will be more efficient just to get the pointer to the instance transforms and update them there rather than create the lists, pass them through and then have that routine loop through them again - if they’re small lists though then it’s not going to make that much difference, but it does when you’re handling large amounts of transforms.

Oh really? I thought you got a big performance boost from doing things with these batch methods.

Have a look at the code behind UInstancedStaticMeshComponent::BatchUpdateInstancesTransformsInternal()
UInstancedStaticMeshComponent::UpdateInstances()

Engine\Source\Runtime\Engine\Private\InstancedStaticMesh.cpp

So maybe I just do this?:

	Modify();

	// Update transforms of whatever instances

	PartialNavigationUpdate(StartInstanceIndex);
	InstanceUpdateCmdBuffer.Edit();
	MarkRenderStateDirty();
1 Like

perfect :slight_smile:

Nice, thanks

1 Like