Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Unity 5.x Shaders and Effects Cookbook
Unity 5.x Shaders and Effects Cookbook
Unity 5.x Shaders and Effects Cookbook
Ebook503 pages3 hours

Unity 5.x Shaders and Effects Cookbook

Rating: 0 out of 5 stars

()

Read preview

About this ebook

Master the art of Shader programming to bring life to your Unity projects

About This Book

- This book will help you master the technique of physically based shading in Unity 5 to add realism to your game quickly through precise recipes
- From an eminent author, this book offers you the fine technicalities of professional post-processing effects for stunning results
- This book will help you master Shader programming through easy-to-follow examples to create stunning visual effects that can be used in 3D games and high quality graphics.

Who This Book Is For

Unity Effects and Shader Cookbook is written for developers who want to create their first Shaders in Unity 5 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 You Will Learn

- Understand physically based rendering to fit the aesthetic of your game
- Enter the world of post-processing effects to make your game look visually stunning
- Add life to your materials, complementing Shader programming with interactive scripts
- Design efficient Shaders for mobile platforms without sacrificing their realism
- Use state-of-the-art techniques such as volumetric explosions and fur shading
- Build your knowledge by understanding how Shader models have evolved and how you can create your own
- Discover what goes into the structure of Shaders and why lighting works the way it does
- Master the math and algorithms behind the most used lighting models

In Detail

Since their introduction to Unity, Shaders have been notoriously difficult to understand and implement in games: complex mathematics have always stood in the way of creating your own Shaders and attaining that level of realism you crave. With Shaders, you can transform your game into a highly polished, refined product with Unity’s post-processing effects.
Unity Shaders and Effects Cookbook is the first of its kind to bring you the secrets of creating Shaders for Unity3D—guiding you through the process of understanding vectors, how lighting is constructed with them, and also how textures are used to create complex effects without the heavy math.
We’ll start with essential lighting and finishing up by creating stunning screen Effects just like those in high quality 3D and mobile games. You’ll discover techniques including normal mapping, image-based lighting, and how to animate your models inside a Shader. We’ll explore the secrets behind some of the most powerful techniques, such as physically based rendering! With Unity Shaders and Effects Cookbook, what seems like a dark art today will be second nature by tomorrow.

Style and approach

The recipes in this book contain step-by-step instructions, complemented by screenshots and code, and real-world examples.
LanguageEnglish
Release dateFeb 26, 2016
ISBN9781785289187
Unity 5.x Shaders and Effects Cookbook

Related to Unity 5.x Shaders and Effects Cookbook

Related ebooks

Software Development & Engineering For You

View More

Related articles

Reviews for Unity 5.x Shaders and Effects Cookbook

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Unity 5.x Shaders and Effects Cookbook - Zucconi Alan

    Table of Contents

    Unity 5.x Shaders and Effects Cookbook

    Credits

    About the Authors

    www.PacktPub.com

    eBooks, discount offers, and more

    Why subscribe?

    Preface

    What this book covers

    What you need for this book

    Who this book is for

    Sections

    Getting ready

    How to do it…

    How it works…

    There's more…

    See also

    Conventions

    Reader feedback

    Customer support

    Downloading the example code

    Downloading the color images of this book

    Errata

    Piracy

    Questions

    1. Creating Your First Shader

    Introduction

    Creating a basic Standard Shader

    Getting ready

    How to do it…

    How it works…

    See also

    Migrating Legacy Shaders from Unity 4 to Unity 5

    Getting ready

    How to do it...

    Upgrading automatically

    Using Standard Shaders

    Migrating custom shaders

    How it works...

    See also

    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

    2. Surface Shaders and Texture Mapping

    Introduction

    Diffuse shading

    Getting ready

    How to do it...

    How it works...

    Using packed arrays

    How to do it...

    Packed matrices

    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…

    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…

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

    4. Physically Based Rendering in Unity 5

    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

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

    6. 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 grab pass

    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…

    7. Mobile Shader Adjustment

    Introduction

    What is a cheap shader?

    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…

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

    Getting ready

    How to do it…

    How it works…

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

    10. Advanced Shading Techniques

    Introduction

    Using CgInclude files that are built into Unity

    Getting ready

    How to do it…

    How it works…

    Making your shader world modular 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…

    Index

    Unity 5.x Shaders and Effects Cookbook


    Unity 5.x Shaders and Effects Cookbook

    Copyright © 2016 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 authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be 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.

    First Published: February 2016

    Production reference: 1220216

    Published by Packt Publishing Ltd.

    Livery Place

    35 Livery Street

    Birmingham B3 2PB, UK.

    ISBN 978-1-78528-524-0

    www.packtpub.com

    Credits

    Authors

    Alan Zucconi

    Kenneth Lammers

    Reviewer

    Kenneth Lammers

    Commissioning Editor

    Priya Singh

    Acquisition Editors

    Rahul Nair

    Erol Staveley

    Content Development Editor

    Mehvash Fatima

    Technical Editors

    Pranil Pathare

    Danish Shaikh

    Copy Editor

    Tasneem Fatehi

    Project Coordinator

    Kinjal Bari

    Proofreader

    Safis Editing

    Indexer

    Monica Ajmera Mehta

    Graphics

    Kirk D'Penha

    Disha Haria

    Production Coordinator

    Nilesh Mohite

    Cover Work

    Nilesh Mohite

    About the Authors

    Alan Zucconi is a passionate developer, author, and motivational speaker, recognized as one of Develop's 30 under 30. His expertise has been built over the past 10 years, while he dedicated his time to academia and the gaming industry. He started his independent career to fully explore his creativity, tearing down the wall between art and gaming. Prior to that, he worked at Imperial College London, where he discovered his passion for teaching and writing. His titles include the gravity puzzle, 0RBITALIS, and the upcoming time travel platformer, Still Time.

    Kenneth Lammers has over 15 years of experience in the gaming industry, working as a character artist, technical artist, technical art director, and programmer. Throughout his career, he has worked on titles such as Call of Duty 3, Crackdown 2, Alan Wake, and Kinect Star Wars. He currently owns and operates Ozone Interactive along with his business partner, Noah Kaarbo. Together, they have worked with clients such as Amazon, Eline Media, IGT, and Microsoft.

    Kenny has worked for Microsoft Games Studios, Activision, and Surreal, and has recently gone out on his own, operating CreativeTD and Ozone Interactive.

    Kenny authored the first version of Unity Shaders and Effects Cookbook by Packt Publishing, and was very happy to be a part of the writing, updating and reviewing of this book.

    www.PacktPub.com

    eBooks, discount offers, and more

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

    https://www2.packtpub.com/books/subscription/packtlib

    Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.

    Why subscribe?

    Fully searchable across every book published by Packt

    Copy and paste, print, and bookmark content

    On demand and accessible via a web browser

    Preface

    Unity 5.x Shaders and Effects Cookbook is your guide to becoming familiar with the creation of shaders and post effects in Unity 5. You will start your journey at the beginning, 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. This edition of the book is written specifically for Unity 5 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 skill sets 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!

    What this book covers

    Chapter 1, Creating Your First Shader, introduces you to the world of shader coding in Unity 4 and 5.

    Chapter 2, 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 3, Understanding Lighting Models, gives you an in-depth explanation of how shaders model the behavior of light. The chapter teaches you how to create custom lighting models used to simulate special effects such as toon shading.

    Chapter 4, 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, mastering transparencies, reflective surfaces, and global illumination.

    Chapter 5, 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 6, Fragment Shaders and Grab Passes, explains how to use grab passes to make materials that emulate the deformations generated by these semi-transparent materials.

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

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

    Chapter 9, 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 10, Advanced Shading Techniques, introduces the most advanced techniques in this book, such as fur shading and heatmap rendering.

    What you need for this book

    The following is a list of the required and optional software to complete the recipes in this book:

    Unity 5

    A 3D application such as Maya, Max, or Blender (optional)

    A 2D image editing application such as Photoshop or Gimp (optional)

    Who this book is for

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

    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, we 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 the reader more knowledgeable about the recipe.

    See also

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

    Conventions

    In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

    Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: Enter the following code into the Properties block of your shader.

    A block of code is set as follows:

    void surf (Input IN, inout SurfaceOutput o)

    {

      float4 c;

      c = pow((_EmissiveColor + _AmbientColor), _MySliderValue);

      o.Albedo = c.rgb;

      o.Alpha = c.a;

    }

    When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

    void

    surf (Input IN, inout SurfaceOutputStandard o) {

      fixed4 c = pow((_Color + _AmbientColor), _MySliderValue);

      o.Albedo = c.rgb;

      o.Metallic = _Metallic;

      o.Smoothness = _Glossiness;

      o.Alpha = c.a;

    }

    New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: In the Project tab in your Unity editor, right-click on the Assets folder and select Create | Folder.

    Note

    Warnings or important notes appear in a box like this.

    Tip

    Tips and tricks appear like this.

    Reader feedback

    Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

    To send us general feedback, simply e-mail <feedback@packtpub.com>, and mention the book's title in the subject of your message.

    If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

    Customer support

    Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

    Downloading the example code

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

    You can download the code files by following these steps:

    Log in or register to our website using your e-mail address and password.

    Hover the mouse pointer on the SUPPORT tab at the top.

    Click on Code Downloads & Errata.

    Enter the name of the book in the Search box.

    Select the book for which you're looking to download the code files.

    Choose from the drop-down menu where you purchased this book from.

    Click on Code Download.

    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

    Downloading the color images of this book

    We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/Unity5xShadersAndEffectsCookbook_SecondEdition_Graphics.pdf.

    Errata

    Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are

    Enjoying the preview?
    Page 1 of 1