Unreal 5 achieves batch rendering of the same objects using GPU Instancing

When I was doing Unity before, I specially studied the use of GPU to render the same objects in batches. Now after switching to UE, I found that UE also has the same function. Next, let me explain how to implement instanced rendering through GPU in ue.

Create components

To achieve GPU instanced rendering, ue provides two components, InstancedStaticMesh (instantiated static mesh component) and HierarchicalInstancedStaticMesh (hierarchical instanced static mesh component). The difference between the two is that instantiated static mesh components can only render one type, and hierarchical instantiation means that lod hierarchical rendering can be achieved.
insert image description here
Instanced components can only use static meshes, not skeletal meshes. I use the tool I bought before to convert skeletal meshes into static meshes. My desire to use it is to be able to render models with actions in large batches. Although the effect will be worse than skeletal meshes, it is really cheap.
insert image description here
Of course, although the instantiated mesh uses the same mesh and the same material, the instantiated mesh also has some personalized information, such as position transformation, or custom color, etc. The position transformation has been built in. If there are other personalized values, you need to set custom data floating point numbers under the instance list. This is the length. If you add data to the scene, there will be related setting data in the array.
insert image description here

Implements the material of the instanced component

If the material uses custom data in the instanced mesh, you need to use the node PerInstanceCustomData, which is a scalar, representing a floating point value, and the brackets represent the subscript of the custom data array value
insert image description here

insert image description here
There is also a three-dimensional vector representing three scalar values
insert image description here
. Normally, this kind of custom data is used in the vertex shader. If it needs to be used in the fragment shader, a node needs to be used to convert it to be used in the fragment shader. I use the custom data to pass in a value, which is used as the setting of the number of frames of the animation played by the current vertex, so that each model can play the animation it needs to play
insert image description here
.
insert image description here

related nodes

Now that we have instanced models, we also have material-related settings. Next, I will introduce adding instances to the instantiated model in the blueprint, as well as updating data and position transformation.
To add an instance, we need to use the Add Instance node of the instantiated model. We can pass in a transformation to set the position, and after the World Space is set, directly set the position of the model in the scene instead of the transformation based on the position of the instantiated model.
Adding an instance node will return the instance's index in the array. When we set custom data and subsequent custom data, we need indexes to set.
insert image description here
Setting the custom data value of the instance is the custom data that can be set before the material to realize the individual requirements of the model.





insert image description here







insert image description here

insert image description here
One is cleared by instance index, only the target instance is cleared
insert image description here
, and the other can pass in an array to clear multiple instances
insert image description here

Precautions

If you use vertex animation to play the model effect, you will find that when the animation is played, there will be afterimages. The reason for this effect is the default anti-aliasing post-processing.
The easiest way to solve this problem is to change the anti-aliasing effect. The
default anti-aliasing of the delayed rendering pipeline is temporary super-resolution TSR anti-aliasing.
insert image description here

Guess you like

Origin blog.csdn.net/qq_30100043/article/details/131477211