UnityShader如何模擬玻璃效果

這篇文章主要為大家展示了Unity Shader如何模擬玻璃效果,內(nèi)容簡而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會(huì)有收獲的,下面讓小編帶大家一起來看看吧。

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:國際域名空間、網(wǎng)絡(luò)空間、營銷軟件、網(wǎng)站建設(shè)、同心網(wǎng)站維護(hù)、網(wǎng)站推廣。

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.隊(duì)列設(shè)置成透明的可以保證渲染該物體的時(shí)候,其他所有不透明的物體都已經(jīng)被渲染到屏幕上了
    //  這樣GrabPass保存屏幕圖像的時(shí)候才能完整
    // 2.渲染類型設(shè)置為不透明是為了使用著色器替換的時(shí)候,物體可以在被需要時(shí)正確的渲染
    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;
      // 可以得到系統(tǒng)保存的紋素的尺寸大小 1/256,1/512
      // 對(duì)屏幕圖像坐標(biāo)采樣進(jìn)行偏移的時(shí)候需要使用該變量
      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);

        // 得到對(duì)應(yīng)被抓取的屏幕圖像的采樣坐標(biāo)
        o.scrPos = ComputeGrabScreenPos(o.pos);

        // 原圖和法線貼圖公用同一套UV坐標(biāo),進(jìn)行紋理偏移縮放設(shè)置
        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都從切線空間轉(zhuǎn)換到了世界空間
        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));

        // 對(duì)法線貼圖進(jìn)行采樣,并使用UnpackNormal解壓*2-1,但實(shí)際上法線貼圖在切線空間下
        // 所以需要將bump從切線空間轉(zhuǎn)換到世界空間
        fixed3 bump = UnpackNormal(tex2D(_BumpMap, i.uv.zw)); 

        // 對(duì)采樣坐標(biāo)進(jìn)行偏移
        float2 offset = bump.xy * _Distortion * _RefractionTex_TexelSize.xy;
        i.scrPos.xy = offset * i.scrPos.z + i.scrPos.xy;

        // 對(duì)屏幕紋理進(jìn)行采樣
        fixed3 refrCol = tex2D(_RefractionTex, i.scrPos.xy/i.scrPos.w).rgb;

        // 將bump從切線空間轉(zhuǎn)換到世界空間
        // TtoW0的xyz:T的X,B的X,N的X
        // 用點(diǎn)積得到一個(gè)常量
        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);

        // 對(duì)_Cubemap進(jìn)行紋理采樣,使用反射方向
        fixed3 reflCol = texCUBE(_Cubemap, reflDir).rgb * texColor.rgb;

        // 反射顏色+折射顏色
        fixed3 finalColor = reflCol * (1 - _RefractAmount) + refrCol * _RefractAmount;

        return fixed4(finalColor, 1);
      }

      ENDCG
    }
  }

  FallBack "Diffuse"
}

以上就是關(guān)于Unity Shader如何模擬玻璃效果的內(nèi)容,如果你們有學(xué)習(xí)到知識(shí)或者技能,可以把它分享出去讓更多的人看到。

網(wǎng)站標(biāo)題:UnityShader如何模擬玻璃效果
文章位置:http://bm7419.com/article20/iihdco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、網(wǎng)站收錄、企業(yè)網(wǎng)站制作營銷型網(wǎng)站建設(shè)、建站公司響應(yīng)式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站