Transcription complète de « Physically Based Cel Shading ». Cliquez sur un horodatage pour ouvrir la vidéo à ce moment précis. Besoin d'une autre vidéo ? Utilisez le générateur de transcription YouTube.
0:00 this video final result can be downloaded by following the link in the description I will be crystal clear it is not possible to obtain a good non photorealistic result in a real engine because of its structure and rendering pipeline every attempt in that direction inevitably translates to a collection of workarounds that pile up onto each other inexorably leading to complete chaos that said let's see how to obtain a shading in a real engine like a proper one without breaking any physically based lighting rules but before doing that let's have a quick look at how it's usually T in basically every other tutorial on YouTube and why that approach does not
0:48 work since a real engine follows a the farad rendering pipeline the only place that makes sense to manipulate lighting data in is the post-process stage I agree on that with everyone else and that's also the first reason why this engine is not well suited for this kind of job by default basically at the post-process stage all the rendering has already happened including the lighting calculations so we have no way of individually controlling how each source of light is applied to the surfaces for example we're forced to deal with the complete final result as one single entity and that already restraints a lot the amount of things we can do but to be fair we can still do many things so let's create a new material let's assign it to an Unbound postprocess volume
1:35 let's set it as a postprocess material and let's make it run before the tone mapper or during s color after off apparently if you switch to 5.4 now we can start let's take a Sy texture node and let's set it to postprocess input zero this is needed to display the render image and now let's start following the most common steps done by most of the other tutorials let's duplicate the syntax to node and this time let's set it to diffuse color let's desaturate both of them and let's divide the postprocess input Zero by the diffuse this operation the tend to say Returns the brightness value of the scene what I can tell you is that at this point you already compromised the image and from now on it will become a
2:21 matter of adding patches to work around problems but let's keep going forward what's done from now on starts to diverge between tutorials as it starts to come into play what the specific person wants to achieve but more or less we could say that it's always some form of this we add an if node to the graph and we Define a brightness threshold that we'll use to either pass through the diffuse color as it is or a darker version of it and that returns a very good self shading feeling image and if you want more bands we can concatenate more reefs each with a different threshold and a different multiplier for the diffuse pass there you go here is the shading in a real but let's give it a closer look the sky is black well
3:08 sadly this will be a problem with every approach we take as its data is not included in the J buffers used by the rest of the scene the most common way to fix it that I've seen is to create a mask based on dep to put back there the original image Sky metals are black this is because the diffuse pass does not contain their color information it has internally been moved from base colar to specular this step has been now made explicit by substrate epic's new material pipeline I have a video that goes through this stuff maybe you want to give it a look anyways this problem can be avoided by simply replacing the diffuse pass with the base color one lights lose their
3:56 color that's because we are desaturating our passes at the very beginning hence removing the color information which will introduce afterwards through the base color pass which knows nothing about lights the only tutorial I happen to find that tried to solve this issue is prismatica devs one his solution is to desaturate again the image to then multiply it by the normalized postprocess input zero pass what he says about that is that the normalization returns Pure Color with without any brightness variance so it can be used to reintroduce all the correct colors into the self- Shaded image that after the the saturation only contains brightness information well
4:41 kind of explaining this thing properly would require a whole dedicated video but I can give you a little overview this normalization is almost the correct operation needed to obtain the Pure Color information aka the Hue of the color the problem is that the normalization sets the length of a vector to one confining its values on the surface of a unit sphere in case of colors which can only be positive we are confining them on a height of a sphere and if you imagine the three axes to be the RGB color components you can see that we're not covering the full spectrum it is possible to obtain the primary colors but not all the other ones not even Pure White which comes out
5:28 as a light gray the RGB color space is actually cubic not spherical so pure colors lie on a unit cube faces the correct operation in this case is to find the maximum RGB color component and use that to divide the color moreover there's another error can you see how the processed image is still darker than the original one this will throw off all your exposure light and and texture values and make them susceptible to changes to the post process dangerous the culprit of this is this the saturation it's used like at the beginning to calculate the brightness of
6:13 the image once again this is not quite the correct operation to do the desaturation doesn't return the true brightness of a color it weights differently the RGB components to return something as close as possible to the humaniz perceived bright Ness and that's why the green is considered the most bright followed by red and blue is the darkest one because our eyes are sensible to those wavelengths of light in that way more or less and by artificially making some colors darker or lighter the black and white result is better perceived as matching its colored counterpart but in this case we need to be mathematically accurate if we consider the case in which we only have pure colors we should will get the
7:00 brightness value of one for all of them regardless of the Hue once again finding the highest component among r g and B is the solution then why did the saturations at the beginning work they do just for a lucky coincidence the two errors caused by the two the saturations nullify each other in the division that comes afterwards let's swap them anyways with the ma of the components as it spares few instructions to hard Cod the thresholds will jeopardize the flexibility of your lighting if you make the environment brighter or darker your thresholds will move back and forth and won't be valid
7:47 anymore so either you add a ton of them or you find a way to organically move them along with the intensity of your lights I could carry on but hopefully by now you understood that this approach is full of fallacies even after fixing some issues there are always more behind the corner and trying to work around all of them would result in a huge ende deir of growing complexity as the patches start to pile up so let's leave only the properly working parts and let's try a better approach or how is it if we want to make a sell Shader in a real without dabbing into its source code and basically rewriting all of it there is one hard constraint that we're not allowed to break our work must stick to the
8:33 physically based rules so the first step will of course be properly setting up lights and on mopper I'll briefly explain the motivations behind the setup I'll walk you through but I won't repeat myself on why and how things work better in that way if you're interested in the topic in my channel you can find a series of videos specifically dedicated to Lighting in video games in which I concept is exposed by someone that knows way more than I do about this stuff in fact a big part of what you'll see me doing in this video is an application of the new knowledge I gained by working on those videos so let's start from the Sun 120,000 lucks and Source angle at zero since for Al Shader we want our shadows
9:20 to be super sharp let's now go to the postprocess volume and let's go with order let's remove the bloom to have a cleaner image we can use it of course and to be honest if I had to use it I would use the convolution given its clear superiority but to be honest it's just a disturbing element at visual development stage so off it goes let's set the correct exposure for a daytime with a clear sky like the one we have now no lens flares no vignette since like the blue they only dirt our image for now let's reduce to a minimum the blue correction so that the blue tones pass through with proper vibrancy yeah at the moment is quite subtle but later it will become more evident I'll Point again at that so
10:06 you can see better what I mean now the most important part setting up the tone mopper if you follow the videos on lighting you'll recognize the values I'm about to set slope at 0.7 toes at 0.3 white clip at zero the aim is to obtain a dull low contrast low saturation image this is normally a friction point in a production context as people that don't know how images work just see a boring outcome and they start to complain at bug onjira I send you task to make a better image and panic spreads among producers and managers while who knows about photography Cinema and video making finally realizes that even in video games we can work professionally on images what you're seeing now is the
10:51 equivalent of a row shot with a properly set professional camera it looks like that because we're compressing in the captured image as much data as possible in order to maximize the freedom we have in postproduction I can give you an immediate example of this let's go to the color grading tab under Global and let's set the contrast to 1.25 and the saturation to two and now our colors become as we would have expected moreover it's now also possible to observe better the effect of the blue correction you know what greens are coming across a bit too extreme let's reduce their duration all right let's continue on the list of parameters in the postprocess volume and we each Lumen now we have a choice to make remove all
11:38 GI and Reflections all together or keep them in fight against performance issues and artifacts the result you can get without them is very good to be honest you have more separation between colors and shapes and for a tun image I think it works very well on the other hand with Lumen everything will look more organic mechanically integrated in the scene keep in mind though that you'll likely need to use higher quality settings than usual to suppress as much as possible its artifacts which will become very appearent and annoying thanks to the quantization of light made by the S shading so your choice in this tutorial I'll keep luming on because it will give me a clearer way to later show off a thing that I'll do in the Shader but
12:23 other than that if you disable it you'll still be totally able to follow this video in its entirety so as I said let me raise Lum and's quality settings the only thing I'll set to a minimum is the update speed as it will make the cell shading bands derived from the GI Luminosity wobble less and we are done with the postprocess volume setup just in case you decided to disable Lumen it's worth mentioning that this enables the screen space ambient occlusion it can be useful to tun it up to give some more volume to the scene but you can disable also that to reach an even flut look o are we finally ready to dive into the Shader not yet as you'll surely know by now perfectly setting up the lights of a scene without also following up
13:10 with a proper material pipeline for the props leads to disaster anyways so let's have a quick overview of the self-imposed rules I've come up with in this project to have consistent results that work well with the tone mapper and postprocess they are a bit different and more restrictive than the usual PBR guidelines I may say for all these test meshes I'm using this single test material that I've set up with a bunch of parameters for base color specular roughness and metallic all of them are set as custom primitive data so I'm able to change them on individual meshes instead of creating a ton of material instances the let's say default setup
13:56 for a material that works well with a sell shaded look is specular at zero roughness at one and metallic at zero so it becomes pure diffusion and it's shaded only based on light Direction without any reflection or highlight that moves relatively to the camera an hard constraint that I've imposed to the Assets in this scene is that their base color saturation must be capped at 0.5 this is yes to ensure a better rooking image out of the tone mapper but mainly because I saw that with values lower than that we start to lose pieces of indirect light contribution look at this red gimly for example if I saturate it more than that I completely lose the bluish contribution coming from the Skylight
14:43 resulting in a more boring result and more than anything else it would then be consistent across the scene as other objects with less saturated colors will still get it creating a visual disparity between high and low saturated props moreover a Satur ver of one introduces some zero values in the base color buffer that as we'll see later always bring some problems aside from that the electrics and metals can be set up as usual let's finally go to the node graph and work on this socalled cell Shader and hear me out now as what I'm about to say is the foundation on which all the subsequent work will be based upon as we already said if our proprocess material runs before the T
15:30 mapper the image we work with is the row render of the engine in HDR this means that colors haven't been remapped to a rench that can be displayed on the screen yet but they still contain literally the Luminosity values of the scene which will be physically correct if we set up our lights properly and as we saw earlier we can grab the brightness of the image by calculating the max value of its RGB component now I want to show you a thing but to do it I need a way to print the pixel values on the screen to do that I divide the screen in a grade and I sample only the pixel at the center of each cell in this way I create nice and wide areas with constant values that I
16:15 can pass to the function the back scar values to Overlay the printed value of each cell now we can clearly see what kind of values we're dealing with and we can finally understand why it is so difficult to keep a cell Shader in check with precision and identify good thresholds to use to quantize the light these values have an incredibly extended range we're going from 100 units to 35k and they clearly change in a nonlinear way well in reality these values by being physically accurate follow a very precise exponential rule let's say I have a cell of brightness 100 for example if I want a second one that is twice as bright I'll give it a value of
17:02 200 now if I want a third one that has the same perceived brightness difference of the second from the first but from the second one I don't give it a value of 300 but I have to double the value again so 400 and so on so let's now say that our base Luminosity value is two if we elevate it to a power we can double or Halen any given value by simply increasing or decreasing the exponent by one and that gives us an exponential Behavior okay that's interesting so let's assume that these values we see in the scene are actually powers of two if we do the inverse operation we should be able to calculate which their exponent is so if the inverse of the addition is
17:49 the subtraction the inverse of the multiplication is the division the inverse of power is is what no is the logarithm and since it's the inverse of a power of two we need the logarithm of base 2 by adding this operation and printing the result we immediately see that everything makes more sense all of the sudden numbers are in a more reasonable range and they actually change in a linear fashion and oh my look at that the number in each Cell matches exactly the EV needed to correctly expose that specific car this is very interesting then I may ask what if we
18:34 quantise these exposure values to generate the cell shading color bands let's round them and let's reconvert them in Luminosity intensity by using them as exponent of a power of two and let's also remove the grid subdivision of the image and the printed values and there our cell shading bands are now we can add the color back to our image the important thing to point out here is that our postprocess is not changing at all the brightness of the scene it's just breaking up the Luminosity gradient in discrete steps avoiding the need of any lighting and texturing workflow change from the standard one moreover if we want control on the amount of bands we have we can sandwich the rounding of
19:22 the values between a multiplication and a division for the same number all right we're finally ready to start working on a proper C Shader let's remove everything and let's start from scratch for the last time as we did before let's remove the base color from the image by using it as a divisor to be sure to not create any numerical error let's ensure that we are not dividing by zero with a Max node the sky for example has always a value of zero in the base color buffer now we have pure P light information including its color not just the brightness let's break it up into its
20:07 components but in a slightly different way than usual let's convert it from RGB to HSV Hue saturation and value the V component is called like that just because of epic's choice but it's the same as saying brightness or luminosity in fact the acronym for this color space is also hsl or HSB they're all the same before starting to mess with things let's first add all the inverse operations to recompose the final image so we don't work blindly and we can also double check that all we're doing is working as intended let's put the HSV components together let's reconvert them to RGB and let's multiply the result by the base color if we done everything
20:53 right we should see the same image that the postprocess input zero outputs aside from the Black scy eyy which will bother to fix later good now we can proceed and replicate what we did earlier to quantize the Luminosity of the scam and here is our C Shader again let's make it even prettier though and flexible with the rounding operation every pixel can only be either of one value or another it's always better to have a little area of transition values between one step and another to achieve so we must develop a function that that has the shape of a smooth step I won't go in the details of how I derive this but it's basically made of two mirrored exponential curves and it works only in
21:38 01 range this is how it's implemented in a real nicely packaged into a material function that you will of course find inside the sample project you can download by following the link in the description you can see it as an announced smooth step anyways in our case we want to use it to replace the rounding but since the function Works only with 01 ranged values we have to first decompose our EB values in their integer and fractional part now we can apply the curve to the fractional part and read it on the integer one and now we can freely choose how much we want to compress our gradient and even if we end up using the maximum contrast we'll always have a tiny bit of smoothing sticking around to
22:24 help [Music] us if we just need to work on the brightness component we wouldn't have needed a full HSV conversion why don't we try to quantize the Hue too just a little disclaimer from now on we go a bit more into the domain of my personal choices and taste at this point you have already successfully s shaded the image but that's not enough to create a strong and unique stylization so I'll walk you through some of the manipulations that I will do to push the image further my idea in this case is to reduce the amount of possible colors that the lighting can have and since the Hue is a number in 01 range we necessarily need to identify a multiplier for the number of Hues we want to retain in this case I
23:10 want to restrain it to only display primary secondary and thary colors so 12 Hues in this case though I won't use the maximum contrast as I find it visually annoying I'll keep a bit of blur between the Bands [Music] let's put the sky back to where it belongs by creating a mask starting from the base color and checking where it has the value of zero then let's use it to put back the original sky in the final image without any s shading down on it as I personally find a quantized sky aul H now I'll tell you what very nice
23:57 the usual cell Shader with with clean and flat bands but I'd like to reintroduce some volume to the scene and to do that I'll blend back in just a tiny bit of the original brightness values I also think that metals have way too many value steps I don't like it we are almost losing the S shading effect so let's grab the metallic pass and let's use it as a mask to reintroduce a lower amount of EV bands only for the metallic surfaces [Music] H you know what let's keep working on those metals and let's give them some more life the technique I'm about to show you
24:44 it's a trick that I've developed in the past while working on a prototype for a stylized game while trying to replicate some painting tricks used by some very good concept artists I was working with it consists in adding a fake Rim reflection around on the object light if the geometry is looking upwards darker if it's looking downwards adding fake lights is always a very dangerous thing to do though but thanks to this approach we developed we can do it in a very smart way but first let's create the gradient for it let's calculate the dot product between the camera vector and the word normals this is the base to obtain a frenel now I lo an operation that is not strictly necessary but that I personally find very helpful to better control
25:31 these kind of values when I want to remap them like in this case the dot product between two unit vectors Returns the cosine of the angle between the two and the cosine is a curve this means that when you look for precise thresholds you have to deal with an uneven distribution of values making the task a bit harder If instead we first calculate the r cosine of this value we obtain directly the angle in radians between the two vectors which has a linear behavior let's convert the radiance into 01 range by dividing by half pi and let's now look for the thresholds we like as Final Touch let's Square the final gradient to give it a more natural look this is our mask the area of the image that will be affected by our fake
26:18 Reflections the values will be modifying the v's width are simply the B component of the word normals after scaling them as we like which means bying case as I like the result you'll see that in a moment let's multiply them with the frenel mask and the metallic pass to obtain our final values this is what we created with positive values colored in red and negative values colored in blue now as I anticipated thanks to our setup we can handle this faked light in a very interesting way we added a CV values variation right after completing our quantisation of the Luminosity of the seen as in this case I don't want it to affect the cell shading color bands
27:03 what we just did in practice is to raise by two exposures the upper silhouette of metallic surfaces while lowering by the same amount the lower one making it self-balanced over the image as a whole it works in every light condition and doesn't make the stylized image diverge from the original physically based one another superpower we unlocked now that we know how to calculate EV values directly inside the shaders is that we can also calculate the camera exposure directly from here we just need to take the pre-exposure value and calculate the base 2 logarithm of its inverse that's so easy that it took me only years of experience a lot of random events happening lately and the stars aligning
27:49 for me to discover I could have used this incredibly useful information so many times before but I guess it was still faster than waiting for for epic to write documentation that actually explains to you what the nodes are for and how they're supposed to be used in this case it's useful to do a very peculiar thing since the exposure value of the camera tells us which Luminosity value of the scene is considered as middle we can use it as a guide to understand where are the shadows and where are the highlights so we can subtract the camera EV from the scene EVs and saturate to create a mask that tells us where the Shadows are and now for example we could use this information to interpolate between two colors that we then multiply by the
28:36 scene brightness the result is a B chromatic version of our scene that we can use to give a strong chromatic stylization to the original this is probably the most dangerous thing we did so far as it may require some specific adjustments based on cases but I think it's still in the manageable field it's also important to keep in mind that that we can add color stylization in the classic way through the color grading in the postprocess volume but in this way we can probably go more extreme now that you saw it I'll disable this feature though for the sake of keeping the image clean all right I'd say there is only one very important element clearly missing from our styze image right where are the
29:21 outlines likely on this I've got almost nothing to explain as to this date I've already done two videos on this very topic the outcome of which you can download by following the link in the description of this or those videos so I'll just include one of the outline filters I've already done and maybe add some little improvements all right let's add the circular Lan filter version and add some thresholds to the output data to obtain our outlines now we can multiply them on top of our shaded image and merge everything into one that's it well more or less the lines can become quite dense and annoying at distance it will be better to gradually remove them as they get further away from the camera this thing
30:07 can be done in several ways but today I'll just show two of them a very easy and straightforward approach and a more elaborate one the first one consists in taking this in depth Define the range in which we want the lines to fade and use it to interpolate to a value of one the outline pass before merging it into the final image it does the job but yeah not with the best looking outcome I don't like how many edges become a bit transparent I'd like something better what if we progressively reduce the caner size directly in that way instead of making the lines progressively more transparent we would actually make them thinner which makes more sense too all right it can be done but it requires a little mod to the
30:52 code let's expose the Kel size as an input for the custom node and let's use it as a replacement of the Direct Value set we had before while also adding a floor to ensure that we only deal with integer numbers all of this now implies that we're not tied to the use of a single Conant value for our kernel size but we can input a gradient to let's grab the fading gradient we did before and let's R up it to desired kernel sizes before passing it as an input H okay it's working but there is a very evident problem where the outlines are supposed to reach the minimum thickness and it makes sense if you think about it as with a size of one the Kel has no neighbor values to compare against and it trads itself as an outline so maybe
31:39 we can add a little exceptions that returns no outlines if the Kel size is lower than two and the issue is fixed and look at this this solution Works definitely better than the previous one while probably sparing some GPU Cycles in most cases cool we have outlines and our stylization is perfect pretty average if you ask me I mean they clearly do the job but we're developing our unique artistic style that is better than everyone else's right so we must think a bit more outside the box so let's try to reinterpret this element under the same light of this physically based stylization the exposure values what we care the most about is to styze the
32:25 image while keeping its overall bright Ness unchanged by adding an amount of black lines that is not really controllable we end up darkening the final result by an unknown and short dependent amount but we still want to add to the shapes that sweet pop the outlines give us how can we do it what if we use the filter output as further EV offset that might work so the value the filter returns can be both positive or negative if the outline is the detected outside of a shape they're positive if inside negative and this is valid for the part based on depth for the normals I'll do a slightly different thing that hopefully returns me a similar result
33:10 I'll do product it with the camera Vector which seems to return something kind analogous now before joining them together let's visualize again the data coming out from the depth probably you already noticed it earlier the filter is a bit too sensitive and it reads as outlines the edges of the mesh triangles too even if they are quite light compared to the correct ones we should filter out that stuff the fact that they detected as light outlines actually gives us a clean way to get rid of them if we imagine the outline values as an actual inclined line in a graph where the higher above zero you go the harder the positive Edge and the lower the harder the Negative Edge we understand that the problematic area should be right around the area close to zero so
33:57 we should try to compress the line until only the values that identify a Sure Edge remain to do that we separate the sign from the absolute value now we can subtract a threshold from the latter clamp all the negative values out and then reapply the sign the higher the threshold the more outlines will be filtered out so we need to find the right value that only removes what we don't want without sacrificing too much the rest one thing that you can notice here is that high poly meshes are easier to deal with while lower poly need a more aggressive approach now we can simply add together the two outlines data my idea is to lower the vs with the Positive outlines the ones outside the shape and do the opposite with the other ones for this
34:44 reason I'm going to negate the values and last thing to do is to clamp as Edge values straight out of the filter can go crazy high I'll use one for the internals and minus two for the externals as I still want to make ly pass through the feeling of having a dark outline and as we did for the fake metallic reflection let's add all of this right before reconverting the scene from EV to actual brightness and there we go we obtain this effect that is a bit lighter than the usual outlines but still makes the shape pop out a lot we could go on indefinitely and add more and more stuff to our style but I can say I'm satisfied with what we achieved today and let's say that's it with founder style now I can go back to the
35:29 postprocess volume and work on my post production with a peace of mind of knowing that we didn't break the physically based Pipeline and everything will still work as intended I once again thank my patrons for all their support with a discount code for the download of the simport project you can find in the link in the description don't forget to subscribe to the channel to not miss future videos and maybe if you fancy join my Discord server as a final note I want to reiterate once again that what you just saw has been unlocked by the new knowledge I've acquired recently on how to properly work with physically based lighting thanks to the video I've been doing with Aros on this very topic I hope that this video demonstrates how not only a knowledge of your tools
36:16 capabilities but especially knowledge of their limitations is the key to get creative and find new Solutions I hope this video gave you new hints to create your own original artworks and if you did I'd be happy to see working progresses and show offs in my Discord server that said I'll catch you in the next video [Music]