Unity 5.x Shaders and Effects Cookbook - Alan Zucconi - E-Book

Unity 5.x Shaders and Effects Cookbook E-Book

Alan Zucconi

0,0
43,19 €

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

Mehr erfahren.
Beschreibung

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.

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

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 274

Veröffentlichungsjahr: 2016

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.



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

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 PacktCopy and paste, print, and bookmark contentOn 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 5A 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.

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 <[email protected]>, 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 WindowsZipeg / iZip / UnRarX for Mac7-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 verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at <[email protected]> with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at <[email protected]>, and we will do our best to address the problem.

Chapter 1. Creating Your First Shader

This chapter will cover some of the more common diffuse techniques found in today's Game Development Shading Pipelines. In this chapter, you will learn about the following recipes:

Creating a basic Standard ShaderMigrating Legacy Shaders from Unity 4 to Unity 5Adding properties to a shaderUsing properties in a Surface Shader

Introduction

Let's imagine a cube that has been painted white uniformly. Even if the color used is the same on each face, they will all have different shades of white depending on the direction that the light is coming from and the angle that we are looking at it. This extra level of realism is achieved in 3D graphics by shaders, special programs that are mostly used to simulate how light works. A wooden cube and a metal one may share the same 3D model, but what makes them look different is the shader that they use. Recipe after recipe, this first chapter will introduce you to shader coding in Unity. If you have little to no previous experience with shaders, this chapter is what you need to understand what shaders are, how they work, and how to customize them.

By the end of this chapter, you will have learned how to build basic shaders that perform basic operations. Armed with this knowledge, you will be able to create just about any Surface Shader.

Migrating Legacy Shaders from Unity 4 to Unity 5

It is undeniable that graphics in videogames have changed massively over the last 10 years. Every new game comes with cutting-edge techniques that are getting us closer to achieving real-time photorealism. We should not be surprised by the fact that shaders themselves have changed massively throughout the lifetime of Unity. This is one of the major sources of confusion when approaching shaders for the first time. Prior to Unity 5, mainly two different shaders were adopted: Diffuseand Specular. As the names suggest, they were used for matte and shiny materials, respectively. If you are already using Unity 5, you can skip this recipe. This recipe will explain how to replicate these effects using Unity 5.

Getting ready

The starting point of this recipe is having a workspace made in Unity 4, which uses some of the built-in shaders that were originally provided. If you are to start a new game, there is no doubt that you should use the latest version of Unity. However, if your project is already in the later stages of development with an older version, you should be very careful before migrating. Many things have changed behind the curtains of the engine, and even if your built-in shaders will most likely work without any problem, your scripts might not. If you are to migrate your entire workspace, the first thing that you should do is take backup. It is important to remember that saving assets and scenes is not enough as most of the configuration in Unity is stored in its metadata files. The safest option to survive a migration is to duplicate the entire folder that contains your project. The best way of doing this is by physically copying the folder from File Explorer (Windows) or Finder (Mac).

How to do it...

There are two main options if you want to migrate your built-in shaders: upgrading your project automatically or switching to Standard Shaders instead.

Upgrading automatically

This option is the easiest one. Unity 5 can import a project made with an earlier version and upgrade it. You should notice that once the conversion is done, you will not be able to use Unity 4; even if none of your assets may have changed directly, Unity metadata has been converted. To proceed with this, open Unity 5 and click on OPEN OTHER to select the folder of your old project. You will be asked if you want to convert it; click on Upgrade to proceed. Unity will reimport all of your assets and recompile all of your scripts. The process might last for several hours if your project is big. Once the conversion is done, your built-in shaders from Unity 4 should have been replaced with their legacy equivalent. You can check this from the inspector of your materials that should have changed (for instance) from Bumped Diffuse to Legacy Shader/Bumped Diffuse.

Note

Even if Diffuse, Specular, and the other built-in shaders from Unity 4 are now deprecated, Unity 5 keeps them for backward compatibility. They can be found in the drop-down menu of a material under the Legacy Shaders folder.

Using Standard Shaders

Instead of using the Legacy Shaders, you might decide to replace them with the new Standard Shaders from Unity 5. Before doing this, you should keep in mind that as they are based on a different lighting model, your materials will most likely look different. Unity 4 came with more than eighty different built-in shaders divided in six different families (Normal, Transparent, Transparent Cutout, Self-Illuminated, and Reflective). In Unity 5, they are all replaced by the Standard Shader introduced in the previous recipe. Unfortunately, there is no magic recipe to convert your shaders directly. However, you can use the following table as a starting point to understand how the Standard Shader can be configured to simulate Unity 4 Legacy Shaders:

Shader

Unity 4

Unity 4 (Legacy)

Unity 5

Diffuse

Diffuse

Lambert

Legacy Shader/Diffuse

Lambert

Standard

Physically-based rendering: Metallic Workflow

Specular

Specular

Blinn-Phong

Legacy Shader/Specular

Blinn-Phong

Standard (Specular setup)

Physically-based rendering: Specular Workflow

Transparent

Transparent Vertex-Lit

Legacy Shader/Transparent Vertex-Lit

Standard

Rendering Mode: Transparent

Transparent Cutout Vertex-Lit

Legacy Shader/Transparent Cutout Vertex-Lit

Standard

Rendering Mode: Cutout

You can change the shader used by your old material using the Shader drop-down menu in Inspector