• Unity shader学习之Alpha Blend


    通过 ShaderLab 的 AlphaBlend 能够实现真正的半透明效果。

    使用 AlphaBlend 时,需要关闭深度写入 ZWrite Off,但得非常小心物体的渲染顺序, RenderQueue。

    转载请注明出处: http://www.cnblogs.com/jietian331/p/7158938.html

    shader如下:

    Shader "Custom/Alpha Blend"
    {
        Properties
        {
            _MainTex("Main Texture", 2D) = "white" {}
            _Specular("Specular,", Color) = (1,1,1,1)
            _Gloss("Gloss", Range(8,256)) = 8
        }
    
        SubShader
        {
            Tags
            {
                "RenderType" = "Transparent"
                "Queue" = "Transparent"
                "IgnoreProjector" = "True"
            }
    
            Pass
            {
                Tags
                {
                    "LightMode" = "ForwardBase"
                }
                ZWrite Off
                Blend SrcAlpha OneMinusSrcAlpha
    
                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
    
                #include "UnityCG.cginc"
                #include "Lighting.cginc"
    
                sampler2D _MainTex;
                fixed4 _Specular;
                float _Gloss;
    
                struct appdata
                {
                    float4 vertex : POSITION;
                    fixed4 color : COLOR;
                    float3 normal : NORMAL;
                    float2 uv : TEXCOORD0;
                };
    
                struct v2f
                {
                    float4 pos : SV_POSITION;
                    fixed4 color : COLOR;
                    float2 uv : TEXCOORD0;
                    float3 worldNormal : TEXCOORD1;
                    float3 worldLight : TEXCOORD2;
                    float3 worldView : TEXCOORD3;
                };
    
                v2f vert(appdata v)
                {
                    v2f o;
                    o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                    o.color = v.color;
                    o.uv = v.uv;
                    o.worldNormal = normalize(UnityObjectToWorldNormal(v.normal));
                    o.worldLight = normalize(WorldSpaceLightDir(v.vertex));
                    o.worldView = normalize(WorldSpaceViewDir(v.vertex));
                    return o;
                }
    
                fixed4 frag(v2f i) : SV_TARGET
                {
                    fixed4 albedo = tex2D(_MainTex, i.uv) * i.color;
                    fixed3 ambient = albedo.rgb * UNITY_LIGHTMODEL_AMBIENT.rgb;
                    fixed3 diff = albedo.rgb * _LightColor0.rgb * max(0, dot(i.worldNormal, i.worldLight));
                    float3 halfDir = normalize(i.worldView + i.worldLight);
                    fixed3 spec = albedo.rgb * _Specular.rgb * pow(max(0, dot(halfDir, i.worldNormal)), _Gloss);
                    fixed3 col = ambient + diff + spec;
                    return fixed4(col, albedo.a);
                }
    
                ENDCG
            }
        }
    
        Fallback "Diffuse"
    }

    效果如下:

    但关闭了深度写入之后,会引发一些问题,如:

  • 相关阅读:
    原创 动态卷积
    BZOJ1565 植物大战僵尸
    BZOJ1143 [CTSC2008] 祭祀river
    BZOJ3438 小M的作物
    BZOJ3144 [HNOI2013]切糕
    BZOJ2039 [2009国家集训队]employ人员雇佣
    BZOJ1066[SCOI2007]蜥蜴
    BZOJ3874 codevs3361 宅男计划
    Codeforces Round #343 (Div. 2)
    [转]后缀自动机(SAM)
  • 原文地址:https://www.cnblogs.com/jietian331/p/7158938.html
Copyright © 2020-2023  润新知