zoukankan      html  css  js  c++  java
  • [Unity]模拟雨水的折射效果

    用GrabPass做的小玩具。

    并不是真的计算了折射,只是简单地扰动了uv,对于雨水来说效果已经足够好了。

    Shader代码:

    Shader "Unlit/Rain"
    {
    	Properties
    	{
    		_MainTex("Texture", 2D) = "white" {}
    		_Opaque("Opaque",float) = 0.02
    	}
    	SubShader
    	{
    		Tags{
    			"RenderType" = "Opaque"
    			"Queue" = "Transparent"
    		}
    		LOD 100
    
    		Blend SrcAlpha OneMinusSrcAlpha
    		GrabPass{ "GrabTexture" }
    		Pass
    		{
    			CGPROGRAM
    			#pragma vertex vert
    			#pragma fragment frag
    
    			#include "UnityCG.cginc"
    
    			struct appdata {
    				float4 vertex : POSITION;
    				float2 uv : TEXCOORD0;
    			};
    
    			struct v2f {
    				float4 vertex:SV_POSITION;
    				float2 uv:TEXCOORD0;
    				float4 screenuv:TEXCOORD1;
    
    			};
    
    			sampler2D _MainTex;
    			sampler2D GrabTexture;
    			float4 _MainTex_ST;
    			float _Opaque;
    
    			v2f vert(appdata v) {
    				v2f o;
    				o.vertex = UnityObjectToClipPos(v.vertex);
    				o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    				o.screenuv = ComputeGrabScreenPos(o.vertex);
    				return o;
    			}
    
    			fixed4 frag(v2f i) : SV_Target
    			{
    				// sample the texture
    				fixed4 col = tex2D(_MainTex, i.uv);
    				float2 suv = i.screenuv / i.screenuv.w;
    				//512和128是测试出来比较正常的魔数。
    				//越大则扰动越剧烈。
    				float2 distort = float2(sin(suv.x * 512 + _Time.w * 10),sin(suv.y * 128 + _Time.w * 10)) / 128;
    				col = _Opaque + tex2D(GrabTexture, suv + distort);
    				col.a = tex2D(_MainTex, i.uv).a;
    				return col;
    			}
    			ENDCG
    		}
    	}
    }
    
  • 相关阅读:
    pip install selenium==版本号 报错
    解决phantomjs输出中文乱码
    phantomjs学习之网页访问测速
    phantomjs学习之截图
    bzoj1069-最大土地面积
    hdu4372-Count the Buildings
    bzoj3786-星系探索
    Codeforces633H-Fibonacci-ish II
    hdu3625-Rooms
    斯特林数
  • 原文地址:https://www.cnblogs.com/yangrouchuan/p/6411537.html
Copyright © 2011-2022 走看看