Unity 纱窗透明效果Shader(Screen-Door Transparency)

Screen-door transparency (also called screen space stipple, stipple patterns, dissolve, fizzle, etc.) 是一种利用alpha testing来实现半透明融合的技术
参考:

http://digitalrune.github.io/DigitalRune-Documentation/html/fa431d48-b457-4c70-a590-d44b0840ab1e.html

https://ocias.com/blog/unity-stipple-transparency-shader/

优点:

  1. 对老显卡友好,几乎支持所有Pixel Shader环境
  2. 是opaque渲染,效率比传统alpha blend高
  3. 不需要z深度排序
  4. 可以实现半透明影子

缺点:

  1. 抖动色,有杂点

3D 游戏的透明度仍然很难做到完美。你想要一个性能优良的着色器,避免排序问题,并且可以跨不同平台/渲染管道工作。纱窗透明是一种老式的解决方案,在大多数情况下都能完美运行。

纱窗透明度的工作原理是随着透明度的增加省略从对象中绘制越来越多的像素。这种效果不完全符合真实世界的物理效果,但在3D渲染中看起来也是可以接受的。

Shader "Ocias/Diffuse (Stipple Transparency)" {
Properties {
    _MainTex ("Base (RGB)", 2D) = "white" {}
    _Transparency ("Transparency", Range(0,1)) = 1.0
}
SubShader {
    Tags { "RenderType"="Opaque" }
    LOD 150

CGPROGRAM
#pragma surface surf Lambert noforwardadd

sampler2D _MainTex;

struct Input {
    float2 uv_MainTex;
    float4 screenPos;
};

half _Transparency;

void surf (Input IN, inout SurfaceOutput o) {
    fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
    o.Albedo = c.rgb;
    o.Alpha = c.a;

    // Screen-door transparency: Discard pixel if below threshold.
    float4x4 thresholdMatrix =
    {  1.0 / 17.0,  9.0 / 17.0,  3.0 / 17.0, 11.0 / 17.0,
      13.0 / 17.0,  5.0 / 17.0, 15.0 / 17.0,  7.0 / 17.0,
       4.0 / 17.0, 12.0 / 17.0,  2.0 / 17.0, 10.0 / 17.0,
      16.0 / 17.0,  8.0 / 17.0, 14.0 / 17.0,  6.0 / 17.0
    };
    float4x4 _RowAccess = { 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 };
    float2 pos = IN.screenPos.xy / IN.screenPos.w;
    pos *= _ScreenParams.xy; // pixel position
    clip(_Transparency - thresholdMatrix[fmod(pos.x, 4)] * _RowAccess[fmod(pos.y, 4)]);
}
ENDCG
}

Fallback "Mobile/VertexLit"
}

 

 

点赞

发表回复

昵称和uid可以选填一个,填邮箱必填(留言回复后将会发邮件给你)
tips:输入uid可以快速获得你的昵称和头像(已失效)