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
- 392 pages
- English
- ePUB (mobile friendly)
- Available on iOS & Android
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
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
Information
Surface Shaders and Texture Mapping
- 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
Diffuse shading
Getting ready
How to do it...
- In the Properties section, remove all of the variables except for _Color:
Properties
{
_Color ("Color", Color) = (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.
- Also, remove the UNITY_INSTANCING_BUFFER_START/END macros and the comments used with them.
- Remove the content of the surf() function and replace it with the following:
void surf (Input IN, inout SurfaceOutputStandard o)
{
o.Albedo = _Color.rgb;
}
- 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"
}
- As this shader has been refitted with a Standard Shader, it will use physically-based rendering to simulate how light behaves on your models.
- 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.
- 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:
- You can double-click on an object in the Hierarchy tab in order to center the camera on the object that's been selected.