Unity 5.x Shaders and Effects Cookbook - podcast episode cover

Unity 5.x Shaders and Effects Cookbook

Sep 25, 202533 min
--:--
--:--
Download Metacast podcast app
Listen to this episode in Metacast mobile app
Don't just listen to podcasts. Learn from them with transcripts, summaries, and chapters for every episode. Skim, search, and bookmark insights. Learn more

Episode description

A comprehensive guide for mastering shader programming within the Unity 5 game engine. Authored by Alan Zucconi and Kenneth Lammers, it explores various lighting models, rendering techniques, and visual effects, ranging from basic diffuse shading and texture mapping to advanced concepts like Physically Based Rendering (PBR) and screen effects. The text emphasizes practical application through recipes, explaining how to optimize shaders for mobile platforms, implement vertex functions to manipulate 3D model geometry, and utilize CgInclude files for modular code. It also provides insights into Unity's built-in tools like the Profiler and Reflection Probes, offering solutions for creating realistic and efficient game visuals.

You can listen and download our episodes for free on more than 10 different platforms:
https://linktr.ee/cyber_security_summary

Get the Book now from Amazon:
https://www.amazon.com/Unity-5-x-Shaders-Effects-Cookbook/dp/1785285246?&linkCode=ll1&tag=cvthunderx-20&linkId=9c7002768ed60073071391998d820693&language=en_US&ref_=as_li_ss_tl

Discover our free courses in tech and cybersecurity, Start learning today:
https://linktr.ee/cybercode_academy

Transcript

Speaker 1

You know that feeling when you're playing a game and the graphics just pop, the way light catches the surface, or those subtle reflections, maybe even that really distinct stylized look, you know that makes a world feel real or sometimes magically hand drawn. Well, a lot of that visual match comes down to something called shads.

Speaker 2

Absolutely, they're kind of like the secret sauce. Whden't you say? They define how light interacts with everything you see exactly.

Speaker 1

It turns simple three D models into something visually rich.

Speaker 2

They're really the unsung heroes working away at the pixel level. It's where art definitely meets code.

Speaker 1

So today we're doing a deep dive into this fascinating world Unity Shads and Effects. Right, we're drawing from a really great resource, the Unity five point x Shaters and Effects Cookbook. Our mission is basically to unpack how developers use shader programming to really bring their Unity projects to life.

Speaker 2

Yeah, and give you a shortcut to understanding what's behind modern game visuals from you know, the basic building blocks right up to some really mind ending real time effects.

Speaker 1

And this cookbook the authors are top notch Alan Zuconi develops thirty under thirty developer behind zero Arbitalis and.

Speaker 2

Kenneth Lammers, He's got what over fifteen years in the industry, worked on huge titles like Call of Duty three, Alan Wake.

Speaker 1

So yeah, there definitely are expert guides for this for sure. So if you've ever wondered how games get those stunning visuals, how a simple cube can look like metal or wood, or how an explosion just looks.

Speaker 2

Right, then this deep dive is definitely for you. Get ready for some aha moments.

Speaker 1

Let's start right at the beginning. Then at its core, what is a shader?

Speaker 2

Okay? So think of it like a tiny program, a specialized program that runs on your graphics card, okay, and its job is to tell the graphics card how light should behave on a surface. Imagine you have a plane three D cube. Yep. The shader is what makes it look like shiny metal or rough wood or maybe even glass. It's not just slapping on a picture. It's simulating how light interacts with that material.

Speaker 1

A right, So it's about the light interaction, not just the color. Makes sense, it's the visual identity exactly. But how does this shader fit into, you know, the bigger picture in unity the rendering process? What's the chain of command.

Speaker 2

Good question. Let's break it down. So in Unity, everything you see is basically a game object.

Speaker 1

Right, characters, props everything.

Speaker 2

Yeah, And for a three D game object to show up, it usually needs a component called a mesh renderer. Think of the mesh render like the stage manager deciding how that three D model appears.

Speaker 1

Okay, like deciding its costume and lighting pretty much.

Speaker 2

Yeah, And that mesh render uses something called a material. The material is like the specific recipe is its silk is a burlap? Got it? And that material contains the shader. The shader is the actual set of instructions defining how light bounces off that silk or burlap. So the chain is game object, mish renderer, material.

Speaker 1

Shader, clear object renderer, material shader. And speaking of Unity, Unity five brought a big change rate hebr oh.

Speaker 2

Huge physically based rendering PBR that became the new standard.

Speaker 1

What was so revolutionary about it? More realistic graphics?

Speaker 2

Well, yes, but it was more than that. The real insight was that PBR provided a consistent, physically plausible way to describe materials. Consistent how meaning artists could define a material, say brushed aluminum, and it would look correct under almost any lighting condition morning sun, dim, indoor lights, whatever. No more endless tweaking for every scene.

Speaker 1

Ah okay, so it behaves predictably based on physics Exactly.

Speaker 2

It was a massive step up from the older diffuse and specular models in Unity four, which often needed a lot more manual fiddling.

Speaker 1

That sounds like a life saver for artists, and I hear properties make it even easier for them.

Speaker 2

What are those properties? Are Billiant. They're basically controls that show up in the Unity Editor in the Materials Inspector tab.

Speaker 1

Like sliders and color pickers.

Speaker 2

Precisely, a shaded programmer writes a line of code like color main, color color and boom, the artist get a color swatch in the editor. Or roughness roughness range zero one zero point gives them a slider, so the artists.

Speaker 1

Can tweet the look, change the color, adjust roughness without needing to code exactly.

Speaker 2

That's the whole point. It separates the technical shader logic from the artistic tweaking.

Speaker 1

And crucially, where are the values for those properties stored in the shader?

Speaker 2

No, that's the key distinction. The properties are defined in the shader, but there's specific values the chosen color the slider setting are stored in the material.

Speaker 1

Ah, so you can have one shader like metal, but multiple materials using it, shiny steel, rusty iron, each with different property values.

Speaker 2

You got it. Change a property on the shiny steel material and all objects using that material update instantly. Super flexible.

Speaker 1

Okay, Now, for anyone brave enough to write their own shaders, debugging sounds like fun. That magenta color, ah, the dreaded magenta.

Speaker 2

Yeah, unlike C sharp scripts that just stop your game shader errors, don't halt anything. Instead, your object turns bright unshaded.

Speaker 1

Magenta, a very clear error message.

Speaker 2

It certainly gets your attention. Commonness, the steaks are you know, typos, missing semicolms forgetting a bracket or having a property defined but not actually using it in the shader code. It needs that link.

Speaker 1

Good to know. Also heard something about floats in CIG being slightly different.

Speaker 2

Yeah, minor thing, but it catches people. In CIG the shader language you just write one point zero, one point oh f like you often do in Sea Shark, small detail, big magenta headache if you forget.

Speaker 1

Okay, so we've got the basics. Shaders tell light what to do. Artists use materials and properties to tweak them. But how do we get specific looks making something actually look like wood or stone?

Speaker 2

Right? That takes us into surface shaders, textures and lighting models. This is where the real visual magic.

Speaker 1

Happens surface shaders. What's the core idea there?

Speaker 2

A surface shad basically has two main parts. First, there's a surface function. In this function, you tell unity about the physical properties of your surface, like it's base color, albedo, how smooth or rough it is, smoothness if it's metallic, metallic, maybe, how transparent?

Speaker 1

And alpha okay, describing the material itself exactly.

Speaker 2

Then all that information gets past to a lighting model. The lighting model takes those surface properties and information about the lights in your scene and calculates the final color for each pixel.

Speaker 1

So it combines the what it is with how it's lit precisely.

Speaker 2

And there are different structs or data packages for these properties, like Surf's output standard for PBR. They hold things like albedo, normal emission, metallic, smoothness, alpha, got it, and.

Speaker 1

I understand c The shader language is pretty smart about handling data pact rays.

Speaker 2

Yeah, CG is really efficient. It uses things like float three or float four. These packed rays. Think a float three holding xyz for position or RGB for color. A float four adds W or alpha.

Speaker 1

So it groups related numbers together, right, and.

Speaker 2

This lets you do cool stuff called swizzling. Like if you have a color and a float four called color, you can grab just the RGB parts really easily by writing color dot RGB, or even rearrange them like color dot bgr meat, trek, or even smearing. Assigning a single value you like point five to a float three will set all three components xyz two point five. It's all about doing math on multiple values at once, which gpuser.

Speaker 1

Great app Okay, back to textures. How do you get a flat image, say bricks, onto a three D wall.

Speaker 2

That's texture mapping. It relies on something called UV data stored in the three D model itself.

Speaker 1

UV data like coordinates.

Speaker 2

Exactly each vertex corner point of your three D model has two coordinates U and V, usually ranging from zero to one. Think of it like flattening a three D model onto a two D plane. These uvs tell the shader exactly which part of the two D texture image corresponds to which part of the three D model's.

Speaker 1

Surface, and the GPU figures out the bits in between the vertices.

Speaker 2

Yep, it interpolates the uvs across the surface. Then the shader uses a function usually text two D, to look up or sample the color from the texture at that specific UV coordinate for each pixel.

Speaker 1

Clever? Can you animate textures this way like flowing water?

Speaker 2

Absolutely? You can modify the UV coordinates over time. Just add unities built in time variable or some fraction of it to the U or V coordinate before sampling the texture. Presto. Scrolling texture a classic way to fake movement.

Speaker 1

Nice, But what about adding really fine detail, like the tiny bumps on that brick wall where scratches on metal. Adding tons of polygons would kill performance, right.

Speaker 2

You definitely don'tant a model every single bump. That's where normal mapping comes in. It's a fantastic technique.

Speaker 1

How does that work? Is it another texture?

Speaker 2

It is. It's a special texture, often purplish, called a normal map or sometimes a bump map. But instead of storing color, the RGB values in this texture actually represent directions direction. Yeah, they represent the direction that the surface normal is pointing at that specific pixel. The normal is like an imaginary line pointing straight out from the surface. By slightly changing the direction of this normal according to the map, you trick the lighting calculation aw.

Speaker 1

So the light bounces off as if there was a bump there, even though the geometry is flat exactly.

Speaker 2

It gives this incredible illusion of surface detail bumps, grooves, rivet scratches without adding any extra polygons. It's super efficient. Unity even provides a function unpacked normal to easily decode the information from the normal map texture.

Speaker 1

That's seriously cool. Okay, so normal maps fake detail on solid surfaces. What about things that aren't solid, like glass or ghosts or fire. Transparency sounds difficult.

Speaker 2

It is surprisingly more complex than you might think. See for solid objects, the graphics card does clever stuff like z ordering, figuring out what's in front and only drawing that, and culling like not drawing the back faces of objects you can't see.

Speaker 1

Makes sense, saves work.

Speaker 2

But transparent objects mess that up. You need to see what's behind them, so they need special handling. You use shader tags to tell Unity how to handle them, like setting que transparent tells Unity to draw them after all.

Speaker 1

The solid stuff, so order matters a lot critically, and.

Speaker 2

You need specific shader directives like hashtag, pregma, surface dot alpha dot fade to enable blending so the it's parent object mixes correctly with the background pixels.

Speaker 1

Can you do stylized transparency too? I think the book mentioned a hologram shader.

Speaker 2

Oh yeah, that's a neat one. You can use a mathematical operation called the dot product. You calculate the dot product between the surface normal and the view direction.

Speaker 1

Okay, what does that tell you?

Speaker 2

It tells you how much a surface is facing towards or away from the camera. Surfaces facing you get one value, Surfaces facing away get another. You can then use this to make only the edges or the parts facing you slightly visible, creating that classic sci fi hologram rim.

Speaker 1

Effect, just showing the outlines kind of yeah.

Speaker 2

Or a faint semi transparent look. It's computationally cheap and great for ghosts, force fields, selected object outlines, that kind of thing.

Speaker 1

All right, we're using all these textures, color normal maps, maybe others. Textures take up memory, right, especially on mobile, any tricks for optimizing that.

Speaker 2

Big time textures are often a major memory hog. A really common technique is texture packing, sometimes called channel.

Speaker 1

Packing, packing like zipping files sort of.

Speaker 2

But for data, many maps you need might only be grayscale, like a roughness map or a metallic map, or an ambient inclusion map. They only need one channel of.

Speaker 1

Data instead of the usual three for RGB color exactly.

Speaker 2

So you can take say your roughness map and put it into the red channel of a texture. Take your metallic map, put it in the green channel, maybe an ambient inclusion map in the blue channel, and something else in the alpha channel.

Speaker 1

Ah. So one our GBA texture holds four different gray scale maps.

Speaker 2

You got it. It saves a ton of memory and reduces the number of texture samples the shader needs to do. Super common for character models and especially terrain. For terrain, you might have one packed texture controlling how grass, dirt, rock, and snow blend together using a function called LURP.

Speaker 1

LURP linear interpolation.

Speaker 2

Yeah, it smoothly blends between two values based on a third value. So you could use the red channel of your blend map to control the blend between dirt and grass, the green channel for grass and rock, and so on. Very efficient.

Speaker 1

Okay, that makes sense. We mentioned lighting models earlier as part of surface shaders. Why would a developer bother making a custom lighting model. If Unity has good built in ones like PBR, well, PBR.

Speaker 2

Is fantastic for realism. But sometimes you don't want realism. You want a specific artistic style. Maybe you want your game to look like a comic book or an old cartoon or something completely unique. Custom lighting models give you that precise control over how light and shadow behave, letting you define your own visual language.

Speaker 1

So for non photorealistic rendering NPR.

Speaker 2

Exactly, or even just for optimizing for very low end platforms where PBR might be too expensive. Understanding the classic models helps too.

Speaker 1

Let's talk about those classics. What's Lambertune reflectants.

Speaker 2

Lambertine was basically the default diffuse lighting model in Unity four. It's super simple, very fast. It calculates brightness based only on the angle between the light hitting the surface and the surface normal itself.

Speaker 1

So how directly the light hits it right, uses a.

Speaker 2

Dot product for that. It results in a very matt non shiny look. Thinks bull flat shading great for stylized low poly games, very efficient.

Speaker 1

And then the opposite end stylistically is maybe tune shading or cell shading. That comic book look.

Speaker 2

Yeah, totally different. Goal tune shading aims for that flat, hand drawn cartoon style. The trick here is usually a custom Whiting model that takes the smooth gradient of light intensity you'd get from.

Speaker 1

Lambursha makes it chunky exactly.

Speaker 2

It uses something called a ramp map. It's usually a small one D texture, like a thin strip with distinct bands of color or brightness. The shader calculates the normal light intensity often called n Dattel, the dot product of normal and light direction. Uses that value to look up a color on the ramp map, and poof, Instead of a smooth gradient, you get sharp defined bands of color. That's the cell shaded look.

Speaker 1

Clever use of a texture to change the lighting result. Okay, what about shininess speculat highlights, We've got pong blind fall right.

Speaker 2

These are about adding those bright spots you see on shiny objects. Is sort of a classic foundational model. It calculates the reflection based on the angle between the direction the camera is viewing from and the direction the light would perfectly reflect off the surface, So.

Speaker 1

It depends on where you're looking from very.

Speaker 2

Much, so it's view dependent. It gives a decent shiny.

Speaker 1

Look and Blindphong is it just better.

Speaker 2

It's often preferred. Yeah, it's generally faster to calculate, and in many cases looks a bit more realistic or at least softer than basic fong. Instead of calculating the full reflection vector, it uses a clever shortcut involving a half vector, which is the vector halfway between the light direction and the view direction. It was the default specular model and unity for still very useful.

Speaker 1

Okay, simple shininess covered. But then there's anisotropic speculator that sounds complex. Brushed metal hair? How does that work?

Speaker 2

Anisotropic is really cool. It simulates materials where the shininess isn't uniform because the surface has tiny parallel grooves or fibers I think brushed aluminum. The back of a CD vinyl records even strands of hair.

Speaker 1

So the highlight isn't just a round dot exactly.

Speaker 2

Instead of a circular highlight, it stretches the specular highlight perpendicularly to the direction of those tiny grooves or strands. It requires extra information, usually from another texture or model data, telling the shadeer which way the grain or fibers run on the surface. It has a massive amount of realism for those kinds of materials.

Speaker 1

Wow, Okay, that's pretty specific control. Let's circle back to PBR and unity five. You said it was a revolution. What are the main workflows?

Speaker 2

The two main ones are standard and standard a specular setup. The most common one is the standard workflow, which uses a metallic map.

Speaker 1

Metallic workflow. How does that enforce realism?

Speaker 2

It works based on a key physical principle. Things are generally either metallic or non metallic dielectric. Pure metals get almost all their color from reflected light. They have very dark or black diffuse color. Their albedo map basically defines the color of the reflection.

Speaker 1

Okay, and non metals like plastic or wood.

Speaker 2

Non metals have a colored diffuse component that's their albedo map color, and their specular reflections are generally noncolored, reflecting the color of the light source itself. The metallic map, usually a grayscale texture packed new channel tells the shade or how metallic. Each part of the surface is. Black means non metal, white means pure metal, gray is somewhere in between like dusty or corroded metal.

Speaker 1

And smoothness is that packed in there too?

Speaker 2

Often? Yes, the smoothness value controlling how sharp or blurry reflections are is frequently packed into the alpha channel of the metallic map texture. So one map controls both metalness and smoothness.

Speaker 1

It's efficient PBR in transparency. How does that work? Does it handle glass and ghosts differently?

Speaker 2

It does. PBR offers specific modes. Transparent mode is for things like proper glass or clear plastics. Crucially, it keeps all the PBR goodness speculat highlights, reflections, Fresnel effect where reflectivity changes with the young angle. It looks physically correct.

Speaker 1

Okay, what about ghosts?

Speaker 2

For that, you'd probably use fade mode. In fade mode, as the object becomes more transparent, everything fades out, including the specular highlights and reflections. Perfect for effects where things need to dissolve completely.

Speaker 1

And the third one cut out.

Speaker 2

Cutout mode is for things that are geometrically solid but need to look like they have holes, like chain link fences, leaves on a tree, or detailed grates. It uses the alpha channel of the texture, but it's not blended transparency. It's either fully opaque or fully invisible based on an alpha cutoff slider, sharp edges, no semi transparency.

Speaker 1

Got it makes sense for foliage. Now realistic reflections like mirrors or shiny floors. You mentioned reflection probes right.

Speaker 2

Real time reflections are super expensive, So reflection probes are a way to capture the environment around a certain point. Think of it like taking a three hundred and sixty degree photo from that spot and storing it as a special texture called a cube map.

Speaker 1

Six images, one for each direction.

Speaker 2

Exactly up, down, left, right, front, back. Then shiny objects near that probe can sample this cube map to show reflections of the surrounding static scenery.

Speaker 1

Are they expensive?

Speaker 2

They can be, especially if they need to update in real time, like if dynamic objects need to be reflected. Often they're baked, meaning they capture the static environment once in the editor, which is much cheaper at run time. They're crucial for making metallic PBR materials look believable.

Speaker 1

Speaking of baking, light baking, that sounds like pre calculating lighting.

Speaker 2

That's exactly what it is. Calculating how light bounces around realistically in real time. Global illumination or GI is incredibly demanding, So light baking lets unity pre calculate all that complex light bouncing for objects that don't move. Your buildings terrain static.

Speaker 1

Props and saves that lighting information.

Speaker 2

Yeah, saves it into special textures called light maps. At runtime, these static objects just look up their pre calculated lighting from the light map. It looks great and is super fast compared to calculating GI every frame.

Speaker 1

But what about things that do move your player character NPCs, moving objects, They can't use static lightmaps.

Speaker 2

Correct for them. We use light probes. You scatter these probes around your scene, especially where lighting conditions change. Like light maps, they get baked. They sample the complex bounced lighting at the specific locations during the bake.

Speaker 1

Process, so they store the lighting info at points in space exactly.

Speaker 2

Then, as your character or a dynamic object moves through the scene, it looks at the nearest light probes, blends the lighting information sampled by them, and uses that to light itself. This allows moving objects to feel realistically integrated into the baked static environment. It's a clever way to get the best of both worlds, detailed lighting on static stuff dynamic objects that still fit in.

Speaker 1

It's all about balancing that realism and performance budget always.

Speaker 2

That's the name of the game.

Speaker 1

In real time graphics, okay, shifting gears a bit, shaders aren't just about color and light, right. They can actually change the shape of objects using vertex functions.

Speaker 2

Absolutely. This is where shaters get really powerful beyond just surface appearance. A Vertex function runs for every vertex or corner point of your three D model before it even gets render, and inside this function you can actually change the vertex's position in three D.

Speaker 1

Space, So you can literally move the points of the model around with code.

Speaker 2

Yep, you can deform the geometry. For example, you could access vertex colors. Did you know you can paint colors directly onto the vertices of a model in many three D modeling tools.

Speaker 1

I didn't realize that.

Speaker 2

Yeah, And you can read those colors in the vertex shader and use the drive effects maybe make parts glow or control how much wind affects certain parts of a mesh.

Speaker 1

Speaking of wind, can you animate vertices like a waving flag?

Speaker 2

Totally? That's a classic use case. Instead of complex physics or bone animations, you can use a simple sine wave based on time time and the vertex's position to make vertices move up and down or back and forth, creating a realistic waving flag or rippling water effect directly in the shader.

Speaker 1

That's really efficient. What's this normal extrusion technique? It sounds weird, making things chubbier or skinnier.

Speaker 2

It is a bit weird, but very effective. You take the vertex position and push it outwards or inwards along its normal direction, that line pointing straight out from the surface.

Speaker 1

So everything expands or contracts along its surface direction.

Speaker 2

Right, you can make a character look inslated or deflated, and you can control how much extrusion happens using a texture map an extrusion map, so you could make certain parts puff out and others sink in.

Speaker 1

And the book mentions zombifying characters.

Speaker 2

Yeah, that's a great example. You could use an extrusion map to make the skin sync in over the bones, giving that gaunt, skeletal look, or for something less gruesome. Snow shaders often use it. They might detect which triangles face upwards, color them white, and then extrude them slightly along their normal to simulate snow piling up.

Speaker 1

Wow and explosions.

Speaker 2

Vertex shads are key from many volumetric effects too. For an explosion, you might start with a sphere mesh, then use noise textures and time in the vertex shader. To displace the vertices outwards in a chaotic expanding way. Combine that with a clip function in the fragment shader to make parts dissolve, and you get a convincing three D fireball effect without complex fluid simulations.

Speaker 1

Incredible control. Okay, beyond surface shaders, there are vertex and fragment shaders. These give even more control they do.

Speaker 2

Surface shators are actually a higher level abstraction that Unity provides, making common tasks easier. Under the hood, they get compiled down into lower level vertex and fragment shaders. Anyway, writing vertex and fragment shaders directly gives you complete control over both stages.

Speaker 1

Vertext processes geometry, Fragment processes.

Speaker 2

Pixels almost it processes fragments, which are potential pixels. A fragment has color, depth, position, etc. Before potentially gets written to the screen. But yet, think of the fragment shader as running for every pixel being rendered for that object.

Speaker 1

And this is where the grab pass comes in. That sounds powerful, It's.

Speaker 2

Very powerful for screen effects. A grab pass is a special command you put in your shader. It tells Unity, before you draw this object, grab whatever has already been drawn. To the screen in this area and put it into a texture for me.

Speaker 1

So it gets a snapshot of the background.

Speaker 2

Exactly, usually into a texture named grab texture. Now in your fragment shader for the current object, say a piece of glass, you can read from that grab texture and distort it precisely. You can sample the grab texture using modified UV coordinates. For glass, you might offset the uvs based on a normal map to simulate refraction. For water, you might use an animated noise texture to make the

UV's wiggle, creating that shimmering distortion effect. It makes the object look like it's realistically interacting with the scene behind it.

Speaker 1

That's how you get those really convincing glass or heat haze effects.

Speaker 2

Yep. Grab pass is the key.

Speaker 1

Okay, amazing visuals, but we need performance, especially on mobile. How do you optimize shaders make them cheaper?

Speaker 2

Several ways. A big one is data precision. GPUs can work with different levels of floating point precision. Full float thirty two bit is most precise, but slowest. Half sixteen bit is less precise, but faster, often perfectly fined for colors, texture coordinates, maybe even normals fixed. Often eleven bit is even less precise, but the fastest good for simple color calculations are things that don't need high accuracy.

Speaker 1

So using smaller variable types saves time and memory definitely.

Speaker 2

Another trick is the hashtag pragma surface no a ford Ad directive. Normally Unity might try to calculate lighting for multiple lights hitting an object in the forward rendering path, adding complexity. No forward DAD tells Unity only calculate the brightest directional light per pixel. Handle all other lights more cheaply, maybe pervertex. Big performance win in scenes with many lights.

Speaker 1

Good tip any others.

Speaker 2

Sharing texture coordinates uves if multiple textures on your material use the same mapping, reducing the number of texture lookups overall using those texture packing techniques you talked about. It's often about reducing memory, bandwidth and calculation complexity.

Speaker 1

With all these shaders potentially running, how do you figure out which one is causing a slow down if your game starts lagging?

Speaker 2

AH. The profiler Unity's built in profiler c TROL plus seven or CMD plus seven on Mac is your best friend here?

Speaker 1

What does it show you?

Speaker 2

Everything? It shows you frame by frame, what your CPU and GPU are spending time on. You can see rendering stats like draw calls, how many separate things are being drawn triangle counts, and crucially, how much time is being spent rendering different objects and executing their shaders.

Speaker 1

So you can spot the expensive ones exactly.

Speaker 2

You can pause the game, scrub through the timeline, find a performance spike, and drill down. The profiler will often list rendering tasks and you can see which materials and therefore which shaders are taking up the most GPU time. It's essential for optimization.

Speaker 1

Okay, essential tool. Now what if you want to change the look of the entire game, like add a film grain effect or adjust overall contrast without editing hundreds of materials.

Speaker 2

That's where screen effects or post processing effects are used. They're incredibly powerful for applying a final artistic touch to the whole scene.

Speaker 1

How do they work? Do they use? Grab pass?

Speaker 2

Conceptually similar, but usually more optimized. The typical flow is Unity renders your entire normally. Instead of drawing it directly to the screen, it draws it into an off screen buffer called a render texture.

Speaker 1

Like an image of the final frame exactly.

Speaker 2

Then a special camera with a post processing shader attached simply draws a full screen quad a plat rectangle using that render texture as its input. The shader on this quad can then modify every single pixel of the final image before it's shown to you.

Speaker 1

Ah, so it's like applying a Photoshop filter to the whole game image.

Speaker 2

That's a perfect analogy. You can do brightness, saturation, contrast adjustments using formulas involving luminance. You can tint the image, apply Vignett's film grain distortions. Anything you can code in a shader you can apply to the entire screen.

Speaker 1

Can you blend images to like overlaying textures.

Speaker 2

Yep. You can sample other textures in your post processing shader and blend them with the scenes render texture using standard blend modes. Multiply for darkening, ad or screen for brightening, overlay for complex contrast. Tons of possibilities for achieving specific moods and stuff.

Speaker 1

So these screen effects are huge for immersion and cinematic feel. What are some classic examples?

Speaker 2

Oh loads, an old movie effect is great when you combine several things, maybe a CPA tone filter to make it brownish, a vignette to darken the corners, maybe some noise or film grain, and then overlay animated dust and scratches textures using time since time perhaps and random offsets to make them flicker and shift instantly. Gives that vintage vibe.

Speaker 1

Yeah, I can picture that. What about something like night vision?

Speaker 2

Another classic usually involves tinting the whole screen green, maybe boosting brightness or contrast in a nonlinear way, adding scan lines, thin horizontal dark lines, some static noise, maybe barel distortion like looking through a lens, and perhaps limiting the color palette. Put it all together and you get that familiar military or sci fi night vision look. These effects really sell the feeling of looking through a specific device or being in a particular state.

Speaker 1

Totally changes the experience. Okay, we're getting into advanced territory for developers writing lots of shaders. Is there a way to make things more modular reusable?

Speaker 2

Get? Yes, definitely. Unity uses cg include files dot CGC extensively itself, and you can create your own.

Speaker 1

Cg include like header files and c plus plus.

Speaker 2

Exactly like that. You put common functions, maybe your custom lighting calculations, helper functions for noise, complex math routines into a dot c gene file. Then in your main shader files you just hashtag include that file.

Speaker 1

And you can reuse that code across many shaders YEP.

Speaker 2

It keeps your main shader files cleaner, easier read, and makes maintenance way simpler. If you need to fix a bug or improve a function in your include file, you fix it once and all the shaders using it benefit. It's standard practice for any non trivial shader development.

Speaker 1

Smart Now, for something really complex, visually realistic fur or grass, how is that usually done? It seems impossible with just textures.

Speaker 2

It's tricky. One well known technique mentioned in the book is Lenk Eel's concentric fur shell technique.

Speaker 1

Technique sounds layered.

Speaker 2

It is, instead of trying to draw millions of individual hairs, which would destroy performance, you draw the base model multiple times, slightly enlarged each time.

Speaker 1

The same model just scaled up a bit.

Speaker 2

Yeah, usually pushed out along the vertex normals. Each shell or layer uses a special testure that has transparency, often combined with noise to simulate clumps of hair. The outermost shell is the most transparent, the inner one's less.

Speaker 1

So, so the layers build up to look like.

Speaker 2

Dense fur exactly when viewed together. These multiple semi transparent layers create the illusion of volume and density. You can even incorporate things like gravity, making the hairs droop slightly, or view direction effects. It's quite clever, but drawing the model multiple times can be expensive, so it needs careful tuning. Works for grass too, or other fuzzy surfaces.

Speaker 1

Fascinating technique. Okay, one last advanced topic using a Raisin shaders. The book mentions heat maps. Can you actually pass array data from C sharp to a shader easily?

Speaker 2

Directly? Passing a C sharp array with a single function call can be a bit awkward or limited in some Unity versions pipelines, though there are ways, like compute buffers. Now often you might pass array data element by element using multiple set float or set vector calls, or encode the data into a texture. But once the data is in the shader, seashee droop itself can work with a rays declared within the shader code.

Speaker 1

So for a heat map, how would that work?

Speaker 2

Imagine you want to show hot spots on a terrain map. You could pass the position and intensity the heat of several hot spots to the shader. Let's say you pass ten hot spot positions and ten intensity values inside the fragment shader for every pixel on the terrain, you would loop through that array of ten hot spots. For each hotspot you calculate its distance to the current pixel and figure out how much heat it contributes. Based on that distance and its intensity.

Speaker 1

Summing up the heat from all nearby.

Speaker 2

Hotspots exactly, you accumulate the total heat value for that pixel. Then you use that final heat value to look up a color on a gradient texture, a ramp map, maybe going from blue cold to red.

Speaker 1

Hot, and that colors the terrain pixel.

Speaker 2

Yep, it creates a visual overlay showing the heat distribution. The main challenge here, as the book points out, is performance. Looping through say fifty or one hundred points for every single pixel on the screen can get very slow, very quickly, so it requires careful optimization or limiting the number of influential points.

Speaker 1

Per pixel right That per pixel cost adds.

Speaker 2

Up fast, hugely fast.

Speaker 1

Wow, what a journey. We've really covered a lot from the absolute basics of what a shader even is through textures, lighting, the PBR revolution, deforming geometry with vertex shaders, screen wide effects optimization. It's incredibly deep, it really is.

Speaker 2

We've seen how shads are. This amazing blend of technical programming and artistry. They give developers the tools to define virtually any look imaginable, from hyperrealism to completely stylized visuals.

Speaker 1

And hopefully you listening now have a much deeper appreciation for what's going on under the hood. When you see those incredible visuals and games, you know how they craft that perfect metal sheen, or the look of rough stone, or even that stylized old film effect.

Speaker 2

It's a powerful toolkit enabling so much of the visual, storytelling and atmosphere we experience.

Speaker 1

So here's a final thought for you. Next time you're playing game, really look at the surfaces. Try to guess what's going on. Can you spot the telltale signs of normal mapping adding detail. Can you see how the speculator highlights change as you move? Could that cool water distortion be a grab pass? Is that enemy dissolving using a clip function driven by noise?

Speaker 2

Yes? Start dissecting the visuals and maybe even think what kind of cool effects could you imagine creating. Now knowing how these pieces

Speaker 1

Fit together, the possibilities with shaders really do feel limitless.

Transcript source: Provided by creator in RSS feed: download file
For the best experience, listen in Metacast app for iOS or Android