Rendering with Replaced Shaders 使用替换着色器来渲染
原文地址:http://unity3d.com/support/documentation/Components/SL-ShaderReplacement.html
如有翻译不当之处,还请帮忙指出!
Some rendering effects require rendering a scene with a different set of shaders. For example, good edge detection would need a texture with scene normals, so it could detect edges where surface orientations differ. Other effects might need a texture with scene depth, and so on. To achieve this, it is possible to render the scene with replaced shaders of all objects.
一些渲染特效需要使用一些不同的着色器来渲染场景。比如,一个好的边界提取效果需要由一个记录场景法线信息的纹理才能检测出场景中的朝向变化的地方;其他效果也可能需要一个记录场景深度的纹理等等。要想达到这些效果,我们可能需要使用所有物体的替代着色器来渲染场景。
Shader replacement is done from scripting usingCamera.RenderWithShader or Camera.SetReplacementShader functions. Both functions take a shader and a replacementTag.
着色器更换是从脚本使用 Camera.RenderWithShader或 Camera.SetReplacementShader函数来完成的。这两个函数均以一个着色器和一个replacementTag作为参数。
It works like this: the camera renders the scene as it normally would. the objects still use their materials, but the actual shader that ends up being used is changed:
它是像这样来进行工作的:相机还是像平常一样来渲染场景,物体也还是使用原有的材质,但是他们的真实材质是在使用时来进行改变:
If replacementTag is empty, then all objects in the scene are rendered with the given replacement shader.
如果replacementTag是空的,那么场景中的所有物体都用给定的替换着色器来进行渲染。
If replacementTag is not empty, then for each object that would be rendered:
如果replacementTag不是空的,那么对于每个物体,将进行以下方式进行渲染:
· The real object's shader is queried for the tag value.
· 按照标签值来查找物体真正的着色器。
· If it does not have that tag, object is not rendered.
· 如果没有标签,物体将不进行渲染。
· A subshader is found in the replacement shader that has a given tag with the found value. If no such subshader is found, object isnot rendered.
· 在替换着色器中,我们可以根据一个具有查找值的标签来找到一个子着色器,如果没有这样的子着色器,该物体将不进行渲染。
· Now that subshader is used to render the object.
· 现在可以应用子着色器来渲染物体。
So if all shaders would have, for example, a "RenderType" tag with values like "Opaque", "Transparent", "Background", "Overlay", you could write a replacement shader that only renders solid objects by using one subshader with RenderType=Solid tag. The other tag types would not be found in the replacement shader, so the objects would be not rendered. Or you could write several subshaders for different "RenderType" tag values. Incidentally, all built-in Unity shaders have a "RenderType" tag set.
所以,如果所有着色器都拥有一个渲染类型标签,比如"不透明"、"透明"、"背景"、"叠加"等,你就可以编写一个含有“RenderType = Solid”的替换着色器来只渲染固体物体。其他标记并不会在该替换着色器中被找到,所以与其对应的物体将不会被渲染。,发现其他标记类型。或者,你可以编写拥有不同的"RenderType"标签值的子着色器。顺便说一下,所有Unity内置着色器都含有一个"RenderType"标签集。
Shader replacement tags in built-in Unity shaders Unity内建着色器的着色器替换标签
All built-in Unity shaders have a "RenderType" tag set that can be used when rendering with replaced shaders. Tag values are the following:
当使用替换着色器进行渲染时,Unity所有的着色器都含有一个“渲染类型”标签集,其中的标签值如下所示:
Opaque: most of the shaders (Normal,Self Illuminated,Reflective, terrain shaders).
Opaque:用于大多数着色器(法线着色器、自发光着色器、反射着色器以及地形的着色器)。
Transparent: most semitransparent shaders (Transparent, Particle, Font, terrain additive pass shaders).
Transparent:用于半透明着色器(透明着色器、粒子着色器、字体着色器、地形额外通道的着色器)。
TransparentCutout: masked transparency shaders (Transparent Cutout, two pass vegetation shaders).
TransparentCutout:蒙皮透明着色器(Transparent Cutout,两个通道的植被着色器)。
Background: Skybox shaders.
Background:天空盒着色器。
Overlay: GUITexture, Halo, Flare shaders.
Overlay: GUITexture、光晕着色器、闪光着色器。
TreeOpaque: terrain engine tree bark.
TreeOpaque:地形引擎中的树皮。
TreeTransparentCutout: terrain engine tree leaves.
TreeTransparentCutout:地形引擎中的树叶。
TreeBillboard: terrain engine billboarded trees.
TreeBillboard:地形引擎中的billboard树。
Grass: terrain engine grass.
Grass:地形引擎中的草。
GrassBillboard: terrain engine billboarded grass.
GrassBillboard:地形引擎何中的billboard草。
Built-in scene depth/normals texture 内建的场景 深度/法线纹理
A Camera has a built-in capability to render depth or depth+normals texture, if you need that in some of your effects. SeeCamera Depth Texture page.
如果你在你的特效中需要的话,照相机自身也有能力来渲染深度或者深度+法线纹理。请参考Camera Depth Texture。