Optimizing Large Scenes in Unity
-
Upload
noam-gat -
Category
Engineering
-
view
3.475 -
download
0
Transcript of Optimizing Large Scenes in Unity
![Page 1: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/1.jpg)
Optimizing Large Scenes in Unity
How we put 5,000 bases on a planet and lived to tell about it
Noam GatTacticsoft
@noamgat
![Page 2: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/2.jpg)
Hello!
Noam Gat
CTO @ Tacticsoft
Past - JoyTunes, Omek Interactive, OGRE
![Page 3: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/3.jpg)
Earth Arena Case Study
Tacticsoft’s upcoming Mobile Strategy MMO
Take over the world with your friends!
http://www.eartharena.com
![Page 4: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/4.jpg)
Earth Arena Case Study
5,000 bases on a planet, with 3 levels of detail and smooth transitions
![Page 5: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/5.jpg)
The challenge (today) is not to make a single object look good, but to make an entire scene run smoothly
- Frames per second- Loading Time- Memory footprint- Artist pipeline
Scene Management
![Page 6: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/6.jpg)
Step 1 - Prove the problem
![Page 7: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/7.jpg)
Step 1 - Prove the problem
Premature optimization is the root of all evil
![Page 8: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/8.jpg)
Naive solution
- Instantiate a prefab for every base in the world- Each object contains 3 LOD objects- Show / hide object groups based on camera
https://youtu.be/Q2gNauFxwMA
![Page 9: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/9.jpg)
Naive solution
Worked surprisingly well!
- It was faster to disable/enable renderers rather than game objects
- Started becoming heavy on iPhone 6 with 1k objects, but was good enough
![Page 10: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/10.jpg)
Test Driven Development (TDD)
Not only about unit tests.
Create a consistent, easy to run, reproducible environment to check that what you created meets your expectations.
![Page 11: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/11.jpg)
Stress Test, Take 1
https://youtu.be/fjEkRZsrUSA
![Page 12: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/12.jpg)
Step 1 - Prove the problem
Not stupid, but naive.
![Page 13: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/13.jpg)
Step 2 - Investigate
![Page 14: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/14.jpg)
Step 2 - Investigate
Between the different aspects of the scene, which one was causing the crash?
- Frames per second- Loading Time- Memory footprint- Artist pipeline
![Page 15: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/15.jpg)
Unity’s Toolset
![Page 16: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/16.jpg)
Benchmarking
A few experiments revealed:
- Unity objects / components cost ~1k per instance
- 5k objects, 10 GOs/object, 3 components / GO -> 150k components -> 150MB scene
- Dynamic batching was the biggest CPU hit, and batching was also becoming a problem
![Page 17: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/17.jpg)
Step 3 - Solve
We wanted a solution that has:
- A stable memory footprint in large worlds- Good FPS / batching performance- Looks like the previous solution- Won’t be a nightmare to control artistically
![Page 18: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/18.jpg)
Solution approaches
Two approaches:
- Pooling - Return out of camera objects to a pool, reuse them when the camera moves
- Baking - Dynamically a large mesh with many objects packed together (similar to Unity’s static batching)
![Page 19: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/19.jpg)
Solution problems
Pooling - When looking at the world from far away, we see half of the objects in the world, so pooling is only a 50% reduction
![Page 20: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/20.jpg)
Solution problems
Baking - When looking at the world from up close, we have 3d models with relatively high poly counts and some with animations
![Page 21: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/21.jpg)
Solution
The problems never happen at the same time, and we can predict which will happen when.
![Page 22: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/22.jpg)
Solution (Artist side)
Baked objects go to baking pipeline
Instanced objects go to pooling pipeline
![Page 23: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/23.jpg)
Array of Structs / Struct of Arrays
- Array of structs is the intuitive way to look at things (every object has several properties)
- Struct of arrays is a bit weird (several properties have values for every object)
Struct of arrays allows us to look at a vertical of all objects (“All baked LOD1 objects”) and do something special for them
![Page 24: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/24.jpg)
Baked pipeline
Not MonoBehaviour, ~ 200 bytes memory
Minimal amount of information to render and order
![Page 25: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/25.jpg)
Baked pipeline
![Page 26: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/26.jpg)
Baked pipeline
Less batches, much less game objects / components
![Page 27: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/27.jpg)
Baked pipeline gotcha
When viewing a lot of sprites from far away, better to turn off “Tight” sprite packing to allow more sprites in same amount of geometry
![Page 28: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/28.jpg)
Pooled pipeline
Which instance to instantiate
Where to put it
![Page 29: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/29.jpg)
Pooled pipeline
Rest of pooled pipeline reiles on previous “camera culling” method from first naive attempt - instead of disabling / enabling objects we take / return them to the pool
![Page 30: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/30.jpg)
Object permutations
The objects have different properties (colors etc) based on their role in the world.
We do not want a different prefab for each permutation of each object.
![Page 31: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/31.jpg)
“Visitor” Design Pattern
![Page 32: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/32.jpg)
“Visitor” Design Pattern
![Page 33: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/33.jpg)
“Visitor” Design Pattern
The visitor pattern allows us to easily reuse prefabs, making modifications as needed in both the baked and pooled pipelines.
It puts healthy design constraints on what you can and can’t put on objects placed on the map (MVC principles etc)
![Page 34: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/34.jpg)
Step 4 - Test
![Page 35: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/35.jpg)
Number of objects
![Page 36: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/36.jpg)
Number of batches
https://youtu.be/Jys5bdeJCtM
![Page 37: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/37.jpg)
Device performance
https://youtu.be/skRCgq093Mc
![Page 38: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/38.jpg)
The future...
https://youtu.be/sOnzYTT793Y
![Page 39: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/39.jpg)
TL;DR
- It’s OK to be naive for 99% of the code- TDD applies to optimization as well- Many approaches to solve problems, the
correct one stems from understanding the problem
- Several design patterns are very useful for optimization
![Page 40: Optimizing Large Scenes in Unity](https://reader035.fdocuments.net/reader035/viewer/2022062901/58f9ad83760da3da068b9913/html5/thumbnails/40.jpg)
Thank You!
Come play our games!http://www.eartharena.com
http://www.tacticsoft.net
@noamgat