Book Image

Unity 2018 Shaders and Effects Cookbook - Third Edition

By : John P. Doran, Alan Zucconi
Book Image

Unity 2018 Shaders and Effects Cookbook - Third Edition

By: John P. Doran, Alan Zucconi

Overview of this book

Since their introduction to Unity, shaders have been seen as notoriously difficult to understand and implement in games. Complex mathematics has always stood in the way of creating your own shaders and attaining the level of realism you crave. Unity 2018 Shaders and Effects Cookbook changes that by giving you a recipe-based guide to creating shaders using Unity. It will show you everything you need to know about vectors, how lighting is constructed with them, and how textures are used to create complex effects without the heavy math. This book starts by teaching you how to use shaders without writing code with the post-processing stack. Then, you’ll learn how to write shaders from scratch, build up essential lighting, and finish by creating stunning screen effects just like those in high-quality 3D and mobile games. You'll discover techniques, such as normal mapping, image-based lighting, and animating your models inside a shader. We'll explore how to use physically based rendering to treat light the way it behaves in the real world. At the end, we’ll even look at Unity 2018’s new Shader Graph system. With this book, what seems like a dark art today will be second nature by tomorrow.
Table of Contents (14 chapters)

Installing the Post Processing Stack

Before we can use the Post Processing Stack, we must first get it from the newly introduced Package Manager. A Unity package is a single file that contains various assets that can be used in Unity in a similar manner to a .zip file. Previously, Unity used the Asset Store to share these files with users, but as time has gone on, the Package Manager has been added to give users an easy way to get free content from Unity. We will actually be using the Package Manager again in Chapter 12, Shader Graph, but right now we will be using it for the Post-Processing package that it contains.

Getting ready

To get started with this recipe, you will need to have Unity running and have created a new project. This chapter also requires you to have an environment to work from. The code files provided with this book will contain a basic scene and content to create the scene Unity's Standard Assets.

Open the Chapter 1 | Starting Point scene inside of the Asset | Chapter 01 | Scenes folder from the Project browser. If all goes well, you should see something like this from the Game tab:

This is a simple environment that will allow us to easily see how changes made in post-processing effects can modify how things are drawn on the screen.

If you are interested in learning how to create the environment used, check out my previous book, Unity 5.x Game Development Blueprints, also available from Packt Publishing.

How to do it...

To begin:

  1. Open up the Package Manager by going to Window | Package Manager (or by pressing Ctrl + 9):
  1. From the list view, click on the All button to display a list of all of the possible packages that are there. Once the list populates with all of the choices, select the Post-processing option:
  1. From there, at the top right of the menu, click on the Install 2.0.7-preview button. You may need to wait for a while for it to complete downloading the content. Once it's completed, you should be brought back to the In Project selection and now you'll see Post-processing added to the list:
  1. Close out of the Packages tab and go back to the Scene window to see the level. Then, from the Hierarchy window, we want to select the object that has our Camera component attached to it, as the Post Processing Stack needs to know which screen we want to modify. If you are using your own project, you may select the MainCamera object that comes with the default Unity scene, but the example project that is being used has the Camera located as a child of the FPSController object. To select it, click on the arrow next to the name to extend the object's children and then select the FirstPersonCharacter object:

This object has the Camera component on it, which is in charge of drawing what it sees to the Game tab when the game starts.

You can double-click on a game object in the Hierarchy tab to zoom the camera from the Scene tab to its location. This makes it very easy to find things, even in a large game level.
  1. With the object selected and our Camera component attached to it, next we need to add the Post-processing Behavior component to the object by going into Component | Rendering | Post-process Layer:
  1. Once added, from the Inspector tab, scroll down to the Post Process Layer (Script) component and, under Layer, change the dropdown to PostProcessing.
  2. This tells the component which objects we want to draw on the screen. When setting this, an object must have its Layer property set to PostProcessing in order to be seen.
  1. To create a Post Process Volume, go to the menu and select GameObject | 3D Object | Post Process Volume. From there, go to the Inspector tab and change the Layer property to PostProcessing. Finally, to make it easier to work with, change the Position to 0, 0, 0 and under the Post Process Volume component, check the Is Global property:

Notice that there is a Profile property to the volume. This property will contain information about how we wish to modify the screen. By checking Is Global, we are saying that this information should always be drawn on an object. By unchecking it, the effects will only be visible from a certain distance from where the volume is placed. Depending on the game, this could allow you to drastically modify how the game appears in certain areas, but we care only about getting the visual effect at this point.