Shader "FX/SoftParticle"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
		[Space(10)][Header(SoftParticle)]
		[Toggle] _USESOFTPARTICLES("Use Soft Particles", float) = 0
		_SoftParticleFade("Soft Particles Fade", Range(0.01,3.0)) = 1.0
		
    }
    SubShader
    {
	    Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }

		Blend SrcAlpha OneMinusSrcAlpha
		
		Cull Off 
		Lighting Off
		ZWrite Off
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
			#pragma multi_compile_particles	
			#pragma multi_compile _USESOFTPARTICLES_OFF _USESOFTPARTICLES_ON 

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
				fixed4 color : COLOR;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
				fixed4 color : COLOR;

				#ifdef SOFTPARTICLES_ON
					float4 ScreenPos : TEXCOORD2;
			    #endif
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
			half _SoftParticleFade;

			sampler2D_float _CameraDepthTexture;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
				o.color = v.color;

				#if _USESOFTPARTICLES_ON
				#ifdef SOFTPARTICLES_ON
					o.ScreenPos = ComputeScreenPos(o.vertex);
					COMPUTE_EYEDEPTH(o.ScreenPos.z);
				#endif
				#endif
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
				#if _USESOFTPARTICLES_ON
				#ifdef SOFTPARTICLES_ON
					float fLinearSceneZ = LinearEyeDepth(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.ScreenPos)));
					float fSoftParticleFadeValue = saturate(_SoftParticleFade * (fLinearSceneZ - i.ScreenPos.z));
					i.color.a *= fSoftParticleFadeValue;
                #endif
				#endif
				fixed4 col = tex2D(_MainTex, i.uv);
                return col * i.color;
            }
            ENDCG
        }
    }
}

 


코드 분석

 

https://forum.unity.com/threads/pragma-multi_compile_particles-and-gpu-instancing.434369/

 

위 자료에 의하면 단순히 SOFTPARTICLES_ON 키워드를 사용하기 위한 다중 컴파일 구문이라고 합니다.

 

Player Settings -> Quality 세팅에서 Soft Particles 옵션 값을 받아오는 것으로 추측되며 해당 옵션을 끄면 

 

#ifdef SOFTPARTICLES_ON 전처리 구문에서 해당 코드가 컴파일에서 제외됩니다.

 

Soft Particles 옵션이 제대로 동작하기 위해서는 Deffered렌더링으로 설정하거나, 카메라의 DepthTextureMode를 활성화 해야합니다.

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class CameraDepthOn : MonoBehaviour
{
    void OnEnable()
    {
        Camera pCamera = GetComponent<Camera>();
        if (pCamera == null) return;
        Debug.Log("Depth On");
        pCamera.depthTextureMode = DepthTextureMode.Depth; ;

    }

    void OnDisable()
    {
        Camera pCamera = GetComponent<Camera>();
        if (pCamera == null) return;
        Debug.Log("Depth Off");
        pCamera.depthTextureMode = DepthTextureMode.None;
    }
}

위 스크립트는 카메라의 DepthTextureMode를 활성화합니다, 카메라 오브젝트에 설치하면 됩니다.

 

 

 

추가로 해당 쉐이더의 Soft Particle On Off 기능을 따로 멀티컴파일 전처리 구문을 작성했습니다.

 


WRITTEN BY
CatDarkGame
Technical Artist dhwlgn12@gmail.com

,