Latest / CyberSecurity Summary / Unity 5.x Shaders and Effects Cookbook
Transcript
- 0:00Speaker 1: You know that feeling when you're playing a game and
- 0:01Speaker 1: the graphics just pop, the way light catches the surface,
- 0:05Speaker 1: or those subtle reflections, maybe even that really distinct stylized look,
- 0:10Speaker 1: you know that makes a world feel real or sometimes
- 0:14Speaker 1: magically hand drawn. Well, a lot of that visual match
- 0:17Speaker 1: comes down to something called shads.
- 0:18Speaker 2: Absolutely, they're kind of like the secret sauce. Whden't you say?
- 0:21Speaker 2: They define how light interacts with everything you see exactly.
- 0:24Speaker 1: It turns simple three D models into something visually rich.
- 0:27Speaker 2: They're really the unsung heroes working away at the pixel level.
- 0:31Speaker 2: It's where art definitely meets code.
- 0:33Speaker 1: So today we're doing a deep dive into this fascinating
- 0:36Speaker 1: world Unity Shads and Effects. Right, we're drawing from a
- 0:40Speaker 1: really great resource, the Unity five point x Shaters and
- 0:43Speaker 1: Effects Cookbook. Our mission is basically to unpack how developers
- 0:48Speaker 1: use shader programming to really bring their Unity projects to life.
- 0:52Speaker 2: Yeah, and give you a shortcut to understanding what's behind
- 0:54Speaker 2: modern game visuals from you know, the basic building blocks
- 0:58Speaker 2: right up to some really mind ending real time effects.
- 1:01Speaker 1: And this cookbook the authors are top notch Alan Zuconi
- 1:05Speaker 1: develops thirty under thirty developer behind zero Arbitalis and.
- 1:09Speaker 2: Kenneth Lammers, He's got what over fifteen years in the industry,
- 1:13Speaker 2: worked on huge titles like Call of Duty three, Alan Wake.
- 1:16Speaker 1: So yeah, there definitely are expert guides for this for sure.
- 1:20Speaker 1: So if you've ever wondered how games get those stunning visuals,
- 1:23Speaker 1: how a simple cube can look like metal or wood,
- 1:26Speaker 1: or how an explosion just looks.
- 1:28Speaker 2: Right, then this deep dive is definitely for you. Get
- 1:31Speaker 2: ready for some aha moments.
- 1:33Speaker 1: Let's start right at the beginning. Then at its core,
- 1:35Speaker 1: what is a shader?
- 1:37Speaker 2: Okay? So think of it like a tiny program, a
- 1:40Speaker 2: specialized program that runs on your graphics card, okay, and
- 1:43Speaker 2: its job is to tell the graphics card how light
- 1:45Speaker 2: should behave on a surface. Imagine you have a plane
- 1:48Speaker 2: three D cube. Yep. The shader is what makes it
- 1:51Speaker 2: look like shiny metal or rough wood or maybe even glass.
- 1:55Speaker 2: It's not just slapping on a picture. It's simulating how
- 1:57Speaker 2: light interacts with that material.
- 1:59Speaker 1: A right, So it's about the light interaction, not just
- 2:02Speaker 1: the color. Makes sense, it's the visual identity exactly. But
- 2:05Speaker 1: how does this shader fit into, you know, the bigger
- 2:08Speaker 1: picture in unity the rendering process? What's the chain of command.
- 2:12Speaker 2: Good question. Let's break it down. So in Unity, everything
- 2:15Speaker 2: you see is basically a game object.
- 2:17Speaker 1: Right, characters, props everything.
- 2:20Speaker 2: Yeah, And for a three D game object to show up,
- 2:23Speaker 2: it usually needs a component called a mesh renderer. Think
- 2:26Speaker 2: of the mesh render like the stage manager deciding how
- 2:31Speaker 2: that three D model appears.
- 2:32Speaker 1: Okay, like deciding its costume and lighting pretty much.
- 2:35Speaker 2: Yeah, And that mesh render uses something called a material.
- 2:38Speaker 2: The material is like the specific recipe is its silk
- 2:40Speaker 2: is a burlap? Got it? And that material contains the shader.
- 2:43Speaker 2: The shader is the actual set of instructions defining how
- 2:46Speaker 2: light bounces off that silk or burlap. So the chain
- 2:49Speaker 2: is game object, mish renderer, material.
- 2:51Speaker 1: Shader, clear object renderer, material shader. And speaking of Unity,
- 2:55Speaker 1: Unity five brought a big change rate hebr oh.
- 2:57Speaker 2: Huge physically based rendering PBR that became the new standard.
- 3:01Speaker 1: What was so revolutionary about it? More realistic graphics?
- 3:04Speaker 2: Well, yes, but it was more than that. The real
- 3:07Speaker 2: insight was that PBR provided a consistent, physically plausible way
- 3:12Speaker 2: to describe materials. Consistent how meaning artists could define a material,
- 3:16Speaker 2: say brushed aluminum, and it would look correct under almost
- 3:20Speaker 2: any lighting condition morning sun, dim, indoor lights, whatever. No
- 3:24Speaker 2: more endless tweaking for every scene.
- 3:26Speaker 1: Ah okay, so it behaves predictably based on physics Exactly.
- 3:30Speaker 2: It was a massive step up from the older diffuse
- 3:33Speaker 2: and specular models in Unity four, which often needed a
- 3:36Speaker 2: lot more manual fiddling.
- 3:37Speaker 1: That sounds like a life saver for artists, and I
- 3:40Speaker 1: hear properties make it even easier for them.
- 3:42Speaker 2: What are those properties? Are Billiant. They're basically controls that
- 3:45Speaker 2: show up in the Unity Editor in the Materials Inspector tab.
- 3:48Speaker 1: Like sliders and color pickers.
- 3:49Speaker 2: Precisely, a shaded programmer writes a line of code like
- 3:52Speaker 2: color main, color color and boom, the artist get a
- 3:55Speaker 2: color swatch in the editor. Or roughness roughness range zero
- 3:59Speaker 2: one zero point gives them a slider, so the artists.
- 4:02Speaker 1: Can tweet the look, change the color, adjust roughness without
- 4:05Speaker 1: needing to code exactly.
- 4:06Speaker 2: That's the whole point. It separates the technical shader logic
- 4:09Speaker 2: from the artistic tweaking.
- 4:10Speaker 1: And crucially, where are the values for those properties stored
- 4:14Speaker 1: in the shader?
- 4:15Speaker 2: No, that's the key distinction. The properties are defined in
- 4:18Speaker 2: the shader, but there's specific values the chosen color the
- 4:22Speaker 2: slider setting are stored in the material.
- 4:24Speaker 1: Ah, so you can have one shader like metal, but
- 4:28Speaker 1: multiple materials using it, shiny steel, rusty iron, each with
- 4:32Speaker 1: different property values.
- 4:33Speaker 2: You got it. Change a property on the shiny steel
- 4:36Speaker 2: material and all objects using that material update instantly. Super flexible.
- 4:41Speaker 1: Okay, Now, for anyone brave enough to write their own shaders,
- 4:44Speaker 1: debugging sounds like fun. That magenta color, ah, the dreaded magenta.
- 4:48Speaker 2: Yeah, unlike C sharp scripts that just stop your game
- 4:50Speaker 2: shader errors, don't halt anything. Instead, your object turns bright unshaded.
- 4:55Speaker 1: Magenta, a very clear error message.
- 4:57Speaker 2: It certainly gets your attention. Commonness, the steaks are you know, typos,
- 5:01Speaker 2: missing semicolms forgetting a bracket or having a property defined
- 5:05Speaker 2: but not actually using it in the shader code. It
- 5:07Speaker 2: needs that link.
- 5:08Speaker 1: Good to know. Also heard something about floats in CIG
- 5:11Speaker 1: being slightly different.
- 5:12Speaker 2: Yeah, minor thing, but it catches people. In CIG the
- 5:16Speaker 2: shader language you just write one point zero, one point
- 5:19Speaker 2: oh f like you often do in Sea Shark, small detail,
- 5:22Speaker 2: big magenta headache if you forget.
- 5:24Speaker 1: Okay, so we've got the basics. Shaders tell light what
- 5:27Speaker 1: to do. Artists use materials and properties to tweak them.
- 5:30Speaker 1: But how do we get specific looks making something actually
- 5:34Speaker 1: look like wood or stone?
- 5:36Speaker 2: Right? That takes us into surface shaders, textures and lighting models.
- 5:39Speaker 2: This is where the real visual magic.
- 5:41Speaker 1: Happens surface shaders. What's the core idea there?
- 5:44Speaker 2: A surface shad basically has two main parts. First, there's
- 5:47Speaker 2: a surface function. In this function, you tell unity about
- 5:50Speaker 2: the physical properties of your surface, like it's base color, albedo,
- 5:54Speaker 2: how smooth or rough it is, smoothness if it's metallic, metallic, maybe,
- 5:59Speaker 2: how transparent?
- 6:00Speaker 1: And alpha okay, describing the material itself exactly.
- 6:03Speaker 2: Then all that information gets past to a lighting model.
- 6:06Speaker 2: The lighting model takes those surface properties and information about
- 6:08Speaker 2: the lights in your scene and calculates the final color
- 6:11Speaker 2: for each pixel.
- 6:12Speaker 1: So it combines the what it is with how it's
- 6:15Speaker 1: lit precisely.
- 6:16Speaker 2: And there are different structs or data packages for these properties,
- 6:19Speaker 2: like Surf's output standard for PBR. They hold things like albedo,
- 6:23Speaker 2: normal emission, metallic, smoothness, alpha, got it, and.
- 6:27Speaker 1: I understand c The shader language is pretty smart about
- 6:30Speaker 1: handling data pact rays.
- 6:32Speaker 2: Yeah, CG is really efficient. It uses things like float
- 6:35Speaker 2: three or float four. These packed rays. Think a float
- 6:37Speaker 2: three holding xyz for position or RGB for color. A
- 6:42Speaker 2: float four adds W or alpha.
- 6:44Speaker 1: So it groups related numbers together, right, and.
- 6:47Speaker 2: This lets you do cool stuff called swizzling. Like if
- 6:48Speaker 2: you have a color and a float four called color,
- 6:50Speaker 2: you can grab just the RGB parts really easily by
- 6:53Speaker 2: writing color dot RGB, or even rearrange them like color
- 6:56Speaker 2: dot bgr meat, trek, or even smearing. Assigning a single
- 6:59Speaker 2: value you like point five to a float three will
- 7:02Speaker 2: set all three components xyz two point five. It's all
- 7:05Speaker 2: about doing math on multiple values at once, which gpuser.
- 7:08Speaker 1: Great app Okay, back to textures. How do you get
- 7:10Speaker 1: a flat image, say bricks, onto a three D wall.
- 7:14Speaker 2: That's texture mapping. It relies on something called UV data
- 7:17Speaker 2: stored in the three D model itself.
- 7:19Speaker 1: UV data like coordinates.
- 7:21Speaker 2: Exactly each vertex corner point of your three D model
- 7:24Speaker 2: has two coordinates U and V, usually ranging from zero
- 7:28Speaker 2: to one. Think of it like flattening a three D
- 7:30Speaker 2: model onto a two D plane. These uvs tell the
- 7:34Speaker 2: shader exactly which part of the two D texture image
- 7:36Speaker 2: corresponds to which part of the three D model's.
- 7:39Speaker 1: Surface, and the GPU figures out the bits in between
- 7:41Speaker 1: the vertices.
- 7:42Speaker 2: Yep, it interpolates the uvs across the surface. Then the
- 7:45Speaker 2: shader uses a function usually text two D, to look
- 7:49Speaker 2: up or sample the color from the texture at that
- 7:51Speaker 2: specific UV coordinate for each pixel.
- 7:53Speaker 1: Clever? Can you animate textures this way like flowing water?
- 7:57Speaker 2: Absolutely? You can modify the UV coordinates over time. Just
- 8:00Speaker 2: add unities built in time variable or some fraction of
- 8:03Speaker 2: it to the U or V coordinate before sampling the texture. Presto.
- 8:07Speaker 2: Scrolling texture a classic way to fake movement.
- 8:10Speaker 1: Nice, But what about adding really fine detail, like the
- 8:13Speaker 1: tiny bumps on that brick wall where scratches on metal.
- 8:16Speaker 1: Adding tons of polygons would kill performance, right.
- 8:18Speaker 2: You definitely don'tant a model every single bump. That's where
- 8:21Speaker 2: normal mapping comes in. It's a fantastic technique.
- 8:23Speaker 1: How does that work? Is it another texture?
- 8:25Speaker 2: It is. It's a special texture, often purplish, called a
- 8:28Speaker 2: normal map or sometimes a bump map. But instead of
- 8:31Speaker 2: storing color, the RGB values in this texture actually represent
- 8:36Speaker 2: directions direction. Yeah, they represent the direction that the surface
- 8:39Speaker 2: normal is pointing at that specific pixel. The normal is
- 8:42Speaker 2: like an imaginary line pointing straight out from the surface.
- 8:46Speaker 2: By slightly changing the direction of this normal according to
- 8:48Speaker 2: the map, you trick the lighting calculation aw.
- 8:51Speaker 1: So the light bounces off as if there was a
- 8:53Speaker 1: bump there, even though the geometry is flat exactly.
- 8:56Speaker 2: It gives this incredible illusion of surface detail bumps, grooves,
- 8:59Speaker 2: rivet scratches without adding any extra polygons. It's super efficient.
- 9:03Speaker 2: Unity even provides a function unpacked normal to easily decode
- 9:07Speaker 2: the information from the normal map texture.
- 9:09Speaker 1: That's seriously cool. Okay, so normal maps fake detail on
- 9:13Speaker 1: solid surfaces. What about things that aren't solid, like glass
- 9:17Speaker 1: or ghosts or fire. Transparency sounds difficult.
- 9:21Speaker 2: It is surprisingly more complex than you might think. See
- 9:25Speaker 2: for solid objects, the graphics card does clever stuff like
- 9:28Speaker 2: z ordering, figuring out what's in front and only drawing that,
- 9:32Speaker 2: and culling like not drawing the back faces of objects
- 9:35Speaker 2: you can't see.
- 9:35Speaker 1: Makes sense, saves work.
- 9:37Speaker 2: But transparent objects mess that up. You need to see
- 9:40Speaker 2: what's behind them, so they need special handling. You use
- 9:44Speaker 2: shader tags to tell Unity how to handle them, like
- 9:46Speaker 2: setting que transparent tells Unity to draw them after all.
- 9:49Speaker 1: The solid stuff, so order matters a lot critically, and.
- 9:53Speaker 2: You need specific shader directives like hashtag, pregma, surface dot
- 9:57Speaker 2: alpha dot fade to enable blending so the it's parent
- 10:00Speaker 2: object mixes correctly with the background pixels.
- 10:02Speaker 1: Can you do stylized transparency too? I think the book
- 10:05Speaker 1: mentioned a hologram shader.
- 10:06Speaker 2: Oh yeah, that's a neat one. You can use a
- 10:08Speaker 2: mathematical operation called the dot product. You calculate the dot
- 10:11Speaker 2: product between the surface normal and the view direction.
- 10:14Speaker 1: Okay, what does that tell you?
- 10:15Speaker 2: It tells you how much a surface is facing towards
- 10:18Speaker 2: or away from the camera. Surfaces facing you get one value,
- 10:21Speaker 2: Surfaces facing away get another. You can then use this
- 10:24Speaker 2: to make only the edges or the parts facing you
- 10:26Speaker 2: slightly visible, creating that classic sci fi hologram rim.
- 10:30Speaker 1: Effect, just showing the outlines kind of yeah.
- 10:32Speaker 2: Or a faint semi transparent look. It's computationally cheap and
- 10:37Speaker 2: great for ghosts, force fields, selected object outlines, that kind
- 10:40Speaker 2: of thing.
- 10:41Speaker 1: All right, we're using all these textures, color normal maps,
- 10:44Speaker 1: maybe others. Textures take up memory, right, especially on mobile,
- 10:48Speaker 1: any tricks for optimizing that.
- 10:49Speaker 2: Big time textures are often a major memory hog. A
- 10:53Speaker 2: really common technique is texture packing, sometimes called channel.
- 10:56Speaker 1: Packing, packing like zipping files sort of.
- 10:59Speaker 2: But for data, many maps you need might only be grayscale,
- 11:03Speaker 2: like a roughness map or a metallic map, or an
- 11:05Speaker 2: ambient inclusion map. They only need one channel of.
- 11:08Speaker 1: Data instead of the usual three for RGB color exactly.
- 11:11Speaker 2: So you can take say your roughness map and put
- 11:13Speaker 2: it into the red channel of a texture. Take your
- 11:16Speaker 2: metallic map, put it in the green channel, maybe an
- 11:18Speaker 2: ambient inclusion map in the blue channel, and something else
- 11:21Speaker 2: in the alpha channel.
- 11:22Speaker 1: Ah. So one our GBA texture holds four different gray
- 11:26Speaker 1: scale maps.
- 11:26Speaker 2: You got it. It saves a ton of memory and
- 11:29Speaker 2: reduces the number of texture samples the shader needs to do.
- 11:32Speaker 2: Super common for character models and especially terrain. For terrain,
- 11:37Speaker 2: you might have one packed texture controlling how grass, dirt, rock,
- 11:41Speaker 2: and snow blend together using a function called LURP.
- 11:44Speaker 1: LURP linear interpolation.
- 11:46Speaker 2: Yeah, it smoothly blends between two values based on a
- 11:49Speaker 2: third value. So you could use the red channel of
- 11:51Speaker 2: your blend map to control the blend between dirt and grass,
- 11:54Speaker 2: the green channel for grass and rock, and so on.
- 11:57Speaker 2: Very efficient.
- 11:57Speaker 1: Okay, that makes sense. We mentioned lighting models earlier as
- 12:00Speaker 1: part of surface shaders. Why would a developer bother making
- 12:04Speaker 1: a custom lighting model. If Unity has good built in
- 12:06Speaker 1: ones like PBR, well, PBR.
- 12:08Speaker 2: Is fantastic for realism. But sometimes you don't want realism.
- 12:13Speaker 2: You want a specific artistic style. Maybe you want your
- 12:15Speaker 2: game to look like a comic book or an old
- 12:17Speaker 2: cartoon or something completely unique. Custom lighting models give you
- 12:21Speaker 2: that precise control over how light and shadow behave, letting
- 12:24Speaker 2: you define your own visual language.
- 12:26Speaker 1: So for non photorealistic rendering NPR.
- 12:30Speaker 2: Exactly, or even just for optimizing for very low end
- 12:33Speaker 2: platforms where PBR might be too expensive. Understanding the classic
- 12:36Speaker 2: models helps too.
- 12:37Speaker 1: Let's talk about those classics. What's Lambertune reflectants.
- 12:40Speaker 2: Lambertine was basically the default diffuse lighting model in Unity four.
- 12:45Speaker 2: It's super simple, very fast. It calculates brightness based only
- 12:49Speaker 2: on the angle between the light hitting the surface and
- 12:52Speaker 2: the surface normal itself.
- 12:53Speaker 1: So how directly the light hits it right, uses a.
- 12:55Speaker 2: Dot product for that. It results in a very matt
- 12:58Speaker 2: non shiny look. Thinks bull flat shading great for stylized
- 13:02Speaker 2: low poly games, very efficient.
- 13:03Speaker 1: And then the opposite end stylistically is maybe tune shading
- 13:06Speaker 1: or cell shading. That comic book look.
- 13:09Speaker 2: Yeah, totally different. Goal tune shading aims for that flat,
- 13:13Speaker 2: hand drawn cartoon style. The trick here is usually a
- 13:17Speaker 2: custom Whiting model that takes the smooth gradient of light
- 13:20Speaker 2: intensity you'd get from.
- 13:21Speaker 1: Lambursha makes it chunky exactly.
- 13:24Speaker 2: It uses something called a ramp map. It's usually a
- 13:26Speaker 2: small one D texture, like a thin strip with distinct
- 13:29Speaker 2: bands of color or brightness. The shader calculates the normal
- 13:33Speaker 2: light intensity often called n Dattel, the dot product of
- 13:37Speaker 2: normal and light direction. Uses that value to look up
- 13:41Speaker 2: a color on the ramp map, and poof, Instead of
- 13:43Speaker 2: a smooth gradient, you get sharp defined bands of color.
- 13:46Speaker 2: That's the cell shaded look.
- 13:48Speaker 1: Clever use of a texture to change the lighting result. Okay,
- 13:51Speaker 1: what about shininess speculat highlights, We've got pong blind fall right.
- 13:56Speaker 2: These are about adding those bright spots you see on
- 13:58Speaker 2: shiny objects. Is sort of a classic foundational model. It
- 14:02Speaker 2: calculates the reflection based on the angle between the direction
- 14:05Speaker 2: the camera is viewing from and the direction the light
- 14:07Speaker 2: would perfectly reflect off the surface, So.
- 14:09Speaker 1: It depends on where you're looking from very.
- 14:11Speaker 2: Much, so it's view dependent. It gives a decent shiny.
- 14:13Speaker 1: Look and Blindphong is it just better.
- 14:17Speaker 2: It's often preferred. Yeah, it's generally faster to calculate, and
- 14:20Speaker 2: in many cases looks a bit more realistic or at
- 14:22Speaker 2: least softer than basic fong. Instead of calculating the full
- 14:25Speaker 2: reflection vector, it uses a clever shortcut involving a half vector,
- 14:29Speaker 2: which is the vector halfway between the light direction and
- 14:31Speaker 2: the view direction. It was the default specular model and
- 14:34Speaker 2: unity for still very useful.
- 14:36Speaker 1: Okay, simple shininess covered. But then there's anisotropic speculator that
- 14:40Speaker 1: sounds complex. Brushed metal hair? How does that work?
- 14:44Speaker 2: Anisotropic is really cool. It simulates materials where the shininess
- 14:48Speaker 2: isn't uniform because the surface has tiny parallel grooves or
- 14:52Speaker 2: fibers I think brushed aluminum. The back of a CD
- 14:55Speaker 2: vinyl records even strands of hair.
- 14:58Speaker 1: So the highlight isn't just a round dot exactly.
- 15:00Speaker 2: Instead of a circular highlight, it stretches the specular highlight
- 15:03Speaker 2: perpendicularly to the direction of those tiny grooves or strands.
- 15:07Speaker 2: It requires extra information, usually from another texture or model data,
- 15:11Speaker 2: telling the shadeer which way the grain or fibers run
- 15:13Speaker 2: on the surface. It has a massive amount of realism
- 15:16Speaker 2: for those kinds of materials.
- 15:17Speaker 1: Wow, Okay, that's pretty specific control. Let's circle back to
- 15:22Speaker 1: PBR and unity five. You said it was a revolution.
- 15:25Speaker 1: What are the main workflows?
- 15:27Speaker 2: The two main ones are standard and standard a specular setup.
- 15:30Speaker 2: The most common one is the standard workflow, which uses
- 15:33Speaker 2: a metallic map.
- 15:35Speaker 1: Metallic workflow. How does that enforce realism?
- 15:39Speaker 2: It works based on a key physical principle. Things are
- 15:41Speaker 2: generally either metallic or non metallic dielectric. Pure metals get
- 15:45Speaker 2: almost all their color from reflected light. They have very
- 15:48Speaker 2: dark or black diffuse color. Their albedo map basically defines
- 15:52Speaker 2: the color of the reflection.
- 15:53Speaker 1: Okay, and non metals like plastic or wood.
- 15:57Speaker 2: Non metals have a colored diffuse component that's their albedo
- 16:00Speaker 2: map color, and their specular reflections are generally noncolored, reflecting
- 16:04Speaker 2: the color of the light source itself. The metallic map,
- 16:06Speaker 2: usually a grayscale texture packed new channel tells the shade
- 16:09Speaker 2: or how metallic. Each part of the surface is. Black
- 16:12Speaker 2: means non metal, white means pure metal, gray is somewhere
- 16:15Speaker 2: in between like dusty or corroded metal.
- 16:17Speaker 1: And smoothness is that packed in there too?
- 16:19Speaker 2: Often? Yes, the smoothness value controlling how sharp or blurry
- 16:24Speaker 2: reflections are is frequently packed into the alpha channel of
- 16:27Speaker 2: the metallic map texture. So one map controls both metalness
- 16:30Speaker 2: and smoothness.
- 16:32Speaker 1: It's efficient PBR in transparency. How does that work? Does
- 16:35Speaker 1: it handle glass and ghosts differently?
- 16:38Speaker 2: It does. PBR offers specific modes. Transparent mode is for
- 16:41Speaker 2: things like proper glass or clear plastics. Crucially, it keeps
- 16:45Speaker 2: all the PBR goodness speculat highlights, reflections, Fresnel effect where
- 16:49Speaker 2: reflectivity changes with the young angle. It looks physically correct.
- 16:53Speaker 1: Okay, what about ghosts?
- 16:55Speaker 2: For that, you'd probably use fade mode. In fade mode,
- 16:57Speaker 2: as the object becomes more transparent, everything fades out, including
- 17:00Speaker 2: the specular highlights and reflections. Perfect for effects where things
- 17:03Speaker 2: need to dissolve completely.
- 17:05Speaker 1: And the third one cut out.
- 17:06Speaker 2: Cutout mode is for things that are geometrically solid but
- 17:09Speaker 2: need to look like they have holes, like chain link fences,
- 17:12Speaker 2: leaves on a tree, or detailed grates. It uses the
- 17:15Speaker 2: alpha channel of the texture, but it's not blended transparency.
- 17:18Speaker 2: It's either fully opaque or fully invisible based on an
- 17:21Speaker 2: alpha cutoff slider, sharp edges, no semi transparency.
- 17:25Speaker 1: Got it makes sense for foliage. Now realistic reflections like
- 17:30Speaker 1: mirrors or shiny floors. You mentioned reflection probes right.
- 17:34Speaker 2: Real time reflections are super expensive, So reflection probes are
- 17:37Speaker 2: a way to capture the environment around a certain point.
- 17:40Speaker 2: Think of it like taking a three hundred and sixty
- 17:42Speaker 2: degree photo from that spot and storing it as a
- 17:45Speaker 2: special texture called a cube map.
- 17:47Speaker 1: Six images, one for each direction.
- 17:49Speaker 2: Exactly up, down, left, right, front, back. Then shiny objects
- 17:53Speaker 2: near that probe can sample this cube map to show
- 17:56Speaker 2: reflections of the surrounding static scenery.
- 17:58Speaker 1: Are they expensive?
- 17:59Speaker 2: They can be, especially if they need to update in
- 18:01Speaker 2: real time, like if dynamic objects need to be reflected.
- 18:05Speaker 2: Often they're baked, meaning they capture the static environment once
- 18:08Speaker 2: in the editor, which is much cheaper at run time.
- 18:10Speaker 2: They're crucial for making metallic PBR materials look believable.
- 18:14Speaker 1: Speaking of baking, light baking, that sounds like pre calculating lighting.
- 18:18Speaker 2: That's exactly what it is. Calculating how light bounces around
- 18:21Speaker 2: realistically in real time. Global illumination or GI is incredibly demanding,
- 18:27Speaker 2: So light baking lets unity pre calculate all that complex
- 18:31Speaker 2: light bouncing for objects that don't move. Your buildings terrain static.
- 18:34Speaker 1: Props and saves that lighting information.
- 18:36Speaker 2: Yeah, saves it into special textures called light maps. At runtime,
- 18:40Speaker 2: these static objects just look up their pre calculated lighting
- 18:43Speaker 2: from the light map. It looks great and is super
- 18:45Speaker 2: fast compared to calculating GI every frame.
- 18:48Speaker 1: But what about things that do move your player character NPCs,
- 18:52Speaker 1: moving objects, They can't use static lightmaps.
- 18:55Speaker 2: Correct for them. We use light probes. You scatter these
- 18:59Speaker 2: probes around your scene, especially where lighting conditions change. Like
- 19:03Speaker 2: light maps, they get baked. They sample the complex bounced
- 19:06Speaker 2: lighting at the specific locations during the bake.
- 19:08Speaker 1: Process, so they store the lighting info at points in
- 19:11Speaker 1: space exactly.
- 19:12Speaker 2: Then, as your character or a dynamic object moves through
- 19:15Speaker 2: the scene, it looks at the nearest light probes, blends
- 19:19Speaker 2: the lighting information sampled by them, and uses that to
- 19:22Speaker 2: light itself. This allows moving objects to feel realistically integrated
- 19:26Speaker 2: into the baked static environment. It's a clever way to
- 19:30Speaker 2: get the best of both worlds, detailed lighting on static
- 19:33Speaker 2: stuff dynamic objects that still fit in.
- 19:36Speaker 1: It's all about balancing that realism and performance budget always.
- 19:39Speaker 2: That's the name of the game.
- 19:40Speaker 1: In real time graphics, okay, shifting gears a bit, shaders
- 19:43Speaker 1: aren't just about color and light, right. They can actually
- 19:46Speaker 1: change the shape of objects using vertex functions.
- 19:50Speaker 2: Absolutely. This is where shaters get really powerful beyond just
- 19:53Speaker 2: surface appearance. A Vertex function runs for every vertex or
- 19:56Speaker 2: corner point of your three D model before it even
- 19:59Speaker 2: gets render, and inside this function you can actually change
- 20:02Speaker 2: the vertex's position in three D.
- 20:04Speaker 1: Space, So you can literally move the points of the
- 20:07Speaker 1: model around with code.
- 20:08Speaker 2: Yep, you can deform the geometry. For example, you could
- 20:11Speaker 2: access vertex colors. Did you know you can paint colors
- 20:14Speaker 2: directly onto the vertices of a model in many three
- 20:16Speaker 2: D modeling tools.
- 20:17Speaker 1: I didn't realize that.
- 20:19Speaker 2: Yeah, And you can read those colors in the vertex
- 20:21Speaker 2: shader and use the drive effects maybe make parts glow
- 20:24Speaker 2: or control how much wind affects certain parts of a mesh.
- 20:27Speaker 1: Speaking of wind, can you animate vertices like a waving flag?
- 20:31Speaker 2: Totally? That's a classic use case. Instead of complex physics
- 20:34Speaker 2: or bone animations, you can use a simple sine wave
- 20:36Speaker 2: based on time time and the vertex's position to make
- 20:40Speaker 2: vertices move up and down or back and forth, creating
- 20:43Speaker 2: a realistic waving flag or rippling water effect directly in
- 20:47Speaker 2: the shader.
- 20:48Speaker 1: That's really efficient. What's this normal extrusion technique? It sounds weird,
- 20:53Speaker 1: making things chubbier or skinnier.
- 20:55Speaker 2: It is a bit weird, but very effective. You take
- 20:58Speaker 2: the vertex position and push it outwards or inwards along
- 21:01Speaker 2: its normal direction, that line pointing straight out from the surface.
- 21:04Speaker 1: So everything expands or contracts along its surface direction.
- 21:07Speaker 2: Right, you can make a character look inslated or deflated,
- 21:10Speaker 2: and you can control how much extrusion happens using a
- 21:14Speaker 2: texture map an extrusion map, so you could make certain
- 21:17Speaker 2: parts puff out and others sink in.
- 21:18Speaker 1: And the book mentions zombifying characters.
- 21:21Speaker 2: Yeah, that's a great example. You could use an extrusion
- 21:24Speaker 2: map to make the skin sync in over the bones,
- 21:26Speaker 2: giving that gaunt, skeletal look, or for something less gruesome.
- 21:30Speaker 2: Snow shaders often use it. They might detect which triangles
- 21:33Speaker 2: face upwards, color them white, and then extrude them slightly
- 21:36Speaker 2: along their normal to simulate snow piling up.
- 21:39Speaker 1: Wow and explosions.
- 21:41Speaker 2: Vertex shads are key from many volumetric effects too. For
- 21:44Speaker 2: an explosion, you might start with a sphere mesh, then
- 21:47Speaker 2: use noise textures and time in the vertex shader. To
- 21:50Speaker 2: displace the vertices outwards in a chaotic expanding way. Combine
- 21:54Speaker 2: that with a clip function in the fragment shader to
- 21:56Speaker 2: make parts dissolve, and you get a convincing three D
- 21:58Speaker 2: fireball effect without complex fluid simulations.
- 22:02Speaker 1: Incredible control. Okay, beyond surface shaders, there are vertex and
- 22:06Speaker 1: fragment shaders. These give even more control they do.
- 22:09Speaker 2: Surface shators are actually a higher level abstraction that Unity provides,
- 22:12Speaker 2: making common tasks easier. Under the hood, they get compiled
- 22:15Speaker 2: down into lower level vertex and fragment shaders. Anyway, writing
- 22:19Speaker 2: vertex and fragment shaders directly gives you complete control over
- 22:22Speaker 2: both stages.
- 22:22Speaker 1: Vertext processes geometry, Fragment processes.
- 22:25Speaker 2: Pixels almost it processes fragments, which are potential pixels. A
- 22:30Speaker 2: fragment has color, depth, position, etc. Before potentially gets written
- 22:34Speaker 2: to the screen. But yet, think of the fragment shader
- 22:37Speaker 2: as running for every pixel being rendered for that object.
- 22:40Speaker 1: And this is where the grab pass comes in. That
- 22:43Speaker 1: sounds powerful, It's.
- 22:44Speaker 2: Very powerful for screen effects. A grab pass is a
- 22:47Speaker 2: special command you put in your shader. It tells Unity,
- 22:50Speaker 2: before you draw this object, grab whatever has already been drawn.
- 22:54Speaker 2: To the screen in this area and put it into
- 22:56Speaker 2: a texture for me.
- 22:57Speaker 1: So it gets a snapshot of the background.
- 22:59Speaker 2: Exactly, usually into a texture named grab texture. Now in
- 23:02Speaker 2: your fragment shader for the current object, say a piece
- 23:05Speaker 2: of glass, you can read from that grab texture and
- 23:07Speaker 2: distort it precisely. You can sample the grab texture using
- 23:11Speaker 2: modified UV coordinates. For glass, you might offset the uvs
- 23:15Speaker 2: based on a normal map to simulate refraction. For water,
- 23:18Speaker 2: you might use an animated noise texture to make the
- 23:21Speaker 2: UV's wiggle, creating that shimmering distortion effect. It makes the
- 23:25Speaker 2: object look like it's realistically interacting with the scene behind it.
- 23:28Speaker 1: That's how you get those really convincing glass or heat
- 23:31Speaker 1: haze effects.
- 23:32Speaker 2: Yep. Grab pass is the key.
- 23:34Speaker 1: Okay, amazing visuals, but we need performance, especially on mobile.
- 23:37Speaker 1: How do you optimize shaders make them cheaper?
- 23:40Speaker 2: Several ways. A big one is data precision. GPUs can
- 23:44Speaker 2: work with different levels of floating point precision. Full float
- 23:47Speaker 2: thirty two bit is most precise, but slowest. Half sixteen
- 23:52Speaker 2: bit is less precise, but faster, often perfectly fined for colors,
- 23:56Speaker 2: texture coordinates, maybe even normals fixed. Often eleven bit is
- 23:59Speaker 2: even less precise, but the fastest good for simple color
- 24:03Speaker 2: calculations are things that don't need high accuracy.
- 24:05Speaker 1: So using smaller variable types saves time and memory definitely.
- 24:08Speaker 2: Another trick is the hashtag pragma surface no a ford
- 24:11Speaker 2: Ad directive. Normally Unity might try to calculate lighting for
- 24:14Speaker 2: multiple lights hitting an object in the forward rendering path,
- 24:17Speaker 2: adding complexity. No forward DAD tells Unity only calculate the
- 24:21Speaker 2: brightest directional light per pixel. Handle all other lights more cheaply,
- 24:25Speaker 2: maybe pervertex. Big performance win in scenes with many lights.
- 24:28Speaker 1: Good tip any others.
- 24:29Speaker 2: Sharing texture coordinates uves if multiple textures on your material
- 24:33Speaker 2: use the same mapping, reducing the number of texture lookups
- 24:36Speaker 2: overall using those texture packing techniques you talked about. It's
- 24:40Speaker 2: often about reducing memory, bandwidth and calculation complexity.
- 24:43Speaker 1: With all these shaders potentially running, how do you figure
- 24:47Speaker 1: out which one is causing a slow down if your
- 24:49Speaker 1: game starts lagging?
- 24:50Speaker 2: AH. The profiler Unity's built in profiler c TROL plus
- 24:55Speaker 2: seven or CMD plus seven on Mac is your best
- 24:58Speaker 2: friend here?
- 24:58Speaker 1: What does it show you?
- 25:00Speaker 2: Everything? It shows you frame by frame, what your CPU
- 25:03Speaker 2: and GPU are spending time on. You can see rendering
- 25:06Speaker 2: stats like draw calls, how many separate things are being
- 25:09Speaker 2: drawn triangle counts, and crucially, how much time is being
- 25:12Speaker 2: spent rendering different objects and executing their shaders.
- 25:15Speaker 1: So you can spot the expensive ones exactly.
- 25:18Speaker 2: You can pause the game, scrub through the timeline, find
- 25:21Speaker 2: a performance spike, and drill down. The profiler will often
- 25:25Speaker 2: list rendering tasks and you can see which materials and
- 25:28Speaker 2: therefore which shaders are taking up the most GPU time.
- 25:31Speaker 2: It's essential for optimization.
- 25:32Speaker 1: Okay, essential tool. Now what if you want to change
- 25:36Speaker 1: the look of the entire game, like add a film
- 25:38Speaker 1: grain effect or adjust overall contrast without editing hundreds of materials.
- 25:44Speaker 2: That's where screen effects or post processing effects are used.
- 25:48Speaker 2: They're incredibly powerful for applying a final artistic touch to
- 25:51Speaker 2: the whole scene.
- 25:52Speaker 1: How do they work? Do they use? Grab pass?
- 25:54Speaker 2: Conceptually similar, but usually more optimized. The typical flow is
- 25:58Speaker 2: Unity renders your entire normally. Instead of drawing it directly
- 26:02Speaker 2: to the screen, it draws it into an off screen
- 26:04Speaker 2: buffer called a render texture.
- 26:06Speaker 1: Like an image of the final frame exactly.
- 26:09Speaker 2: Then a special camera with a post processing shader attached
- 26:12Speaker 2: simply draws a full screen quad a plat rectangle using
- 26:16Speaker 2: that render texture as its input. The shader on this
- 26:19Speaker 2: quad can then modify every single pixel of the final
- 26:21Speaker 2: image before it's shown to you.
- 26:23Speaker 1: Ah, so it's like applying a Photoshop filter to the
- 26:25Speaker 1: whole game image.
- 26:26Speaker 2: That's a perfect analogy. You can do brightness, saturation, contrast
- 26:31Speaker 2: adjustments using formulas involving luminance. You can tint the image,
- 26:35Speaker 2: apply Vignett's film grain distortions. Anything you can code in
- 26:39Speaker 2: a shader you can apply to the entire screen.
- 26:41Speaker 1: Can you blend images to like overlaying textures.
- 26:44Speaker 2: Yep. You can sample other textures in your post processing
- 26:47Speaker 2: shader and blend them with the scenes render texture using
- 26:50Speaker 2: standard blend modes. Multiply for darkening, ad or screen for brightening,
- 26:54Speaker 2: overlay for complex contrast. Tons of possibilities for achieving specific
- 26:59Speaker 2: moods and stuff.
- 27:00Speaker 1: So these screen effects are huge for immersion and cinematic feel.
- 27:05Speaker 1: What are some classic examples?
- 27:06Speaker 2: Oh loads, an old movie effect is great when you
- 27:09Speaker 2: combine several things, maybe a CPA tone filter to make
- 27:12Speaker 2: it brownish, a vignette to darken the corners, maybe some
- 27:14Speaker 2: noise or film grain, and then overlay animated dust and
- 27:18Speaker 2: scratches textures using time since time perhaps and random offsets
- 27:22Speaker 2: to make them flicker and shift instantly. Gives that vintage vibe.
- 27:24Speaker 1: Yeah, I can picture that. What about something like night vision?
- 27:27Speaker 2: Another classic usually involves tinting the whole screen green, maybe
- 27:31Speaker 2: boosting brightness or contrast in a nonlinear way, adding scan lines,
- 27:36Speaker 2: thin horizontal dark lines, some static noise, maybe barel distortion
- 27:40Speaker 2: like looking through a lens, and perhaps limiting the color palette.
- 27:43Speaker 2: Put it all together and you get that familiar military
- 27:46Speaker 2: or sci fi night vision look. These effects really sell
- 27:49Speaker 2: the feeling of looking through a specific device or being
- 27:52Speaker 2: in a particular state.
- 27:53Speaker 1: Totally changes the experience. Okay, we're getting into advanced territory
- 27:58Speaker 1: for developers writing lots of shaders. Is there a way
- 28:00Speaker 1: to make things more modular reusable?
- 28:02Speaker 2: Get? Yes, definitely. Unity uses cg include files dot CGC
- 28:08Speaker 2: extensively itself, and you can create your own.
- 28:10Speaker 1: Cg include like header files and c plus plus.
- 28:13Speaker 2: Exactly like that. You put common functions, maybe your custom
- 28:15Speaker 2: lighting calculations, helper functions for noise, complex math routines into
- 28:19Speaker 2: a dot c gene file. Then in your main shader
- 28:22Speaker 2: files you just hashtag include that file.
- 28:24Speaker 1: And you can reuse that code across many shaders YEP.
- 28:28Speaker 2: It keeps your main shader files cleaner, easier read, and
- 28:32Speaker 2: makes maintenance way simpler. If you need to fix a
- 28:35Speaker 2: bug or improve a function in your include file, you
- 28:37Speaker 2: fix it once and all the shaders using it benefit.
- 28:41Speaker 2: It's standard practice for any non trivial shader development.
- 28:44Speaker 1: Smart Now, for something really complex, visually realistic fur or grass,
- 28:50Speaker 1: how is that usually done? It seems impossible with just textures.
- 28:54Speaker 2: It's tricky. One well known technique mentioned in the book
- 28:57Speaker 2: is Lenk Eel's concentric fur shell technique.
- 29:00Speaker 1: Technique sounds layered.
- 29:01Speaker 2: It is, instead of trying to draw millions of individual hairs,
- 29:05Speaker 2: which would destroy performance, you draw the base model multiple times,
- 29:08Speaker 2: slightly enlarged each time.
- 29:10Speaker 1: The same model just scaled up a bit.
- 29:12Speaker 2: Yeah, usually pushed out along the vertex normals. Each shell
- 29:16Speaker 2: or layer uses a special testure that has transparency, often
- 29:19Speaker 2: combined with noise to simulate clumps of hair. The outermost
- 29:22Speaker 2: shell is the most transparent, the inner one's less.
- 29:24Speaker 1: So, so the layers build up to look like.
- 29:26Speaker 2: Dense fur exactly when viewed together. These multiple semi transparent
- 29:30Speaker 2: layers create the illusion of volume and density. You can
- 29:34Speaker 2: even incorporate things like gravity, making the hairs droop slightly,
- 29:37Speaker 2: or view direction effects. It's quite clever, but drawing the
- 29:41Speaker 2: model multiple times can be expensive, so it needs careful tuning.
- 29:44Speaker 2: Works for grass too, or other fuzzy surfaces.
- 29:47Speaker 1: Fascinating technique. Okay, one last advanced topic using a Raisin shaders.
- 29:53Speaker 1: The book mentions heat maps. Can you actually pass array
- 29:56Speaker 1: data from C sharp to a shader easily?
- 29:59Speaker 2: Directly? Passing a C sharp array with a single function
- 30:02Speaker 2: call can be a bit awkward or limited in some
- 30:04Speaker 2: Unity versions pipelines, though there are ways, like compute buffers.
- 30:08Speaker 2: Now often you might pass array data element by element
- 30:11Speaker 2: using multiple set float or set vector calls, or encode
- 30:14Speaker 2: the data into a texture. But once the data is
- 30:17Speaker 2: in the shader, seashee droop itself can work with a
- 30:19Speaker 2: rays declared within the shader code.
- 30:21Speaker 1: So for a heat map, how would that work?
- 30:24Speaker 2: Imagine you want to show hot spots on a terrain map.
- 30:26Speaker 2: You could pass the position and intensity the heat of
- 30:29Speaker 2: several hot spots to the shader. Let's say you pass
- 30:31Speaker 2: ten hot spot positions and ten intensity values inside the
- 30:34Speaker 2: fragment shader for every pixel on the terrain, you would
- 30:37Speaker 2: loop through that array of ten hot spots. For each
- 30:40Speaker 2: hotspot you calculate its distance to the current pixel and
- 30:43Speaker 2: figure out how much heat it contributes. Based on that
- 30:46Speaker 2: distance and its intensity.
- 30:47Speaker 1: Summing up the heat from all nearby.
- 30:49Speaker 2: Hotspots exactly, you accumulate the total heat value for that pixel.
- 30:53Speaker 2: Then you use that final heat value to look up
- 30:56Speaker 2: a color on a gradient texture, a ramp map, maybe
- 31:00Speaker 2: going from blue cold to red.
- 31:02Speaker 1: Hot, and that colors the terrain pixel.
- 31:05Speaker 2: Yep, it creates a visual overlay showing the heat distribution.
- 31:08Speaker 2: The main challenge here, as the book points out, is performance.
- 31:11Speaker 2: Looping through say fifty or one hundred points for every
- 31:14Speaker 2: single pixel on the screen can get very slow, very quickly,
- 31:18Speaker 2: so it requires careful optimization or limiting the number of
- 31:21Speaker 2: influential points.
- 31:22Speaker 1: Per pixel right That per pixel cost adds.
- 31:24Speaker 2: Up fast, hugely fast.
- 31:25Speaker 1: Wow, what a journey. We've really covered a lot from
- 31:28Speaker 1: the absolute basics of what a shader even is through textures, lighting,
- 31:32Speaker 1: the PBR revolution, deforming geometry with vertex shaders, screen wide
- 31:36Speaker 1: effects optimization. It's incredibly deep, it really is.
- 31:40Speaker 2: We've seen how shads are. This amazing blend of technical
- 31:43Speaker 2: programming and artistry. They give developers the tools to define
- 31:47Speaker 2: virtually any look imaginable, from hyperrealism to completely stylized visuals.
- 31:52Speaker 1: And hopefully you listening now have a much deeper appreciation
- 31:55Speaker 1: for what's going on under the hood. When you see
- 31:58Speaker 1: those incredible visuals and games, you know how they craft
- 32:01Speaker 1: that perfect metal sheen, or the look of rough stone,
- 32:03Speaker 1: or even that stylized old film effect.
- 32:06Speaker 2: It's a powerful toolkit enabling so much of the visual,
- 32:09Speaker 2: storytelling and atmosphere we experience.
- 32:11Speaker 1: So here's a final thought for you. Next time you're
- 32:14Speaker 1: playing game, really look at the surfaces. Try to guess
- 32:17Speaker 1: what's going on. Can you spot the telltale signs of
- 32:20Speaker 1: normal mapping adding detail. Can you see how the speculator
- 32:23Speaker 1: highlights change as you move? Could that cool water distortion
- 32:26Speaker 1: be a grab pass? Is that enemy dissolving using a
- 32:29Speaker 1: clip function driven by noise?
- 32:31Speaker 2: Yes? Start dissecting the visuals and maybe even think what
- 32:35Speaker 2: kind of cool effects could you imagine creating. Now knowing
- 32:38Speaker 2: how these pieces
- 32:39Speaker 1: Fit together, the possibilities with shaders really do feel limitless.