Unity Shader realizes translucent shadow

In the shader, if you want to be compatible with the mobile terminal, if you don’t want to achieve two sets of separate compatibility,

#pragma exclude_renderers gles gles3 glcore
#pragma target 4.5

These two sentences must be changed. The first line of code directly removes the rendering of gles, but most mobile terminals use gles (Android platform), so the first line must be removed. The second line is for ShaderMod, that is, some new features of the shader, which can be lowered. The built-in lit implements two sets of high-quality ones to match the PC. A set of high-performance rendering is specially written for the gles platform. .
If we need multi-terminal compatibility, we recommend a target value of 3 or 3.5 to give up some incompatible features.

Currently, unity URP handles translucent objects: it cannot participate in the rendering of Shadow Map, Depth, and Depth Normal, and can be viewed in the material Debug mode.
And translucency cannot produce shadows, screen space shadows (this requires depth), SSAO
translucent shadows: use the UnityDitherMask3D texture to solve
the implementation:
first define the macro in the head of ShadowPass:

#if (SHADER_API_D3D11 || SHADER_API_GLCORE || SHADER_API_GLES3 || SHADER_API_METAL || SHADER_API_VULKAN)
    #define CAN_SKIP_VPOS
#endif

Used to determine whether this method is currently supported.

Declare a texture, which is a 3D texture, provided by unity

TEXTURE3D(_DitherMaskLOD); SAMPLER(sampler_DitherMaskLOD);

Then add parameters above the fragment shader:

half4 ShadowPassFragment(Varyings input
#if !defined(CAN_SKIP_VPOS)
    , UNITY_VPOS_TYPE vpos : VPOS
#endif
) : SV_TARGET
{
    
    

This is the location where the screen space can be obtained directly.

Finally, add noise-related operations in the shader:

half4 ShadowPassFragment(Varyings input
#if !defined(CAN_SKIP_VPOS)
    , UNITY_VPOS_TYPE vpos : VPOS
#endif
) : SV_TARGET
{
    
    
    half4 BaseColorAlpha = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.uv) * _BaseColor;
    half3 BaseColor = BaseColorAlpha.rgb;
    half BaseAlpha = BaseColorAlpha.a;
    #if defined(_ALPHATEST_ON)
        clip(BaseAlpha - _Cutoff);
    #endif

    #if defined(_SURFACE_TYPE_TRANSPARENT)
        #if defined(CAN_SKIP_VPOS)
            float2 vpos = input.positionCS;
        #endif
        half alphaRef = SAMPLE_TEXTURE3D(_DitherMaskLOD, sampler_DitherMaskLOD, float3(vpos.xy * 0.25, BaseAlpha * 0.9375)).a;
        clip(alphaRef - 0.01);
    #endif

    return 0;
}

insert image description here
Note that the ShadowPass for viewing the material should not be disabled, otherwise the shadow cannot be rendered.
insert image description here
You can see that when the transparency is set to 1, it has the same effect as the opacity.
insert image description here
Modify the transparency, not only the model becomes transparent, but also the shadow becomes transparent.

The principle of its implementation is not really transparent, it is realized through Dither, the noise map that will be updated all the time, and then the simulated translucent effect.

Guess you like

Origin blog.csdn.net/qq_30100043/article/details/130400857