Daniel Ilett
Building Quality Shaders for Unity
Using Shader Graphs and HLSL Shaders
The Apress logo.
Daniel Ilett
Coventry, UK
ISBN 978-1-4842-8651-7 e-ISBN 978-1-4842-8652-4
https://doi.org/10.1007/978-1-4842-8652-4
Daniel Ilett 2022
Standard Apress
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
The publisher, the authors and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, express or implied, with respect to the material contained herein or for any errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional claims in published maps and institutional affiliations.
This Apress imprint is published by the registered company APress Media, LLC, part of Springer Nature.
The registered company address is: 1 New York Plaza, New York, NY 10004, U.S.A.
To Lewis.
Introduction
So youve decided to start writing shaders in Unity. Thats a good decision if I do say so myself! Before I started writing shaders, I saw many people in the game development space talk about them in hushed tones, as if they were some kind of arcane secret bestowed from the heavens, but its my belief that anyone can write shaders with a soft introduction. Its like learning any other skill; you dont learn to ride a push-bike by imitating Evel Knievel right off the bat.
This is the kind of book I wish I had when I was learning to write shaders in Unity. The early chapters establish the core concepts and syntax youll need to know when writing shaders, and throughout the course of the book, I will steadily introduce more complex topics until we have covered some of the lesser-known shader topics in Unity. By that point, you should be able to create complicated, versatile effects by yourself.
Who This Book Is For
This book covers topics ranging from the beginner to the intermediate level. Ill assume you already know your way around the Unity Editor, but I will explain the interface of unfamiliar sections such as the Shader Graph editor. Here are a few hypothetical readers I think will enjoy this book:
Ive never written a shader before.
If this is you, then I definitely recommend starting with Chapter is optional it covers all the math related to shaders, but not everyone learns math easily by reading it all up front, so feel free to skip it to start with and come back whenever later chapters introduce math that youre unfamiliar with.
I wrote shaders a few years ago for old Unity versions, but Im unfamiliar with Unity versions past 20182019 or so.
The early chapters will serve as a good refresher, but you can probably skim-read Chapter at the very least, perhaps more. Pick out a few of the examples and give them a go just to remind yourself of the syntax and concepts before diving into later chapters head-on!
Im experienced with writing shaders in code, but Id like to transition to using Shader Graph (or vice versa).
Every example in the book is presented in both shader code and Shader Graph where possible (there are a small number of effects and features that, for various reasons, are only possible in one of those tools). It may be helpful to read through examples you are familiar with and use that as a springboard to learn the tool youre not as experienced with.
Ive grasped the basics, but I want to move on to more difficult topics like compute shaders and tessellation shaders.
Although the earlier chapters will be useful as a reference should you need it, you will want to skip or skim-read Chapters onward, but Ive attempted to sprinkle in intermediate features throughout the book.
Recommended Software
Every example in the book will work in Unity 2021.3, which is the latest Long-Term Support (LTS) version of the engine at the time of the writing of this book. If you are reading this book in the future and there is a later LTS version, then every example will most likely still work, although you may potentially need to change the names of functions and macros. If you are using an earlier version than 2021.3, most of the effects will work, but there are features that are completely missing in certain versions. The book will still be helpful if you are working on a project started in an earlier Unity version and you are unable to upgrade to a newer version, and I will warn you when I am using a recently added feature.
Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the books product page, located at www.apress.com/. For more detailed information, please visit http://www.apress.com/source-code.
Acknowledgments
With thanks to Lewis, who reads all my tweets out loud back to me seconds after posting. Something tells me he wont be reading this whole book to me. You also had to deal with me in the morning before Id had a coffee, which couldnt have been easy.
Thank you also to my family, especially my mum, who has apparently been saying Did you know hes writing a book? whenever my name comes up in conversation with people.
With thanks to the Warwick Game Design Society and its members, who have given me a space to flourish and work on games while at university. You also showed me how to eat a gigantic slice of chocolate fudge cake in under a minute, which surely got us some strange looks from other pubgoers. There are too many of you to count!
Finally, my thanks to the Apress team and everyone who was involved in writing this book with me. From the first invitation to write the book to every bit of advice and correction, youve all been invaluable partners on this journey.
Table of Contents
About the Author
Daniel Ilett
A photo of Daniel Ilett.
is an ambitious and motivated PhD student at the University of Warwick. He is a passionate game developer, specializing in shaders and technical art. He publishes a range of educational and tutorial content, including videos and written work, aimed at beginners and intermediate developers. He also does freelance work on shaders and visual effects for games.
About the Technical Reviewer