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)

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...

  1. 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.
  2. Select the Post-process volume object and, from the Inspector window, go to the Post Processing Volume component and assign the Profile property to the HorrorProflie we just created.
  3. Unlike previous settings, though, the fog settings are located in the Lighting window, which can be accessed by going into Window | Lighting.
  4. From there, scroll all the way to the bottom until you reach the Other Settings option. Once there, check Fog and set the color to a value that is close to the skybox. I used the following settings:
If you know the hex values of the color from your graphic editing software you can just type it in the Hex Color property of the Color window.
  1. Next, change the Mode to Exponential and the Density to 0.03:

As you can see, it's already much more spooky that it was previously, but there are still more options that we can change.

  1. Open up the HorrorProfile again and go to the Inspector tab. Press the Add effect... button and select Unity | Ambient Occlusion. Check the Mode option and select Scalable Ambient Obscurance. Afterward, change the Intensity to 2 and the Radius to 20:
  1. Lastly, lighting often has a big effect on the theme of a scene as well. If you're using the example map, select the Directional Light object in the Hierarchy tab and, from the Inspector tab under the Light component, change the Intensity to 0.5 and then adjust the Color to something darker. (I used the same color as in step 4 with a HEX of 5F7684FF.)
  1. Save your game and then start it to see the effect of all of the changes:
The final result of our horror look

How it works...