Mastering Unity Shaders and Effects - Jamie Dean - E-Book

Mastering Unity Shaders and Effects E-Book

Jamie Dean

0,0
38,39 €

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

Mehr erfahren.
Beschreibung

Harness the power of Unity 5 tools to write shaders and create stunning effects for next generation games

About This Book

  • Leverage the power of Unity 5 compatible shaders to create a variety of realistic surfaces
  • Create amazing visual effects for your Unity 5.x games by blending advanced post-processing techniques and lighting effects
  • Add that extra visual edge to your games by learning pro level techniques

Who This Book Is For

This book is for users who are familiar with creating Unity projects and are looking for the next step to create stunning high-end visuals in their game project using the latest tools and features. You are expected to understand the creation of basic Unity projects, game object types, and scene navigation. You will need some basic programming experience in C# or JavaScript.

What You Will Learn

  • Build your own custom shaders to suit a variety of organic and inorganic surfaces
  • Create amazing visual effects using Unity 5's improved toolset
  • Compare the advantages of Metallic and Specular workflows in Unity's Physically Based Lighting System
  • Take your Unity 5 project to the next level with advanced real-time and baked lighting methods
  • Design powerful particle effects
  • Find out how shaders, materials, and scripts work together within the Unity framework
  • Develop high-end and optimized game visuals suitable for mobile devices and other platforms

In Detail

With the inclusion of Physically Based Shading and the Standard Shader, Unity has changed the game for independent and large scale developers alike. Never before has it been so easy to create startling high quality visuals in games and other interactive projects.

This book will give you a solid understanding of the interaction of shaders, lighting and effects in Unity 5, and give you some great ideas for how you can maximize the visual impact of your own games for PC, mobile and other platforms. In the sequence of chapters you will find solutions to some of the major challenges to the creation of responsive and realistic looking games.

As you work on example scenes, you will get to know the syntax of ShaderLab and CG languages, creating simple and complex shader solutions for a variety of different surface effects. You will see how Unity's responsive lighting solutions can be harnessed to build immersive and appealing game experiences.

Style and approach

This is the most up-to-date resource on creating amazing visual effects for your Unity 5.x games. With the perfect mix of advanced techniques and their illustrations in real-world examples, this is the most comprehensive advanced guide on Unity shaders and effects you'll find.

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

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 221

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

Mastering Unity Shaders and Effects
Credits
About the Author
About the Reviewer
www.PacktPub.com
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. Getting to Grips with Standard Shaders
Creating the project
Importing the project files
Loading and navigating the spacecraft maintenance scene
Creating the astronaut material
Creating a material for the astronaut's accessories
Making objects transparent
Creating the spacecraft material
Creating the spacecraft's decal material
Creating the planet material
Setting up the skybox
Adjusting the scene lighting and adding effects
Summary
2. Creating Custom Shaders
Opening the project
Opening the scene
Creating our first custom shader
Seeing the shader in action
Adding a texture to the moon shader
Making the moon shader compatible with the scene lighting
Creating better transparency for the astronaut's helmet
Creating a custom transparent shader
Editing the new glass shader
Creating the inner surface of the helmet
Separating front and back faces
Improving the planet's atmosphere
Creating the custom planet shader
Applying the planet shader
Editing the planet shader
Adding new properties to the planet shader
Adding the atmosphere shader pass
Setting the planet material inputs
Summary
3. Working with Lighting and Light-Emitting Surfaces
Looking at the scene light setup
Adding emissive properties to a material
Adding the Bloom effect
Adding a reflection probe
Creating a wireframe emissive material for the planet-surface scanner
Viewing the wireframe emissive shader in the scene
Adding the wireframe shader's second pass
Completing the planet scan projection effect
Summary
4. Animating Surfaces with Code and Shaders
Starting the scene
Creating a dynamic warning light effect
Animating the light
Animating the control panel illumination
Animating UV coordinates
Animating the planet projection display
Creating an animated hotspot on the planet
Creating the hotspot geometry
Creating the hotspot material
Writing the hotspot animation script
Setting up the hotspot variables and finalizing the effect
Summary
5. Exploring Transparent Surfaces and Effects
Starting the scene
Creating the dust cloud material
Adding fog to the scene
Animating the dust cloud transparency
Switching the cloud texture atlas positions
Creating a better transparent glass material
Setting up the whirlwind effect
Summary
6. Working with Specular and Metallic Surfaces
Starting the scene
Altering the crate's secondary material at runtime
Applying a secondary albedo texture
Creating a custom decal shader for the crate
Switching the decal texture at runtime
Locating and modifying shader light models
Modifying the shader lighting model
Adding specularity to our custom lighting model
Summary
7. Shaders for Organic Surfaces
Start the scene
Understanding the complexities of human skin
Creating the skin shader
Adding complexity to the skin shader
Writing the custom lighting model
Adding the thickness map input to the shader
Creating the eye material
Creating the custom eye shader
Creating the hair material
Creating the custom hair shader
Summary
8. Custom Particle Shaders – Smoke, Steam, and Fluids
Starting the scene
Adding a particle system
Adjust the particle system parameters
Setting up a new material for the particle system
Creating the particle shader
Adding a color to the particle shader
Adding the steam particle effect
Adjusting the steam particle's parameters
Completing the steam particle effect
Setting up the steam particle material
Creating the slime-drip effect
Creating the floor damage effect
Writing the control script
Modifying the drop prefab
Summary
9. Optimizing Shaders for Mobile
Starting the scene
Building the scene for a device
Building for Android
Building for iOS
Viewing the frame rate in the Profiler
Writing a simple mobile shader
Replacing shaders in scene materials
Writing an advanced mobile shader
Summary

Mastering Unity Shaders and Effects

Mastering Unity Shaders and Effects

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 author, 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: September 2016

Production reference: 1210916

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham

B3 2PB, UK.

ISBN 978-1-78355-367-9

www.packtpub.com

Credits

Author

Jamie Dean

Copy Editor

Vibha Shukla

Reviewer

Kenneth Lammers

Project Coordinator

Shweta H Birwatkar

Commissioning Editor

Akram Hussain

Proofreader

Safis Editing

Acquisition Editor

Aaron Lazar

Indexer

Mariammal Chettiyar

Content Development Editor

Deepti Thore

Production Coordinator

Arvindkumar Gupta

Technical Editor

Manthan Raja

About the Author

Jamie Dean is a game artist, instructor, and freelancer, with over seven years of teaching experience in higher education. He is currently focused on developing content for mobile games.

Jamie also wrote Unity Character Animation with Mecanim, Packt Publishing, in 2015.

I would like to thank my family, Carey, Silas, and Rowan, for their love and encouragement, and all the people at Packt Publishing who helped get this project off the ground and keep the momentum. Thanks to Keith, Chip, and rest of my colleagues at Concrete Software Inc. Thanks for the great opportunities!

About the Reviewer

Kenny Lammers has worked over 16 years in the gaming industry 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, E-line Media, IGT, Microsoft, and BioLucid.

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

Kenny authored Unity Shaders and Effects Cookbook, Packt Publishing, and was very happy to be a part of the writing and updating of Unity 5.x Shaders and Effects Cookbook, Packt Publishing.

www.PacktPub.com

For support files and downloads related to your book, please visit www.PacktPub.com.

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

Free access for Packt account holders

If you have an account with Packt at www.packtpub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.

Preface

Unity offers a range of tools to get your game project up and running. In version 5, the shaders got a significant upgrade, mostly being replaced with a set of Standard Shaders that can be easily set up to emulate the physical characteristics of real surfaces.

In this book, we will be exploring the options made possible with this new set of shaders and how they interact with lighting and effects in the game scene to produce high quality results.

It has been a long journey towards this level of realism in games. For a long time, the processing overhead of real-time lighting made it problematic. The burden of realism was placed squarely on the texture artist who had to bake static surface effects into the textures of characters and props.

Particularly with dynamic game objects such as characters, this made for some disappointing results—characters would move into darker or more brightly lit spaces with a disappointing level of light variation, exposing the simplicity and lack of realism in the game scene.

Advances with graphics, particularly the rapid improvement of graphics cards, have made real-time lighting techniques more cost effective.

This has resulted in higher expectations in games—more realistic surfaces that respond to dynamic lighting in a game scene.

In this book, readers will explore these shader and lighting concepts through the context of a typical sci-fi horror game. This will allow us to explore a variety of lighting and surface effects.

What this book covers

In this book, we will work through each of the major shader and effects concepts in different chapters. In each chapter, we will work through a different scene file:

Chapter 1, Getting to Grips with Standard Shaders, introduces the context and explores the capabilities and limitations of Unity's Standard Shaders as we set up the materials for our sci-fi horror-themed spacecraft bridge scene.

Chapter 2, Creating Custom Shaders, continues with spacecraft maintenance scene—creating custom shaders to improve the appearance of the game scene. We will create our first custom shader from scratch and then build on this code to create more complex effects for the helmet transparency and planet's atmosphere.

Chapter 3, Working with Lighting and Light-Emitting Surfaces, delves into the relationship between scene lighting and light-emitting surfaces, creating an animated holographic display for our spacecraft cockpit.

Chapter 4, Animating Surfaces with Code and Shaders, explores different techniques to implement animation in a shader workflow. We will demonstrate UV scrolling and iterating through texture arrays with C# to complete our animated holographic spacecraft display before showing more complex vertex animation in a custom shader.

Chapter 5, Exploring Transparent Surfaces and Effects, introduces different applications of transparency on our planet's surface scene and setting up various materials with existing and purpose-created shaders.

Chapter 6, Working with Specular and Metallic Surfaces, highlights the differences between Unity 5 Standard Shaders' specular and metallic workflows. We will create custom shaders for both types to create some special in-game effects.

Chapter 7, Shaders for Organic Surfaces, covers the creation of unique skin and hair shaders for a character in an interior scene. The first shader explores methods of representing subsurface scattering effects for partially translucent materials, such as our astronaut character's skin. We follow this up with creation of PBR-compatible hair and eye shaders.

Chapter 8, Custom Particle Shaders – Smoke, Steam, and Fluids, teaches how to create shaders to work with particle effects such as smoke, steam, and fire. As earlier, we will go beyond Unity's default shaders to create more advanced cinematic effects to work within our sci-fi horror context, this time a spacecraft corridor.

Chapter 9, Optimizing Shaders for Mobile, explores testing custom shaders on mobile platforms, such as Android and iOS.

What you need for this book

Understanding key 3D graphics concepts is necessary to complete the chapters in this book. In addition, you will need the following:

Unity 5 installed on your machine (macOS or Windows). The free personal version of the software is sufficient for all of the project content in this book. This can be downloaded from the Unity webpage.
A basic understanding of Unity script or C# will be helpful to complete the projects, though the code that is included is clearly explained.

Who this book is for

This book is intended for intermediate-level game developers that have experience with Unity and C# and are interested in taking the next step with shaders and special effects for next-generation games. A basic background in game development and art is assumed.

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

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/mastering-unity-shaders-and-effects. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

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 http://www.packtpub.com/sites/default/files/downloads/MasteringUnityShadersAndEffects_ColorImages.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. Getting to Grips with Standard Shaders

In this chapter, we will explore Unity's Standard Shaders, creating a set of materials that we will apply to our first scene.

This new set of shaders was introduced in Unity's version 5 release. The Standard Shaders utilize physically-based rendering.

Before the Standard Shader arrangement, specialized shaders were required to create transparent, reflective, and metallic materials. This made including a lot of different surfaces in a single game scene problematic—each shader is essentially a small program that needs to be run, negatively impacting the performance.

The Standard Shaders offer a universal approach—they can be reflective, transparent, metallic, and all of these. Unused map channels are discarded at runtime, making for an efficient shader and reduced performance time.

Combined with Unity's versatile Enlighten lighting solution, Standard Shaders allow for more responsive, realistic surfaces in a game scene.

We will compare the Standard Shader metallic and specular workflows, and offer different opportunities to use both within our game context.

This chapter will cover the following topics:

Discussing the project setup and importing a custom Asset PackageCreating complex materials to cover a variety of surfacesDefining specular and metallic values using sliders and texture mapsUtilizing material groups on imported modelsMaking surfaces transparentLayering materialsDiscussing key differences between Standard (metallic) and standard specular workflowsDiscussing Skybox asset creationDiscussing basic scene light adjustment

Throughout this book, we will develop an understanding of shaders and the effects within the context of a next generation science-fiction horror game.

In the game, a lone astronaut journeys in her spacecraft to a distant planet, Ridley VI, to make contact with a research team after communication with the team was mysteriously cut off.

This context will give us plenty of opportunities to explore the use of shaders and effects in a game development setting.

Let's get to work!

Creating the project

The project is where all the scenes, models, materials, shaders, textures, and other assets are kept together.

At this stage, we will create the project and import the project files that are required to follow the examples in this book:

Launch Unity.When prompted to open an existing project or create a new one, choose Create New Project.Give the project a suitable name, such as Shaders and Effects, and assign a location on your computer's hard drive from where it can easily be accessed.Keep the default 3D option.Choose the Effects assets package.Click on the Create project button.

Unity will take a moment to copy the appropriate files. When it is finished, you will have a new blank space in the Scene view.

The next step is to import the project files needed for the chapters.

Tip

Downloading the example code

Detailed steps to download the code bundle are mentioned in the Preface of this book. Please have a look.

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Mastering-Unity-Shaders-And-Effects. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Importing the project files

The project files for this book have been saved as a Unity assets package that contains all the models and textures needed to follow the tasks in the chapters. Follow these steps to import the project files:

On the menu bar in the main Unity interface, click on the Assets tab.Choose Import Package | Custom Package... from the list that appears.In the window that appears, navigate to the location where you saved and unzipped the PACKT_SAE zip file.

Within the folder, there will be a PACKT_SAE.unitypackage file.

Select it.

Unity will take a moment or two to decompress the package.

The contents of the package will then be displayed as a list in the Import Unity Package dialogue:

Unity's package import

All assets will be selected by default.

Click on the Import button.

When the process is finished, there should be a number of project folders visible in the Project panel.

In the next step, we will load our first scene.

Loading and navigating the spacecraft maintenance scene

Our first scene represents a location near to the planet Ridley VI. Our astronaut has stopped to perform some essential repairs before exploring the planet.

Here, we will get comfortable working with Unity's Standard Shaders, setting up specialized materials with unique qualities for the various surfaces they will represent:

In the Project panel, locate the PACKT_Scenes folder and click on it once in order to view its contents in the Assets panel.In the Assets panel, locate Chapter1_Start.Double-click on the asset to load this scene in the Unity project.

The scene will become visible in the Scene view:

The initial state of the scene

The scene consists of the spacecraft, astronaut, and distant planet with its moon.

Currently, all the models have a basic, default material applied to them.

The scene is already lit with Unity's default scene lighting, so we will be able to compare the way different materials respond to light when we set up our Standard Shaders.

In the next step, we will start with the astronaut's material.

Creating the astronaut material

The astronaut model has a number of different surfaces—her face, glass helmet, and spacesuit, which has metal and fabric sections. Unity's Standard Shader can handle these differences, using sliders or texture values to determine the metallic and shiny surfaces.

We will start by creating the material asset:

In the Project panel, click on the PACKT_Materials folder to view its contents in the Assets panel.Right-click on an empty area of the Assets panel and choose Create | Material from the drop-down list that appears.

The material asset will be created in the PACKT_Materials folder.

Rename it Astronaut.

By default, in Unity 5, new materials are created using Standard Shader, which uses a metallic workflow.

This is a versatile shader that can be used for many different surfaces.

Click on the Astronaut material in the Assets panel to view its properties in the Inspector:
Default material settings

In its blank state, the Astronaut material has a white Albedo color and some amount of Smoothness that will make the surface appear slightly shiny.

Click on the white rectangle next to the Albedo slot in the Inspector and choose a color other than white.

Setting a temporary color will allow us to see where the material is used in the scene before we assign more texture maps to it.

At the top of the center of the main Unity interface, switch to the Scene view by clicking the Scene tab.

This will allow us to get a better view of the parts of the scene that we are working on.

Focus in on the astronaut character using the camera controls: Alt + LMB to rotate, Alt + MMB to pan, and Alt + RMB to zoom.Drag the Astronaut material from the Assets panel onto the Astronaut model in the Scene view and release.

The model's appearance should change, indicating that it now uses the Astronaut material:

The Astronaut material applied in the Scene view

Now, we will be able to see the changes that we make to the material on the model in the Scene view.

In the Project panel, click on the PACKT_Textures folder to view its contents in the Assets panel.In the Assets panel, locate the astronaut_suit subfolder.

This is where all the models textures are located.

Within the astronaut_suit subfolder, locate the astronaut_albedo texture.Drag this to the Albedo slot in the Inspector.

This texture contains the flat surface color information for the astronaut. This is a TGA file without an alpha channel.

Return the Albedo color to white by clicking on the rectangle and choosing pure white from the color picker:
The Astronaut mode with the albedo texture applied

The different parts of the model such as the face and fabric currently respond to the scene's lighting in the same way.

The metallicity and smoothness are uniform as these values are currently defined by value sliders in the Astronaut material.

In the Assets panel, locate astronaut_metal and drag this into the Metallic slot in the Inspector.

The astronaut's appearance will change again in the Scene view.

Note

In metallic maps, the metallicity or metalness value is defined in the red channel of an RBGA texture file. Surfaces that are more metallic have a higher red value. Other channels, such as green and blue, are ignored.

Note that when we dragged the astronaut_metal file into the material, the smoothness slider disappeared.

Smoothness is defined in the alpha channel of the metallic texture. When this is not present, the material is assigned full smoothness.

In the Assets panel, locate astronaut_normal and drag this to the Normal Map slot in the Inspector.

Note

A Normal Map overrides the surface normals of a model defining the direction in which it will reflect light. This technique is commonly used to fake high-resolution detail on a model's surface. The Normal Map slot uses an RGB map, with each of the channels used to define a direction of the normal's surface.

Locate astronaut_suit_ao and drag this to the Occlusion slot in the Inspector.

Note

Occlusion or ambient occlusion