Version: 6000.3
语言: 中文
表面着色器中的粒子系统GPU实例化示例
在内置渲染管线中使用自定义顶点流进行粒子系统GPU实例化示例

内置渲染管线中的粒子系统GPU实例化示例

这是自定义的完整工作示例着色器在 GPU 上运行的程序。更多信息
请参阅术语表
粒子系统通过在场景中生成大量小型 2D 图像并为其制作动画来模拟液体、云和火焰等流体实体的组件。更多信息
请参阅术语表
GPU 实例化。此自定义着色器添加了标准粒子着色器所没有的功能 - 纹理表动画的各个帧之间的淡入淡出。

Shader "Instanced/ParticleMeshesCustom"
{
    Properties
    {
        _MainTex("Albedo", 2D) = "white" {}
        [Toggle(_TSANIM_BLENDING)] _TSAnimBlending("Texture Sheet Animation Blending", Int) = 0
    }
    SubShader
    {
        Tags{ "RenderType" = "Opaque" }
        LOD 100
        Pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile __ _TSANIM_BLENDING
            #pragma multi_compile_instancing
            #pragma instancing_options procedural:vertInstancingSetup
            #include "UnityCG.cginc"
            #include "UnityStandardParticleInstancing.cginc"
            struct appdata
            {
                float4 vertex : POSITION;
                fixed4 color : COLOR;
                float2 texcoord : TEXCOORD0;
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };
            struct v2f
            {
                float4 vertex : SV_POSITION;
                fixed4 color : COLOR;
                float2 texcoord : TEXCOORD0;
#ifdef _TSANIM_BLENDING
                float3 texcoord2AndBlend : TEXCOORD1;   
#endif
            };
            sampler2D _MainTex;
            float4 _MainTex_ST;
            fixed4 readTexture(sampler2D tex, v2f IN)
            {
                fixed4 color = tex2D(tex, IN.texcoord);
#ifdef _TSANIM_BLENDING
                fixed4 color2 = tex2D(tex, IN.texcoord2AndBlend.xy);
                color = lerp(color, color2, IN.texcoord2AndBlend.z);
#endif
                return color;
            }
            v2f vert(appdata v)
            {
                v2f o;
                UNITY_SETUP_INSTANCE_ID(v);
                o.color = v.color;
                o.texcoord = v.texcoord;
                vertInstancingColor(o.color);
#ifdef _TSANIM_BLENDING
                vertInstancingUVs(v.texcoord, o.texcoord, o.texcoord2AndBlend);
#else
                vertInstancingUVs(v.texcoord, o.texcoord);
#endif
                o.vertex = UnityObjectToClipPos(v.vertex);
                return o;
            }
            fixed4 frag(v2f i) : SV_Target
            {
                half4 albedo = readTexture(_MainTex, i);
                return i.color * albedo;
            }
            ENDHLSL
        }
    }
}

此示例包含与表面着色器为内置渲染管线编写着色器的简化方法。更多信息
请参阅术语表
用于加载位置数据:

        #pragma instancing_options procedural:vertInstancingSetup
        #include "UnityStandardParticleInstancing.cginc"

对顶点函数的修改也与表面着色器非常相似:

                vertInstancingColor(o.color);
#ifdef _TSANIM_BLENDING
                vertInstancingUVs(v.texcoord, o.texcoord, o.texcoord2AndBlend);
#else
                vertInstancingUVs(v.texcoord, o.texcoord);
#endif

与上面的第一个示例相比,这里唯一的区别是纹理表动画混合。这意味着着色器需要一组额外的纹理坐标来读取纹理表动画的两帧(而不是一帧),并将它们混合在一起。

最后,片段着色器读取纹理并计算最终颜色。

表面着色器中的粒子系统GPU实例化示例
在内置渲染管线中使用自定义顶点流进行粒子系统GPU实例化示例