Cg Programming/Unity/Transparency

地址

https://en.wikibooks.org/wiki/Cg_Programming/Unity/Transparency

重点

1.渲染顺序
2.透明物体的混合

代码一

Shader "Cg shader using blending" {
   SubShader {
      Tags { "Queue" = "Transparent" } 
         // draw after all opaque geometry has been drawn
         //此处表示渲染顺序,这个渲染队列在几何体队列之后被渲染,采用由后到前的次序
      Pass {
         ZWrite Off // don't write to depth buffer 
            // in order not to occlude other objects
         //渲染不透明物体是要进行深度比较,比较后不将深度值写到DepthBuffer
         Blend SrcAlpha OneMinusSrcAlpha // use alpha blending 颜色混合
         
         CGPROGRAM 
 
         #pragma vertex vert 
         #pragma fragment frag
 
         float4 vert(float4 vertexPos : POSITION) : SV_POSITION 
         {
            return mul(UNITY_MATRIX_MVP, vertexPos);
         }
 
         float4 frag(void) : COLOR 
         {
            return float4(0.0, 1.0, 0.0, 0.3); 
               // the fourth component (alpha) is important: 
               // this is semitransparent green
         }
 
         ENDCG  
      }
   }
}
1.Tags   子着色器使用标签来告诉渲染引擎期望何时和如何渲染对象。

Rendering Order - Queue tag
渲染次序 - 队列 标签

You can determine in which order your objects are drawn using the Queue tag. A Shader decides which render queue its objects belong to, this way any Transparent shaders make sure they are drawn after all opaque objects and so on.

你能使用 Queue 标签决定对象被渲染的次序。着色器决定它所归属的对象的渲染队列,任何透明渲染器可以通过这个办法确认可以在所有不透明对象渲染后再进行渲染。

There are four pre-defined render queues, but there can be more queues in between the predefined ones. The predefined queues are:

有四种预定义的渲染队列,在预定义队列之间还可以定义更多队列

  • Background - this render queue is rendered before any others. It is used for skyboxes and the like. 
    后台 - 这个渲染队列在所有队列之前被渲染,被用于渲染天空盒之类的对象。
  • Geometry (default) - this is used for most objects. Opaque geometry uses this queue. 
    几何体(缺省) - 这个队列被用于大多数对象。 不透明的几何体使用这个队列。
  • Transparent - this render queue is rendered after Geometry, in back-to-front order. Anything alpha-blended (i.e. shaders that don't write to depth buffer) should go here (glass, particle effects). 
    透明 - 这个渲染队列在几何体队列之后被渲染,采用由后到前的次序。任何采用alpha混合的对象(不对深度缓冲产生写操作的着色器)应该在这里渲染(玻璃,粒子效果)
  • Overlay - this render queue is meant for overlay effects. Anything rendered last should go here (e.g. lens flares). 
    覆盖 - 这个渲染队列被用于实现叠加效果。任何需要最后渲染的对象应该放置在此处。(如镜头光晕)
  • 参考:http://www.ceeger.com/Components/SL-SubshaderTags.html
Blend Alpha混合
参考:
http://docs.manew.com/Components/238.html
http://blog.sina.com.cn/s/blog_471132920101d8z5.html

这部分我只是理解,但不透彻,可以自己去看。
我得理解就是把当前的透明物体和背景进行颜色混合,按照
             float4 result = SrcFactor * fragment_output + DstFactor * pixel_color 
             最终颜色 = 源颜色 * 源透明值 + 目标颜色*(1 - 源透明值)
这个公式,主要看前后的两个系数,前面系数代表贴图的,后面代表背景的,系数越大表示混合的越多,以下看几个极值
1.Blend one  one 贴图和背景叠加,无Alpha透明通道处理
2.Blend SrcAlpha  zero:仅仅显示贴图,贴图含Alpha透明通道处理。

代码二

Shader "Cg shader using blending" {
   SubShader {
      Tags { "Queue" = "Transparent" } 
         // draw after all opaque geometry has been drawn
      Pass {
         Cull Front // first pass renders only back faces 
             // (the "inside")
         ZWrite Off // don't write to depth buffer 
            // in order not to occlude other objects
         Blend SrcAlpha OneMinusSrcAlpha // use alpha blending

         CGPROGRAM 
 
         #pragma vertex vert 
         #pragma fragment frag
 
         float4 vert(float4 vertexPos : POSITION) : SV_POSITION 
         {
            return mul(UNITY_MATRIX_MVP, vertexPos);
         }
 
         float4 frag(void) : COLOR 
         {
            return float4(1.0, 0.0, 0.0, 0.3);
               // the fourth component (alpha) is important: 
               // this is semitransparent red
         }
 
         ENDCG  
      }

      Pass {
         Cull Back // second pass renders only front faces 
             // (the "outside")
         ZWrite Off // don't write to depth buffer 
            // in order not to occlude other objects
         Blend SrcAlpha OneMinusSrcAlpha // use alpha blending

         CGPROGRAM 
 
         #pragma vertex vert 
         #pragma fragment frag
 
         float4 vert(float4 vertexPos : POSITION) : SV_POSITION 
         {
            return mul(UNITY_MATRIX_MVP, vertexPos);
         }
 
         float4 frag(void) : COLOR 
         {
            return float4(0.0, 1.0, 0.0, 0.3);
               // the fourth component (alpha) is important: 
               // this is semitransparent green
         }
 
         ENDCG  
      }
   }
}

这部分相比前一部分只是添加了通道的处理,类似前一篇的内容
效果图

前者是在外部看的情况,后者是在内部看的情况。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值