gpu instancer crowd 使用自定义材质并且只修改单个物体的材质参数

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);
相关推荐
入目星河滚烫19 小时前
Unity避坑——继承了MonoBehaviour的对象不能通过new来创建
unity·c#·游戏引擎
爱吃小胖橘20 小时前
Unity-动画目标匹配
3d·unity·c#·游戏引擎
SmalBox21 小时前
【光照】[PBR][漫反射]实现方法对比
unity·渲染
PaoloBanchero1 天前
Unity 虚拟仿真实验中设计模式的使用 ——命令模式(Command Pattern)
unity·设计模式·命令模式
PaoloBanchero1 天前
Unity 虚拟仿真实验中设计模式的使用 ——策略模式(Strategy Pattern)
unity·设计模式·策略模式
PaoloBanchero1 天前
Unity 虚拟仿真实验中设计模式的使用 —— 观察者模式(Observer Pattern)
观察者模式·unity·设计模式
软件黑马王子1 天前
2025Unity超详细《坦克大战3D》项目实战案例(上篇)——UI搭建并使用和数据持久化(附资源和源代码)
游戏·ui·unity·c#
机器人行业研究员2 天前
六维力传感器材质选择:影响性能与精度的关键因素
材质
航Hang*2 天前
Kurt-Blender零基础教程:第3章:材质篇——第1节:材质基础~原理化BSDF,添加有纹理材质与用蒙版做纹理叠加
笔记·blender·材质·建模
爱吃小胖橘2 天前
Unity-动画子状态机
3d·unity·c#·游戏引擎