Cg Programming/Unity/Transparency
2016-03-03 11:45
435 查看
地址
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 } } }
这部分相比前一部分只是添加了通道的处理,类似前一篇的内容
效果图
前者是在外部看的情况,后者是在内部看的情况。
相关文章推荐
- unity如何播放父级的父级物体所绑定的动画功能
- 【Unity开发】通过C#读取CSV表,创建并存储到LUA table中
- wiki/Cg Programming/Unity/Cutaways
- 增强现实贺卡项目展示
- [Unity3D]Unity3D游戏开发之鼠标滚轮实现放大缩小
- Unity-编写插件
- unity音频管理
- unity5.3多线程渲染的坑
- unity3d的四元数 Quaternion
- Unity3D项目之《坦克大战》
- Unity Assets目录下的文件夹用途
- Unity中加入Android项目的Build步骤 .<unity引用Android的jar包><Android对unity做二次开发>
- [Unity3D]蓝港面试题
- Unity3d 换装Avatar系统
- 项目记录25--unity-tolua框架 View02---BasePanel.lua
- Unity3D与android交互---构建android插件
- unity3d,android平台下,高德地图定位,定位模式设定
- Unity3D 调用 Android jar 包制作方法
- Unity 5 中的全局光照技术详解(建议收藏) - CocoaChina_让移动开发更简单
- unity脚本自带函数执行顺序