ParallelFor loop to update thousands of instances in HierarchicalInstancedStaticMeshComponent ?

On Begin play I add about or greater 200 x 200 instances like this with my FPS clocking to 120 fps :



for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
            {
                FVector newLocation = CompOrigin + FVector(i*OffsetX, j*OffsetY, k*OffsetZ);
                Positions.Add(newLocation);
                HISMC->AddInstance(FTransform(newLocation));
            }
        }


I try to update the position of these meshes at runtime using parallel for loop in a similar spirit of runtime mesh component example here https://github.com/Koderz/RuntimeMes…tedTerrain.cpp .

Rather than use tick for this actor I call a member function thusly :


// Call RepeatingFunction once per .05 second, starting five seconds from now.
    GetWorldTimerManager().SetTimer(MemberTimerHandle, this, &AHISMActor::RepeatingFunction, 0.05f, true, 5.0f);


In that member function I simply try to update positions of already created instanced meshes like this :



  ParallelFor(someN *someN , &SomeComp, &somePositions, someScale, someAnimOffsetX, someAnimOffsetY, someN, someHeightSensitivity](int32 Index)
    {
        int32 XIndex = Index % someN;
        int32 YIndex = Index / someN;
        float HeightScale = FMath::Cos((XIndex * someScale) + someAnimOffsetX) + FMath::Sin((YIndex * someScale) + someAnimOffsetY);
        somePositions[Index].Z += HeightScale * someHeightSensitivity;

        SomeComp->UpdateInstanceTransform(Index, FTransform(somePositions[Index]), false,false, true);
    }, true);


First the parallel for loop cannot be used hence I witness tremendous drop in frame rate when I try to update instances. It only works with single thread that you have to force otherwise you get plenty of runtime errors including MallocTBB and Emplace error in ArrayList. I wonder what is a nice way to update huge number of meshes, I don’t care about collisions or physics, just want to simulate some movement of already rendered meshes. Please advise.

@timtimmy you are the bomb ! Now I can generate about as many as 128 * 128 * 18 meshes and still no dropping the frame rate below 90 fps ! That is the power of GPU. I still have to figure out the things you talked about the conversion from RandomPerInstance node to have the enumeration and then provide the proper positions, but I think it can be done with some effort and understanding. Thank you so much timtimmy ! I would like to know more about what kind of projects you do, let me know I can share with you my gmail. You can find out more about me here : syedalamabbas (Syed Alam Abbas) · GitHub