unity 两个unity双面透明材质质为什么渲染后面的那个

NO Batch& ? 游戏场景中存在大量例子的时候,DrallCall的压力很大,但是遍历一遍之后发现,为啥一样的粒子特效竟然没有合并,why?经过很多测试后发现,如果把透明材质的修改为非半透明的,就可以合并,这就让我很奇怪了?Unity提供了的功能,虽然限制很多,但是对于透明材质的合并,Unity似乎给了一个很玄乎的答案: Semitransparent shaders most often require objects to be rendered in back-to-front order for transparency to work. Unity first orders objects in this order, and then tries to batch them - but because the order must be strictly satisfied, this often means less batching can be achieved than with opaque objects. 这尼玛等于没说。。。。但是原因其实很明显:透明材质 透明材质 测试的时候发现: 1& 带有透明材质的粒子如果把深度完全分开,那么相同材质的基于可以进行合并 2 把不同透明材质的渲染次序完全分开也可以进行合并 透明材质使用alpha blend的方式按摄像机距离从后向前进行渲染,如果对深度相同、渲染顺序相同的粒子进行渲染,Unity可能就完全按照乱序的方式进行渲染。但是修改深度或者渲染次序后,同样材质的粒子可能就会在相邻的次序进行渲染。 也就是说使用相同材质的物体需要在相邻的渲染队列中才会进行合并。那这样思路就很清晰,只需要对粒子的渲染次序或者深度进行适当的排序即可。 渲染次序 粒子一般是放在一起,修改深度可能不太现实,美术的妹子也不乐意,那OK ,只能修改渲染次序,存在两种情况: (1)不同shader的材质,可以直接在shader中RenderQueue中定义,RenderQueue具体可以查看Unity官方定义:。一般来说透明材质是从3000开始,渲染的时候这个数值从小向大渲染(之前美术做的渲染次序全部在3000)。
(2) 相同shader不同贴图:这个我也没辙,只能通过Unity 属性面板中debug模式进行修改
经过场面的处理后,不同的材质使用不用的渲染次序,粒子的drallcall 就可以完全进行合并。 & 一些思考 (1)场景中渲染次序问题 其实Unity已经把场景中的物体区分为不同类型,如下图,但是在使用中还是需要注意很多。最好是在项目初期就会好好场景中可能用到的渲染次序分类。
(2)透明度交叉渲染(OIT) 粒子比较小,所以可能不存在交叉渲染的问题,但是对于透明物体而言,alpha blend模式下需要从后向前渲染,如果存在透明物体交叉的情况就很麻烦。如下图:
具体详情可以参考这篇文章
对于和排序无关的透明渲染可以参考:
阅读(...) 评论()&&国之画&&&& &&
版权所有 京ICP备号-2
迷上了代码!Unity(9)
批渲染(Batch)
batch render 是大部分引擎提高渲染效率的方法,基本原理就是通过将一些渲染状态一致的物体合成一个大物体,一次提交给gpu进行绘制,如果不batch的话,就要提交给很多次,这可以显著的节省drawcall,实际上这主要节省了cpu的时间,cpu从提交多次到提交一次,对gpu来说也不用多次切换渲染状态。当然能batch的前提一定是渲染状态一致的一组物体。
Unity3d的批渲染分为两种,动态和静态
要求:必须使用同一张material,然后在编辑器里设置为static batching的。
特点:静态批是无法运动的。
所以一般制作流程上,对于场景这些静态的物体都采用静态批,美术会根据场景的规模,将相邻的一片物件的贴图合并到一张或几张的大图上,这样这些物件可以使用同一个material,就可以静态批在一起,大幅节省dc。
静态批的时候Unity3d会在运行时生成一个合并的大模型,并且为这个模型指定一张共同的贴图,所以这个批在一起的数量是有限的,如果批在一起的定点数过多,它就会自动分成两个批,一个静态批的上限我自测大约在60000多个顶点单元。
动态批是对那些没有标记成static batching的物体在runtime unity自动将他们批在一起,这个是可以支持运动物体的,但是限制较为严格:
1.一个批次总顶点单元少于900
2.批在一起的所有的模型应用同样的缩放值
3.使用相同的材质
4.相同的一张lightmap
5.不能使用多pass的shader
6.不能接收阴影
所以一般是小的运动的模型才比较容易动态批在一起,当然unity又说可能在后续版本放宽这个限制
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:13101次
排名:千里之外
原创:29篇
(2)(4)(2)(4)(6)(6)(2)(2)(1)(1)(2)Unity3d&Shader(三)&Pass(Texturing)混合纹理(同时显示2张图),自发光
After the basic vertex lighting has been calculated, textures
are applied. In ShaderLab this is done using
SetTexture command.
纹理在基本的顶点光照被计算后被应用。在着色器中通过SetTexture 命令来完成。
SetTexture commands have no e as in that case pixel
operations are completely described in the shader.
SetTexture 命令在片面程序被使用时不会生效;这种模式下像素操作被完全描述在着色器中。
<img ALT="" src="/blog7style/images/common/sg_trans.gif" real_src ="/Components/Images/SL-SetTexture-0.jpg" NAME="image_operate_97762"
TITLE="Unity3d&Shader(三)&Pass(Texturing)混合纹理(同时显示2张图),自发光" />
Texturing is the place to do old-style combiner effects. You can
have multiple SetTexture commands inside a pass - all textures are
applied in sequence, like layers in a painting program. SetTexture
commands must be placed at the end of a .
材质贴图可以用来做老风格的混合器效果。你能在一个通道中使用多个SetTexture 命令 -
所有纹理被顺序的应用,如同绘画程序中的层一样。SetTexture 命令必须放置在通道的末尾。
Syntax 语法
SetTexture [TexturePropertyName]
{ Texture Block }
Assigns a texture. TextureName must be defined as a
texture property. How to apply the texture is defined inside the
TextureBlock.
分配一个纹理,TextureName必须定义为一个纹理属性。如何应用纹理被定义在TextrueBlock中
The texture block controls how the texture is applied. Inside
the texture block can be up to three commands: combine, matrix and
constantColor.
纹理块控制纹理如何被应用。在纹理块中能执行3种命令:合并,矩阵和不变色。
Texture block combine command 纹理块合并命令
combine src1 * src2
Multiplies src1 and src2 together. The result will be darker
than either input.
将源1和源2的元素相乘。结果会比单独输出任何一个都要暗
combine src1 + src2
Adds src1 and src2 together. The result will be lighter than
either input.
将将源1和源2的元素相加。结果会比单独输出任何一个都要亮
combine src1 - src2
Subtracts src2 from src1.
源1 减去源2
combine src1 +- src2
Adds src1 to src2, then subtracts 0.5 (a signed add).
先相加,然后减去0.5(添加了一个符号)
combine src1 lerp (src2) src3
Interpolates between src3 and src1, using the alpha of src2.
Note that the interpolation is opposite direction: src1 is used
when alpha is one, and src3 is used when alpha is zero.
使用源2的透明度通道值在源3和源1中进行差值,注意差值是反向的:当透明度值是1是使用源1,透明度为0时使用源3
combine src1 * src2 + src3
Multiplies src1 with the alpha component of src2, then adds
源1和源2的透明度相乘,然后加上源3
combine src1 * src2 +- src3
Multiplies src1 with the alpha component of src2, then does a
signed add with src3.
源1和源2的透明度相乘,然后和源3做符号加
combine src1 * src2 - src3
Multiplies src1 with the alpha component of src2, then
subtracts src3.
源1和源2的透明度相乘,然后和源3相减
All the src properties can be either one of
previous, constant, primary or
所有源属性都可以是previous, constant, primary or texture其中的一个。
Previous is the the result of the previous
SetTexture.
上一次SetTexture的结果
Primary is the color from the
or the vertex color if it
来自光照计算的颜色或是当它绑定时的顶点颜色
Texture is the color of the texture specified
by [_TextureName] in the SetTexture (see above).
在SetTexture中被定义的纹理的颜色
Constant is the color specified in
ConstantColor.
被ConstantColor定义的颜色
Modifiers 解释:
The formulas specified above can optionally be followed by the
keywords Double or Quad to make
the resulting color 2x or 4x as bright.
上述的公式都均能通过关键字 Double 或是 Quad
将最终颜色调高亮度2倍或4倍。
All the src properties, except lerp argument,
can optionally be preceded by one - to make the
resulting color negated.
所有的src属性,除了差值参数都能被标记一个-符号来使最终颜色反相。
All the src properties can be followed by
alpha to take only the alpha channel.
所有src属性能通过跟随 alpha
标签来表示只取用alpha通道。
Texture block constantColor command (纹理块constantColor 命令)
ConstantColor color
Defines a constant color that can be used in the combine
定义在combine命令中能被使用的不变颜色
Texture block matrix command (纹理块matrix命令)
matrix [MatrixPropertyName]
Transforms texture coordinates used in this command with the
given matrix.
使用给定矩阵变换纹理坐标
Details 细节
Older graphics cards use a layered approach to textures. The
textures are applied one after each other, modifying the color that
will be written to the screen. For each texture, the texture is
typically combined with the result of the previous operation.
较老的显卡对纹理使用分层的方案。纹理在每一层后被应用一次颜色的修改。对每一个纹理,一般来说纹理都是和上一次操作的结果混合。
<img ALT="" src="/blog7style/images/common/sg_trans.gif" real_src ="/Components/Images/SL-SetTexture-1.jpg" NAME="image_operate_86150"
TITLE="Unity3d&Shader(三)&Pass(Texturing)混合纹理(同时显示2张图),自发光" />
Note that on "true fixed function" devices (OpenGL, OpenGL ES
1.1, Wii) the value of each SetTexture stage is clamped to 0..1
range. Everywhere else (Direct3D, OpenGL ES 2.0) the range may or
may not be higher. This might affect SetTexture stages that can
produce values higher than 1.0.
注意,在“真正的固定功能”设备(OpenGL, OpenGL ES 1.1,
Wii),每个SetTexture阶段的值被限制为0..1范围。其他地方(Direct3D, OpenGL ES
2.0)该范围可能或不可能更高。这可能会影响SetTexture阶段,可能产生的值高于1.0。
Separate Alpha & Color computation
分离的透明度和颜色混合
By default, the combiner formula is used for calculating both
the RGB and alpha component of the color. Optionally, you can
specify a separate formula for the alpha calculation. This looks
like this:
缺省情况下,混合公式被同时用于计算纹理的RGB通道和透明度。同时,你也能指定只针对透明度进行计算,如下:
SetTexture [_MainTex] { combine previous * texture, previous + texture }
Here, we multiply the RGB colors and add the alpha.
如上所述,我们对RGB的颜色做乘然后对透明度相加
Specular highlights 反射高光
By default the primary color is the sum of the
diffuse, ambient and specular colors (as defined in the ). If you specify
SeparateSpecular On in the pass options, the
specular color will be added in after the combiner
calculation, rather than before. This is the default behavior of
the built-in VertexLit shader.
默认情况下primary颜色是漫反射,阴影色和高光颜色(在光线计算中定义)的加和。如果你将通道设置中的SeparateSpecular
打开,高光色会在混合计算后被加入,而不是之前。这是内置顶点着色器的默认行为。
Graphics hardware support 图形硬件支持
Some old graphics cards might not support some texture combine
modes, and different cards have different number of SetTexture
stages available. The shader author should write separate
for the cards he or she wants to
一些旧的显示卡不能支持某些纹理混合模式,不同的卡有不同数目的SetTexture阶段可用。着色器作者应该为他们想支持的卡分开写
Graphics cards with pixel shader 1.1 support (NVIDIA GeForce 3
and up, ATI Radeon 8500 and up, Intel 9xx) support all combiner
modes and have at least 4 texture stages available. The following
table summarizes the hardware support:
支持像素着色器1.1版本的图形卡(...)支持所有的混合器模式并且可以拥有至少4级渲染阶段。下表简述了硬件支持。
Stage count
Combiner modes not
NVIDIA GeForce 3/4Ti and up
In OpenGL on Windows, src1*src2-src3 is not
NVIDIA TNT2, GeForce 256, GeForce 2, GeForce
In OpenGL on Windows, src1*src2-src3 is not
ATI Radeon 9500 and up
8 in OpenGL, 4 in D3D9
ATI Radeon
6 in OpenGL, 4 in D3D9
ATI Radeon 7500
src1*src2+src3
src1*src2+-src3
src1*src2-src3
Examples 示例
Alpha Blending Two Textures (Alpha混合两个纹理)
This small examples takes two textures. First it sets the first
combiner to just take the _MainTex, then is uses
the alpha channel of _BlendTex to fade in the RGB
colors of _BlendTex
这个小例子使用了两张纹理。首先设置第一个混合器只使用_MainTex,然后使用_BlendTex的Alpha通道来淡出_BlendTex的RGB颜色。
Shader "Examples/2 Alpha Blended Textures" {
&&& Properties {
&&&&&&& _MainTex ("Base (RGB)", 2D) = "white" {}
&&&&&&& _BlendTex ("Alpha Blended (RGBA) ", 2D) = "white" {}
&&& SubShader {
&&&&&&& Pass {
&&&&&&&&&&& // Apply base texture
&&&&&&&&&&&&&&&&&&&&&&& // 应用主纹理
&&&&&&&&&&& SetTexture [_MainTex] {
&&&&&&&&&&&&&&& combine texture
&&&&&&&&&&& }
&&&&&&&&&&& // Blend in the alpha texture using the lerp operator
&&&&&&&&&&&&&&&&&&&&&&& // 使用差值操作混合Alpha纹理
&&&&&&&&&&& SetTexture [_BlendTex] {
&&&&&&&&&&&&&&& combine texture lerp (texture) previous&&&&&&&&&&& }
Alpha Controlled Self-illumination (Alpha控制自发光)
This shader uses the alpha component of the
_MainTex to decide where to apply lighting. It
does this by applying the t In the first
stage, the alpha value of the texture is used to blend between the
vertex color and solid white. In the second stage, the RGB values
of the texture are multiplied in.
这个着色器使用_MainTex的Alpha来描述什么地方应用光照。它通过分两个阶段应用纹理来实现;第一个阶段,纹理的Alpha值被用来在顶点颜色和纯白色之间混合。第二阶段,纹理的RGB值被乘入。
Shader "Examples/Self-Illumination" {
&&& Properties {
&&&&&&& _MainTex ("Base (RGB) Self-Illumination (A)", 2D) = "white" {}
&&& SubShader {
&&&&&&& Pass {
&&&&&&&&&&& // Set up basic white vertex lighting
&&&&&&&&&&&&&&&&&&&&&&& //设置白色顶点光照
&&&&&&&&&&& Material {
&&&&&&&&&&&&&&& Diffuse (1,1,1,1)
&&&&&&&&&&&&&&& Ambient (1,1,1,1)
&&&&&&&&&&& }
&&&&&&&&&&& Lighting On
&&&&&&&&&&& // Use texture alpha to blend up to white (= full illumination)
&&&&&&&&&&&&&&&&&&&&&&& // 使用纹理Alpha来混合白色(完全发光)
&&&&&&&&&&& SetTexture [_MainTex] {
&&&&&&&&&&&&&&& constantColor (1,1,1,1)
&&&&&&&&&&&&&&& combine constant lerp(texture) previous
&&&&&&&&&&& }
&&&&&&&&&&& // Multiply in texture
&&&&&&&&&&&&&&&&&&&&&&& // 和纹理相乘
&&&&&&&&&&& SetTexture [_MainTex] {
&&&&&&&&&&&&&&& combine previous * texture
&&&&&&&&&&& }
We can do something else for free here, instead of
blending to solid white, we can add a self-illumination color and
blend to that. Note the use of ConstantColor to
get a _SolidColor from the properties into the texture
我们能在这里尽可能自由的做些事,除了混合纯白色,我们还能添加一个自发光颜色然后和它混合和。注意ConstantColor的使用能从属性中获取一个纯色并应用到纹理混合中去。
Shader "Examples/Self-Illumination 2" {
&&& Properties {
&&&&&&& _IlluminCol ("Self-Illumination color (RGB)", Color) = (1,1,1,1)
&&&&&&& _MainTex ("Base (RGB) Self-Illumination (A)", 2D) = "white" {}
&&& SubShader {
&&&&&&& Pass {
&&&&&&&&&&& // Set up basic white vertex lighting
&&&&&&&&&&& Material {
&&&&&&&&&&&&&&& Diffuse (1,1,1,1)
&&&&&&&&&&&&&&& Ambient (1,1,1,1)
&&&&&&&&&&& }
&&&&&&&&&&& Lighting On
&&&&&&&&&&& // Use texture alpha to blend up to white (= full illumination)
&&&&&&&&&&& SetTexture [_MainTex] {
&&&&&&&&&&&&&&& // Pull the color property into this blender
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& // 使颜色属性进入混合器
&&&&&&&&&&&&&&& constantColor [_IlluminCol]
&&&&&&&&&&&&&&& // And use the texture's alpha to blend between it and
&&&&&&&&&&&&&&& // vertex color
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& // 使用纹理的alpha通道混合顶点颜色
&&&&&&&&&&&&&&& combine constant lerp(texture) previous
&&&&&&&&&&& }
&&&&&&&&&&& // Multiply in texture
&&&&&&&&&&& SetTexture [_MainTex] {
&&&&&&&&&&&&&&& combine previous * texture
&&&&&&&&&&& }
And finally, we take all the lighting properties of the
vertexlit shader and pull that in:
最后,我们把顶点光照着色器的所有的光照属性放入:
Shader "Examples/Self-Illumination 3" {
&&& Properties
_IlluminCol ("Self-Illumination color (RGB)", Color) =
_Color ("Main Color", Color) = (1,1,1,0)
_SpecColor ("Spec Color", Color) = (1,1,1,1)
_Emission ("Emmisive Color", Color) = (0,0,0,0)
_Shininess ("Shininess", Range (0.01, 1)) = 0.7
_MainTex ("Base (RGB)", 2D) = "white" {}
&&& SubShader
&&&&&&&&&&&
// Set up basic vertex lighting
&&&&&&&&&&&
Material {
&&&&&&&&&&&&&&&
Diffuse [_Color]
&&&&&&&&&&&&&&&
Ambient [_Color]
&&&&&&&&&&&&&&&
Shininess [_Shininess]
&&&&&&&&&&&&&&&
Specular [_SpecColor]
&&&&&&&&&&&&&&&
Emission [_Emission]
&&&&&&&&&&&
&&&&&&&&&&&
Lighting On
&&&&&&&&&&&
// Use texture alpha to blend up to white (= full
illumination)
&&&&&&&&&&&
SetTexture [_MainTex] {
&&&&&&&&&&&&&&&
constantColor [_IlluminCol]
&&&&&&&&&&&&&&&
combine constant lerp(texture) previous
&&&&&&&&&&&
&&&&&&&&&&&
// Multiply in texture
&&&&&&&&&&&
SetTexture [_MainTex] {
&&&&&&&&&&&&&&&
combine previous * texture
&&&&&&&&&&&
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。}

我要回帖

更多关于 unity3d 透明玻璃材质 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信