• Unity shader学习之Alpha Test的阴影


    Alpha Test的阴影,

    shader如下:

    // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    
    Shader "Custom/Alpha Test Shadow"
    {
        Properties
        {
            _Color ("Main Color", Color) = (1,1,1,1)          // 必需
            _MainTex("Main Texture", 2D) = "white" {}
            _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5        // 必需
        }
    
        SubShader
        {
            Tags
            {
                "Queue" = "AlphaTest"
                "RenderType" = "TransparentCutout"
            }
    
            Pass
            {
                Tags
                {
                    "LightMode" = "ForwardBase"
                }
                Cull Off                                    // 背面
    
                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                #pragma multi_compile_fwdbase
    
                #include "UnityCG.cginc"
                #include "Lighting.cginc"
                #include "AutoLight.cginc"
    
                float4 _Color;
                sampler2D _MainTex;
                float _Cutoff;
    
                struct appdata
                {
                    float4 vertex : POSITION;
                    float2 uv : TEXCOORD0;
                    float3 normal : NORMAL;
                };
    
                struct v2f
                {
                    float4 pos : SV_POSITION;
                    float2 uv : TEXCOORD0;
                    float3 worldNormal : TEXCOORD1;
                    float3 worldPos : TEXCOORD2;
                    SHADOW_COORDS(3)
                };
    
                v2f vert(appdata v)
                {
                    v2f o;
                    o.pos = UnityObjectToClipPos(v.vertex);
                    o.uv = v.uv;
                    o.worldNormal = UnityObjectToWorldNormal(v.normal);
                    o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
                    TRANSFER_SHADOW(o);
                    return o;
                }
    
                fixed4 frag(v2f i) : SV_TARGET
                {
                    fixed4 albedo = tex2D(_MainTex, i.uv) * _Color;
    
                    clip(albedo.a - _Cutoff);
    
                    fixed3 ambient = albedo.rgb * UNITY_LIGHTMODEL_AMBIENT.rgb;
    
                    float3 worldLight = UnityWorldSpaceLightDir(i.worldPos);
                    fixed3 diff = albedo.rgb * _LightColor0.rgb * max(0, dot(i.worldNormal, worldLight));
    
                    UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);
    
                    fixed3 col = ambient + diff * atten;
    
                    return fixed4(col, 1);
                }
    
                ENDCG
            }
        }
    
        Fallback "Transparent/Cutout/VertexLit"                    // 必需,阴影投射pass
    }

    效果如下:

  • 相关阅读:
    ShopNum1网店系统:组建电子商务运营团队
    jquery 日期+时间 date & time 插件
    写代码如坐禅:你是哪一类程序员
    杨卫华:新浪微博的架构发展历程
    win7 搜索 在新窗口【打开文件位置】
    架构师应该了解的知识1
    flash cs4 和 flex builder 联合开发
    Div拖动/调整大小实例
    asp.net mvc 2.o 中使用JQuery.uploadify
    jquery 拖动改变div 容器大小
  • 原文地址:https://www.cnblogs.com/jietian331/p/7198887.html
Copyright © 2020-2023  润新知