歡迎您光臨本站 註冊首頁

Unity Shader模擬玻璃效果

←手機掃碼閱讀     qp18502452 @ 2020-05-06 , reply:0

Shader "Glass Refraction" { Properties { _MainTex ("Main Tex", 2D) = "white" {} _BumpMap ("Normal Map", 2D) = "bump" {} _Cubemap ("Environment Cubemap", Cube) = "_Skybox" {} _Distortion ("Distortion", Range(0, 100)) = 10 _RefractAmount ("Refract Amount", Range(0.0, 1.0)) = 1.0 } SubShader {

 // 1.隊列設置成透明的可以保證渲染該物體的時候,其他所有不透明的物體都已經被渲染到屏幕上了 

// 這樣GrabPass保存屏幕圖像的時候才能完整

 // 2.渲染類型設置為不透明是為了使用著色器替換的時候,物體可以在被需要時正確的渲染 

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

 // 抓取屏幕圖像並保存到紋理 

_RefractionTex 中 GrabPass { "_RefractionTex" } Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" sampler2D _MainTex; float4 _MainTex_ST; sampler2D _BumpMap; float4 _BumpMap_ST; samplerCUBE _Cubemap; float _Distortion; fixed _RefractAmount; sampler2D _RefractionTex; 

// 可以得到系統保存的紋素的尺寸大小 1/256,1/512

 // 對屏幕圖像座標採樣進行偏移的時候需要使用該變量 float4 _RefractionTex_TexelSize; struct a2v { float4 vertex : POSITION; float3 normal : NORMAL; float4 tangent : TANGENT; float2 texcoord: TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; float4 scrPos : TEXCOORD0; float4 uv : TEXCOORD1; float4 TtoW0 : TEXCOORD2; float4 TtoW1 : TEXCOORD3; float4 TtoW2 : TEXCOORD4; }; v2f vert (a2v v) { v2f o; o.pos = mul(UNITY_MATRIX_MVP, v.vertex); 

// 得到對應被抓取的屏幕圖像的採樣座標

 o.scrPos = ComputeGrabScreenPos(o.pos);

 // 原圖和法線貼圖公用同一套UV座標,進行紋理偏移縮放設置 

o.uv.xy = TRANSFORM_TEX(v.texcoord, _MainTex); o.uv.zw = TRANSFORM_TEX(v.texcoord, _BumpMap); 

// 世界方向 

float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz; // 法線世界方向

 fixed3 worldNormal = UnityObjectToWorldNormal(v.normal); 

// 切線世界方向

 fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz); 

// 世界副切線方向

 fixed3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w; 

// 定義:切線空間到世界空間的變換矩陣 

// 所有的TBN都從切線空間轉換到了世界空間 

o.TtoW0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x); o.TtoW1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y); o.TtoW2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z); return o; } fixed4 frag (v2f i) : SV_Target { 

// 世界空間 float3 worldPos = float3(i.TtoW0.w, i.TtoW1.w, i.TtoW2.w); 

// 世界空間下的觀察方向 fixed3 worldViewDir = normalize(UnityWorldSpaceViewDir(worldPos)); 

// 對法線貼圖進行採樣,並使用UnpackNormal解壓*2-1,但實際上法線貼圖在切線空間下 

// 所以需要將bump從切線空間轉換到世界空間 fixed3 bump = UnpackNormal(tex2D(_BumpMap, i.uv.zw)); 

// 對採樣座標進行偏移 float2 offset = bump.xy * _Distortion * _RefractionTex_TexelSize.xy; i.scrPos.xy = offset * i.scrPos.z + i.scrPos.xy; // 對屏幕紋理進行採樣 fixed3 refrCol = tex2D(_RefractionTex, i.scrPos.xy/i.scrPos.w).rgb; // 將bump從切線空間轉換到世界空間 

// TtoW0的xyz:T的X,B的X,N的X 

// 用點積得到一個常量 bump = normalize(half3(dot(i.TtoW0.xyz, bump), dot(i.TtoW1.xyz, bump), dot(i.TtoW2.xyz, bump)));

// 用法線貼圖求反射方向 fixed3 reflDir = reflect(-worldViewDir, bump); 

// 主紋理採樣 fixed4 texColor = tex2D(_MainTex, i.uv.xy); 

// 對_Cubemap進行紋理採樣,使用反射方向

 fixed3 reflCol = texCUBE(_Cubemap, reflDir).rgb * texColor.rgb; 

// 反射顏色+折射顏色 

fixed3 finalColor = reflCol * (1 - _RefractAmount) + refrCol * _RefractAmount; return fixed4(finalColor, 1); } ENDCG } } FallBack "Diffuse" }



[qp18502452 ] Unity Shader模擬玻璃效果已經有258次圍觀

http://coctec.com/docs/program/show-post-233058.html