注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Faye Euler's blog

技術交流歡迎留言

 
 
 

日志

 
 

unity3d 基于顶点的周围发光效果仅用2版本Planet shader  

2011-08-16 10:11:41|  分类: Unity3D |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

Shader "Planet"
{
 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
    {
     V2F_POS_FOG;
     float3 normal;
     float3 viewdir;
     float2 texcoord;
    };

    v2f vert(appdata_base v)
    {
     v2f o;
     
     PositionFog(v.vertex, o.pos, o.fog);
     o.normal = v.normal;
     o.viewdir = _ObjectSpaceCameraPos-v.vertex;
     o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
     
     return o;
    }
     
    float4 frag(v2f i) : COLOR
    {
     i.normal = normalize(i.normal);
     i.viewdir = normalize(i.viewdir);
     
     float4 atmo = _AtmoColor;
     atmo.a = pow(1.0-saturate(dot(i.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;
    }
   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
    {
     V2F_POS_FOG;
     float3 normal;
     float3 viewdir;
    };

    v2f vert(appdata_base v)
    {
     v2f o;
     
     v.vertex.xyz += v.normal*_Size;
     PositionFog(v.vertex, o.pos, o.fog);
     o.normal = v.normal;
     o.viewdir = v.vertex-_ObjectSpaceCameraPos;
     
     return o;
    }
     
    float4 frag(v2f i) : COLOR
    {
     i.normal = normalize(i.normal);
     i.viewdir = normalize(i.viewdir);
     
     float4 color = _AtmoColor;
     color.a = pow(saturate(dot(i.viewdir, i.normal)), _Falloff);
     color.a *= _Transparency*_Color;
     return color;
    }
   ENDCG
  }
  
  Pass
  {
   Name "PlanetLight"
   Tags {"LightMode" = "Pixel"}
   Cull Back
   Blend AppSrcAdd AppDstAdd
   
   CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag
    
    #pragma multi_compile_builtin
    #pragma fragmentoption ARB_fog_exp2
    #pragma fragmentoption ARB_precision_hint_fastest
    
    #include "UnityCG.cginc"
    #include "AutoLight.cginc"
    
    uniform sampler2D _MainTex;
    uniform float4 _MainTex_ST;
    uniform float4 _AtmoColor;
    uniform float _FalloffPlanet;
    uniform float _TransparencyPlanet;
    
    struct v2f
    {
     V2F_POS_FOG;
     LIGHTING_COORDS
     float3 normal;
     float3 viewdir;
     float3 lightdir;
     float2 texcoord;
    };

    v2f vert(appdata_base v)
    {
     v2f o;
     
     PositionFog(v.vertex, o.pos, o.fog);
     o.normal = v.normal;
     o.viewdir = _ObjectSpaceCameraPos-v.vertex;
     o.lightdir = ObjSpaceLightDir(v.vertex);
     o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
     TRANSFER_VERTEX_TO_FRAGMENT(o);
     
     return o;
    }
     
    float4 frag(v2f i) : COLOR
    {
     i.normal = normalize(i.normal);
     i.viewdir = normalize(i.viewdir);
     i.lightdir = normalize(i.lightdir);
     
     float4 atmo = _AtmoColor;
     atmo.a = pow(1.0-saturate(dot(i.viewdir, i.normal)), _FalloffPlanet);
     atmo.a *= _TransparencyPlanet;
    
     float4 color = tex2D(_MainTex, i.texcoord);
     color.rgb = lerp(color.rgb, atmo.rgb, atmo.a);
     color = DiffuseLight(i.lightdir, i.normal, float4(color.rgb, 1.0), LIGHT_ATTENUATION(i));
     
     return color;
    }
   ENDCG
  }
 
  Pass
  {
   Name "AtmosphereLight"
   Tags {"LightMode" = "Pixel"}
   Cull Front
   Blend SrcAlpha One
   
   CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag
    
    #pragma multi_compile_builtin
    #pragma fragmentoption ARB_fog_exp2
    #pragma fragmentoption ARB_precision_hint_fastest
    
    #include "UnityCG.cginc"
    #include "AutoLight.cginc"
    
    uniform float4 _AtmoColor;
    uniform float _Size;
    uniform float _Falloff;
    uniform float _Transparency;
    
    struct v2f
    {
     V2F_POS_FOG;
     LIGHTING_COORDS
     float3 normal;
     float3 viewdir;
     float3 lightdir;
    };

    v2f vert(appdata_base v)
    {
     v2f o;
     
     v.vertex.xyz += v.normal*_Size;
     PositionFog(v.vertex, o.pos, o.fog);
     o.normal = v.normal;
     o.viewdir = v.vertex-_ObjectSpaceCameraPos;
     o.lightdir = ObjSpaceLightDir(v.vertex);
     TRANSFER_VERTEX_TO_FRAGMENT(o);
     
     return o;
    }
     
    float4 frag(v2f i) : COLOR
    {
     i.normal = normalize(i.normal);
     i.viewdir = normalize(i.viewdir);
     i.lightdir = normalize(i.lightdir);
     
     float4 color = _AtmoColor;
     color.a = pow(saturate(dot(i.viewdir, i.normal)), _Falloff);
     color.a *= _Transparency;
     
     color.a *= DiffuseLight(i.lightdir, i.normal, float4(1.0, 1.0, 1.0, 1.0), LIGHT_ATTENUATION(i));
     
     return color;
    }
   ENDCG
  }
 }
 
 FallBack "Diffuse"
}

  评论这张
 
阅读(1956)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018