Unity边缘光shader

shader脚本

新建一个shader脚本,命名为RimLight
Unity边缘光shader
shader代码如下

Shader "CM/RimLight" {
    Properties
    {
        _Color ("Main Color", Color) = (1,1,1,1)
        _MainTex ("Base (RGB)", 2D) = "white" {}
        _IlluminPower("Illumin Power", Range(0, 2)) = 1
        _RimColor ("Rim Color", Color) = (1, 1, 1, 1)
        _RimPower ("Rim Power", Float) = 0.7
    }
    SubShader
    {
        Pass
        {
            Lighting Off
            CGPROGRAM
             
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
 
            uniform sampler2D _MainTex;
            uniform fixed4 _Color;
            uniform float4 _MainTex_ST;
            float _IlluminPower;
            uniform fixed4 _RimColor;
            float _RimPower;
             
            struct appdata {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
                float2 texcoord : TEXCOORD0;
            };
 
            struct v2f {
                float4 pos : SV_POSITION;
                float2 uv : TEXCOORD0;
                fixed3 color : COLOR;
            };
 
            v2f vert (appdata_base v)
            {
                v2f o;
                o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
 
                float3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
                float dotProduct = 1 - dot(v.normal, viewDir);
                
                o.color = _RimColor * pow(dotProduct, _RimPower);//smoothstep(1 - _RimPower, 1.0, dotProduct);
 
                o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
                return o;
            }
 
 
            fixed4 frag(v2f i) : COLOR
            {
                fixed4 texcol = tex2D(_MainTex, i.uv);
                texcol.rgb = texcol.rgb  * _Color.rgb * _IlluminPower + i.color;
                return texcol;
            }
             
            ENDCG
        }
    }
}

制作材质球

新建一个Material,命名为RimLightMaterial
Unity边缘光shader
选择使用刚刚创建的shader
Unity边缘光shader

使用材质球

创建一个球体,把材质球赋值给球体的Mesh Renderer组件的Materials属性
Unity边缘光shader
调整材质球的发光颜色和强度,效果如下
Unity边缘光shader

上一篇:Redis实战(四):redis的消息订阅、pipeline、事务、modules、布隆过滤器、缓存LRU


下一篇:Unity #pragma multi_compile说明