Does the entire scene get drawn on screen? - unity3d

Ok so i'm building a game and just using the WebPlayer.
It plays just fine for me and others with no performance issues.
I loaded it to an iPhone 5 to see how it would handle performance and it's far from acceptable. Likely due to the nature of it and all of the objects and effects being drawn.
Is the entire scene loaded at once or are the items that are out of range only drawn when the camera is in that area?
Here is the game
http://burningfistentertainment.com/3D/DevilsNote/index.html
Any pointers would be great.

Is the entire scene loaded at once
Yes it is. When you load a scene everything contained is loaded into memory. Eventually try to split the scene.
are the items that are out of range only drawn when the camera is in
that area
Each active GameObject inside camera frustum with a Renderer component will be rendered. Always animated Animator components can affect performances even if not rendered.
I loaded it to an iPhone 5 to see how it would handle performance and
it's far from acceptable. Likely due to the nature of it and all of
the objects and effects being drawn.
It's hard to say what could be the bottleneck without knowing more about your code. If you think the problem is GPU related, here's
some tips for mobile:
reduce draw-calls (it depends on the device, I'd say no more than 50-70) => reduce materials count(use atlas), mark static objects as static (this way can be static batched),...
limit overdraw: reduce size and number of transparent objects (what about rain? how do you implemented it?)
consider using occlusion culling (probably not a problem in your game, but if the depth complexity increases it could save you a lot of GPU workload)

Related

Decreasing decoration gameobjects performance cost

A lot of my gameobjects in my scene only serve as decoration. In blender it shows me that I have about 100k vertices and in unity it says that I have 240k. After initialization nothing about them gets changed, not even their shadows change.
Ideas:
Some of the meshes are doubled but I assumed it wouldn't change much if I they would derive from the same mesh because it still needs to get loaded to times.
I already made them static but they are still pretty performance heavy.
The camera doesn't always move so I thought about creating an image of the current background and setting it as the render texture and setting the background as inactive as long as the camera isn't moving.
I hope, however, that there's a better solution.
My settings for the gameobjects' mesh renderer:
All of them have the same material:
The graphics stats in the editor:

Does setting camera's culling mask to Nothing when UI covers the screen is good?

I have a pretty large and full scene and therefore gets a lot of draw calls.
Sometimes I display a video in the game, which covers the entire screen.
When I tested my game with Unity's profiler tool I noticed that the camera still renders everything (although occlusion culling is enabled and calculated), and it causes the video to lag.
My question is how can I disable the camera?
When I disable the Camera component or the camera's GameObject I get a warning ⚠ in the game view that says No camera is rendering to this display. Which, I guess, is not good (correct me if I'm wrong).
So I was wondering if cancelling the culling mask on the camera (by setting it to Nothing) would force unity to stop render the scene.
Or does it still do some work in the background?
(Like with UI elements that still being rendered even though they are fully transparent).
Thanks in advance
I have a pretty large and full scene and therefore gets a lot of draw
calls.
I recommend activating "Instancing" on your materials, it can greatly reduce draw calls.
When the UI Pops open, it can help removing the "Default" layer (or whatever layer the majority of your renderers are) from the active cameras. You can do this easily with layer masks. Or you can just set Camera.main.farClippingPlane to 1 or any low number.

daydream app crashes on quick head rotation

My daydream app is working fine when used slowly. But when I rotate my head too quick it starts giving glitches first and then the app gets crashed. I am guessing it has to do something with frames/sec loaded during high quality objects rendering or something similar. If someone has a solution, please help me out.
Assuming you have no errors in logcat, visual glitches are generally indicative of extremely high per frame GPU load.
It would be good to profile your app and share the crash report - but the only way I've ever been able to actually crash an app this way is with very large textures.
Check the size and number of textures in your scene - it's possible rapid head rotation could be causing a large number of textures to need to be loaded as objects become visible. You can also see a good list of how large the assets are at build time in Unity by inspecting the editor log after a build. This can help make sure you aren't running out of RAM on device.
Make sure you have texture compression and mipmaps enabled on all textures. Disabling mip-maps on minified textures can easily overload the GPU.
Make sure you don't have too much transparency. Adding a lot of overdraw to the scene can overload the GPU.
Follow performance optimization guidelines https://docs.unity3d.com/Manual/OptimizingGraphicsPerformance.html
https://unity3d.com/learn/tutorials/topics/virtual-reality/optimisation-vr-unity
Make sure renderViewportScale is around 0.7, MSAA is at 2x or less, and you aren't using post-process effects, shadows, or any kind of deferred rendering
Stay below 100 draw calls, 200k vertices on screen.

Hexagon Unity TIles - Slow Performance

When implementing a large hexagonal grid (256x256) of tiles in a Unity game, the game becomes very slow and hardly able to function. The hexagons are in a prefab. A script controls the size of the grid and the spacing between each hexagon. How does one go about rendering a 1024x1024 grid of Unity objects?
When the game is built on Win64 it is also still quite slow.
This is an image of hexagons rendered:
http://i.imgur.com/UbA6USt.png
Try making the grid elements static and make sure static batching is turned ON in player settings. This will optimize their rendering significantly. You probably should even go as far as combining them all into a single mesh (see tools like this one for that purpose).
If you can show us the actual scene hierarchy and the actual structure of your grid nodes then we can help even more.
Because of how Unity works, non-static objects have a tendency to get heavy - they each end up with their own transforms and end up getting drawn even when they're not on screen.
It's the reason more minecraft clones aren't seen coming out of Unity.
If you can't set the hexagons to static for some reason (i.e.: creating procedural levels etc), you'll have to perhaps simulate the hexagons through creative shader manipulation (like saving each mesh into a single array of vertices with a second that tracks a corresponding mesh id) or by writing a script that creates/adds vertices and faces to a single mesh on a single game object.
You may also speed up the scene by creating smaller levels and loading/unloading them as the player moves towards them. See: Application.LoadLevelAdditive

SpriteKit where to load texture atlases for thousands of sprites

In my game I have thousands of "tile" nodes which make up a game map (think simcity), I am wondering what the most frame-rate/memory efficient route for texturing and animating each node would be? There a a handful of unique tile "types" which each have their own texture atlas / animations, so making sure textures are being reused when possible is key.
All my tile nodes are children of a single map node, should the map node handle recognising a tile type and loading the necessary atlas & animations (e.g. by loading texture & atlas names from a plist?)
Alternatively, each tile type is a certain subclass. Would it be better for each SKSpriteNode tile to handle their own sprite atlas loading e.g. [tileInstance texturise]; (how does sprite kit handle this? would this method result in the same texture atlas being loaded into memory for each instance of a certain tile type?)
I have been scrounging the docs for a deeper explanation of atlases and texture reusage but I don't know what the typical procedure is for a scenario like this. Any help would be appreciated, thanks.
Memory first: there won't be any noticeable difference. You have to load the tile's textures, textures will account for at least 99% of the memory of the Map+Tiles and that's that.
Texture reuse: textures are being reused/cached automatically. Two sprites using the same texture will reference the same texture rather than each having its own copy of the texture.
Framerate/Batching: this is all about batching properly. Sprite Kit approaches batching children of a node by rendering them in the order they are added to the children array. As long as the next child node uses the same texture as the previous one, they will all be batched into one draw call. Possibly the worst thing you could do is to add a sprite, a label, a sprite, a label and so on. You'll want to add as many sprites using the same texture in consecutive order as is possible.
Atlas Usage: here's where you can win the most. Commonly developers try to categorize their atlases, which is the wrong way to go about it. Instead of creating one atlas per tile (and its animations), you'll want to create as few texture atlases as possible, each containing as many tiles as possible. On all iOS 7 devices a texture atlas can be 2048x2048 and with the exception of iPhone 4 and iPad 1 all other devices can use textures with up to 4096x4096 pixels.
There are exceptions to this rule, say if you have such a large amount of textures that you can't possibly load them all at once into memory on all devices. In that case use your best judgement to find a good compromise on memory usage vs batching efficiency. For example one solution might be to create one or two texture atlases per each unique scene or rather "scenery" even if that means duplicating some tiles in other texture atlases for another scene. If you have tiles that almost always appear in any scenery it would make sense to put those in a "shared" atlas.
As for subclassing tiles, I'm a strong proponent to avoid subclassing node classes. Especially if the main reason to subclass them is to merely change which texture they are using/animating. A sprite already is a container of a texture, so you can as well change the sprite texture and animate it from the outside.
To add data or additional code to a node you can peruse its userData property by creating your own NSMutableDictionary and adding any object you need to it. A typical component-based approach would go like this:
SKSpriteNode* sprite = [SKSpriteNode spriteWithWhatever..];
[self addChild:sprite];
// create the controller object
sprite.userData = [NSMutableDictionary dictionary];
MyTileController* controller = [MyTileController controllerWithSprite:sprite];
[sprite.userData setObject: forKey:#"controller"];
This controller object then performs any custom code needed for your tiles. It could be animating the tile and whatever else. The only important bit is to make the reference to the owning node (here: sprite) a weak reference:
#interface MySpriteController
#property (weak) sprite; // weak is important to avoid retain cycle!
#end
Because the sprite retains the dictionary. The dictionary retains the controller. If the controller would retain the sprite, the sprite couldn't deallocate because there would still be a retaining reference to it - hence it will continue to retain the dictionary which retains the controller which retains the sprite.
The advantages of using a component-based approach (also favored by and implemented in Kobold Kit):
If properly engineered, works with any or multiple nodes. If what if some day you want a label, effect, shape node tile?
You don't need a subclass for every tile. Some tiles may be simple static sprites. So use simple static SKSpriteNode for those.
It lets you start/stop or add/remove individual aspects as needed. Even on tiles you didn't initially expect to have or need a certain aspect.
Components allow you to build a repertoire of functionality you're going to need often and possibly even in other projects.
Components make for better architecture. A classical OOP design mistake is to have Player and Enemy classes, then realize both need to be able to shoot arrows and equip armor. So you move the code to the root GameObject class, making the code available to all subclasses. With components you simply have an equipment and a shooting component add to those objects that need it.
The great benefit of component-based design is that you start developing individual aspects separately from other things, so they can be reused and added as needed. You'll almost naturally write better code because you approach things with a different mindset.
And from my own experience, once you modularize a game into components you get far fewer bugs and they're easier to solve because you don't have to look at or consider other component's code - unless used by a component but even then when one component triggers another you have a clear boundary, ie is the passed value still correct when the other component takes over? If not, the bug must be in the first component.
This is a good introduction on component-based design. The hybrid approach is certainly the way to go. Here are more resources on component based design but I strongly advice against straying from the path and looking into FRP as the "accepted answer's author" suggests - FRP is an interesting concept but has no real world application (yet) in game development.