Reader small image

You're reading from  Become a Unity Shaders Guru

Product typeBook
Published inJul 2023
Reading LevelN/a
PublisherPackt
ISBN-139781837636747
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Mina Pêcheux
Mina Pêcheux
author image
Mina Pêcheux

Mina Pêcheux is a freelance content creator who has been passionate about game development since an early age. She is a graduate of the French Polytech School of Engineering in applied mathematics and computer science. After a couple of years of working as a data scientist and web developer in startups, she turned to freelancing and online instructional content creation to reconnect with what brightens her days: learning new things everyday, sharing with others and creating multi-field projects mixing science, technology, and art.
Read more about Mina Pêcheux

Right arrow

Preface

Do you really know all the ins and outs of Unity shaders? Time to step up your game and dive into the new Universal Render Pipeline (URP), the Shader Graph tool, and advanced shading techniques to bring out the beauty in your 2D/3D game projects!

Become a Unity Shaders Guru is here to help you transition from the built-in render pipeline to the SRPs and learn about the latest shading tools. It dives deeper into Unity shaders by explaining the essential concepts through practical examples. You will first get a refresher on how to create a simple shading model in the Unity built-in render pipeline, and then move on to the Unity URP render pipeline and Shader Graph, discovering a wide range of applications in the process. You will explore common game shader techniques, ranging from interior mapping to burning down a candle or simulating the wobble of a fish. You will even read about alternative rendering techniques, such as ray marching.

By the end of this book, you’ll have learned how to create a wide variety of 2D and 3D shaders with Unity’s URP (both in HLSL code and with the Shader Graph tool), and you’ll know some optimization tricks to make your games more friendly to low-tier devices.

Who this book is for

This book is intended for technical artists who have worked with Unity and want to get a deeper understanding of Unity render pipelines and its visual node-based editing tool. Seasoned game developers who are looking for reference shaders using the recent URP render pipeline may also find this book useful. Basic-level programming experience in HLSL can help you with the code-oriented chapters, and you should already be familiar with Unity, its layout, and its common usage.

What this book covers

Chapter 1, Re-Coding a Basic Blinn-Phong Shader with Unity/CG, gives a quick reminder on how to create shaders in Unity with the built-in render pipeline and the Nvdia Cg language.

Chapter 2, The Three Unity Render Pipelines, details the differences between Unity’s built-in render pipeline and the two new render pipelines: the URP and the High-Definition Render Pipeline (HDRP).

Chapter 3, Writing Your First URP Shader, introduces the fundamentals of writing shaders for the new URP pipeline using HLSL instead of Cg.

Chapter 4, Transforming Your Shader into a Lit PBS Shader, builds on the previous chapter and shows how to integrate physically based shading in your URP shaders.

Chapter 5, Discovering the Shader Graph with a Toon Shader, introduces Unity’s new node-based visual shader editing tool, Shader Graph, and explains how to use it with a common example of custom shading – toon cel-shading.

Chapter 6, Simulating Geometry Efficiently, covers a few techniques for cleverly giving the illusion of extra geometry while maintaining correct real-time performance, such as billboarding, parallax mapping, and interior mapping.

Chapter 7, Exploring the Unity Compute Shaders and Procedural Drawing, discusses what compute shaders are and how we can use them to offload computation from the CPU to the GPU and increase the performance of our games.

Chapter 8, The Power of Ray Marching, dives into an alternative rendering method where you build your entire scene just using mathematics!

Chapter 9, Shader Compilation, Branching, and Variants, shifts the focus to shader optimization and tells you how you can use various keywords in your shaders to improve the performance of your final game after the build.

Chapter 10, Optimizing Your Code or Making Your Own Pipeline?, adds to the knowledge of the previous chapter and offers another set of tricks for optimizing your shaders, as well as a basic overview of custom render pipelines.

Chapter 11, A Little Suite of 2D Shaders, takes you through the creation of a series of shaders for your sprites ranging from simple color-swapping to outlining or dissolving effects.

Chapter 12, Vertex Displacement Shaders, explains how to use the vertex displacement technique to create procedural animations or simulate the waves on a water plane.

Chapter 13, Wireframes and Geometry Shaders, teaches you about geometry shaders and highlights the limitations of this tool, especially when targeting Mac users, before taking you through a common application of the technique to create a wireframe render.

Chapter 14, Screen Effect Shaders, wraps up this book by discussing fullscreen shaders, which make it easy to apply screen-wide effects and make a personal atmosphere for your game.

Appendix: Some Quick Refreshers on Shaders in Unity, compiles a few handy worth-remembering base concepts for creating shaders in Unity, be it with the built-in render pipeline or the new URP and HDRP pipelines.

To get the most out of this book

To really benefit from this book, you should have prior experience with Unity and be familiar with the basics of shaders. The book is written with Unity 2022 LTS (Unity 2022.3.11f1), which is the most recent Unity LTS version at the time of writing this book..

Software/hardware covered in the book

Operating system requirements

Unity 2022 LTS (Unity 2022.3.11f1)

Windows, macOS, or Linux

If you are using the digital version of this book, we advise you to type the code yourself or access the code from the book’s GitHub repository (a link is available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.

Note that this book is not based on the latest Unity 2023 editor because it is still in preview (beta version) at the time of writing this book, and therefore is subject to too many changes in the near future and is not robust enough for learning. Instead, this book is based on the Unity 2022 editor, which is currently in the Tech Stream release, and thus provides more stability.

Download the example code files

You can download the example code files for this book from GitHub at https://github.com/PacktPublishing/Become-a-Unity-Shaders-Guru. If there’s an update to the code, it will be updated in the GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots and diagrams used in this book. You can download it here: https://packt.link/rE7c8

Conventions used

There are a number of text conventions used throughout this book.

Code in text: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: “At that point, our URP shader is set up to use the scriptable render feature, which will automatically call the compute shader we prepared in our FillWithRed.compute asset and return a full red opaque color for each pixel on the screen.”

A block of code is set as follows:

using UnityEngine;
using UnityEngine.Rendering;
[CreateAssetMenu(menuName = "Compute Assets/CH07/FillWithRed")]
public class ComputeFillWithRed : URPComputeAsset {
    public override void Render(CommandBuffer commandBuffer,
        int kernelHandle) {}
}

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

private void _GenerateGrid() {
    _cubes = new List<GameObject>();
    _cubeRenderers = new List<MeshRenderer>();
    _data = new Cube[gridSize * gridSize];
    ...
    for (int x = 0; x < gridSize; x++) {
        for (int y = 0; y < gridSize; y++) {
            ...
            _cubes.Add(g);
            _cubeRenderers.Add(r);
            _data[x * gridSize + y] = new Cube() {
                position = g.transform.position,
                color = color
            };
        }
    }
}

Bold: Indicates a new term, an important word, or words that you see onscreen. For instance, words in menus or dialog boxes appear in bold. Here is an example: “To get this incoming vector, we use the View Direction node – and the offset is then computed by multiplying it with a given float value.”

Tips or important notes

Appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, email us at customercare@packtpub.com and mention the book title in the subject of your message.

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata and fill in the form.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at copyright@packtpub.com with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Download a free PDF copy of this book

Thanks for purchasing this book!

Do you like to read on the go but are unable to carry your print books everywhere?

Is your eBook purchase not compatible with the device of your choice?

Don’t worry, now with every Packt book you get a DRM-free PDF version of that book at no cost.

Read anywhere, any place, on any device. Search, copy, and paste code from your favorite technical books directly into your application.

The perks don’t stop there, you can get exclusive access to discounts, newsletters, and great free content in your inbox daily

Follow these simple steps to get the benefits:

  1. Scan the QR code or visit the link below

https://packt.link/free-ebook/9781837636747

  1. Submit your proof of purchase
  2. That’s it! We’ll send your free PDF and other benefits to your email directly
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Become a Unity Shaders Guru
Published in: Jul 2023Publisher: PacktISBN-13: 9781837636747
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Mina Pêcheux

Mina Pêcheux is a freelance content creator who has been passionate about game development since an early age. She is a graduate of the French Polytech School of Engineering in applied mathematics and computer science. After a couple of years of working as a data scientist and web developer in startups, she turned to freelancing and online instructional content creation to reconnect with what brightens her days: learning new things everyday, sharing with others and creating multi-field projects mixing science, technology, and art.
Read more about Mina Pêcheux