zoukankan      html  css  js  c++  java
  • 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"
    }

    效果如下:

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

  • 相关阅读:
    Dungeon Master(BFS)
    Shuffle'm Up(串)
    分解素因子
    Snowflake Snow Snowflakes(查找)
    求素数(素数筛选+打表)
    A simple problem(并查集判环)
    Eqs(枚举+ hash)
    Squares(枚举+set 查找)
    从控制台输入10个人的年龄放入数组,将十个人的年龄求总和
    数组
  • 原文地址:https://www.cnblogs.com/jietian331/p/7158938.html
Copyright © 2011-2022 走看看