Traffic simulation - large number of objects - c#

I have a CSV with the following columns: time, carId, x, y. I am writing a script which will read the CSV and spawn car objects and simply update their positions over time based on the data.
Over the span of about 20 minutes, around 3500 car objects will have to have been instantiated. While they won't all be in the simulation at one point in time (once a car gets to certain points in my road network, it will disappear), but I want to be prepared for a situation where hundreds of car objects move through the network at once.
I'm still new to Unity and its rendering pipeline when it comes to optimizing for this big of a project. I know in some cases it's better to setActive(false) on GameObjects as opposed to destroy() and maybe this is one of them. What else should I consider when handling this many gameobjects in Unity?

What else should I consider when handling this many gameobjects in
Unity?
For the most part, this really depends on the amount of Objects or cars that will be displayed at the same time on the screen.
If it's just few cars then use Object Pooling to recycle the Objects.
You should also use LOD to optimize and reduce the number of triangles rendered for an object.
If it's a traffic simulation with hundreds of cars moving at the same time then you should use GPU Instancing which is now built into Unity. To enable GPU, use a Standard Shader and check the "Enable GPU Instancing" box.
After enabling it on the material, you can use Graphics.DrawMeshInstanced to instantiate the Objects and MaterialPropertyBlock to change how they look.

This sounds like a great opportunity to use Object Pooling. You are on the right track with using setActive.
Follow this short tutorial : https://unity3d.com/learn/tutorials/topics/scripting/object-pooling
It should reduce a lot of the lag you would get by instantiating/ destroying a lot of objects.

Related

Is there an elegant way to populate an area with RigidBody objects?

I'm creating a penny pusher (like the game machines you find in arcades) game in Unity in my spare time and obviously that means I need to populate the shelf with coins so the player's spawned coins actually have function.
My issue is I haven't worked out how to spawn that many coins in an elegant way which won't either slow the game down with spawning so many objects at once or result in a lumped pile of coins rather than the neat spread I need.
I even tried speeding up the simulation by several times and dropping a pile of coins on the pushing shelf so they would "settle" before the player input was enabled. It didn't work.
So I ask if there's a good way to do this as part of the initialisation of the game and any Unity or C# function which would have the intended effect.
Thanks in advance.
If I understand your request correctly it really does dependent. The problem is that your game will be lagging when you have to many physic interactions. So even if you spawned them without alot of physic interactions, once u move one coin it will start lagging.
I would look for an alternative way then physics to handle them. Maybe you can think of a more simple calculation you do your self where you have less interactions going on each frame.
That beeing said a way to spawn the coins would be to calculate a random position at a fixed height (max height) and then find out what the lowest point is where the coin can sit on (so it does not share space with another coin). Now place it there. Even better would be to calculate if it could actually be placed there without instanly falling.
I think the best way would be having predefined "Coin Towers" (prefabs of them) where you have like 10 variations and then place them at different positions but do not allow to spawn any "Coin Towers" on another one. This would result in less physic interactions when spawned too.
May be helpful too: https://www.youtube.com/watch?v=pTz3LMQpvfA

Unity Procedural Game Optimization

I am currently developing a 3d dungeon crawler which implements a procedural dungeon generation system which works perfectly fine. However the game is not very performant. I have tried a few optimization tricks I heard like:
Adding LOD Groups to each room prefab (this works perfectly fine), has impact on fps and batches
Tried addig static batching, I made every room prefab static and enabled dynamic and static batching in the player settings, however there is no change in the batches count or the saved batches count.
Tried addind occlusion culling but i cannot bake it before the game, as it's a procedural generated world.
The room prefabs are low poly rooms and are not even decorated yet, just the floor and walls and torches.
I should mention that i am using deffered lightning and I am activating the torch point lights as the players walks in the room. There aren't any baked lights everything is realtime. Every room prefab uses the same metrial and is not copied when the room is instantiated.
Some screenshots to help you understand better:
If you need anymore info, please don't hesitate to ask. Thanks you
Might be overkill, but have you thought about making the meshes a subscene / an entity using dots. Then just handle the colliders separately.
Hope that works for you.
If that is still having issues you may consider removing any colliders that are outside of a certain distance from you, and loading them in when you get close.

Spatial Understanding limited by a small area

I am working with the Hololens in Unity and trying to map a large area (15x15x25) meters. I am able to map the whole area using the SpatialMapping prefab, but I want to do some spatial processing on that mesh to smoothen out the floors and walls. I have been trying to use SpatialUnderstanding for this, but there seems to be a hard limit on how big of an area you can scan with this, which has been detailed by hololens forums thread.
Currently, I don't understand how the pipeline of data works from SpatialMapping to SpatialUnderstanding. Why can I not simply use the meshes generated from SpatialMapping in SpatialUnderstanding? Is there some better method of creating smooth surfaces?
This solution works best for pre-generated rooms. In other words a general solution, one that could be expected to be used by end users, is not possible, given the current limitations.
I will start with the last question: "Is there some better method of creating smooth surfaces?"
Yes, use a tripod on wheels to generate the initial scan. Given the limited resolution of the accelometers and compasses in the hardware, reducing the variance in one linear axis, height, and one rotational axis, roll(roll should not vary during at all during a scan), will result in a much more accurate scan.
The other method to create smooth surfaces is to export the mesh to a 3D editing program and manually flatten the surfaces, then reimport the mesh into Unity3D.
"Why can I not simply use the meshes generated from SpatialMapping in SpatialUnderstanding?"
SpacialUnderstanding further divides the generated mesh into (8cm,8cm,8cm) voxels and then calculates the surfels based on each voxel. To keep performance and memory utilization in check, a hard limit of approximately(10m,10m,10m). That is implemented as(128,128,128) voxels.
Any attempt to use SpacialUnderstanding beyond its defined limits, will produce spurious results due to overflow of the underlying data structures.

Unity big scale world simulation performance problem.

We are working on simulation game. We have about 25000 objects at world. All has 1 unity c# script. If we activate empty update function we get 15 fps if we activate empty fixed update with 0.02 time scale we get 1-2 fps at average spec computer. In Will we need to do something on update function. So need some help for this performance problem.
In that case, what can we do for performance?
Thanks for advices and help.
Well even though your question is broad i will try to give some tips. My guess is you are doing some heavy calculations in your Update and this causes fps problems. Also rendering 25000 object at the same time would cause fps issues. First i suggest using Unity Profiler and Unity statistics to find out what is causing the issue. Then my suggestions would be based on the problem i assume:
Use Coroutines instead of doing all the calculations in Update.
Use Occlusion culling for rendering as minimum objects as possible. You do not need to render objects which are not in Camera frustum.
If you have meshes which are detailed use level of detail if you have the chance.
If you narrow down your problem i am happy to help further. Good luck!
The very need to make a call to Update() is one of the major slowdowns in Unity. That's why they are now pusing towards Entity Component System where there is no need for such jumps.
If all the objects are having the same script, it should be quite easy to modify the code so that only 1 object has the script, but operates (i.e. via a for (;;) loop) on all other objects. This should bring massive improvements already.
Aslo if theres any chance your objects share meshes - do use Instanced Mesh Rendering it is faster by two orders of magnitude

Render an object multiple times a frame using different textures. What's more performant? Multiple Materials or calling SetTexture multiple times?

I need to render an object multiple times a frame using different textures. I was wondering about the most performant way to do so. My first approach was to have one Material and in OnRenderImage() call SetTexture() on it for the given number of textures I have. Now I'm wondering if it would be a noticeable improvement if I set up one Material per Texture in Start() and change between Materials in OnRenderImage(). Then I wouldn't need the SetTexture() call. But I can't find what SetTexture() actually does. Does it just set a flag or does it copy or upload the texture somewhere?
From working with low-end devices extensively, performance comes from batching. It's hard to pin point what would improve performance in your context w/o a clear understanding of the scope:
How many objects
How many images
Target platform
Are the images packaged or external at runtime
...
But as a general rule you want to use least amount of materials and individual textures possible. If pre-processing is an option, I would recommend creating spritesheets with as many images as possible on a single image. Then using UV offsetting you can show multiple images on multiple objects for 1 draw call.
I have used extensively a solution called TexturePacker which supports Unity. It's not cheap, there's an app to buy plus a plugin for Unity but it saves time, and draw calls, in the end.
Things like packing hundreds of images into a few 4k textures and down to 3 or 4 draw calls vs 100s before.
That might not be a solution in your case, but the concept is still valid.
Also unity prefabs will not save Draw Calls, but reduce memory usage.
hth.
J.

Categories