gpu instancer 中由于所有物体只使用同一个材质和网格,不像普通渲染每一个物体都有单独的材质副本,所以大量物体渲染只需要使用一次draw call
,节省了很多性能。而修改单个物体材质的方式也跟普通渲染不一样,每个物体有自己的参数buff,着色器需要获取参数时只需要根据实例id在这个buff中获取参数。
gpu instancer crowd 中也要在着色器中修改才能自定义着色器,根据这个非常反人类的文档,自定义着色器那么重要的部分一部分写在crowd 说明中,一部分写在q&a中
urp中引入cginc文件和 #pragma
csharp
Pass {
...
HLSLPROGRAM
...
#pragma multi_compile_instancing
#pragma instancing_options procedural:setupGPUI
...
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl"
#include "./../../../GPUInstancer/Shaders/Include/GPUInstancerInclude.cginc"
#include "./../../../GPUInstancer-CrowdAnimations/Shaders/Include/GPUICrowdInclude.cginc"
#pragma multi_compile _ GPUI_CA_BINDPOSEOFFSET
#pragma shader_feature_vertex GPUI_CA_TEXTURE
#pragma multi_compile_instancing
#pragma instancing_options procedural:setupGPUI
...
ENDHLSL
}
在顶点着色器加入GPUI_CROWD_VERTEX
csharp
v2f vert(appdata_full v)
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
GPUI_CROWD_VERTEX(v);
...
return o;
}
但其实GPUI_CROWD_VERTEX会报错,非常弱智,根据一个隐秘的示例shader 使用gpuiCASkinning才是不会报错的,GPUI_CROWD_VERTEX就是gpuiCASkinning的封装
csharp
v2f vert(appdata_full v)
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
gpuiCASkinning(v.texcoord2, v.texcoord3, v.vertex.xyz, v.normal.xyz, v.tangent.xyz, v.vertex.xyz, v.normal.xyz, v.tangent.xyz);
...
return o;
}
自定义的appdata一定要有texcoord2 texcoord3
csharp
struct appdata_custom
{
float4 vertex : POSITION;
float4 tangent : TANGENT;
float3 normal : NORMAL;
float4 texcoord2 : TEXCOORD2;
float4 texcoord3 : TEXCOORD3;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
自定义的加入
csharp
#define GPUI_CUSTOM_INPUT 1 // needs to be declared so GPUI does not override your defines
#define GPUI_STRUCT_NAME appdata_custom // the name of your input struct
#define GPUI_BONE_INDEX texcoord2 // TEXCOORD2
#define GPUI_BONE_WEIGHT texcoord3 // TEXCOORD3
#define GPUI_VERTEX vertex // VERTEX
#define GPUI_NORMAL normal // NORMAL
#define GPUI_TANGENT tangent // TANGENT
至此,自定义shader就没有问题了
那如何只修改单个物体的材质参数
首先在shader中加入
csharp
uniform StructuredBuffer<float4> gpuiFloat4Variation;
float4 gpuiF4Var_float( float4 Float4_Out)
{
#ifdef UNITY_PROCEDURAL_INSTANCING_ENABLED
Float4_Out = gpuiFloat4Variation[gpui_InstanceID];
#else
Float4_Out = float4(1, 1, 1, 1);
#endif
return Float4_Out;
}
gpuiFloat4Variation是buff的名字 ,单个gpu instance使用时通过gpui_InstanceID在buff中获取属于它的数据
csharp
void Start()
{
// Define the buffer to the Prefab Manager.
if (prefabManager != null && prefabManager.isActiveAndEnabled)
{
GPUInstancerAPI.DefinePrototypeVariationBuffer<Vector4>(prefabManager, prefab.prefabPrototype, bufferName);
}
}
来自demo里的ColorVariations脚本,首先注册这个buff,bufferName是gpuiFloat4Variation,一定要相同
生成物体后进行加入参数
csharp
prefabInstance.AddVariation(bufferName, (Vector4)Random.ColorHSV());
修改物体材质颜色参数,goList为生成的物体的list
csharp
Vector4 v = (Vector4)Random.ColorHSV();
GPUInstancerAPI.UpdateVariation(prefabManager, goList[Random.Range(0, goList.Count)], bufferName, v);