首页 > Unity3D专栏 > 【Unity3D】 Unity3D Shader之外发光材质
2017
07-25

【Unity3D】 Unity3D Shader之外发光材质

Shader "Faye/OutLightting"  
{  
    Properties  
    {  
        _MainTex("Texture (RGB)", 2D) = "black" {}  
        _Color("Color", Color) = (0, 0, 0, 1)  
        _AtmoColor("Atmosphere Color", Color) = (0.5, 0.5, 1.0, 1)  
        _Size("Size", Float) = 0.1  
        _Falloff("Falloff", Float) = 5  
        _FalloffPlanet("Falloff Planet", Float) = 5  
        _Transparency("Transparency", Float) = 15  
        _TransparencyPlanet("Transparency Planet", Float) = 1  
    }  
      
 SubShader  
    {  
        Pass  
        {  
            Name "PlanetBase"  
            Tags {"LightMode" = "Always"}  
            Cull Back  
              
            CGPROGRAM  
                #pragma vertex vert  
                #pragma fragment frag  
                 
                #pragma fragmentoption ARB_fog_exp2  
                #pragma fragmentoption ARB_precision_hint_fastest  
                 
                #include "UnityCG.cginc"  
                  
                uniform sampler2D _MainTex;  
                uniform float4 _MainTex_ST;  
                uniform float4 _Color;  
                uniform float4 _AtmoColor;  
                uniform float _FalloffPlanet;  
                uniform float _TransparencyPlanet;  
                  
                struct v2f  
                {  
                    float4 pos : SV_POSITION;  
                    float3 normal : TEXCOORD0;  
                    float3 worldvertpos : TEXCOORD1;  
                    float2 texcoord : TEXCOORD2;  
                };  
   
                v2f vert(appdata_base v)  
                {  
                    v2f o;  
                      
                    o.pos = mul (UNITY_MATRIX_MVP, v.vertex);  
                    o.normal = v.normal;  
                    o.worldvertpos = mul(_Object2World, v.vertex).xyz;  
                    o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);  
                      
                    return o;  
                }  
                 
                float4 frag(v2f i) : COLOR  
                {  
                    i.normal = normalize(i.normal);  
                    float3 viewdir = normalize(_WorldSpaceCameraPos-i.worldvertpos);  
                      
                    float4 atmo = _AtmoColor;  
                    atmo.a = pow(1.0-saturate(dot(viewdir, i.normal)), _FalloffPlanet);  
                    atmo.a *= _TransparencyPlanet*_Color;  
                  
                    float4 color = tex2D(_MainTex, i.texcoord)*_Color;  
                    color.rgb = lerp(color.rgb, atmo.rgb, atmo.a);  
                  
                    return color*dot(normalize(i.worldvertpos-_WorldSpaceLightPos0), i.normal);  
                }  
            ENDCG  
        }  
      
        Pass  
        {  
            Name "AtmosphereBase"  
            Tags {"LightMode" = "Always"}  
            Cull Front  
            Blend SrcAlpha One  
              
            CGPROGRAM  
                #pragma vertex vert  
                #pragma fragment frag  
                 
                #pragma fragmentoption ARB_fog_exp2  
                #pragma fragmentoption ARB_precision_hint_fastest  
                 
                #include "UnityCG.cginc"  
                  
                uniform float4 _Color;  
                uniform float4 _AtmoColor;  
                uniform float _Size;  
                uniform float _Falloff;  
                uniform float _Transparency;  
                  
                struct v2f  
                {  
                    float4 pos : SV_POSITION;  
                    float3 normal : TEXCOORD0;  
                    float3 worldvertpos : TEXCOORD1;  
                };  
   
                v2f vert(appdata_base v)  
                {  
                    v2f o;  
                      
                    v.vertex.xyz += v.normal*_Size;  
                    o.pos = mul (UNITY_MATRIX_MVP, v.vertex);  
                    o.normal = v.normal;  
                    o.worldvertpos = mul(_Object2World, v.vertex);  
                      
                    return o;  
                }  
                 
                float4 frag(v2f i) : COLOR  
                {  
                    i.normal = normalize(i.normal);  
                    float3 viewdir = normalize(i.worldvertpos-_WorldSpaceCameraPos);  
                      
                    float4 color = _AtmoColor;  
                    color.a = pow(saturate(dot(viewdir, i.normal)), _Falloff);  
                    color.a *= _Transparency*_Color*dot(normalize(i.worldvertpos-_WorldSpaceLightPos0), i.normal);  
                    return color;  
                }  
            ENDCG  
        }  
    }  
      
    FallBack "Diffuse"  
} 

 



虚拟世界提醒您:如果您觉得本文不错,快快将这篇文章分享出去吧 ,感谢您的支持和关注,谢谢!
最后编辑:
作者:虚拟世界
专注互联网技术及资讯 技术研究中心|有相关Unity3D问题或者作事宜请联系我哦!
捐 赠如果您愿意花10块钱请我喝一杯咖啡的话,请用手机扫描二维码即可通过支付宝或者微信直接向我捐款,在此非常感谢您对虚拟世界的捐赠。

留下一个回复

你的email不会被公开。