全息效果

作者:追风剑情 发布于:2020-6-16 18:14 分类:Shader

参考 https://blog.csdn.net/xiaoge132/article/details/77801931


  1. //全息效果
  2. Shader "Custom/Silhouette"
  3. {
  4. Properties
  5. {
  6. _MainTex ("Texture", 2D) = "white" {}
  7. _Color("Color", Color) = (1,1,1,1)
  8. _DotProduct("Rim effect", Range(-1,1)) = 0.25
  9. }
  10. SubShader
  11. {
  12. Tags {
  13. "Queue" = "Transparent"
  14. "IgnoreProjector" = "True"
  15. "RenderType" = "Transparent"
  16. "LightMode" = "ForwardBase"
  17. }
  18.  
  19. LOD 200
  20. Cull Off
  21.  
  22. Pass
  23. {
  24. Blend SrcAlpha OneMinusSrcAlpha
  25.  
  26. CGPROGRAM
  27. #pragma vertex vert
  28. #pragma fragment frag
  29.  
  30. #include "UnityCG.cginc"
  31.  
  32. struct appdata
  33. {
  34. float4 vertex : POSITION;
  35. //模型空间法线
  36. float3 normal : NORMAL;
  37. float2 uv : TEXCOORD0;
  38. };
  39.  
  40. struct v2f
  41. {
  42. float2 uv : TEXCOORD0;
  43. float4 vertex : SV_POSITION;
  44. //世界空间法线
  45. float3 worldNormal : TEXCOORD1;
  46. //世界空间坐标
  47. float3 worldPosition : TEXCOORD2;
  48. };
  49.  
  50. sampler2D _MainTex;
  51. float4 _MainTex_ST;
  52. fixed4 _Color;
  53. fixed _DotProduct;
  54.  
  55. v2f vert (appdata v)
  56. {
  57. v2f o;
  58. o.vertex = UnityObjectToClipPos(v.vertex);
  59. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  60. o.worldNormal = UnityObjectToWorldNormal(v.normal);
  61. o.worldPosition = mul(unity_ObjectToWorld, v.vertex).xyz;
  62. return o;
  63. }
  64.  
  65. fixed4 frag (v2f i) : SV_Target
  66. {
  67. fixed3 worldNormal = normalize(i.worldNormal);
  68. fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPosition));
  69.  
  70. //因为viewDir和worldNormal都是归一化矢量,点乘后的值为它们夹角的cos值。
  71. //考虑到半透明物体需要双面渲染, 所以对点乘结果求绝对值abs。
  72. float border = 1 - (abs(dot(viewDir, worldNormal)));
  73. //_DotProduct: 调整透明度
  74. float alpha = (border * (1 - _DotProduct) + _DotProduct);
  75.  
  76. float4 c = tex2D(_MainTex, i.uv) * _Color;
  77. c.a = alpha;
  78. return c;
  79. }
  80. ENDCG
  81. }
  82. }
  83. }


1111.png

标签: Shader

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号