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);
相关推荐
future_studio2 天前
聊聊 Unity(小白专享、C# 小程序 之 播放器)
unity·小程序·c#
向宇it2 天前
【unity实战】MapMagic 2实战例子
游戏·3d·unity·c#·游戏引擎
SlowFeather2 天前
Unity TMP可控角度多色渐变文字
unity·游戏引擎
霜绛2 天前
Unity:UGUI笔记(一)——三大基础控件、组合控件
笔记·学习·unity·游戏引擎
小趴菜82272 天前
Android中加载unity aar包实现方案
android·unity·游戏引擎
今夕资源网3 天前
牛童三国单机游戏Unity源码 免费开源
游戏·unity·单机游戏·游戏源码·unity源码·unity游戏
UTwelve3 天前
【UE】材质与半透明 - 00.什么是半透明材质
性能优化·材质
future_studio3 天前
聊聊 Unity(小白专享、C# 小程序 之 图片播放器)
unity·小程序·c#
ellis19703 天前
toLua[七] Examples 06_LuaCoroutine2分析
unity
L X..3 天前
Unity 光照贴图异常修复笔记
unity·c#·游戏引擎