Unity3dShader邊緣發(fā)光效果

shader知識(shí):http://imgtec.eetrend.com/blogs/%E5%A2%A8%E5%8D%8A%E6%88%90%E9%9C%9C

最終效果
Shader "Learning Unity Shader/Lecture 14/Basic Rim Shader"  
{  
    //-----------------------------------【屬性 || Properties】------------------------------------------   
    Properties  
    {  
        //主顏色 || Main Color  
        _MainColor("【主顏色】Main Color", Color) = (0.5,0.5,0.5,1)  
        //漫反射紋理 || Diffuse Texture  
        _TextureDiffuse("【漫反射紋理】Texture Diffuse", 2D) = "white" {}   
        //邊緣發(fā)光顏色 || Rim Color  
        _RimColor("【邊緣發(fā)光顏色】Rim Color", Color) = (0.5,0.5,0.5,1)  
        //邊緣發(fā)光強(qiáng)度 ||Rim Power  
        _RimPower("【邊緣發(fā)光強(qiáng)度】Rim Power", Range(0.0, 36)) = 0.1  
        //邊緣發(fā)光強(qiáng)度系數(shù) || Rim Intensity Factor  
        _RimIntensity("【邊緣發(fā)光強(qiáng)度系數(shù)】Rim Intensity", Range(0.0, 100)) = 3  
    }  
   
    //----------------------------------【子著色器 || SubShader】---------------------------------------   
    SubShader  
    {  
        //渲染類型為Opaque,不透明 || RenderType Opaque  
        Tags  
        {  
            "RenderType" = "Opaque" 
        }  
   
        //---------------------------------------【唯一的通道 || Pass】------------------------------------  
        Pass  
        {  
            //設(shè)定通道名稱 || Set Pass Name  
            Name "ForwardBase" 
   
            //設(shè)置光照模式 || LightMode ForwardBase  
            Tags  
            {  
                "LightMode" = "ForwardBase" 
            }  
   
            //-------------------------開(kāi)啟CG著色器編程語(yǔ)言段 || Begin CG Programming Part----------------------   
            CGPROGRAM  
   
                //【1】指定頂點(diǎn)和片段著色函數(shù)名稱 || Set the name of vertex and fragment shader function  
                #pragma vertex vert  
                #pragma fragment frag  
   
                //【2】頭文件包含 || include  
                #include "UnityCG.cginc"  
                #include "AutoLight.cginc"  
   
                //【3】指定Shader Model 3.0 || Set Shader Model 3.0  
                #pragma target 3.0  
   
                //【4】變量聲明 || Variable Declaration  
                //系統(tǒng)光照顏色  
                uniform float4 _LightColor0;  
                //主顏色  
                uniform float4 _MainColor;  
                //漫反射紋理  
                uniform sampler2D _TextureDiffuse;   
                //漫反射紋理_ST后綴版  
                uniform float4 _TextureDiffuse_ST;  
                //邊緣光顏色  
                uniform float4 _RimColor;  
                //邊緣光強(qiáng)度  
                uniform float _RimPower;  
                //邊緣光強(qiáng)度系數(shù)  
                uniform float _RimIntensity;  
   
                //【5】頂點(diǎn)輸入結(jié)構(gòu)體 || Vertex Input Struct  
                struct VertexInput   
                {  
                    //頂點(diǎn)位置 || Vertex position  
                    float4 vertex : POSITION;  
                    //法線向量坐標(biāo) || Normal vector coordinates  
                    float3 normal : NORMAL;  
                    //一級(jí)紋理坐標(biāo) || Primary texture coordinates  
                    float4 texcoord : TEXCOORD0;  
                };  
   
                //【6】頂點(diǎn)輸出結(jié)構(gòu)體 || Vertex Output Struct  
                struct VertexOutput   
                {  
                    //像素位置 || Pixel position  
                    float4 pos : SV_POSITION;  
                    //一級(jí)紋理坐標(biāo) || Primary texture coordinates  
                    float4 texcoord : TEXCOORD0;  
                    //法線向量坐標(biāo) || Normal vector coordinates  
                    float3 normal : NORMAL;  
                    //世界空間中的坐標(biāo)位置 || Coordinate position in world space  
                    float4 posWorld : TEXCOORD1;  
                    //創(chuàng)建光源坐標(biāo),用于內(nèi)置的光照 || Function in AutoLight.cginc to create light coordinates  
                    LIGHTING_COORDS(3,4)  
                };  
   
                //【7】頂點(diǎn)著色函數(shù) || Vertex Shader Function  
                VertexOutput vert(VertexInput v)   
                {  
                    //【1】聲明一個(gè)頂點(diǎn)輸出結(jié)構(gòu)對(duì)象 || Declares a vertex output structure object  
                    VertexOutput o;  
   
                    //【2】填充此輸出結(jié)構(gòu) || Fill the output structure  
                    //將輸入紋理坐標(biāo)賦值給輸出紋理坐標(biāo)  
                    o.texcoord = v.texcoord;  
                    //獲取頂點(diǎn)在世界空間中的法線向量坐標(biāo)   
                    o.normal = mul(float4(v.normal,0), _World2Object).xyz;  
                    //獲得頂點(diǎn)在世界空間中的位置坐標(biāo)   
                    o.posWorld = mul(_Object2World, v.vertex);  
                    //獲取像素位置  
                    o.pos = mul(UNITY_MATRIX_MVP, v.vertex);  
   
                    //【3】返回此輸出結(jié)構(gòu)對(duì)象  || Returns the output structure  
                    return o;  
                }  
   
                //【8】片段著色函數(shù) || Fragment Shader Function  
                fixed4 frag(VertexOutput i) : COLOR  
                {  
                    //【8.1】方向參數(shù)準(zhǔn)備 || Direction  
                    //視角方向  
                    float3 ViewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);  
                    //法線方向  
                    float3 Normalection = normalize(i.normal);  
                    //光照方向  
                    float3 LightDirection = normalize(_WorldSpaceLightPos0.xyz);  
   
                    //【8.2】計(jì)算光照的衰減 || Lighting attenuation  
                    //衰減值  
                    float Attenuation = LIGHT_ATTENUATION(i);  
                    //衰減后顏色值  
                    float3 AttenColor = Attenuation * _LightColor0.xyz;  
   
                    //【8.3】計(jì)算漫反射 || Diffuse  
                    float NdotL = dot(Normalection, LightDirection);  
                    float3 Diffuse = max(0.0, NdotL) * AttenColor + UNITY_LIGHTMODEL_AMBIENT.xyz;  
   
                    //【8.4】準(zhǔn)備自發(fā)光參數(shù) || Emissive  
                    //計(jì)算邊緣強(qiáng)度  
                    half Rim = 1.0 - max(0, dot(i.normal, ViewDirection));  
                    //計(jì)算出邊緣自發(fā)光強(qiáng)度  
                    float3 Emissive = _RimColor.rgb * pow(Rim,_RimPower) *_RimIntensity;  
   
                    //【8.5】計(jì)在最終顏色中加入自發(fā)光顏色 || Calculate the final color  
                    //最終顏色 = (漫反射系數(shù) x 紋理顏色 x rgb顏色)+自發(fā)光顏色 || Final Color=(Diffuse x Texture x rgbColor)+Emissive  
                    float3 finalColor = Diffuse * (tex2D(_TextureDiffuse,TRANSFORM_TEX(i.texcoord.rg, _TextureDiffuse)).rgb*_MainColor.rgb) + Emissive;  
                   
                    //【8.6】返回最終顏色 || Return final color  
                    return fixed4(finalColor,1);  
                }  
   
            //-------------------結(jié)束CG著色器編程語(yǔ)言段 || End CG Programming Part------------------   
            ENDCG  
        }  
    }  
   
    //后備著色器為普通漫反射 || Fallback use Diffuse  
    FallBack "Diffuse" 
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容