当前位置: 移动技术网 > IT编程>开发语言>c# > Unity Shader模拟玻璃效果

Unity Shader模拟玻璃效果

2020年05月13日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了unity shader实现玻璃效果的具体代码,供大家参考,具体内容如下

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"
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网