Search Unity

Monobehaviour.OnMouse_ FPS Spikes in Profiler

Discussion in 'Scripting' started by KingOfColly, May 25, 2013.

  1. KingOfColly

    KingOfColly

    Joined:
    May 25, 2013
    Posts:
    15
    Hello,

    I am trying to stress test a custom terrain engine I am writing. It uses chunked LOD to render terrain tiles (using quadtree). For performance I prepare my chunk data using threading and only create a mesh for 1 chunk per frame. I also use a gameobject pool rather than instantiating each time. I am performing the stress test by increasing the radius at which high detail tiles are created.

    When I try to identify bottlenecks in the profiler using both normal and deep profiling I notice FPS spikes caused by Monobehaviour.OnMouse_. This happens at points where lots of chunks are being queued for rendering.

    Has anyone else experienced this problem? I do not see why the lag is produced by this, I would expect it to be caused by my own routines. To clarify I don't have any GUI elements elements or OnGUI code and my camera movement script simply tells the camera to move forward using a simple call to rigidbody.AddForce in FixedUpdate(). I'm not utilising the mouse at all basically.

    Thanks in advance!
     
  2. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    Is your code being called from within the OnMouse_ methods? If so then they would have to take at least as long as your own code.
     
  3. KingOfColly

    KingOfColly

    Joined:
    May 25, 2013
    Posts:
    15
    Thank you for your prompt reply. My code makes no use of OnMouse methods, to be sure i searched for the keyword 'mouse'.

    Another strange issue with this - if I replace my own camera and script with a first person controller I get an FPS spike in CharacterMotor.FixedUpdate() instead on OnMouse_. I've also just gone through and added some debug code that measures how long each of my key functions takes and to log to console if it exceeds 0.1seconds - when i notice the game skip it does not log anything to the console.

    I have tried writing many different types of terrain engine in Unity and they all seem to hit this problem. Ive tried CLOD, quad trees, octrees, planetary engines they all suffer the same fate. It's as if Unity gets confused once draw calls hit a certain limit and it starts randomly farting out completely unrelated calls in the overview :)
     
  4. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,191
    You can try creating a script with this line in the Awake method:
    Code (csharp):
    1. camera.eventMask = 0;
    This would need to be attached to the same gameObject that the camera is attached to. Alternatively you can just find the main camera and set the event mask from a script not attached to the gameObject your camera is on.

    According to this page, that is suppose to disable all mouse events, which should ideally remove your spikes.

    However, I have not had much luck using this method. Querying the eventMask before/after setting it to 0 shows it changes from -1 to 0, but being set to 0 doesn't seem to do anything. OnMouse_ still shows up in the profiler. Please let me know if you figure out how to stop this from happening!
     
    Last edited: Aug 18, 2013
  5. KingOfColly

    KingOfColly

    Joined:
    May 25, 2013
    Posts:
    15
    Thanks gilley I'll have a play with that!

    I have since created a new NLOD terrain system from scratch that let's me swap out different terrain generation methods. If I use heightmap generation it is extremely fast, no problems whatsoever even moving at speed. As soon as I switch to my marching cubes generation I start getting lag spikes every second or so. The only way I can remove this lag is by reducing my 'view distance' (the distance I generate chunks to).

    My Pro trial has since ran out so I no longer have access to the profiler but I just can't understand what causes the lag. I understand that marching cubes is computationally more expensive but we are only talking 5ms per chunk and they are using co-routines only generating one chunk per frame. Threading makes no difference either.

    I am contemplating buying Pro but this is kind of a show-stopper for me so far.
     
  6. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    Without knowing more about your code there's not really a lot we can help with.

    You say that the spikes happen in Unity functions, but do they also happen in your own? Is there any possibility it's garbage collection (ie; are you allocating anything)? That wouldn't necessarily spike within your own code, it'll spike in whatever code happens to be running when the Mono runtime decides it's time to clean up.
     
  7. KingOfColly

    KingOfColly

    Joined:
    May 25, 2013
    Posts:
    15
    Hi,

    I'll try and give some more info I know it's difficult to advise without seeing code.

    I have a gameobject with my NLOD script attached, this handles the generation of chunks. I have a chunk prefab with a chunk generation script, this handles the voxel data generation and mesh rendering for the chunk.

    At startup the NLOD script instantiates a pool of gameobjects (using the chunk prefab) which are used to generate the chunks. The NLOD script has a main loop (coroutine) that yields every 0.1 seconds. The main loop will first check if the player has moved to a different chunk and if so update a work queue (list of chunk coords that need a chunk). After this it takes the first item off the work queue (if any) allocates a chunk from the pool and calls the Init method on the chunk generation script.

    The Init method calls a coroutine that generates the voxels (just a 3d array of floats) using noise and fractals; then the marching cubes algorithm is computed; then finally building the vertices, triangles and uv arrays ready for generating the mesh (this all takes about 5ms per chunk on average). Then finally I call another co routine that generates the mesh.

    I get lag issues when I'm using a view distance of 256 which equates to around 800 chunks the lag is there even when standing still and looking with the camera (using the standard CharacterController). The lag isn't immediate from starting the game but only becomes apparent after most of the chunks have completed rendering. If I reduce the view distance to 128 then the lag is eliminated.

    I have tried threading the chunk data generation part but that only increases the speed the chunks are generated, the lag pulses are effectively the same.

    Thanks for taking the time to read!
     
  8. ParadoxLiving

    ParadoxLiving

    Joined:
    Dec 14, 2017
    Posts:
    13
    Sorry to resurrect an old thread, but by chance did you ever pinpoint the issue?
     
  9. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Yes, it's been discussed at length elsewhere. This spike is editor-önly. If you profile a build, it should not show up at all.