Version: Unity 6.0 (6000.0)
언어 : 한국어
표면 셰이더의 파티클 시스템 GPU 인스턴싱 예시
커스텀 버텍스 스트림을 사용하는 파티클 시스템 GPU 인스턴싱 예시

커스텀 셰이더의 파티클 시스템 GPU 인스턴싱 예시

다음은 파티클 시스템 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
        {
            CGPROGRAM
            #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;
            }
            ENDCG
        }
    }
}

이 예제에서는 표면 셰이더와 동일한 설정 코드를 사용하여 포지션 데이터를 로드합니다.

        #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 인스턴싱 예시