Article directory
Preface
In the previous article, we mainly analyzed the definition of GPU instantiation in Unity and what the instantiation ID step does.
In this article, we use the defined instance ID to make the rendered object coordinates contained in the synthesized model of a batch display correctly.
一、UNITY_SETUP_INSTANCE_ID(v);
UNITY_SETUP_INSTANCE_ID(v); Place it at the beginning of the vertex shader/fragment shader (optional) so that the global variable unity_InstanceID can be accessed.
- Before modification (although the GPU instantiation batch was successful, the location of the object was wrong):
- After modification: (GPU instantiation batch is successful and the position is correct)
2. In the UnityInstancing.cginc file, take a look at what Unity does.
- UNITY_SETUP_INSTANCE_ID(v);
1. After using .cginc, the function will be automatically predefined
2. The GPU instantiation conditions need to be met before the corresponding statement will be executed.
- This sentence must be used at the beginning of the vertex shader and fragment shader so that subsequent steps can correctly access the global variable unity_InstanceID.
- UNITY_INSTANCING_ENABLED: When GPU instantiation can be turned on
- UNITY_SUPPORT_INSTANCING: GPU instantiation hardware support
3. After the GPU is instantiated, the following functions are mainly executed:
- UNITY_GET_INSTANCE_ID(input): Get the batch ID function of the point before batching
- UnitySetupInstanceID(UNITY_GET_INSTANCE_ID(input)): Convert to get the batch ID of the point after the batch is combined.
- UnitySetupCompoundMatrices();: Unity performs corresponding matrix transformation according to the global variable unity_InstanceID to make the vertex position correct
In this article, we solve the problem of incorrect object position after GPU instantiation. In the next article, we will implement how to make the material color of each object different.