Reader small image

You're reading from  Unity 5.x Shaders and Effects Cookbook

Product typeBook
Published inFeb 2016
Reading LevelIntermediate
Publisher
ISBN-139781785285240
Edition1st Edition
Languages
Tools
Concepts
Right arrow
Author (1)
Alan Zucconi
Alan Zucconi
author image
Alan Zucconi

Alan Zucconi is a passionate developer, author, and lecturer, recognized as one of Develop's 30 under 30. His expertise has been built over the past 10 years, working in the intersection between creativity and education. Starting in 2015, he published a series of educational tutorials for other developers and machine learning enthusiasts.
Read more about Alan Zucconi

Right arrow

Chapter 7. Mobile Shader Adjustment

In the next two chapters, we are going to take a look at making the shaders that we write performance-friendly for different platforms. We won't be talking about any one platform specifically, but we are going to break down the elements of shaders we can adjust to make them more optimized for mobiles and efficient on any platform in general. These techniques range from understanding what Unity offers in terms of built-in variables that reduce the overhead of the shaders memory to learning about ways in which we can make our own shader code more efficient. This chapter will cover the following recipes:

  • What is a cheap shader

  • Profiling your shaders

  • Modifying our shaders for mobile

Introduction


Learning the art of optimizing your shaders will come up in just about any game project that you work on. There will always come a point in any production where a shader needs to be optimized, or maybe it needs to use less textures but produce the same effect. As a technical artist or shader programmer, you have to understand these core fundamentals to optimize your shaders so that you can increase the performance of your game while still achieving the same visual fidelity. Having this knowledge can also help in setting the way in which you write your shader from the start. For instance, by knowing that the game built using your shader will be played on a mobile device, we can automatically set all our lighting functions to use a half vector as the view direction or set all of our float variable types to fixed or half. These, and many other techniques, all contribute to your shaders running efficiently on your target hardware. Let's begin our journey and start learning how to...

What is a cheap shader?


When first asked the question, what is a cheap shader, it might be a little tough to answer as there are many elements that go into making a more efficient shader. It could be the amount of memory used up by your variables. It could be the amount of textures the shader is using. It could also be that our shader is working fine, but we can actually produce the same visual effect with half the amount of data by reducing the amount of code we are using or data we are creating. We are going to explore a few of these techniques in this recipe and show how they can be combined to make your shader fast and efficient but still produce the high-quality visuals everyone expects from games today, whether on a mobile or PC.

Getting ready

In order to get this recipe started, we need to gather a few resources together. So let's perform the following tasks:

  1. Create a new scene and fill it with a simple sphere object and single directional light.

  2. Create a new shader and material and assign...

Profiling your shaders


Now that we know how we can reduce the overhead that our shaders might take, let's take a look at how to find problematic shaders in a scene where you might have a lot of shaders or a ton of objects, shaders, and scripts, all running at the same time. To find a single object or shader among a whole game can be quite daunting, but Unity provides us with its built-in Profiler. This allows us to actually see, on a frame-by-frame basis, what is happening in the game and each item being used by the GPU and CPU.

Using the Profiler, we can isolate items such as shaders, geometry, and general rendering items using its interface to create blocks of profiling jobs. We can filter out items till we are looking at the performance of just a single object. This then lets us see the effects on the CPU and GPU that the object has while it is performing its functions at runtime.

Let's take a look through the different sections of the Profiler and learn how to debug our scenes and, most...

Modifying our shaders for mobile


Now that we have seen quite a broad set of techniques to make really optimized shaders, let's take a look at writing a nice, high-quality shader targeted for a mobile device. It is actually quite easy to make a few adjustments to the shaders we have written so that they run faster on a mobile device. This includes elements such as using the approxview or halfasview lighting function variables. We can also reduce the amount of textures we need and even apply better compression for the textures we are using. By the end of this recipe, we will have a nicely optimized normal-mapped, Specular shader for use in our mobile games.

Getting ready

Before we begin, let's get a fresh new scene and fill it with some objects to apply our Mobile shader:

  1. Create a new scene and fill it with a default sphere and single directional light.

  2. Create a new material and shader, and assign the shader to the material.

  3. Finally, assign the material to our sphere object in our scene.

When completed...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Unity 5.x Shaders and Effects Cookbook
Published in: Feb 2016Publisher: ISBN-13: 9781785285240
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Alan Zucconi

Alan Zucconi is a passionate developer, author, and lecturer, recognized as one of Develop's 30 under 30. His expertise has been built over the past 10 years, working in the intersection between creativity and education. Starting in 2015, he published a series of educational tutorials for other developers and machine learning enthusiasts.
Read more about Alan Zucconi