40
Optimizing Large Scenes in Unity How we put 5,000 bases on a planet and lived to tell about it Noam Gat Tacticsoft @noamgat

Optimizing Large Scenes in Unity

Embed Size (px)

Citation preview

Page 1: Optimizing Large Scenes in Unity

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

Hello!

Noam Gat

CTO @ Tacticsoft

Past - JoyTunes, Omek Interactive, OGRE

Page 3: Optimizing Large Scenes in Unity

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

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

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

Step 1 - Prove the problem

Page 7: Optimizing Large Scenes in Unity

Step 1 - Prove the problem

Premature optimization is the root of all evil

Page 8: Optimizing Large Scenes in Unity

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

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

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 12: Optimizing Large Scenes in Unity

Step 1 - Prove the problem

Not stupid, but naive.

Page 13: Optimizing Large Scenes in Unity

Step 2 - Investigate

Page 14: Optimizing Large Scenes in Unity

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

Unity’s Toolset

Page 16: Optimizing Large Scenes in Unity

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

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

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

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

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

Solution

The problems never happen at the same time, and we can predict which will happen when.

Page 22: Optimizing Large Scenes in Unity

Solution (Artist side)

Baked objects go to baking pipeline

Instanced objects go to pooling pipeline

Page 23: Optimizing Large Scenes in Unity

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

Baked pipeline

Not MonoBehaviour, ~ 200 bytes memory

Minimal amount of information to render and order

Page 25: Optimizing Large Scenes in Unity

Baked pipeline

Page 26: Optimizing Large Scenes in Unity

Baked pipeline

Less batches, much less game objects / components

Page 27: Optimizing Large Scenes in Unity

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

Pooled pipeline

Which instance to instantiate

Where to put it

Page 29: Optimizing Large Scenes in Unity

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

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

“Visitor” Design Pattern

Page 32: Optimizing Large Scenes in Unity

“Visitor” Design Pattern

Page 33: Optimizing Large Scenes in Unity

“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

Step 4 - Test

Page 35: Optimizing Large Scenes in Unity

Number of objects

Page 36: Optimizing Large Scenes in Unity

Number of batches

https://youtu.be/Jys5bdeJCtM

Page 38: Optimizing Large Scenes in Unity

The future...

https://youtu.be/sOnzYTT793Y

Page 39: Optimizing Large Scenes in Unity

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

Thank You!

Come play our games!http://www.eartharena.com

http://www.tacticsoft.net

@noamgat