Unity 2018 Shaders and Effects Cookbook
eBook - ePub

Unity 2018 Shaders and Effects Cookbook

Transform your game into a visually stunning masterpiece with over 70 recipes, 3rd Edition

John P. Doran, Alan Zucconi

Share book
  1. 392 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Unity 2018 Shaders and Effects Cookbook

Transform your game into a visually stunning masterpiece with over 70 recipes, 3rd Edition

John P. Doran, Alan Zucconi

Book details
Book preview
Table of contents
Citations

About This Book

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

Key Features

  • Learn the secrets of creating AAA quality shaders without writing long algorithms
  • Master shader programming through easy-to-follow examples
  • Create stunning visual effects that can be used in 3D games

Book 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 learn

  • Understand physically based rendering to fit the aesthetic of your game
  • Write shaders from scratch in ShaderLab and HLSL/Cg
  • Combine shader programming with interactive scripts to add life to your materials
  • Design efficient shaders for mobile platforms without sacrificing their realism
  • Use state-of-the-art techniques, such as volumetric explosions and fur shading
  • Master the math and algorithms behind the most used lighting models
  • Understand how shader models have evolved and how you can create your own

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

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Unity 2018 Shaders and Effects Cookbook an online PDF/ePUB?
Yes, you can access Unity 2018 Shaders and Effects Cookbook by John P. Doran, Alan Zucconi in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming Games. We have over one million books available in our catalogue for you to explore.

Information

Year
2018
ISBN
9781788390958
Edition
3

Surface Shaders and Texture Mapping

In this chapter, we will explore the topic of Surface Shaders in greater detail than in the previous chapter. We will start from building a very simple matte material and end with holographic projections and the advanced blending of terrains. We will also see how you can use textures to animate, blend, and drive any other property that they like.
In this chapter, you will learn about the following methods:
  • Diffuse shading
  • Accessing and modifying packed arrays
  • Adding a texture to a shader
  • Scrolling textures by modifying UV values
  • Creating a shader with normal mapping
  • Creating a transparent material
  • Creating a Holographic Shader
  • Packing and blending textures
  • Creating a circle around your terrain

Introduction

Surface Shaders were introduced in Chapter 2, Creating Your First Shader, as the main type of shader that's used in Unity. This chapter will show you in detail what these actually are and how they work. Generally speaking, there are two essential steps in every Surface Shader. First, you have to specify certain physical properties of the material that you want to describe, such as its diffuse color, smoothness, and transparency. These properties are initialized in a function called the surface function and are stored in a structure called the SurfaceOutput. Secondly, the SurfaceOutput is passed to a lighting model. This is a special function that will also take information about the nearby lights in the scene. Both of these parameters are then used to calculate the final color for each pixel of your model. The lighting function is where the real calculations of a shader take place as it's the piece of code that determines how light should behave when it touches a material.
The following diagram loosely summarizes how a Surface Shader works. Custom lighting models will be explored in Chapter 4, Understanding Lighting Models, while Chapter 6, Vertex Functions, will focus on vertex modifiers:

Diffuse shading

Before starting our journey into texture mapping, it is important to understand how diffuse materials work. Certain objects might have a uniform color and smooth surface, but are not smooth enough to shine in reflected light. These matte materials are best represented with a Diffuse Shader. While, in the real world pure diffuse materials do not exist, Diffuse Shaders are relatively cheap to implement and are largely applied in games with low-poly aesthetics, so they're worth learning about.
There are several ways in which you can create your own Diffuse Shader. A quick way is to start with Unity's Standard Surface Shader and edit it to remove any additional texture information.

Getting ready

Before starting this recipe, you should have created a Standard Surface Shader with the name SimpleDiffuse. For instructions on creating a Standard Surface Shader, look at the Creating a basic Standard Surface Shader recipe located in Chapter 2, Creating Your First Shader, if you haven't done so already.

How to do it...

Open up the SimpleDiffuse shader you've created and make the following changes:
  1. In the Properties section, remove all of the variables except for _Color:
 Properties 
{
_Color ("Color", Color) = (1,1,1,1)
}
  1. From the SubShader{} section, remove the _MainTex, _Glossiness, and _Metallic variables. You should not remove the reference to uv_MainTex as Cg does not allow the Input struct to be empty. The value will simply be ignored.
  2. Also, remove the UNITY_INSTANCING_BUFFER_START/END macros and the comments used with them.
  1. Remove the content of the surf() function and replace it with the following:
void surf (Input IN, inout SurfaceOutputStandard o) 
{
o.Albedo = _Color.rgb;
}
  1. Your shader should look as follows:
Shader "CookbookShaders/Chapter03/SimpleDiffuse" {
Properties
{
_Color ("Color", Color) = (1,1,1,1)
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200

CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows

// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0


struct Input
{
float2 uv_MainTex;
};

fixed4 _Color;

void surf (Input IN, inout SurfaceOutputStandard o)
{
o.Albedo = _Color.rgb;
}

ENDCG
}
FallBack "Diffuse"
}
The two lines below CGPROGRAM is actually one line and is cut off due to the size of the book.
  1. As this shader has been refitted with a Standard Shader, it will use physically-based rendering to simulate how light behaves on your models.
If you are trying to achieve a non-photorealistic look, you can change the first #pragma directive so that it uses Lambert rather than Standard. If you do so, you should also replace the SurfaceOutputStandard parameter of the surf function with SurfaceOutput. For more information on this and the other lighting models that Unity supports, Jordan Stevens put together a very nice article about it, which you can see here: http://www.jordanstevenstechart.com/lighting-models
  1. Save the shader and dive back into Unity. Using the same instructions as in the Creating a basic Standard Surface Shader recipe located in Chapter 2, Creating Your First Shader, create a new material called SimpleDiffuseMat and apply our newly created shader to it. Change the color to something different, such as red, by clicking on the window next to the Color property in the Inspector window while selected.
  2. Then, go into the Models folder of this book's example code and bring the bunny object into our scene by dragging and dropping it from the Project window into the Hierarchy window. From there, assign the SimpleDiffuseMat material to the object:
  1. You can double-click on an object in the Hierarchy tab in order to center the camera on the object that's been selected.

How it works...

The way shaders allow you to communicate the rendering properties of your material to their lighting model is via their SurfaceOutput. It is basically a wrapper around all the parameters that the cur...

Table of contents