3D残影Shader

前段时间有人说想要个残影的代码

今天给大家分享下这个是效果

下面几张是不同透明度的

好了上菜吧

  1. Shader "Phantom" {
  2. Properties {
  3. _normalMap ("normal map", 2D) = "bump" {} //法线贴图
  4. _rimColor ("Rim Color", Color) = (1, 1, 1, 1)//颜色
  5. _power ("power", Float) = 1//明暗程度
  6. }
  7. SubShader {
  8. Tags
  9. {
  10. "RenderType"="Transparent"
  11. "LightMode"="ForwardBase"
  12. "Queue"="Transparent"
  13. }
  14.  
  15. LOD 200
  16.  
  17. Pass {
  18. Name "ForwardBase"
  19.  
  20. Tags {
  21. "LightMode"="ForwardBase"
  22. }
  23.  
  24. Blend SrcAlpha OneMinusSrcAlpha
  25. //Blend SrcAlpha One
  26.  
  27. CGPROGRAM
  28. #pragma vertex vert
  29. #pragma fragment frag
  30. //#define UNITY_PASS_FORWARDBASE
  31. #include "UnityCG.cginc"
  32. #include "AutoLight.cginc"
  33. //#pragma multi_compile_fwdbase_fullshadows
  34. //#pragma exclude_renderers xbox360 ps3 flash d3d11_9x
  35. //#pragma target 3.0
  36.  
  37. uniform sampler2D _normalMap; uniform float4 _normalMap_ST;
  38. uniform float4 _rimColor;
  39. uniform float _power;
  40.  
  41. struct VertexInput {
  42. float4 vertex : POSITION;
  43. float3 normal : NORMAL;
  44. float4 tangent : TANGENT;
  45. float2 texcoord0 : TEXCOORD0;
  46. };
  47.  
  48. struct VertexOutput {
  49. float4 pos : SV_POSITION;
  50. float2 uv0 : TEXCOORD0;
  51. float4 posWorld : TEXCOORD1;
  52. float3 normalDir : TEXCOORD2;
  53. float3 tangentDir : TEXCOORD3;
  54. float3 binormalDir : TEXCOORD4;
  55. float3 eyeDir : TEXCOORD5;
  56. LIGHTING_COORDS(10,11)
  57. };
  58.  
  59. VertexOutput vert (VertexInput v) {
  60. VertexOutput o;
  61. o.uv0 = v.texcoord0;
  62. o.normalDir = mul(float4(v.normal,0), _World2Object).xyz;
  63. o.tangentDir = normalize( mul( _Object2World, float4( v.tangent.xyz, 0.0 ) ).xyz );
  64. o.binormalDir = normalize(cross(o.normalDir, o.tangentDir) * v.tangent.w);
  65. o.posWorld = mul(_Object2World, v.vertex);
  66. o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  67.  
  68. // Eye direction vector
  69. o.eyeDir.xyz = normalize( _WorldSpaceCameraPos.xyz - mul( _Object2World, v.vertex )).xyz;
  70.  
  71. TRANSFER_VERTEX_TO_FRAGMENT(o)
  72. return o;
  73. }
  74.  
  75. float4 frag(VertexOutput i) : COLOR
  76. {
  77. float3x3 tangentTransform = float3x3( i.tangentDir, i.binormalDir, i.normalDir);
  78. float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
  79. /////// Normals:
  80. float2 node_64 = i.uv0;
  81. float3 normalLocal = UnpackNormal(tex2D(_normalMap,TRANSFORM_TEX(node_64.rg, _normalMap))).rgb;
  82. float3 normalDirection = normalize(mul( normalLocal, tangentTransform )); // Perturbed normals
  83. float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
  84. float3 halfDirection = normalize(viewDirection + lightDirection);
  85. ////// Fallout:
  86. float normalDotEye = 1 - dot( normalDirection, i.eyeDir.xyz );
  87. float4 retColor = float4(_rimColor.r, _rimColor.g, _rimColor.b, _rimColor.a * pow(normalDotEye, _power));
  88. return retColor;
  89. }
  90. ENDCG
  91. }
  92. }
  93. FallBack "Diffuse"
  94. }![](/upload/201510/12/201510122324391263.jpg)
  1. 只要拖入一张角色的法线贴图就OK
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值