Version: Unity 6.0 (6000.0)
언어 : 한국어
파티클 시스템에 GPU 인스턴싱 적용
커스텀 셰이더의 파티클 시스템 GPU 인스턴싱 예시

표면 셰이더의 파티클 시스템 GPU 인스턴싱 예시

다음은 파티클 시스템 GPU 인스턴싱을 사용하는 표면 셰이더의 완전한 활용 예제입니다.


Shader "Instanced/ParticleMeshesSurface" {
    Properties {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
    }
    SubShader {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        // And generate the shadow pass with instancing support
        #pragma surface surf Standard nolightmap nometa noforwardadd keepalpha fullforwardshadows addshadow vertex:vert
        // Enable instancing for this shader
        #pragma multi_compile_instancing
        #pragma instancing_options procedural:vertInstancingSetup
        #pragma exclude_renderers gles
        #include "UnityStandardParticleInstancing.cginc"
        sampler2D _MainTex;
        struct Input {
            float2 uv_MainTex;
            fixed4 vertexColor;
        };
        fixed4 _Color;
        half _Glossiness;
        half _Metallic;
        void vert (inout appdata_full v, out Input o)
        {
            UNITY_INITIALIZE_OUTPUT(Input, o);
            vertInstancingColor(o.vertexColor);
            vertInstancingUVs(v.texcoord, o.uv_MainTex);
        }

        void surf (Input IN, inout SurfaceOutputStandard o) {
            // Albedo comes from a texture tinted by color
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * IN.vertexColor * _Color;
            o.Albedo = c.rgb;
            // Metallic and smoothness come from slider variables
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

위 예시에는 일반 표면 셰이더와 많은 부분에서 약간의 차이가 있으며, 이는 파티클 인스턴싱에 사용할 수 있도록 하기 위한 것입니다.

먼저 다음 두 줄을 추가하여 절차적 인스턴싱을 활성화하고 빌트인 버텍스 설정 함수를 지정해야 합니다. 이 함수는 UnityStandardParticleInstancing.cginc에 있으며 인스턴스당(파티클당) 위치 데이터를 로드합니다.

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

예시에서 다른 수정 부분은 버텍스 함수로, 인스턴스당 특성, 즉 파티클 컬러와 텍스처 시트 애니메이션 텍스처 좌표를 적용하는 두 개의 줄이 추가됩니다.

            vertInstancingColor(o.vertexColor);
            vertInstancingUVs(v.texcoord, o.uv_MainTex);
파티클 시스템에 GPU 인스턴싱 적용
커스텀 셰이더의 파티클 시스템 GPU 인스턴싱 예시