Unity 2018 Shaders and Effects Cookbook - John P. Doran - E-Book

Unity 2018 Shaders and Effects Cookbook E-Book

John P. Doran

0,0
39,59 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Bring realism to your games by mastering post-processing effects and advanced shading techniques in Unity 2018


Key FeaturesLearn the secrets of creating AAA quality shaders without writing long algorithmsMaster shader programming through easy-to-follow examplesCreate stunning visual effects that can be used in 3D gamesBook Description


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.


What you will learnUnderstand physically based rendering to fit the aesthetic of your gameWrite shaders from scratch in ShaderLab and HLSL/CgCombine shader programming with interactive scripts to add life to your materialsDesign efficient shaders for mobile platforms without sacrificing their realismUse state-of-the-art techniques, such as volumetric explosions and fur shadingMaster the math and algorithms behind the most used lighting modelsUnderstand how shader models have evolved and how you can create your ownWho this book is for


Unity Shaders and Effects Cookbook is for developers who want to create their first shaders in Unity 2018 or wish to take their game to a whole new level by adding professional post-processing effects. A solid understanding of Unity is required to get the most from this book.


John P. Doran is a passionate and seasoned technical game designer, software engineer, and author who is based in Redmond, Washington. For over a decade, John has built games in roles ranging from game designer to lead UI programmer. Additionally, John has taught game development in Singapore, South Korea, and the United States. To date, he has authored over 10 books pertaining to game development. You can follow him on Twitter @jodoran. 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. From 2015, he published a series of educational tutorials for other developers and Machine Learning enthusiasts

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 338

Veröffentlichungsjahr: 2018

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Unity 2018 Shaders and Effects CookbookThird Edition

 

 

Transform your game into a visually stunning masterpiece with over 70 recipes

 

 

 

 

 

 

 

 

 

 

John P. Doran

 

Alan Zucconi

 

 

 

 

 

 

 

 

BIRMINGHAM - MUMBAI

Unity 2018 Shaders and Effects Cookbook Third Edition

 

Copyright © 2018 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Commissioning Editor: Kunal ChaudhariAcquisition Editor: Larissa PintoContent Development Editor: Arun NadarTechnical Editor: Surabhi KulkarniCopy Editor: Safis EditingProject Coordinator: Sheejal ShahProofreader: Safis EditingIndexer: Mariammal ChettiyarGraphics: Jason MonteiroProduction Coordinator: Arvindkumar Gupta

First published: June 2013

Second edition: February 2016

Third edition: June 2018

Production reference: 1280618

Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.

ISBN 978-1-78839-623-3

www.packtpub.com

mapt.io

Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.

Why subscribe?

Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals

Improve your learning with Skill Plans built especially for you

Get a free eBook or video every month

Mapt is fully searchable

Copy and paste, print, and bookmark content

PacktPub.com

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.

Contributors

About the authors

John P. Doran is a passionate and seasoned technical game designer, software engineer, and author who is based in Redmond, Washington. For over a decade, John has built games in roles ranging from game designer to lead UI programmer. Additionally, John has taught game development in Singapore, South Korea, and the United States. To date, he has authored over 10 books pertaining to game development.

Thanks so much to my wife Hien and my family for all of their support while working on this book.

 

 

 

 

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

About the reviewers

Andrew Hlynka is a software engineer and game developer from Windsor, Ontario, Canada. He earned his MSc degree in Computer Science in 2015 from the University of Windsor and has worked in the industry for over five years on software projects involving multimedia, interactive displays, games, mobile apps, and hybrid simulation systems. He continues his research in innovative animation techniques for games as an independent developer, publishing his projects under Dust Scratch Games.

I would like to thank my professors at the University of Windsor for their support and encouragement during my research as a student, and for introducing me to the Unity3D game engine nearly a decade ago, forever changing my workflow in the world of game development.

 

 

 

Michael Miles is a Manufacturing Engineer working in the aerospace industry by day and is a Unity game designer, C#, JavaScript, and Python developer at all other times. With these skills, and upon discovering Unity3D and Blender, Michael began creating interactive training and production-ready instruction tools for the assembly of the aircraft his company builds.

When not in front of the screen of a computer, he likes to play games with family and friends or immerse himself in books whose genres range from fantasy to theoretical physics.

Thanks to my parents for always being, there supporting every decision I made, however unwise you thought it was at the time.

 

 

Packt is searching for authors like you

If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.

Table of Contents

Title Page

Copyrights and Credits

Unity 2018 Shaders and Effects Cookbook Third Edition

PacktPub.com

Why subscribe?

PacktPub.com

Contributors

About the authors

About the reviewers

Packt is searching for authors like you

Preface

Who this book is for

What this book covers

To get the most out of this book

Download the example code files

Download the color images

Conventions used

Sections

Getting ready

How to do it…

How it works…

There's more…

See also

Get in touch

Reviews

Post Processing Stack

Introduction

Installing the Post Processing Stack

Getting ready

How to do it...

Getting a filmic look using grain, vignetting, and depth of field

Getting ready

How to do it...

How it works...

Mimicking real life with bloom and anti-aliasing

Getting ready

How to do it...

How it works...

Setting mood with color grading

Getting ready

How to do it...

Creating a horror game look with fog

Getting ready

How to do it...

How it works...

Creating Your First Shader

Introduction

Creating a basic Standard Shader

Getting ready

How to do it...

How it works...

There's more...

Adding properties to a shader

Getting ready

How to do it...

How it works...

See also

Using properties in a Surface Shader

How to do it...

How it works...

There's more...

See also

Surface Shaders and Texture Mapping

Introduction

Diffuse shading

Getting ready

How to do it...

How it works...

Accessing and modifying packed arrays

How to do it...

There's more...

See also

Adding a texture to a shader

Getting ready

How to do it...

How it works...

There's more...

See also

Scrolling textures by modifying UV values

Getting ready

How to do it...

How it works...

Creating a shader with normal mapping

Getting ready

How to do it...

How it works...

There's more...

Creating a transparent material

Getting ready

How to do it...

How it works...

Creating a Holographic Shader

Getting ready

How to do it...

How it works...

There's more...

See also

Packing and blending textures

Getting ready

How to do it...

How it works...

Creating a circle around your terrain

Getting ready

How to do it...

Moving the circle

How it works...

Understanding Lighting Models

Introduction

Creating a custom diffuse lighting model

Getting ready

How to do it...

How it works...

Creating a toon shader

Getting ready

How to do it...

How it works...

There's more...

Creating a Phong Specular type

Getting ready

How to do it...

How it works...

Creating a BlinnPhong Specular type

Getting ready

How to do it...

How it works...

See also

Creating an Anisotropic Specular type

Getting ready

How to do it...

How it works...

Physically-Based Rendering

Introduction

Understanding the metallic setup

Getting ready

How to do it...

How it works...

See also

Adding transparency to PBR

Getting ready

How to do it...

Semi-transparent materials

Fading objects

Solid geometries with holes

See also

Creating mirrors and reflective surfaces

Getting ready

How to do it...

How it works...

See also

Baking lights in your scene

Getting ready

How to do it...

Configuring the static geometry

Configuring the light probes

Baking the lights

How it works...

See also

Vertex Functions

Introduction

Accessing a vertex color in a Surface Shader

Getting ready

How to do it…

How it works…

There's more…

Animating vertices in a Surface Shader

Getting ready

How to do it…

How it works…

Extruding your models

Getting ready

How to do it…

How it works…

There's more…

Adding extrusion maps

Implementing a snow shader

Getting ready

How to do it…

How it works…

Coloring the surface

Altering the geometry

See also

Implementing a volumetric explosion

Getting ready

How to do it…

How it works…

There's more…

See also

Fragment Shaders and Grab Passes

Introduction

Understanding Vertex and Fragment Shaders

Getting ready

How to do it…

How it works…

There's more…

Input semantics

Output semantics

See also

Using the grab pass to draw behind objects

Getting ready

How to do it…

How it works…

There's more…

Implementing a Glass Shader

Getting ready

How to do it…

How it works…

There's more…

Implementing a Water Shader for 2D games

Getting ready

How to do it…

How it works…

Mobile Shader Adjustment

Introduction

Techniques to make shaders more efficient

Getting ready

How to do it...

How it works...

Profiling your shaders

Getting ready

How to do it...

How it works...

There's more...

Modifying our shaders for mobile

Getting ready

How to do it...

How it works...

Screen Effects with Unity Render Textures

Introduction

Setting up the screen effects script system

Getting ready

How to do it...

How it works...

There's more...

Using brightness, saturation, and contrast with screen effects

Getting ready

How to do it...

How it works...

Using basic Photoshop-like Blend modes with screen effects

Getting ready

How to do it...

How it works...

There's more...

Using the Overlay Blend mode with screen effects

How to do it...

How it works...

Gameplay and Screen Effects

Introduction

Creating an old movie screen effect

Getting ready

How to do it...

How it works...

See also

Creating a night vision screen effect

Getting ready

How to do it...

How it works...

There's more...

Advanced Shading Techniques

Introduction

Using Unity's built-in CgInclude files

Getting ready

How to do it...

How it works...

There's more...

Making your shader world in a modular way with CgInclude

Getting ready

How to do it...

How it works...

Implementing a Fur Shader

Getting ready

How to do it...

How it works...

There's more...

Implementing Heatmaps with arrays

Getting ready

How to do it...

How it works...

Shader Graph

Introduction

Creating a Shader Graph project

How to do it...

How it works...

Implementing a simple a Shader Graph

Getting ready

How to do it...

How it works...

Exposing properties to the Inspector via Shader Graph

Getting ready

How to do it...

How it works...

Implementing a glowing highlight system

Getting ready

How to do it...

How it works...

Other Books You May Enjoy

Leave a review - let other readers know what you think

Preface

Unity 2018 Shaders and Effects Cookbook is your guide to becoming familiar with the creation of shaders and post-processing effects in Unity 2018. You will start your journey at the beginning, exploring the Post Processing Stack to see some of the possible ways you can use shaders to affect what you see without having to write scripts at all. Afterwards, we learn how to create shaders from scratch, starting by creating the most basic shaders and learning how the shader code is structured. This foundational knowledge will arm you with the means to progress further through each chapter, learning advanced techniques such as volumetric explosions and fur shading. We also explore the newly added Shader Graph editor to see how you can create shaders through a drag and drop interface as well! This edition of the book is written specifically for Unity 2018 and will help you to master physically-based rendering and global illumination to get as close to photorealism as possible. 

By the end of each chapter, you will have gained new skills that will increase the quality of your shaders and even make your shader writing process more efficient. These chapters have been tailored so that you can jump into each section and learn a specific skill from beginner to expert. For those who are new to shader writing in Unity, you can progress through each chapter, one at a time, to build on your knowledge. Either way, you will learn the techniques that make modern games look the way they do.

Once you have completed this book, you will have a set of shaders that you can use in your Unity 3D games as well as the understanding to add to them, accomplish new effects, and address performance needs. So, let's get started!

Who this book is for

Unity Effects and Shader Cookbook is written for developers who want to create their first shaders in Unity 2018 or wish to take their game to a whole new level by adding professional post-processing effects. A solid understanding of Unity is required.

What this book covers

Chapter 1, Post Processing Stack, introduces readers to the Post Processing Stack, which will allow users to tweak their game's appearance without having to write any additional scripts.

Chapter 2, Creating Your First Shader, introduces you to the world of shader coding in Unity. You will build some basic shaders and learn how to introduce tweakable properties in your shaders to make them more interactive.

Chapter 3, Surface Shaders and Texture Mapping, covers the most common and useful techniques that you can implement with Surface Shaders, including how to use textures and normal maps for your models.

Chapter 4, Understanding Lighting Models, gives you an in-depth explanation of how shaders model the behavior of light. This chapter teaches you how to create custom lighting models used to simulate special effects, such as toon shading.

Chapter 5, Physically-Based Rendering in Unity 5, shows you that physically-based rendering is the standard technology used by Unity 5 to bring realism to your games. This chapter explains how to make the most out of it by mastering transparencies, reflective surfaces, and global illumination.

Chapter 6, Vertex Functions, teaches you how shaders can be used to alter the geometry of an object. This chapter introduces vertex modifiers and uses them to bring volumetric explosions, snow shaders, and other effects to life.

Chapter 7, Fragment Shaders and Grab Passes, explains how to use grab passes to make materials that emulate the deformations generated by semi-transparent materials.

Chapter 8, Mobile Shader Adjustment, helps you optimize your shaders to get the most out of any device.

Chapter 9, Screen Effects with Unity Render Textures, shows you how to create special effects and visuals that would otherwise be impossible to achieve.

Chapter 10, Gameplay and Screen Effects, tells you how post-processing effects can be used to complement your gameplay, simulating, for instance, a night-vision effect.

Chapter 11, Advanced Shading Techniques, introduces the most advanced techniques in this book, such as fur shading and heatmap rendering.

Chapter 12, Shader Graph, explains how to set up a project to use Unity's newly added Shader Graph editor. We cover how to create a simple shader graph, how to expose properties, and how to interact with the shader graph through code using a glow highlight system.

To get the most out of this book

Readers are expected to have experience of working with Unity and some scripting experience (C# or JavaScript is fine). The book is written with Unity 2018.1.0f2, but should work with future versions of the engine with some minor tweaks.

Download the example code files

You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

Log in or register at

www.packtpub.com

.

Select the

SUPPORT

tab.

Click on

Code Downloads & Errata

.

Enter the name of the book in the

Search

box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR/7-Zip for Windows

Zipeg/iZip/UnRarX for Mac

7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Unity-2018-Shaders-and-Effects-Cookbook-Third-Edition. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://www.packtpub.com/sites/default/files/downloads/Unity2018ShadersandEffectsCookbookThirdEdition_ColorImages.pdf.

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There's more..., and See also).

To give clear instructions on how to complete a recipe, use these sections as follows:

Getting ready

This section tells you what to expect in the recipe and describes how to set up any software or any preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of additional information about the recipe in order to make you more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packtpub.com.

Post Processing Stack

In this chapter, you will learn about the following recipes:

Installing the Post Processing Stack

Getting a filmic look using grain, vignetting, and anti-aliasing

Mimicking real life with bloom and anti-aliasing

Setting the mood with color grading

Creating a horror game look with fog

Introduction

It's great to write your own shaders and effects so that you can fine-tune your project to look just the way that you want, and this is what we will be spending the majority of the book looking into. However, it's also good to point out that Unity already comes with some prebuilt ways to get some of the more common effects that users like to have through the use of the Post Processing Stack.

For those who just want to get something up and running, the Post Processing Stack can be an excellent way for you to tweak the appearance of your game without having to write any additional code. Using the Post Processing Stack can also be useful in showing you just what shaders can do and how they can improve your game projects as, behind the scenes, the Post Processing Stack is itself a shader that is applied to the screen, aptly called a screen shader.

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:

Open up the 

Package

Manager

 by going to

Window

|

Package Manager

(or by pressing

Ctrl + 9

):

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:

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:

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.

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

:

Once added, from the

Inspector

tab, scroll down to the

Post Process Layer

 

(Script)

component and, under

Layer

, change the dropdown to

PostProcessing

.

  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. 

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.

Getting a filmic look using grain, vignetting, and depth of field

Now that we have the Post Processing Stack installed, we can create our first Post-processing Volume. The new Post Processing Stack relies on using volumes that describe how things should be drawn, either globally or within a certain area.

One of the most common appearances people like projects to have is that of a film. This is used quite frequently in titles such as the Uncharted series and Grand Theft Auto V. It's also used quite effectively in the Left 4 Dead series, as its creator are trying to emulate the B-movie zombie films that the games are based on.

Getting ready

Make sure you have completed the Installing the Post Processing Stack recipe before starting this one.

How to do it...

We first create a new

Post Processing Profile

by right-clicking within the 

Assets 

Chapter 1

 folder in the

Project

window and then selecting

Create

|

Post Processing Profile

. Once selected, it will allow us to rename the item. Go ahead and set the name to

FilmicProfile

:

If you happen to not enter the name correctly, you can rename an item from the Project tab by clicking on the name and then clicking again.

Once created, you should notice that, when selected, the

Inspector

window will now contain a button that says

Add effect...

(as you can see in the preceding image), which will allow us to augment what is normally drawn on the screen.

From the

Hierarchy

tab, select the 

Post-process Volume 

object again and from the

Inspector

tab go to the

Post Process Volume

component and assign the 

Profile

 property to the 

FilmicProflie

 we just created:

Notice that, once the Profile has been set, the Add effect... button shows up here as well. We can use this at either place and the changes will be saved in the file.

To get started, click on the

Add effect...

button and select the

Unity

|

Grain

 option. By default, you'll only see the

Grain

option with a check, so click on the arrow to expand its contents:

By default, you'll see that everything is greyed out. In order to have it affect anything, you have to click on the checkbox on the left-hand side. You can quickly turn them all on or off by pressing the All or None buttons on the screen.

In our case, check the

Intensity

option and set it to

0.2

. Then, check the

Size

property and set it to

0.3

. Afterward, switch to the

Game

tab to see a representation of what our tweaks have done:

You will notice that the screen has become much fuzzier than before. Decrease the

Intensity

to

0.2

, the

Size

to

0.3

, and uncheck the

Colored

option.

Unlike how users typically work in Unity, due to Post Processing Profiles being filed, you can modify them while playing your game and, upon stopping the game, the values are still saved. This can be useful for tweaking values to achieve the exact look that you're after.

The next property we want to tweak is the

Vignette

property. Note the blackened edges around the screen. Click on

Add effect...

and select

Unity

|

Vignette

. Open up the properties and change the

Intensity

to

0.5

 and the

Smoothness

to

0.35

:

Next, select

Add effect...

again and, this time, select

Unity

|

Depth of Field

. Check the

Depth of Field

option. It may be difficult to see a change right off the bat, but change the

Focus Distance

to

6

and 

Focal Length

to

80

and you should notice the grass in front of the background and the mountain in the distance are now blurred:

Now, if we go into the game itself, you should see our filmic look in action:

The final result of the filmic look

And with that, we now have a scene that looks much more like a film than what we had to begin with!

How it works...

Each time we add an effect to a Post-processing Volume, we are overriding what would normally be put onto the screen.

If you've been to a movie theater that still uses film, you may have noticed how there were little specks in the filmstock while the film was playing. The grain effect simulates this film grain, causing the effect to become more pronounced the more the movie is played. This is often used in horror games to obscure the player's vision.

For more information on the grain effect, check out: https://github.com/Unity-Technologies/PostProcessing/wiki/Grain.

In the film world, vignetting can be an unintended effect of using the wrong type of lens for the type of shot you are trying to achieve or the aspect ratio that you are shooting for. In game development, we typically use vignetting for dramatic effect or to have players focus on the center of the screen by darkening and/or desaturating the edges of the screen compared to the center.

For more information on the vignette effect, check out: https://github.com/Unity-Technologies/PostProcessing/wiki/Vignette.

The depth of field setting basically determines what is blurry and what isn't. The idea is to have items of importance in focus while items in the background are not.

For more information on the depth of field effect, check out: https://github.com/Unity-Technologies/PostProcessing/wiki/Depth-of-Field.

Mimicking real life with bloom and anti-aliasing

The bloom optical effect aims to mimic the imaging effects of real-world cameras, where things in areas with lights will glow along the edges, thus overwhelming the camera. The bloom effect is very distinctive and you've likely seen it employed in areas in a game that are magical or heaven-like.

Getting ready

Make sure you have completed the Installing the Post Processing Stack recipe before starting this one.

How to do it...

We first create a new

Post Processing Profile

by right-clicking within the 

Assets

folder in the

 

Project

 

window and then selecting

 

Create

|

Post Processing Profile

. Once selected, it will allow us to rename the item. Go ahead and set the name to

 

BloomProfile

.

Select the

 

Post-process volume

 

object and, f

rom the 

Inspector

 window, go to the 

Post Processing Volume

 component and 

assign the

 

Profile

 

property

t

o the

 

BloomProflie

 

we just created.

Afterward, select the

Game

tab, if it hasn't been selected already to see the results of the changes we are about to make in the following steps.

Select the

Add effect...

button and select

Unity

 | 

Bloom

. Once the effect has been added to the

Overrides

section of the

Post Processing Volume

component, select the arrow to open its properties. Check the

Intensity

property and set it to

3

. Afterward, check and set the

Threshold

to

0.75

, the

Soft Knee

to

0.1

, and the

Radius

to

3

:

Next, select the object with the

Post Process Layer

component attached to it (in the example, it is the

FPSController

|

FirstPersonCharacter

object) and, from the

Inspector

tab, scroll down to the

Post Process Layer

script. From there, change the

Anti-aliasing

property dropdown to

Fast Approximate Anti-aliasing

:

Afterward, save your scene and hit the Play button to check out your project:

The final result of using bloom and anti-aliasing

How it works...

As mentioned before, bloom will make bright things even brighter while adding a glow to lighter areas. In this recipe, you may notice that the path is much lighter than it was previously. This can be used in order to ensure that players will follow the path to get to the next section of gameplay.

For more information on bloom, check out: https://github.com/Unity-Technologies/PostProcessing/wiki/Bloom.

Anti-aliasing attempts to reduce the appearance of aliasing, which is the effect of lines appearing jagged on the screen. This is typically due to the fact that the display the player is using to play the game doesn't have a high enough resolution to be displayed properly. Anti-aliasing will combine colors with nearby lines in order to remove their prominence, at the cost of the game appearing blurry.

For more information on anti-aliasing and what each mode means, check out: https://github.com/Unity-Technologies/PostProcessing/wiki/Anti-aliasing.

Setting mood with color grading

One of the best ways to easily change the mood of a scene is by changing the colors a scene uses. One of the best examples of this is in the Matrix series of films, where the real world is always blue-tinted, while the computer-generated world of the Matrix is always tinted green. We can emulate this in our games through the use of color grading.

Getting ready

Make sure you have completed the Installing the Post Processing Stack recipe before starting this one.

How to do it...

We first create a new

Post Processing Profile

by right-clicking within the 

Assets

folder in the

 

Project

 

window and then selecting

 

Create

 

|

 

Post Processing Profile

. Once selected, it will allow us to rename the item. Go ahead and set it to

 

ColorProfile

.

Select the

 

Post-process volume

 

object and, f

rom the 

Inspector

 window, go to the 

Post Processing Volume

 component and 

assign the

 

Profile

 

property to the

 

ColorProflie

 

we just created.

Afterward, select the

 

Game

 

tab, if it hasn't been selected already, to see the results of the changes to be made.

Select the

 

Add effect...

 

button and select

 

Unity

Color Grading

 

Check the

Mode

property and set it to

Low Definition Range

(LDR). From there, you'll see a number of properties that can be used to adjust the colors on the screen in a similar manner to how Photoshop's hue/saturation menu works. Check the

Temperature

property and set it to

30

. Afterward, set the

Hue Shift

property to

-20

and the

Saturation

to

15

:

After making the changes, dive into the game to see what it looks like when playing:

The final result of using color grading

Notice how the previously very green environment is now much warmer and more yellow than before.

For more information on the color grading effect, check out: https://github.com/Unity-Technologies/PostProcessing/wiki/Color-Grading.

Creating a horror game look with fog

One of the types of games that best utilize the features of the Post Processing Stack is the horror genre. Using things such as depth of field to hide scary objects, as well as static to make the screen more menacing, can really help set your game firmly in the right place and provide the mood you are going for.

Getting ready

Make sure you have completed the Installing the Post Processing Stack recipe before starting this one.

How to do it...

We first create a new Post Processing Profile by right-clicking within the 

Assets

folder in the

 

Project

 

window and then selecting

 

Create

 

|

 

Post Processing Profile

. Once selected, it will allow us to rename the item. Go ahead and set the name to

 

HorrorProfile

.

Select the

 

Post-process volume

 

object and, f

rom the 

Inspector

 window, go to the 

Post Processing Volume

 component and 

assign the

 

Profile

 

property to the

 

HorrorProflie