Visible game objects entering camera frustrum is slow

Hi,

I am new to the Unity community. During the last month I have been creating a game for the IPhone with the basic version and have a question regarding the perf related to operations done in the CPU for those game objects that enter fr the first time on the camera frustrum.

I have 8 models that when rendered at the same time in the screen push poly count to 10k or so. I am using no lights, brought physics calcs to a minimum (and colliders are all spheres).

I have optimized the code, but the thing is that when four of the models enter the camera frustrum area at the same time, there is a small glitch (less than a sec) that I cannot avoid. Stats show the following on the cpu-player> max 138. Funny thing is that it only happens the first time those models enter the viewable area, and after that everything plays smoothly even if they get out the playable area and then re-enter it. What is more, everything still plays smoothly with 12k+ polys but only after they all enter the field for the first time.

Since all models use the same shader (only textures change) I must ask: is there any set up that Unity does when an object is visible for the first time? I mean, say, instantiating vertex/index buffers when the object hits the camera frustrum for the first time instead of when it is created, or something else that keeps “cpu-player” stat that high for a brief period of time.

Thanks for reading.
~Pete

Hi, welcome to the forum!

Do the models have skinned animation? If so, then the animations might not play until the objects become visible (see animation.animateOnlyIfVisible). It is possible that the animations do some initialisation just as they enter the view frustum.

I assume you’re using Unity 1.7? In versions of Unity prior to 3, textures were loaded the first time they entered the camera frustrum. On an older iDevice, jamming a lot of stuff into memory at once will cause noticeable hitches. The way I used to work around this was to have a “loading screen”, which was just a full screen polygon covering up a bunch of primitives that had my textures applied. This forced the textures into memory immediately, after which I could delete the primitives, loading screen, and go straight into gameplay without worrying about hitches mid game.

In Unity 3, this practice is entirely unnecessary because textures are loaded in advance by default, unless you manually load them via Resources.Load.

Thanks for the responses. It’s good to know that info.

In may case, I’m using v3 and meshes have no animations at all. It’s a top-view shoot’em up space game like, say -saving distances, Ikaruga.

Since I have the basic version of Unity IPhone I cannot profile it further, but it feels as if some “late-settings” were processed in that precise moment when game objects enter the scene. Once visible,10k polys (and more) are render flawlessly on my IPod (3rd gen).

So, since changes in render state are close to none since I 'm using the same shader for all the ships (so, only textures change), the only thing I can think of is vertex/index buffer creation.

Post the stats you do have from the stats pane?

I have reduced tris a bit more but I am still getting similar results when game objects enter the screen :frowning:

Here’s the stats:

iPhone Unity internal profiler stats:
cpu-player>    min: -39.0   max: 1642.9   avg: 63.8
cpu-ogles-drv> min:  0.0   max: 69.8   avg:  7.1
cpu-waits-gpu> min:  0.0   max: 17.5   avg:  1.4
cpu-present>   min:  0.0   max:  3.5   avg:  0.8
frametime>     min:  0.0   max: 1710.3   avg: 91.3
draw-call #>   min:   0    max:   7    avg:   5     | batched:     0
tris #>        min:     0  max:  2400  avg:  1595   | batched:     0
verts #>       min:     0  max:  1373  avg:   915   | batched:     0
player-detail> physx:  2.8 animation:  0.0 culling  0.0 skinning:  0.0 batching:  0.0 render: 10.5 fixed-update-count: 0 .. 5
mono-scripts>  update:  2.6   fixedUpdate:  0.0 coroutines:  7.9 
mono-memory>   used heap: 270336 allocated heap: 356352  max number of collections: 0 collection total duration:  0.0
----------------------------------------
iPhone Unity internal profiler stats:
cpu-player>    min:  4.5   max:  6.4   avg:  5.6
cpu-ogles-drv> min:  0.6   max:  1.5   avg:  0.9
cpu-waits-gpu> min:  0.3   max:  1.6   avg:  0.5
cpu-present>   min:  0.4   max:  0.9   avg:  0.4
frametime>     min: 32.9   max: 33.9   avg: 33.4
draw-call #>   min:   7    max:   7    avg:   7     | batched:     0
tris #>        min:  2384  max:  2394  avg:  2389   | batched:     0
verts #>       min:  1341  max:  1361  avg:  1351   | batched:     0
player-detail> physx:  0.5 animation:  0.0 culling  0.0 skinning:  0.0 batching:  0.0 render:  3.2 fixed-update-count: 0 .. 1
mono-scripts>  update:  1.0   fixedUpdate:  0.0 coroutines:  0.0 
mono-memory>   used heap: 294912 allocated heap: 356352  max number of collections: 0 collection total duration:  0.0
----------------------------------------
iPhone Unity internal profiler stats:
cpu-player>    min:  4.8   max: 132.8   avg: 12.0
cpu-ogles-drv> min:  0.6   max:  1.8   avg:  0.9
cpu-waits-gpu> min:  0.2   max:  1.2   avg:  0.4
cpu-present>   min:  0.4   max:  1.2   avg:  0.5
frametime>     min: 33.3   max: 137.1   avg: 37.2
draw-call #>   min:   7    max:   7    avg:   7     | batched:     0
tris #>        min:  2384  max:  2394  avg:  2388   | batched:     0
verts #>       min:  1341  max:  1361  avg:  1350   | batched:     0
player-detail> physx:  6.7 animation:  0.0 culling  0.0 skinning:  0.0 batching:  0.0 render:  3.1 fixed-update-count: 0 .. 2
mono-scripts>  update:  1.0   fixedUpdate:  0.0 coroutines:  0.0 
mono-memory>   used heap: 364544 allocated heap: 479232  max number of collections: 0 collection total duration:  0.0
----------------------------------------
iPhone Unity internal profiler stats:
cpu-player>    min:  3.5   max: 15.1   avg:  6.5
cpu-ogles-drv> min:  0.6   max:  1.7   avg:  0.7
cpu-waits-gpu> min:  0.3   max:  0.5   avg:  0.4
cpu-present>   min:  0.4   max:  0.5   avg:  0.4
frametime>     min: 33.3   max: 33.8   avg: 33.4
draw-call #>   min:   6    max:   7    avg:   6     | batched:     0
tris #>        min:  1328  max:  2394  avg:  2072   | batched:     0
verts #>       min:   792  max:  1361  avg:  1186   | batched:     0
player-detail> physx:  2.1 animation:  0.0 culling  0.0 skinning:  0.0 batching:  0.0 render:  2.8 fixed-update-count: 0 .. 1
mono-scripts>  update:  1.1   fixedUpdate:  0.0 coroutines:  0.0 
mono-memory>   used heap: 307200 allocated heap: 479232  max number of collections: 0 collection total duration:  0.0
----------------------------------------
iPhone Unity internal profiler stats:
cpu-player>    min:  4.3   max: 125.3   avg: 10.1
cpu-ogles-drv> min:  0.6   max:  2.3   avg:  0.8
cpu-waits-gpu> min:  0.2   max:  0.6   avg:  0.4
cpu-present>   min:  0.4   max:  1.5   avg:  0.5
frametime>     min: 32.1   max: 129.0   avg: 36.6
draw-call #>   min:   6    max:   9    avg:   7     | batched:     0
tris #>        min:  1330  max:  4608  avg:  2735   | batched:     0
verts #>       min:   796  max:  2429  avg:  1507   | batched:     0
player-detail> physx:  0.7 animation:  0.0 culling  0.0 skinning:  0.0 batching:  0.0 render:  2.6 fixed-update-count: 0 .. 2
mono-scripts>  update:  5.9   fixedUpdate:  0.0 coroutines:  0.0 
mono-memory>   used heap: 335872 allocated heap: 479232  max number of collections: 0 collection total duration:  0.0
----------------------------------------
iPhone Unity internal profiler stats:
cpu-player>    min:  5.4   max:  8.0   avg:  6.6
cpu-ogles-drv> min:  0.7   max:  1.9   avg:  1.0
cpu-waits-gpu> min:  0.3   max:  1.7   avg:  0.6
cpu-present>   min:  0.4   max:  0.5   avg:  0.4
frametime>     min: 33.2   max: 33.6   avg: 33.4
draw-call #>   min:   9    max:  11    avg:  10     | batched:     0
tris #>        min:  4608  max:  6828  avg:  6084   | batched:     0
verts #>       min:  2429  max:  3509  avg:  3141   | batched:     0
player-detail> physx:  0.5 animation:  0.0 culling  0.0 skinning:  0.0 batching:  0.0 render:  3.2 fixed-update-count: 0 .. 1
mono-scripts>  update:  1.8   fixedUpdate:  0.0 coroutines:  0.0 
mono-memory>   used heap: 385024 allocated heap: 479232  max number of collections: 0 collection total duration:  0.0
----------------------------------------
iPhone Unity internal profiler stats:
cpu-player>    min:  5.3   max: 11.6   avg:  7.3
cpu-ogles-drv> min:  0.8   max:  2.5   avg:  1.2
cpu-waits-gpu> min:  0.3   max:  1.0   avg:  0.4
cpu-present>   min:  0.4   max:  0.5   avg:  0.4
frametime>     min: 33.3   max: 33.5   avg: 33.4
draw-call #>   min:  11    max:  11    avg:  11     | batched:     0
tris #>        min:  6818  max:  6828  avg:  6822   | batched:     0
verts #>       min:  3489  max:  3509  avg:  3498   | batched:     0
player-detail> physx:  0.5 animation:  0.0 culling  0.0 skinning:  0.0 batching:  0.0 render:  4.5 fixed-update-count: 0 .. 1
mono-scripts>  update:  1.4   fixedUpdate:  0.0 coroutines:  0.0 
mono-memory>   used heap: 311296 allocated heap: 479232  max number of collections: 0 collection total duration:  0.0
----------------------------------------
iPhone Unity internal profiler stats:
cpu-player>    min:  4.9   max: 10.1   avg:  7.3
cpu-ogles-drv> min:  0.7   max:  2.2   avg:  1.2
cpu-waits-gpu> min:  0.3   max:  1.2   avg:  0.4
cpu-present>   min:  0.4   max:  2.1   avg:  1.0
frametime>     min: 31.7   max: 35.5   avg: 33.5
draw-call #>   min:   9    max:  11    avg:  10     | batched:     0
tris #>        min:  4600  max:  6824  avg:  6083   | batched:     0
verts #>       min:  2413  max:  3501  avg:  3139   | batched:     0
player-detail> physx:  0.7 animation:  0.0 culling  0.0 skinning:  0.0 batching:  0.0 render:  4.0 fixed-update-count: 0 .. 1
mono-scripts>  update:  1.7   fixedUpdate:  0.0 coroutines:  0.0 
mono-memory>   used heap: 344064 allocated heap: 479232  max number of collections: 0 collection total duration:  0.0
----------------------------------------
iPhone Unity internal profiler stats:
cpu-player>    min:  4.3   max: 20.6   avg:  8.5
cpu-ogles-drv> min:  0.6   max:  1.2   avg:  0.8
cpu-waits-gpu> min:  0.3   max:  0.5   avg:  0.3
cpu-present>   min:  0.4   max:  0.9   avg:  0.4
frametime>     min: 32.9   max: 33.9   avg: 33.4
draw-call #>   min:   7    max:   9    avg:   7     | batched:     0
tris #>        min:  2390  max:  4604  avg:  3158   | batched:     0
verts #>       min:  1353  max:  2421  avg:  1769   | batched:     0
player-detail> physx:  3.1 animation:  0.0 culling  0.0 skinning:  0.0 batching:  0.0 render:  3.0 fixed-update-count: 0 .. 1
mono-scripts>  update:  1.7   fixedUpdate:  0.0 coroutines:  0.0 
mono-memory>   used heap: 311296 allocated heap: 479232  max number of collections: 0 collection total duration:  0.0
----------------------------------------
iPhone Unity internal profiler stats:
cpu-player>    min:  4.3   max:  5.9   avg:  5.0
cpu-ogles-drv> min:  0.7   max:  1.0   avg:  0.8
cpu-waits-gpu> min:  0.3   max:  0.4   avg:  0.4
cpu-present>   min:  0.4   max:  0.5   avg:  0.4
frametime>     min: 33.3   max: 33.5   avg: 33.4
draw-call #>   min:   6    max:   8    avg:   7     | batched:     0
tris #>        min:  1328  max:  2488  avg:  1680   | batched:     0
verts #>       min:   792  max:  1549  avg:  1028   | batched:     0
player-detail> physx:  0.4 animation:  0.0 culling  0.0 skinning:  0.0 batching:  0.0 render:  2.7 fixed-update-count: 0 .. 1
mono-scripts>  update:  1.0   fixedUpdate:  0.0 coroutines:  0.0 
mono-memory>   used heap: 323584 allocated heap: 479232  max number of collections: 0 collection total duration:  0.0
----------------------------------------
iPhone Unity internal profiler stats:
cpu-player>    min:  4.4   max:  6.4   avg:  5.1
cpu-ogles-drv> min:  0.6   max:  1.2   avg:  0.7
cpu-waits-gpu> min:  0.3   max:  0.5   avg:  0.4
cpu-present>   min:  0.4   max:  0.5   avg:  0.4
frametime>     min: 33.3   max: 34.1   avg: 33.5
draw-call #>   min:   7    max:   7    avg:   7     | batched:     0
tris #>        min:  2384  max:  2396  avg:  2388   | batched:     0
verts #>       min:  1341  max:  1365  avg:  1350   | batched:     0
player-detail> physx:  0.4 animation:  0.0 culling  0.0 skinning:  0.0 batching:  0.0 render:  2.9 fixed-update-count: 0 .. 1
mono-scripts>  update:  0.9   fixedUpdate:  0.0 coroutines:  0.0 
mono-memory>   used heap: 364544 allocated heap: 479232  max number of collections: 0 collection total duration:  0.0
----------------------------------------
kill
quit

The Debugger has exited with status 0.

does this happen after you reboot the iphone? some glitches aren’t really avoidable on the development device after extended periods of development.

Yes, it happens even when I reboot my IPod 3rd gen.

You can see there the “stalls” stemmed from the cpu-player process (132 and 125) which last just some millisecs but still noticeable and then everything renders smoothly.

Any ideas?

Is this all just one scene or are you loading some bits yourself, or loading some bits in a previous scene with nodestroy? Does it happen every time objects leave and enter the screen or just the first time?

Does it happen if these models have no textures at all?

The best workaround is a warm start. When the level loads, render it on screen under other geometry or behind a billboard of some kind. This is a solution that will at least work right now.

It’s just one scene. And it happens when objects enter teh scene for the first time.

Didn’t try that. Let me run it with no textures and see how it goes :slight_smile:

If it’s texure based then what device and iOS are you using as well as render path? Hopefully unity staff can shed some light on it.

Sorry for the late response but I decided to get Unity 3.1 before posting any further.

So, I stripped out all textures on models, set shaders to Diffuse Fast, set two layers (one for the background and the other ignoring raycasting) and lower quality to the minimum, but I still get same results even with Unity 3.1.

So I used XCode profiler to assess what’s going on and hereunder you will find a couple of screenshots:

Samples

As you can see, when GraphicServices take 90 ms when some objects enter the viewable part of the scene on sample 1101 and 10 on sample 1102. Before and after, everything goes well.

Leaks

Also, found this memory leak of 32KB.

Thoughts?

Stalls stemming from this are always present in “i” devices, and ocassionally -which is rare, also happens on other target platforms (Win/Mac/Browser). So, I guess is Unity is doing something “big” when a visible game objects hits the camera frustrum regardless the target platform.

Any ideas? Anyone in the Unity staff? :slight_smile:

I have tried many things and it all seems to point that some big setup operations are done when game objects hit the camera frustrum.

Can anyone from Unity staff shed some light on this issue, please? I’m stuck with development and I don’t know what else to do … :frowning:

Hummm. I’m using Unity 3.1 but the game was created with 1.7. Is there any way to verify that the mono project is referencing the updated assemblies of the Unity engine? And ditto for the final XCode project?

Perhaps textures, 3D models a/o shaders are being loaded into memory when they hit the camera frustrum for the first time, and that is causing the stalls (which are significant on the “i” devices).

oops, completely missed the thread. Just submit bug report with repro project attached and send me the case number :wink:

Ok, I’ve copied the project and deleted all unnecessary assets from it.

I’m now reporting the bug (the new project is attached to it).

As soon as the uploading process finishes I will send you the case number. Thanks :slight_smile:

[Please don’t use, copy and or distribute the 3d meshes; I’m just attaching them so that you can reproduce the issue]

Ok, PM sent with case number. Thanks!

In 3.0/3.1 very is a small performance impact when you render mesh for the first time (internal VBOs are being filled at that moment, so bigger the mesh - longer it takes). We’re currently working on solving issues like that!

Thanks for submitting a bug - it gives us better coverage for testing.