123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- Shader "Mirza Beig/Particles/Distortion/Add"
- {
- Properties
- {
- _opacity ("Opacity", range(0.0, 2.0)) = 0.5
-
- _MainTex ("Distortion Texture", 2D) = "white" {}
- _InvFade ("Soft Particles Factor", Range(0.01, 8.0)) = 1.0
- _distortion ("Distortion", range(0.0, 1000.0)) = 10.0
- }
- Category
- {
- // Transparent = other objects drawn before this one.
- Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
- Blend SrcAlpha One
- AlphaTest Greater .01
- ColorMask RGB
- Cull Off Lighting Off ZWrite Off
-
- SubShader {
- // Grab screen behind object into a texture.
- // Accessed in next pass in _GrabTexture.
- GrabPass
- {
- Name "BASE"
- Tags { "LightMode" = "Always" }
- }
-
- // Preturb texture from above using bump map.
- Pass
- {
- Name "BASE"
- Tags { "LightMode" = "Always"
- }
-
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- #pragma multi_compile_particles
- #pragma multi_compile_fog
- #include "UnityCG.cginc"
- sampler2D _MainTex;
- // ...
-
- struct appdata_t
- {
- fixed4 color : COLOR;
- float4 vertex : POSITION;
- float2 uv : TEXCOORD0;
- };
- // ...
- struct v2f
- {
- fixed4 color : COLOR;
- float4 vertex : SV_POSITION;
- float2 uv : TEXCOORD0;
-
- #ifdef SOFTPARTICLES_ON
- float4 uv_depth : TEXCOORD1;
- #endif
- float4 uv_grab : TEXCOORD2;
- UNITY_FOG_COORDS(4)
- };
- // ...
-
- float4 _MainTex_ST;
- v2f vert (appdata_t v)
- {
- v2f o;
- o.vertex = UnityObjectToClipPos(v.vertex);
-
- #ifdef SOFTPARTICLES_ON
- o.uv_depth = ComputeScreenPos(o.vertex);
- COMPUTE_EYEDEPTH(o.uv_depth.z);
- #endif
- o.color = v.color;
- o.uv = TRANSFORM_TEX(v.uv, _MainTex);
- #if UNITY_UV_STARTS_AT_TOP
- float scale = -1.0;
- #else
- float scale = 1.0;
- #endif
-
- o.uv_grab.zw = o.vertex.zw;
- o.uv_grab.xy = (float2(o.vertex.x, o.vertex.y * scale) + o.vertex.w) * 0.5f;
-
- UNITY_TRANSFER_FOG(o, o.vertex);
-
- return o;
- }
- sampler2D_float _CameraDepthTexture;
- float _InvFade;
- float _distortion;
- sampler2D _GrabTexture;
- float4 _GrabTexture_TexelSize;
-
- float _opacity;
- // ...
-
- fixed4 frag (v2f i) : SV_Target
- {
- // Get texture alpha mask.
- float4 texColour = tex2D(_MainTex, i.uv);
- float opacity = texColour.a * _opacity;
- // Soft particles.
-
- #ifdef SOFTPARTICLES_ON
-
- // Scene depth.
-
- //float sceneZ = LinearEyeDepth(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.uv_depth)).r);
- float sceneZ = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.uv_depth)));
-
- // Distance to the camera.
- float partZ = i.uv_depth.z;
- // Soft particles (soft factor).
- // "Comparing depth values of the particle with depth values of world geometry (in view space)." - Special Effects with Depth (Siggraph, 2011).
-
- // float softFactor = saturate((depthEye - zEye) * fade)
-
- // Inverse fade: 0.0f = off.
- opacity *= saturate(_InvFade * (sceneZ - partZ));
- #endif
- // Input colour.
- opacity *= i.color.a;
- //opacity *= i.color.a * 2.0f;
- // Distortion.
- texColour.rg *= texColour.b * 5;
-
- half2 bump = UnpackNormal(texColour).rg;
- float2 offset = bump * _distortion * _GrabTexture_TexelSize.xy;
- i.uv_grab.xy = offset * i.uv_grab.z + i.uv_grab.xy;
- half4 distortionColour = tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(i.uv_grab));
- distortionColour.a = opacity;
- // Fog towards black (additive black = nothing).
- //UNITY_APPLY_FOG_COLOR(i.fogCoord, distortionColour, fixed4(0.0f, 0.0f, 0.0f, 0.0f));
-
- return distortionColour;
- }
- ENDCG
- }
- }
- }
- }
|