Avoiding spikes on enabling groups of entities

Hello,
i am working on a physics driven game. Basically a tunnel racer. Drone is always centered third person like and shallow fly through tunnels. Tunnels are constructed by assembling template tunnel parts. There are straight, curve, up down and other templates (in unity prefabs).

Now it build ups to quite a lot parts, and I am having performance problems. Seems frustum related. If I disable frustum culling on the camera performance is very bad all the time. If its on I often reach 60FPS on a current MacBook Pro.

However, depending on were the camera looks at the game stalls for a second or more or stutters. The profiler doesn’t help me here.

So I tried to group tunnel entities into areas and then enable / disable them by flying though some trigger entities. This helps to keep the frame rate up during flights, but the game stalls again while doing so (means on enabling/disabling areas of entitys).

The tunnels are constructed of templates. usually they consist of a left side (and a mirrored rightside) and both sides have a mesh collider. Then every part has a light,but its area is so small, it can only lighten up its parent. Shadows are disabled on 90% of everything.

  1. Every tunnel template has one point light, but it spreads out in small radius to only light up the single template.
  2. Every tunnel template has two mesh colliders, low poly
  3. very little amount of materials. Most templates share materials.

My solution was now to group tunnelentitys into areas and entering exiting doors will toggle them of or on, so there is only one area active at a time.

How can I optimize this further?

Here is an image of the current Tunnelsystem.

In this context I read about new lights in the latest release: clustered lights

I guess this might help, but it is not available in the editor. Or is it? Well it should be, since most fellows assemble there stuff with the editor. is there a toggle to switch this on or not? if not how can I try to use that? Iterate to every entity, find lights and exchange them to the new system?

Have you tried using the Chrome Dev Tools profiler to narrow down where the time is spent on the stalls?

Would you be able to provide a build/publish link of the project for people to take a look at?

1 Like

Also I would take a look if there is a material being rendered for the first time. Spikes when enabling an entity/model can point to shader compilation, which can pause rendering for a while.

Edit: Similarly check if rigid bodies are being added to the hierarchy for the first time. Creating the Ammo shape can sometimes take a while for more complex models.

yaustar: not yet

Leonidos: Hmm, guess not, because most of the templates share the same material. (I tried to pay a lot of attention to this)

Do a profiling in Chrome tools … and zoom in on the slow frame, and post the screenshot here, if you cannot share the build.

yaustar: which element of chrome should I use? Performance?

I am afraid, whatever I see there wouldn’t tell me much

what do you need? a build or a public project? Or a private one with invite?

In current engine implementation, all enabled lights are used on all objects pretty much, it does not make a difference to make their range small.

Clustered lighting changes this, and only nearby lights are used. In Editor, you can create this script and add it on your root object to enable them. Note that that would mean no shadows for spot and point lights in the current preview implementation.

var Global = pc.createScript('global');

// initialize code called once per entity
Global.prototype.initialize = function() {
    
    // enabled clustered lighting. This is a temporary API and will change in the future
    pc.LayerComposition.clusteredLightingEnabled = true;

    // adjust default clusterered lighting parameters to handle many lights:
    // 1) subdivide space with lights into this many cells:
    this.app.scene.layers.clusteredLightingCells = new pc.Vec3(12, 16, 12);

    // 2) and allow this many lights per cell:
    this.app.scene.layers.clusteredLightingMaxLights = 48;

};
1 Like

either of these would be fine.

A build link would be fine in this case. eg https://playcanv.as/p/XXXXXXXX

Yes please.

yaustar should have access to a fork. I will delete this project ASAP. its just for him to have a look.

yaustar should have received an email to access it.

might be a good idea to add mvaligursky to the project too :wink:

mvaligursky thanks, should I apply this script to the root?

what is this.app.scene.layers.clusteredLightingCells ???

a world space area to affect lights in world space?

just number of cells. A grid size in 3d space. If you level is flat, you might want something like 20x2x20 or so

I added you too.

1 Like

Apply the script at the root, yes.

It seems the massive slowdowns you get are related to shader compilation. It’s possibly due to lights … shaders need to recompile for different sets of lights. Try clustered lighting, that could be a good win here.

well, might be a little bit faster.

But now I get error messages like “failed to compile fragment shader”

Profiler is very useful here /s

Looks like something rendering bound