Level of Detail in Unity Using LODGroups, Dithering, and Crossfade! ✔️ 2020.3 | Game Dev Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi i'm ned and i make games today i want to introduce you to a very useful concept level of detail often abbreviated as lod level of detail is a technique to speed up rendering by lowering the detail of objects which take up a small screen area it's often used in open world games to keep distant objects from eating up the gpu in this video i'll explain how to use unity's lod group component adjust led settings for different quality levels import models with led levels from modeling software script a crossfade shader and react to lod changes in c-sharp before we get started thank you for watching if you're new to my channel i upload weekly game development tutorials so consider subscribing and clicking the bell also check out our community discord server it's great for game development discussion there's a link in the video description alright this video was made using unity 2020.2.3 f1 if you're using a newer version of unity check the video description for any fixes you should know about i'll also use the universal render pipeline 10.2.2 however beside the crossfade shader nothing in this tutorial is urp dependent so the core idea behind level of detail is to calculate the amount of screen space an object takes up and dynamically choose a mesh to render based on that we only need a ton of details if an object is close to the camera when using lod and unity the lod group component is your best friend let's learn about it by creating three sphere game objects and assigning them three different colored materials now create an empty game object and place each of your spheres as children under it then give the parent game object and lod group component in the component inspector is a bar with several lod levels click on the lod0 level and a panel appears below labeled renderers here you can assign child game objects to this lod level meaning that child will render if and only if this lod level is active for a game object to show up in this list it needs to have a mesh renderer component attached okay so add a different sphere each to lod0 lod1 and lod2 when you zoom in and out in the scene view unity will show or hide the appropriate renderers depending on the size it takes up on the screen select the lod group game object and unity will even display the currently active led level right there for you there's an easier way to preview different lod levels select your led game object and click and drag the light blue camera icon in the inspector unity will position the scene view camera correctly to display your selected object at the correct lod level you can see lod and effect in the game view as well move the camera back and forth note that the led group inspector camera icon still indicates the position of the scene view camera and not the game view camera you'll also see that there's a special led level called cold when this is active unity turns off all mesh renderers if an object takes up so little screen real estate you might as well not even draw it when assigning meshes to lod levels you should know that you can add more than one mesh to each this is useful if you have a detail mesh that should only be visible close to the camera a mesh renderer can also be part of multiple levels at once in which case it will be visible if any of the levels are visible if you take a look at the lod levels bar and the lod group component you'll also notice a percentage below the level name this is the approximate percentage of screen space the game object must take up for an lod level to become active click and drag on the edges between levels to edit these boundaries if you need more lod levels right click on any level and select insert before similarly you can delete a level by right-clicking on it and then selecting delete depending on your current project's settings you may notice this message about lod bias which provides the perfect segue into quality settings open your project's quality settings and scroll down to the led bias setting basically this is a multiplier applied by the lod group to game object's screen size when it's choosing which level to display as an example if lod bias is 2 unity will treat each object as if it takes up double the screen space that it really does this creates a bias towards more detailed led levels hence the name you can tune this value differently for each quality preset for instance at the lowest quality you may set the bias to 0.5 so less detailed levels display more often another quality setting to know of is the maximum led level led group components will never pick a led level that's more detailed than this setting and will even exclude unused meshes from builds automatically for example if the maximum led level is 1 an led group will never choose level 0 the most detailed level to display this setting is useful if you don't want very detailed meshes to display on lightweight platforms alright so we know how to set up and adjust led groups but wouldn't it be nice if unity automatically created them when we imported a mesh it turns out that that is possible i'll show how to do this in blender but the technique should work with most modeling programs open blender and create three meshes with differing complexity i generated three ico spheres with different subdivision levels now in the outliner select each mesh object and add an underscore log n to the end of its name replacing in with the mesh's lod level save the project as a blend file into your unity project's asset folder back in unity drag the model prefab into the scene hierarchy and notice that the lod group has been automatically created with each mesh and the correct lod level you can adjust the edge percentages and you're good to go now i've ignored the led group component's fade mode drop down until now this enables smooth transitions between lod levels however in the universal render pipeline changing modes does nothing for whatever reason urp doesn't support crossfading in its lit shaders thankfully however you can relatively easily create a crossfade shader yourself in the shader graph create a urp lit shader graph named lod fade and open it unity defines a shader variable called unity underscore lod fade which tells us when an object should fade out to get its value we need to create a simple custom function node create one and in the graph inspector node settings give it a single float output lod fade set the type to string and the name to get lod fade in the body write this code which sets led fade if unity's value is greater than 0 indicating that we should fade out now you could feed this into the alpha node of the master stack but we'd rather not have to resort to transparency blending to get around that let's use dithering a useful technique that throws out pixels in a checkerboard pattern the shader graph has a dither node so create one and feed the led fade value and a screen position node into it then in the graph inspector graph settings tab turn on alpha clip this tells the shader to throw out pixels if the alpha value is below the defined threshold route the dither output into the alpha and set alpha clip to zero now only pixels where the dither value is greater than zero will display finish up your graph however you want i'm just going to route a color property into the base color field and call it a day save the asset and return to the scene editor change your sphere's materials to use this new shader you also should scale down your lod1 and led2 spheres so that you can see the transition clearly now select your led group game object and switch the fade mode to cross fade for now turn animate cross fading off and then select the led zero level you'll see a fade transition width slider this is best explained through example set the width to 1 and led 0 will smoothly transition to led1 through its entire range drag the camera icon to get a look now set the crossfade width to 0.1 and led 0 will begin transitioning into led1 through the last 10 percent of its range you can edit these widths independently for each led level note of course that when cross fading is active both the current led level and the next led level must render be careful not to set the crossfade width too high or you will quickly lose any performance gains that you achieved using led in the first place now turn on animate crossfading instead of using the width this makes crossfades time-based whenever an led level changes unity will fade out the previous level for one-tenth of a second you can change this period by setting the led group dot cross fade animation duration variable in a c-sharp script [Music] a quick note about the speed tree fade mode this is used for special speed tree vegetation meshes downloaded from the asset store i won't cover them in this video but you know they're there there's one last tip i want to share with you before i sign off if you ever need to know if an led level is shown or hidden you can use the on became visible and on became invisible callbacks in c-sharp to try it out create a c-sharp monobehaviour script and then add these two functions write a debug log in each then in the scene editor add the component to your lod0 game object press play and play around with the camera you'll get a message whenever the led level 0 is activated or deactivated and that's all for level of detail at least in this video it's a useful tool to add to your unity toolbox so keep it in mind when it's time to optimize your game how do you plan to use led in your project please let me know in the comments thanks so much for watching it really means a lot to me if you can please leave a like it lets youtube know to recommend this video and helps out the channel also be sure to subscribe and turn on bell notifications so you won't miss next week's video i want to take a second to plug my patreon don't feel pressured to contribute but if you'd like to i've prepared some special goodies for you these include early viewing of videos voting power and tutorial topic polls and downloadable unity project files thanks to all my patrons you helped me keep up this channel and to everyone thanks again for watching make games [Music] you
Info
Channel: Ned Makes Games
Views: 12,461
Rating: undefined out of 5
Keywords: gamedev, game development, development, unity, unity3d, madewithunity, programming, game design, csharp, nedmakesgames, nedmakesgames dev log, indiedev, indie game, dev log, shaders, 3d modeling, blender, tutorial, walkthrough, shader, universal render pipeline, urp, unitytip, unitytips, mesh, asset, editor, script, level of detail, lod, level, detail, lodgroup, dithered, dithering, crossfade, crossfading, cross, fade, transition, vfx, open world, camera, screen, screen space, c#, shader graph
Id: -mE4qreuqJY
Channel Id: undefined
Length: 11min 56sec (716 seconds)
Published: Wed Feb 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.