반응형

Shader "CatDarkGame/SoftParticle"
{
    Properties
    { 
        [Header(Surface Options)] [Space(5)]
        [Enum(Additive,1,AlphaBlend,10)] _DstFactor("Blend Mode", float) = 10
        [Enum(UnityEngine.Rendering.CullMode)] _Cull("Cull Mode", float) = 2
        [Enum(Off,0,On,1)] _ZWrite("Z Write", float) = 0
        [Enum(UnityEngine.Rendering.CompareFunction)] _ZTest("Z Test", float) = 4
        
        [Space(5)]
        [Header(Surface Inputs)] [Space(5)]
        [MainTexture] _BaseMap("Texture", 2D) = "white" {}
        [MainColor] _BaseColor("Color", Color) = (1, 1, 1, 1)
        
        [Space(10)]
        [Toggle(_SOFTPARTICLES_ON)] _SoftParticles_On("Soft Particles", float) = 0
        _DepthFadeNear("DepthFade Near", Range(-1, 4)) = 0.0
        _DepthFadeFar("DepthFade Far", Range(-1, 8)) = 4.0
    }

    SubShader
    {
        Tags { "RenderType" = "Transparent" "Queue"="Transparent" "RenderPipeline" = "UniversalPipeline" }
        LOD 100

        Pass
        {
            Name  "URPSoftParticle"
            Tags {"LightMode" = "SRPDefaultUnlit"}

            Blend SrcAlpha [_DstFactor]
            Cull[_Cull]
            ZTest[_ZTest]
            ZWrite[_ZWrite]

            HLSLPROGRAM
            #pragma target 4.5
            #pragma exclude_renderers gles d3d9
            
            #pragma shader_feature_local _SOFTPARTICLES_ON
            #pragma multi_compile_fog

            #pragma vertex vert
            #pragma fragment frag

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"


            CBUFFER_START(UnityPerMaterial)
                float4 _BaseMap_ST;
                float4 _BaseColor;

                half _DepthFadeNear;
                half _DepthFadeFar;
            CBUFFER_END

            TEXTURE2D(_BaseMap);    SAMPLER(sampler_BaseMap);


            struct Attributes
            {
                float4 positionOS    : POSITION;
                half4 color          : COLOR;   
                float2 uv            : TEXCOORD0;
            };

            struct Varyings
            {
                float4 positionCS    : SV_POSITION;
                float4 color         : COLOR;
                float2 uv            : TEXCOORD0;
                float fogFactor      : TEXCOORD1;
                #if defined(_SOFTPARTICLES_ON)
                    float4 screenPos : TEXCOORD2;
                #endif
            }; 
            

            Varyings vert(Attributes input)
            {
                Varyings output = (Varyings)0;

                float4 positionOS = input.positionOS;
                float3 positionWS = TransformObjectToWorld(positionOS.xyz);
                // float3 positionVS = TransformWorldToView(positionWS);
                float4 positionCS = TransformWorldToHClip(positionWS);

                output.fogFactor = ComputeFogFactor(positionCS.z);
                #if defined(_SOFTPARTICLES_ON)
                    output.screenPos = ComputeScreenPos(positionCS);    // Calc NDC Position
                #endif
                output.positionCS = positionCS;
                output.uv = input.uv;
                output.color = input.color;
                return output;
            }

            float4 frag(Varyings input) : SV_Target
            {
                float depthFade = 1;
                #if defined(_SOFTPARTICLES_ON)
                    float4 screenPos = input.screenPos;
                    float rawDepth = SampleSceneDepth(screenPos.xy / screenPos.w);
                    float sceneDepth = (unity_OrthoParams.w == 0) ? LinearEyeDepth(rawDepth, _ZBufferParams) : LinearDepthToEyeDepth(rawDepth);
                    float thisDepth = LinearEyeDepth(screenPos.z / screenPos.w, _ZBufferParams);
                    depthFade = saturate(_DepthFadeFar * ((sceneDepth - _DepthFadeNear) - thisDepth));
                #endif

                float2 baseMapUV = input.uv.xy * _BaseMap_ST.xy + _BaseMap_ST.zw;
                float4 texColor = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, baseMapUV);
                
                float4 finalColor = texColor * _BaseColor * input.color;
                finalColor.rgb = MixFog(finalColor.rgb, input.fogFactor);
                finalColor.a *= depthFade;
                return finalColor;
            }
            
            ENDHLSL
        }
    }
}

 

 

포스팅 업데이트

 - 23.03.07

URP 쉐이더 코드 스타일로 최신화

MVP 행렬 변환 부분 분할

DepthFade 코드 잘못된 연산 부분 올바르게 수정.

반응형

WRITTEN BY
CatDarkGame
Technical Artist dhwlgn12@gmail.com

,