ShaderLab偏移量未在另一个多边形上方渲染

时间:2019-05-26 04:01:39

标签: unity3d shader shaderlab

我有两个着色器,UnlitRGBA和TransparentNoOverlap。它们如下:

UnlitRGBA.shaderlab

// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
// modified by rbjacob101

Shader "Particles/Standard Unlit (RGBA)"
{
    Properties
    {
        _MainTex("Albedo", 2D) = "white" {}
    _Color("Color", Color) = (1,1,1,1)

        _Cutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5

        _BumpScale("Scale", Float) = 1.0
        _BumpMap("Normal Map", 2D) = "bump" {}

    _EmissionColor("Color", Color) = (0,0,0)
        _EmissionMap("Emission", 2D) = "white" {}

    _DistortionStrength("Strength", Float) = 1.0
        _DistortionBlend("Blend", Range(0.0, 1.0)) = 0.5

        _SoftParticlesNearFadeDistance("Soft Particles Near Fade", Float) = 0.0
        _SoftParticlesFarFadeDistance("Soft Particles Far Fade", Float) = 1.0
        _CameraNearFadeDistance("Camera Near Fade", Float) = 1.0
        _CameraFarFadeDistance("Camera Far Fade", Float) = 2.0

        // Hidden properties
        [HideInInspector] _Mode("__mode", Float) = 0.0
        [HideInInspector] _ColorMode("__colormode", Float) = 0.0
        [HideInInspector] _FlipbookMode("__flipbookmode", Float) = 0.0
        [HideInInspector] _LightingEnabled("__lightingenabled", Float) = 0.0
        [HideInInspector] _DistortionEnabled("__distortionenabled", Float) = 0.0
        [HideInInspector] _EmissionEnabled("__emissionenabled", Float) = 0.0
        [HideInInspector] _BlendOp("__blendop", Float) = 0.0
        [HideInInspector] _SrcBlend("__src", Float) = 1.0
        [HideInInspector] _DstBlend("__dst", Float) = 0.0
        [HideInInspector] _ZWrite("__zw", Float) = 1.0
        [HideInInspector] _Cull("__cull", Float) = 2.0
        [HideInInspector] _SoftParticlesEnabled("__softparticlesenabled", Float) = 0.0
        [HideInInspector] _CameraFadingEnabled("__camerafadingenabled", Float) = 0.0
        [HideInInspector] _SoftParticleFadeParams("__softparticlefadeparams", Vector) = (0,0,0,0)
        [HideInInspector] _CameraFadeParams("__camerafadeparams", Vector) = (0,0,0,0)
        [HideInInspector] _ColorAddSubDiff("__coloraddsubdiff", Vector) = (0,0,0,0)
        [HideInInspector] _DistortionStrengthScaled("__distortionstrengthscaled", Float) = 0.0
    }

        Category
    {
        SubShader
    {
        Tags{ "RenderType" = "Opaque" "IgnoreProjector" = "True" "PreviewType" = "Plane" "PerformanceChecks" = "False" }

        BlendOp[_BlendOp]
        Blend[_SrcBlend][_DstBlend]
        ZWrite[_ZWrite]
        Offset -1, -1
        Cull[_Cull]
        ColorMask RGBA

        GrabPass
    {
        Tags{ "LightMode" = "Always" }
        "_GrabTexture"
    }

        Pass
    {
        Name "ShadowCaster"
        Tags{ "LightMode" = "ShadowCaster" }

        BlendOp Add
        Blend One Zero
        ZWrite On
        Cull Off

        CGPROGRAM
#pragma target 2.5

#pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON _ALPHAMODULATE_ON
#pragma shader_feature _ _COLOROVERLAY_ON _COLORCOLOR_ON _COLORADDSUBDIFF_ON
#pragma shader_feature _REQUIRE_UV2
#pragma multi_compile_shadowcaster
#pragma multi_compile_instancing
#pragma instancing_options procedural:vertInstancingSetup

#pragma vertex vertParticleShadowCaster
#pragma fragment fragParticleShadowCaster

#include "UnityStandardParticleShadow.cginc"
        ENDCG
    }

        Pass
    {
        Tags{ "LightMode" = "ForwardBase" }

        CGPROGRAM
#pragma multi_compile __ SOFTPARTICLES_ON
#pragma multi_compile_fog
#pragma target 2.5

#pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON _ALPHAMODULATE_ON
#pragma shader_feature _ _COLOROVERLAY_ON _COLORCOLOR_ON _COLORADDSUBDIFF_ON
#pragma shader_feature _NORMALMAP
#pragma shader_feature _EMISSION
#pragma shader_feature _FADING_ON
#pragma shader_feature _REQUIRE_UV2
#pragma shader_feature EFFECT_BUMP

#pragma vertex vertParticleUnlit
#pragma fragment fragParticleUnlit
#pragma multi_compile_instancing
#pragma instancing_options procedural:vertInstancingSetup

#include "UnityStandardParticles.cginc"
        ENDCG
    }
    }
    }

        Fallback "VertexLit"
        CustomEditor "StandardParticlesShaderGUI"
}

TransparentNoOverlap.shader

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

/*
 * Taken from:
 * https://forum.unity.com/threads/transparent-depth-shader-good-for-ghosts.149511/
 */

Shader "Transparent/TransparentNoOverlap" {
Properties {
     [PerRendererData]_Color ("Main Color", Color) = (1,1,1,1)
    _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
}

SubShader {
    Tags {"RenderType"="Transparent" "Queue"="Transparent" "IgnoreProjector"="True"}
    LOD 200

    Pass {
        Name "TRANSPARENT"

        ZWrite On
        ZTest LEqual
        Blend SrcAlpha OneMinusSrcAlpha
        ColorMask RGBA

        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag
        #include "UnityCG.cginc"

        fixed4 _Color;

        struct v2f {
            float4 pos : SV_POSITION;
        };

        v2f vert (appdata_base v)
        {
            v2f o;
            o.pos = UnityObjectToClipPos (v.vertex);
            return o;
        }

        half4 frag (v2f i) : COLOR
        {
            return _Color;
        }
        ENDCG  
    }

    CGPROGRAM
    #pragma surface surf Lambert alpha
    #pragma debug

    sampler2D _MainTex;
    fixed4 _Color;

    struct Input {
        float2 uv_MainTex;
    };

    void surf (Input IN, inout SurfaceOutput o) {
        fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
        o.Albedo = c.rgba;
        o.Alpha = c.a;
    }
    ENDCG
}

Fallback "Transparent/Diffuse"
}

我希望使用UnlitRGBA的材质始终在TransparentNoOverlap上渲染。我要怎么做才能达到这个结果?我尝试将Offset -1, -1Offset 0, -1添加到UnlitRGBA。在标准表面着色器上进行测试时,它可以正常工作,并且UnlitRGBA始终在顶部渲染。但是,当使用TransparentNoOverlap进行渲染时,要么进行z格斗,要么使用TransparentNoOverlap始终在顶部进行渲染。为什么是这样?我认为这可能与ZTests或ZWrites有关,但我最终不确定。

对于大型代码示例,我深表歉意。感谢您提供的任何帮助。

rbjacob

1 个答案:

答案 0 :(得分:1)

请注意,预期结果和设置略有歧义,因此可能有几种方法。

简单地说:带有着色器A(未点亮)的材质被B(透明)遮盖了,因为B是在其后绘制的。 “ Queue” =“透明” 是负责B中该效果的标志。 着色器A没有它,因此它使用默认的“ Geometry” 队列。 (您可以在https://docs.unity3d.com/Manual/SL-SubShaderTags.html中了解有关引擎盖下使用的索引值)。 因此,在这方面,您可以明确指定队列索引,并确保它们与您的预期行为相符。

这无视另一个方面:ZBuffer可以处理一个网格相互遮挡的情况,因此实际上不依赖于绘制顺序。可以根据深度测试丢弃像素,如果指定了深度,则可以按对象写入深度。 (https://docs.unity3d.com/Manual/SL-CullAndDepth.html

如果网格物体都进行写入和测试,并且它们是共面的(在同一位置),或者缓冲区的精度较低,则网格将显示z形战斗。

假设两个网格都需要被遮挡,而其他网格和它们自己也必须被遮挡,一种方法是将B(透明)设置为使用 ZTest Less ,这样它仅在不存在A时出现即使在A之后绘制,甚至与A共面。