Unity, replacing shader rendering (Rendering with replaced Shaders)

Source: Internet
Author: User

To achieve special effects, especially some post-processing effects, it is often necessary to replace each object's shader with another set of shader for rendering to the texture, then compositing or superimposed on the final screen in some way.

A little more complicated, maybe different objects are replaced with shader.

Camera.renderwithshader in Unity enables this functionality.

The following is an official document quote:

Rendering with replaced Shaders

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 Orientati ONS differ. Other effects might need a texture with scene depth, and so on. To achieve this, it's possible to render the scene with replaced shaders of all objects.

Shader replacement is do from scripting using Camera.renderwithshader or Camera.setreplacementshader functions. Both functions take a shader and a replacementtag.

It works like This:the camera renders the scene as it normally would. The objects still use their materials, but the actual shader this ends up being used is changed:

    • If Replacementtag is empty and then all objects in the scene was rendered with the given replacement shader.
    • If Replacementtag is not empty and then for each object that would be rendered:
      • The real object ' s shader is queried for the tag value.
      • If It does not has that tag, the object is not rendered.
      • A Subshader is found in the replacement shader, which has a given tag with the found value. If No such subshader is found, object is not rendered.
      • Now, Subshader is used to render the object.

So if all shaders would has, for example, a "rendertype" tag with the values like "Opaque", "Transparent", "Background", "Ove Rlay ", you could write a replacement shader the only renders solid objects by using one subshader with Rendertype=solid t Ag. The other tag types would is found in the replacement shader and so the objects would is not rendered. Or you could write several subshaders for different "rendertype" tag values. Incidentally, all built-in Unity shaders has a "rendertype" tag set.

One of the most need to understand is Replacementtag, the above document detailing the logic of Replacementtag, in order to understand, the following different explanation again:

* Suppose the script calls getcomponent<camera> (). Renderwithshader (Shader.find ("Shaderx"), ""), all objects rendered by this camera will be rendered using Shaderx.

* Assume that the getcomponent<camera> () is called in the foot. Renderwithshader (Shader.find ("Shaderx"), "Myreplacementtag"), then for each object of this minor rendering object (i), assume that the Shader of object (i) itself is Shader (i) If all subshader of shader (i) are not labeled "Myreplacementtag", then object (i) is not rendered, if Subshader (j) in Shader (i) has "Myreplacementtag" Label, set this label as "myreplacementtag" = "a", then unity will go to Shaderx to find "myreplacementtag" = "a" subshader, if found, The original shader of object (i) is replaced with this subshader of Shaderx, otherwise object (i) is not rendered.

It should be noted that "Myreplacementtag" should always be "rendertype" because all shader built into unity have rendertype tags.

Give two examples:

Example 1, Replace all opaque objects shader with another shader for rendering:

Write a Shaderx that contains a subshader with the label "rendertype" = "Opaque",

Call Getcomponent<camera> (). Renderwithshader (Shader.find ("Shaderx"), "Rendertype");

Example 2, replace all opaque objects shader with one subshader for rendering, and replace all transparent objects shader with another shader for rendering:

Write a Shaderx that contains a subshader with "rendertype" = "Opaque" label, and then write a rendertype with "Transparent" = "subshader" tag,

Call Getcomponent<camera> (). Renderwithshader (Shader.find ("Shaderx"), "Rendertype");

Example 3, replace the shader of all "rendertype" = "Myrendertype" objects with another shader for rendering:

Write a Shaderx that contains a subshader with the label "rendertype" = "Myrendertype",

Call Getcomponent<camera> (). Renderwithshader (Shader.find ("Shaderx"), "Rendertype");

In addition, the difference between Camera.renderwithshader and Camera.setreplacementshader:

Camera.renderwithshader only the shader;camera.setreplacementshader used for this rendering is to render with the replacement shader since the call, until the camera.resetreplacement is manually invoked Shader so far, revert to rendering with its own shader. http://m.blog.csdn.net/blog/QUAN2008HAPPY/39380463 a reference to;

In addition, the official document of Camera.renderwithshader writes:

This was used for special effects, e.g. rendering screenspace normal buffer of the whole scene, heat vision and so on. To make use of the this feature, usually you create a camera and disable it. Then call Renderwithshader on it.

That is, when using renderwithshader to implement effects, you should usually set the camera that calls Renderwithshader this function to disable, namely:getcomponent<camera> (). Enabled = False, or it can be removed directly in front of the camera component in Inspector, which is the same effect.

Unity, replacing shader rendering (Rendering with replaced Shaders)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.