软粒子(soft particles)

作者:追风剑情 发布于:2019-1-6 16:53 分类:Shader

参见官方文档 https://docs.unity3d.com/Manual/shader-StandardParticleShaders.html

11111.png

示例

  1. // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
  2.  
  3. Shader "Particles/Alpha Blended" {
  4. Properties {
  5. _TintColor ("Tint Color", Color) = (0.5,0.5,0.5,0.5)
  6. _MainTex ("Particle Texture", 2D) = "white" {}
  7. _InvFade ("Soft Particles Factor", Range(0.01,3.0)) = 1.0
  8. [Toggle(ALPHA_FROM_GRAY_ON)] //是否用灰度值代替alpha值
  9. _AlphaFromGray ("Alpha From Gray", Float) = 0
  10. }
  11.  
  12. Category {
  13. Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "PreviewType"="Plane" }
  14. Blend SrcAlpha OneMinusSrcAlpha
  15. ColorMask RGB
  16. Cull Off Lighting Off ZWrite Off
  17.  
  18. SubShader {
  19. Pass {
  20.  
  21. CGPROGRAM
  22. #pragma vertex vert
  23. #pragma fragment frag
  24. #pragma target 2.0
  25. //开启软粒子功能(编译时会自动定义SOFTPARTICLES_ON宏)
  26. //https://docs.unity3d.com/Manual/shader-StandardParticleShaders.html
  27. #pragma multi_compile_particles
  28. #pragma multi_compile_fog
  29. #pragma shader_feature ALPHA_FROM_GRAY_ON
  30.  
  31. #include "UnityCG.cginc"
  32.  
  33. sampler2D _MainTex;
  34. fixed4 _TintColor;
  35.  
  36. struct appdata_t {
  37. float4 vertex : POSITION;
  38. fixed4 color : COLOR;
  39. float2 texcoord : TEXCOORD0;
  40. UNITY_VERTEX_INPUT_INSTANCE_ID
  41. };
  42.  
  43. struct v2f {
  44. float4 vertex : SV_POSITION;
  45. fixed4 color : COLOR;
  46. float2 texcoord : TEXCOORD0;
  47. UNITY_FOG_COORDS(1)
  48. #ifdef SOFTPARTICLES_ON
  49. float4 projPos : TEXCOORD2;
  50. #endif
  51. UNITY_VERTEX_OUTPUT_STEREO
  52. };
  53.  
  54. float4 _MainTex_ST;
  55.  
  56. v2f vert (appdata_t v)
  57. {
  58. v2f o;
  59. UNITY_SETUP_INSTANCE_ID(v);
  60. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  61. o.vertex = UnityObjectToClipPos(v.vertex);
  62. #ifdef SOFTPARTICLES_ON
  63. o.projPos = ComputeScreenPos (o.vertex);
  64. COMPUTE_EYEDEPTH(o.projPos.z);
  65. #endif
  66. o.color = v.color * _TintColor;
  67. o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
  68. UNITY_TRANSFER_FOG(o,o.vertex);
  69. return o;
  70. }
  71.  
  72. UNITY_DECLARE_DEPTH_TEXTURE(_CameraDepthTexture);
  73. float _InvFade;
  74.  
  75. fixed4 frag (v2f i) : SV_Target
  76. {
  77. #ifdef SOFTPARTICLES_ON
  78. float sceneZ = LinearEyeDepth (SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)));
  79. float partZ = i.projPos.z;
  80. float fade = saturate (_InvFade * (sceneZ-partZ));
  81. i.color.a *= fade;
  82. #endif
  83.  
  84. fixed4 col = 2.0f * i.color * tex2D(_MainTex, i.texcoord);
  85. col.a = saturate(col.a); // alpha should not have double-brightness applied to it, but we can't fix that legacy behaior without breaking everyone's effects, so instead clamp the output to get sensible HDR behavior (case 967476)
  86. #if ALPHA_FROM_GRAY_ON
  87. col.a = Luminance(col.rgb);
  88. #endif
  89. UNITY_APPLY_FOG(i.fogCoord, col);
  90. return col;
  91. }
  92. ENDCG
  93. }
  94. }
  95. }
  96. }


效果

333.png2222.png

标签: Shader

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号