UE4 Optimization: Instancing

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi welcome to the new tutorial have you ever wondered what is the most efficient way of placing identical actors in the level like a dance forest across the spectators in the arena or in this case both of asteroids usually several thousands of factors are enough to slow the engine down below an acceptable frame rate yet here I got 35 thousands of asteroids each with its own individual rotation almost all of them are visible at once casting shadows in total they have up to three million triangles thrown every frame yet everything is perfectly fluid a common sense among 3d artists is that a higher number of polygons means the troubles with performance in fact pushing polygons onto the screen long as rasterizing is the fastest thing in the modern graphics pipeline next in terms of cost is reading textures it's limited by bandwidth and the physical distance of computer's memory to the video cards memory shading takes even more time as stranger programs usually contain between 100 and 200 instructions they need to be evaluated at least once for every pixel of the screen only 10 comes the real bottleneck the graphics driver the driver translates and checks every OpenGL and DirectX function call into the final comment for the GPU it's a slow inefficient process and given that it frequently runs on a single CPU car it's even slower while GPUs really escalated in performance in recent years the CPUs get like 20% advantage over the models from 10 years ago when you compare a single core in OpenGL and DirectX up to 11 every separate object or material might potentially require an action from the CPU causing the halt in the pipeline if we were able to draw all the vertices in a single call we would unleash the raw power of the graphics card to park multiple meshes into a single efficient object so let's compare your typical static mesh component to a single instance basically static mesh components are heavy every single mesh you place on the level will contain a transform to lash materials assigned to it physics properties collision properties lighting properties and when you render them you will use at least one draw code per removable static mesh so to draw an individual static mesh the GPU has to be told what material to use what ledge to use and then to draw so in comparison a single instance is very lightweight and instance contains the transform and the tip all the other properties are common for the whole group so the managed material they are all set once for all the instances and the same goes for rendering you basically set up an array of transforms that contains the positions of the rotations of instances and the GPU can take on that and draw them at once in a single call so why rendering separate objects is so slow well the GPUs are crazy fast and rendering polygons but they need to be fed with data by the comparatively slow CPU OpenGL and the Direct X up to 11 were designed in the way that they have a global state the global state contains the information about the current material to use and the current set of vertices to use only one at the time so do you want to draw another mesh with a different material the CPU will have to stop change the properties and issue a new comment and your draw call so drawing 100 lashes looks like that apply the properties of the static map set the material and draw 100 times and the simple drum command is actually slow a partial solution to this would be batching so you combine pieces into a single big mesh in fact you will really get only a few locals but the culling hice the entire object when you move the camera around or when it has to find out the distance it will hide the entire object at once and the weight of the meshes in terms of disk space is really huge basically you have to store this whole information about the copies in a single mesh and you can imagine that level of detail is really hard to work with so yes it's often worth it but only four groups of nearby objects now comparing it to 100 instances you just upload a whole array of 100 transforms set the material and draw in a single call the instance static match the feature that you will use for that in other Eng form so it has one time thank for all copies or the properties are common but how does it work with level of detail when you have all the lashes combined into a single object well it doesn't that's why they introduced hierarchical instant static lashes it's basically the same but it creates multiple groups of instances one per a level of detail then move the instances from one group to another based on these cells now how do we proceed with setting up instances in Unreal Engine so using is the link in unreal requires us to create a blueprint blueprints in Unreal Engine are templates of objects so when you create a blueprint in the beginning you can love it it's an empty object but what they also called the blueprint is the visual language of scripting so instead of using C++ which is the main programming language of Unreal Engine you can achieve roughly the same functionality with blueprints the even begin play note the whole concept is based on an execution flow so the flow starts for example here on L begin play note which will fire off when the game starts and then you specify what is about to happen for example it will print add text of hello when the game begins it will print hello and then the next thing good can happen it can also print hi to apply any changes you have to hit compile now we drop the object onto the level and hit play it says hi and ho once again please check the upper left corner hi hello great for a more detailed introduction into blueprints I really recommend looking into the official channel of other engine now how do we proceed with setting up instances in the upper left corner in the upper left corner you can add components for example thank you or a sphere and send them up from beginning like changing materials and so on in the same way we create a component of hierarchical is the static clash initially it is empty so we have to specify the merge to use and then after setting up the materials which are OK by default for me you need to add new instances as you can see it says zero elements so we add the first one right at the next one now if you were to move an instance like you usually do with your objects even most all instances at once because you are not moving the instance you are moving the entire is the single group here so what you need to do is reset to this receptors how it was before then go to instances and throws from it manually here for the transfers of the instances are specified here like the rotation as well yep right I can see the instances here in the editor while has a strong scale set by me well maybe I should reset this too well right now the blueprint way which will be the much more common way is to delete all these instances and create them in the blueprint you could do it on begin play but then you won't see any effect in the editor only after restart the game so there is the construction script tab and it's the blueprint that is called every time an object is constructed that is dropped into a level or modifies or it's variables are modified by anyway every such time in the construction script is updated so in the construction script we can we can drag the instance static clash here guests enter the script to add an instance so the execution flow goes to this load is the only monthly low it will add an instance and here we can specify the transformation to it so we drop from this input Lake transform okay and for example the location will be minus 100 and the initial rotation will be 40 let us see what we got is here so no another one for example like that but the new transformation will be minus 200 rotation 50 Kampai okay - guts to attach the instance group to create the instance in now it works yeah we got two instances but the method will be really tedious to use it's not a production method so what we need to do is to get rid of this for now so to automate the method a little bit we'll create a for loop it's a basic programming group here you insert 0 and here you insert how many times plus 1 do you want the function to be called so for example pretty to create 50 instances and the room body is what happens with every group so every time we want to create an instance to the instance that image make the transform now we have only one input node in the location of 3 so we drag from here to make vector and now we have 3 X Y Z so this index is the current index of the loop the current run so 0 1 2 3 up to 15 so I want to take this multi tie is value by for example one meter 100 centimeters in X so the first will be at 0 the second will be at 100 and effort in 200 300 and so on it will happen with every run of the book and when it's completed I want to print down just like that oh okay you can see the flow happening here and in the viewport I got 50 instances separated by 1 meter so maybe this shouldn't be 1 meter maybe I would like to specify this value in the variable stop on the Left I click the + to create a variable a changing value that I can also change in the editor and KOed separation plug for example it can be anything and the variety type will get float close our numbers with the decimal part also I drag the value onto the variable onto the graph click again and use this value is that of this hard-coded 100 compile now they are separated by 0 but if I click the I here near the variable I will be able to change the value so notice the person elf is zero and as I move around it gets bigger 550 300 right so by using this basic blocks and creating the value to be specified as the instance is the straw strong but you can create a function that will place your you can create a function that will place your instances in any manner you want so you can download the busted files from the link that is visible on the screen or in the description of the video editor you will see my function that is creating the asteroid ring it's called BP instancing asteroid ring looks like that I have added multiple variables to this to it like for example ring radius which we can change to 42,000 or to 12,000 and there is instance count by width and distance held by the web so for example we want to have 100 distances over the entire length or 200 or 500 the function looks like that but it's not everything I even sell the image to be used by the variable to be able to specify it in the editor then in the for group I create the instances and the transform is calculated in another function the function also created by me which looks like that and you can look into it in the materials that you download there is also a more simple way but with a more manual work involved to use it I think it's the foliage tool the foliage tool is obviously based around instancing because otherwise it won't be efficient at all so you can specify your meshes instead of grass or trees and just paint them or a loop to polish to instance ink in Unreal Engine is working out of the box and quite automatically but there are some things you have to watch out for the first thing which is not obvious at all is that the scale of theoretical distance static mesh components and the apparent actors has to always be 1.0 always be 1 non unit scale will cost calling issues along with color issues now remember to add instances to a single instance in group instead of creating multiple instance in groups it's a common mistake but if you follow the method I shown here in the tutorial you will probably avoided now avoid setting mobility to static it's possible to take the instance in group and set it to static and even baked right into it but you shouldn't be doing this really instances our method of copying dynamic objects which don't have static properties that are individual they are just three copies keeping only the transform and the rest is common as you could see in the video before my instances were also rotating like that and the way I did it was to enter the event graph but in the evil tick which is equivalent to do it every frame I increase the actors local rotation by a specified angle the rotation speeds variable it is also possible to change the transform of a single instance but then you will loose all the performance of this letter update instance transform and here you can specify the final transform to be set on the given instance so as you can see here we specify the index it is for a single instance so we will also need a look for that but the thing to remember if you still want to use this method because for example you have only hundreds of instances and not several thousands like I got then please remember to set mark render state dirty only on the last instance so you update all the instances with dis turned off and the last instance will just turn on how to achieve it in the blueprint is shown again in the materials you can download you can enter the blueprints folder is the red ring go to the event graph and this is this part called experimental which sets the rotation of the individual stir it to get it working you just take the executed flow from here and sorry and place it here in the loop like that to file head run but I will use it in disguise it will be slow yep it's no now how we can optimize even more and it is very recommended in the workflow with instances you should combine the meshes into bigger meshes first then copy them with instancing you're basically limiting the number of instances by that so the method of creating bigger meshes from individual meshes was introduced only in the unreal 414 the latest release now and here you can click convert actors to static mesh then we can use this group within our instance in group instead of individual lashes another very important thing when using instancing will be to avoid physics basically when you can disable physics because with this amount of objects it's really a hard thing for the cpu even to set up the physics not only to collide so when you are setting up the object selected instance group and in the collision tab remember to disable generate overlap events it has to be set to OFF to maximize performance from the collision presets choose an ultra vision location at all can the character step upon it nope it can't and this is you can also play with shadow casting for example if the instances are so far away that it doesn't matter you can disable shadow casting at all the obvious place to gain more performers is also to enter the settings of the static mesh we are using our template let's change the screen size when different level of detail show up for example when the screen size is 1 0 I have the LOD 0 the most disabled but as I go from it further when the screen size on the object is like 6 percent of the screens high then the note1 kicks in and when it's 1 percent of the screen size the load 2 kicks in l so on you can really go forward with just adjusting these numbers to achieve really the maximum perform as you can now as you play inside the editor and you can click lit in the viewing mode and check a level of detail collaboration - level of details you can see is the hierarchical Eastern static mesh works hard to move the instances between the groups we should be measuring our performance on quite often so in order to do this we click on the left top corner press staff engine unit and also start advanced engine here we can see how much time it takes to render a frame in milliseconds in my case it's 8 point 33 milliseconds now and in the counters area you have static mesh triangles you can also enable start engine fps but it's not a good practice to measure an FPS because for example when you want to check how much of a hit on performances some feature then it doesn't mean anything that it takes you 15 FPS or 30 FPS because it will mean that using this feature four times or this kind of objects placing it four times on the level we leave you with zero APs no it certainly doesn't work like that so we measure in milliseconds it takes the random krob if you want to target 120 fps then you have to rather listen in about eight milliseconds 60 FPS will require you to fit in 16 milliseconds and 40 fps in about 32 milliseconds okay so we're done with now for the current video we have learned how to set up instances how to avoid common mistakes with them and maximize performance should you have any questions and you will have please ask in the comments at home place a nice discussion emerges from that and in the next video we will learn not how to copy a single mesh infinitely arrived but how to run the most delicious how to randomize the instances obviously we can't use too many measures to make the randomization and the natural look of the instances because we will lose the main advantage of this cleansing system so we will have to use different methods shader based methods for that but it's the topic of another video so thank you for today I'll see you later
Info
Channel: Tech Art Aid
Views: 144,411
Rating: undefined out of 5
Keywords: unreal tutorial, ue4 tutorial, ue4 technical art, ue4 hism, ism, draw calls, draw count, instances, procedural mesh, instanced static mesh component, ue4 infinite, ue4 construction script, unreal engine geometry performance, improve performance profiling, rendering tweaks, benchmark
Id: oMIbV2rQO4k
Channel Id: undefined
Length: 28min 34sec (1714 seconds)
Published: Mon Nov 07 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.