窗户雨滴效果

Lui2019 发表于    2020年08月23日 13:52
3222
1
21

转https://zhuanlan.zhihu.com/p/62393096 黄小明

被甩后一个人打车看着窗外下大雨之雨滴玻璃shader【P2】 

时光飞逝,岁月如梭~

来~我们开始第二部分,记得还是要保持一颗失恋的心

PS:我是认真的:)

被甩后一个人打车看着窗外下大雨之雨滴玻璃shader【P2】 

-------------------

先给贴上Part1部分我连的shaderforge

ShaderForge的话虽然说制作时候效果观察很方便,但是复杂的话不太好连,而且不好检查,有时候错了一下子都找不到头,不过如果不能写码的话就只能连线了

其实上一个部分我觉得最重要的是计算水滴+拖尾的位置以及大小的部分,然后就是X,Y值的偏移计算,这两个部分好好看看还是很有意思的,一步步先来画格子,算水滴然后再添加位移。

被甩后一个人打车看着窗外下大雨之雨滴玻璃shader【P2】
如果看不清,就去链接下载吧,ShaderForge制作的shader和流程图都放里面了

https://pan.baidu.com/s/1AZ1ocqC4CDYQccJt3sXI4g 密码:wm1t

-------------------

先说下这部分做什么,首先我们需要作出模糊,然后一层水滴肯定是不够的,不够丰富,而且也不够随机,所以我们需要添加多层水滴,再然后就是做真实玻璃的透明。

好的我们开始,回到上一部分结尾的代码上

  1. fixed4 frag (v2f i) : SV_Target

  2.                         {        

  3.                                 float t = fmod(_Time.y,7200);

  4.                                 float4 col =0;

  5.                                 float2 aspect = float2(2, 1);

  6.                                 float2 uv = i.uv*_Size*aspect;


  7.                                 uv.y += t * 0.25;

  8.                                 float2 gv = frac(uv)-0.5;

  9.                                 float2 id = floor(uv); 

  10.                         


  11.                                 float n =N21(id);


  12.                                 t+= n*6.28631;



  13.                                 float w = i.uv.y *10;


  14.                                 float x = (n-0.5)*0.8;


  15.                                 x += (0.4-abs(x))*sin(3*w)*pow(sin(w),6)*0.45;

  16.                                 float y =-sin(t+sin(t+sin(t)*0.5))*0.45;


  17.                                 y -= (gv.x-x)*(gv.x-x);

  18.                         


  19.                                 float2 dropPos =(gv - float2(x, y))/aspect;

  20.                                 float drop = smoothstep(0.05,0.03,length(dropPos));


  21.                 

  22.                                 float2 dropTrailPos = (gv - float2(x, t*0.25)) / aspect;

  23.                                 dropTrailPos.y = (frac(dropTrailPos.y* 8)/8)-0.03;

  24.                                 float dropTrail = smoothstep(0.03, 0.02, length(dropTrailPos));

  25.                                 float fogTrail = smoothstep(-0.05, 0.05, dropPos.y);

  26.                                 fogTrail *= smoothstep(0.5, y, gv.y);

  27.                                 dropTrail *= fogTrail;


  28.                                 fogTrail *= smoothstep(0.05,0.04,abs(dropPos.x));


  29.                                 col += fogTrail*0.5;

  30.                                 col += dropTrail;

  31.                                 col += drop;


  32.                                 

  33.                                 float2 offs = drop*dropPos+dropTrail*dropTrailPos;

  34.                                 col = tex2D(_MainTex,i.uv+offs*_Distortion);

  35.                                 return col;

  36.                         }

复制代码



我们先添加模糊效果,我们就不自己计算模糊了,直接用Mipmap然后在shader中使用Tex2Dlod,一定要注意使用的图片有没有打开MipMap,然后就是FilterMode的选择如果选择了Trilinear,point会产生块面模糊(像素风格), Bilinear是逐步模糊(阶梯化渐变),trilinear是会在两个模糊度上平滑过渡

被甩后一个人打车看着窗外下大雨之雨滴玻璃shader【P2】 

做好以后我们修改下代码,如果没有模糊变化的话,检查下Texture的设置吧


  1. col = tex2Dlod(_MainTex,float4 (i.uv + offs * _Distortion,0,4));

复制代码



https://docs.microsoft.com/zh-cn ... phics-hlsl-tex2dlod

接下来我们给一个_Blur值来控制模糊程度

  1. float blur = _Blur * 7;

  2.            float2 offs = drop * dropPos + dropTrail * dropTrailPos;

  3.             col = tex2Dlod(_MainTex,float4 (i.uv + offs * _Distortion,0, blur));

  4.            return col;

复制代码



被甩后一个人打车看着窗外下大雨之雨滴玻璃shader【P2】 
目前的模糊结果

因为正常水滴划过的地方会更加清晰,所以我们来处理下

  1. //fogTrail是水滴的拖尾因为我们需要拖尾处清楚所以我们需要取个反减值

  2.         float blur = _Blur * 7 * (1-fogTrail);

复制代码



被甩后一个人打车看着窗外下大雨之雨滴玻璃shader【P2】 
现在得到的模糊效果

现在我们第一个部分模糊已经处理好了,开始制作多层水滴,新建一个Layer struct 输入一个uv值和t值返回我们需要的offs,fogTrail。

注意:i.uv需要换成UV了,还有一些col现在没有了,然后改一下frag里面的参数就行

每一个drops都是一层水滴,只要修改输入的UV值就可以得到大小不一位置不一样的水滴了

  1. float3 Layer(float2 UV,float t){

  2.                                         float2 aspect = float2(2, 1);

  3.                                         float2 uv = UV*_Size*aspect;


  4.                                         uv.y += t * 0.25;

  5.                                         float2 gv = frac(uv) - 0.5;

  6.                                         float2 id = floor(uv);



  7.                                         float n = N21(id);


  8.                                         t += n * 6.28631;



  9.                                         float w = UV.y * 10;


  10.                                         float x = (n - 0.5)*0.8;


  11.                                         x += (0.4 - abs(x))*sin(3 * w)*pow(sin(w),6)*0.45;

  12.                                         float y = -sin(t + sin(t + sin(t)*0.5))*0.45;


  13.                                         y -= (gv.x - x)*(gv.x - x);



  14.                                         float2 dropPos = (gv - float2(x, y)) / aspect;

  15.                                         float drop = smoothstep(0.05,0.03,length(dropPos));



  16.                                         float2 dropTrailPos = (gv - float2(x, t*0.25)) / aspect;

  17.                                         dropTrailPos.y = (frac(dropTrailPos.y * 8) / 8) - 0.03;

  18.                                         float dropTrail = smoothstep(0.03, 0.02, length(dropTrailPos));

  19.                                         float fogTrail = smoothstep(-0.05, 0.05, dropPos.y);

  20.                                         fogTrail *= smoothstep(0.5, y, gv.y);

  21.                                         dropTrail *= fogTrail;


  22.                                         fogTrail *= smoothstep(0.05,0.04,abs(dropPos.x));


  23.                                 //        col += fogTrail * 0.5;

  24.                                 //        col += dropTrail;

  25.                                 //        col += drop;

  26.                                         

  27.                                         float2 offs = drop * dropPos + dropTrail * dropTrailPos;

  28.                                         return float3(offs,fogTrail);

  29. }


  30.                                 fixed4 frag(v2f i) : SV_Target

  31.                                 {

  32.                                         float t = fmod(_Time.y,7200);

  33.                                         float4 col = 0;

  34.                                    //水滴层

  35.                                         float3 drops = Layer(i.uv,t);

  36.                                    //*是放大UV向量+,—是位移UV向量,瞎j8乘

  37.                                         drops += Layer(i.uv*1.35+7.51,t);

  38.                                         drops += Layer(i.uv*0.95+1.54,t);

  39.                                         drops += Layer(i.uv*1.57-6.54,t);

  40.                    //fogTrail是水滴的拖尾因为我们需要拖尾处清楚所以我们需要取个反减值

  41.                               float blur = _Blur * 7 * (1-drops.z);

  42.                                         col = tex2Dlod(_MainTex,float4 (i.uv +drops.xy* _Distortion,0, blur));

  43.                                         return col;

  44.                                 }

复制代码



被甩后一个人打车看着窗外下大雨之雨滴玻璃shader【P2】 

现在我们做第三部分,透明这里用GrapPass,其实用GrapPass很耗性能,不过反正这也不是一个低性能的shader

不BB开始,添加一个GrapPass,输入col看一下,然后需要设置Queue到Transparent,在Transparent这个渲染序列下才能在所有Geometry和AlphaTest渲染后,再从后向前渲染一遍

  1. Shader "Billy/RainGlass_OP"

  2. {

  3.         Properties

  4.         {

  5.                 _MainTex("Texture", 2D) = "white" {}

  6.                 _Size("Size",Float) = 1

  7.                 _Distortion("Distortion",range(0,1)) = 1

  8.                 _Blur("Blur",range(0,1)) = 1

  9.         }

  10.                 SubShader

  11.                 {

  12.                         Tags { "RenderType" = "Opaque" "Queue"="Transparent"}

  13.                         

  14.                          GrabPass{"_GrabTexture"}


  15.                         Pass

  16.                         {

  17.                                 CGPROGRAM

  18.                                 #pragma vertex vert

  19.                                 #pragma fragment frag

  20.                                 #include "UnityCG.cginc"


  21.                                 struct appdata

  22.                                 {

  23.                                         float4 vertex : POSITION;

  24.                                         float2 uv : TEXCOORD0;

  25.                                 };


  26.                                 struct v2f

  27.                                 {

  28.                                         float2 uv : TEXCOORD0;

  29.                                         float4 vertex : SV_POSITION;

  30.                                 };


  31.                                 sampler2D _MainTex;

  32.                                 sampler2D _GrabTexture;

  33.                                 float4 _MainTex_ST;

  34.                                 float _Size;

  35.                                 float _Distortion;

  36.                                 float _Blur;


  37.                                 v2f vert(appdata v)

  38.                                 {

  39.                                         v2f o;

  40.                                         o.vertex = UnityObjectToClipPos(v.vertex);

  41.                                         o.uv = TRANSFORM_TEX(v.uv, _MainTex);

  42.                                         return o;

  43.                                 }



  44.                                 float N21(float2 p) {

  45.                                         p = frac(p*float2(123.34,345.45));

  46.                                         p += dot(p,p + 34.345);

  47.                                         return frac(p.x*p.y);

  48.         }


  49.                                 float3 Layer(float2 UV,float t){

  50.                                         float2 aspect = float2(2, 1);

  51.                                         float2 uv = UV*_Size*aspect;


  52.                                         uv.y += t * 0.25;

  53.                                         float2 gv = frac(uv) - 0.5;

  54.                                         float2 id = floor(uv);



  55.                                         float n = N21(id);


  56.                                         t += n * 6.28631;



  57.                                         float w = UV.y * 10;


  58.                                         float x = (n - 0.5)*0.8;


  59.                                         x += (0.4 - abs(x))*sin(3 * w)*pow(sin(w),6)*0.45;

  60.                                         float y = -sin(t + sin(t + sin(t)*0.5))*0.45;


  61.                                         y -= (gv.x - x)*(gv.x - x);



  62.                                         float2 dropPos = (gv - float2(x, y)) / aspect;

  63.                                         float drop = smoothstep(0.05,0.03,length(dropPos));



  64.                                         float2 dropTrailPos = (gv - float2(x, t*0.25)) / aspect;

  65.                                         dropTrailPos.y = (frac(dropTrailPos.y * 8) / 8) - 0.03;

  66.                                         float dropTrail = smoothstep(0.03, 0.02, length(dropTrailPos));

  67.                                         float fogTrail = smoothstep(-0.05, 0.05, dropPos.y);

  68.                                         fogTrail *= smoothstep(0.5, y, gv.y);

  69.                                         dropTrail *= fogTrail;


  70.                                         fogTrail *= smoothstep(0.05,0.04,abs(dropPos.x));


  71.                                 //        col += fogTrail * 0.5;

  72.                                 //        col += dropTrail;

  73.                                 //        col += drop;

  74.                                         

  75.                                         float2 offs = drop * dropPos + dropTrail * dropTrailPos;

  76.                                         return float3(offs,fogTrail);

  77. }


  78.                                 fixed4 frag(v2f i) : SV_Target

  79.                                 {

  80.                                         float t = fmod(_Time.y,7200);

  81.                                         float4 col = 0;

  82.                                    //水滴层

  83.                                         float3 drops = Layer(i.uv,t);

  84.                                    //*是放大UV向量+,—是位移UV向量,瞎j8乘

  85.                                         drops += Layer(i.uv*1.35+7.51,t);

  86.                                         drops += Layer(i.uv*0.95+1.54,t);

  87.                                         drops += Layer(i.uv*1.57-6.54,t);

  88.                    //fogTrail是水滴的拖尾因为我们需要拖尾处清楚所以我们需要取个反减值

  89.                               float blur = _Blur * 7 * (1-drops.z);

  90.                                         col = tex2Dlod(_MainTex,float4 (i.uv +drops.xy* _Distortion,0, blur));

  91.                                         col = tex2D(_GrabTexture,i.uv);

  92.                                         return col;

  93.                                 }

  94.                                 ENDCG

  95.                         }

  96.                 }

  97. }

复制代码



被甩后一个人打车看着窗外下大雨之雨滴玻璃shader【P2】 

接下来我们就对齐GrapPassTex到屏幕坐标就好了,很开心UNity有这个宏UNITY_PROJ_COORD(ComputeGrabScreenPos(o.vertex)

https://www.jianshu.com/p/df878a386bec

被甩后一个人打车看着窗外下大雨之雨滴玻璃shader【P2】 

先贴上最后代码,立个Flag过两天再接着写

  1. Shader "Billy/RainGlass"

  2. {

  3.         Properties

  4.         {

  5.                 _MainTex("Texture", 2D) = "white" {}

  6.                 _Size("Size",Float) = 1

  7.                 _Distortion("Distortion",range(0,5)) = 4

  8.                 _Blur("Blur",range(0,1)) = 1

  9.         }

  10.                 SubShader

  11.                 {

  12.                         Tags { "RenderType" = "Opaque" "Queue"="Transparent"}

  13.                         

  14.                      GrabPass{"_GrabTexture"}


  15.                         Pass

  16.                         {

  17.                                 CGPROGRAM

  18.                                 #pragma vertex vert

  19.                                 #pragma fragment frag

  20.                                 #include "UnityCG.cginc"


  21.                                 struct appdata

  22.                                 {

  23.                                         float4 vertex : POSITION;

  24.                                         float2 uv : TEXCOORD0;


  25.                                 };


  26.                                 struct v2f

  27.                                 {

  28.                                         float2 uv : TEXCOORD0;

  29.                                         float4 vertex : SV_POSITION;

  30.                                         float4 grabUv : TEXCOORD1;

  31.                                 };


  32.                                 sampler2D _MainTex;

  33.                                 sampler2D _GrabTexture;

  34.                                 float4 _MainTex_ST;

  35.                                 float _Size;

  36.                                 float _Distortion;

  37.                                 float _Blur;


  38.                                 v2f vert(appdata v)

  39.                                 {

  40.                                         v2f o;

  41.                                         o.vertex = UnityObjectToClipPos(v.vertex);

  42.                                         o.uv = TRANSFORM_TEX(v.uv, _MainTex);

  43.                                         o.grabUv = UNITY_PROJ_COORD(ComputeGrabScreenPos(o.vertex));

  44.                                         return o;

  45.                                 }



  46.                                 float N21(float2 p) {

  47.                                         p = frac(p*float2(123.34,345.45));

  48.                                         p += dot(p,p + 34.345);

  49.                                         return frac(p.x*p.y);

  50.         }


  51.                                 float3 Layer(float2 UV,float t){

  52.                                         float2 aspect = float2(2, 1);

  53.                                         float2 uv = UV*_Size*aspect;


  54.                                         uv.y += t * 0.25;

  55.                                         float2 gv = frac(uv) - 0.5;

  56.                                         float2 id = floor(uv);



  57.                                         float n = N21(id);


  58.                                         t += n * 6.28631;



  59.                                         float w = UV.y * 10;


  60.                                         float x = (n - 0.5)*0.8;


  61.                                         x += (0.4 - abs(x))*sin(3 * w)*pow(sin(w),6)*0.45;

  62.                                         float y = -sin(t + sin(t + sin(t)*0.5))*0.45;


  63.                                         y -= (gv.x - x)*(gv.x - x);



  64.                                         float2 dropPos = (gv - float2(x, y)) / aspect;

  65.                                         float drop = smoothstep(0.05,0.03,length(dropPos));



  66.                                         float2 dropTrailPos = (gv - float2(x, t*0.25)) / aspect;

  67.                                         dropTrailPos.y = (frac(dropTrailPos.y * 8) / 8) - 0.03;

  68.                                         float dropTrail = smoothstep(0.03, 0.02, length(dropTrailPos));

  69.                                         float fogTrail = smoothstep(-0.05, 0.05, dropPos.y);

  70.                                         fogTrail *= smoothstep(0.5, y, gv.y);

  71.                                         dropTrail *= fogTrail;


  72.                                         fogTrail *= smoothstep(0.05,0.04,abs(dropPos.x));


  73.                                 //        col += fogTrail * 0.5;

  74.                                 //        col += dropTrail;

  75.                                 //        col += drop;

  76.                                         

  77.                                         float2 offs = drop * dropPos + dropTrail * dropTrailPos;

  78.                                         return float3(offs,fogTrail);

  79. }


  80.                                 fixed4 frag(v2f i) : SV_Target

  81.                                 {

  82.                                         float t = fmod(_Time.y,7200);

  83.                                         float4 col = 0;

  84.                                    //水滴层

  85.                                         float3 drops = Layer(i.uv,t);

  86.                                    //*是放大UV向量+,—是位移UV向量,瞎j8乘

  87.                                         drops += Layer(i.uv*1.35+7.51,t);

  88.                                         drops += Layer(i.uv*0.95+1.54,t);

  89.                                         drops += Layer(i.uv*1.57-6.54,t);

  90.                    //fogTrail是水滴的拖尾因为我们需要拖尾处清楚所以我们需要取个反减值

  91.                                         float fade = 1-saturate(fwidth(i.uv)*60);

  92.                               float blur = _Blur * 7 * (1-drops.z*fade);


  93.                                 //        col = tex2Dlod(_MainTex,float4(i.uv+drops.xy*_Distortion,0,blur));


  94.                                         float2 projUv = i.grabUv.xy / i.grabUv.w;

  95.                                         projUv += drops.xy*_Distortion*fade;

  96.                                         blur *= 0.01;

  97.                                         const float numSamples =32;

  98.                                         float a = N21(i.uv)*6.2831;

  99.                                         for(float i=0; i

  100.                                                 float2 offs = float2(sin(a),cos(a))*blur;

  101.                                                 float d = frac(sin((i+1)*546)*5421);

  102.                                                 d = sqrt(d); 

  103.                                                 offs *=d;

  104.                                                 col += tex2D(_GrabTexture,projUv+offs);

  105.                                                 a++;

  106. }

  107.                                         col /= numSamples;

  108.                                         return col;

  109.                                 }

  110.                                 ENDCG

  111.                         }

  112.                 }

  113. }

复制代码


发表于    2020年08月23日 13:52
关于作者
Lui2019
回答
0
文章
0
粉丝
114

相关文章

URL已复制到剪贴板!