[Unity shaders] uses unity render textures to implement screen effects-overlay hybrid mode in screen Effects

Source: Internet
Author: User

This series mainly references the book "Unity shaders and effects cookbook" (thanks to the author of the original book) and adds a bit of personal understanding or expansion.

Here are all the illustrations in this book. Here is the code and resources required for this book (you can also download them from the official website ).

========================================================== ===================================================== =====



Preface


In this article, we will learn another hybrid mode, overlay. This hybrid mode uses a condition judgment statement to determine the final pixel value. Therefore, the computation used here is a little more complex (in fact, it is also very simple ).


Let's review the formula used for superposition:

Where a is the base color and B is the mixed color.



Preparations


  1. Create a new script named overlay_imageeffect and a new shader named overlay_effect.
  2. Copy the C # code in the previous article to the new script.
  3. Copy the shader code in the previous article to the new shader.
  4. Add the overlay_imageeffect script to camera and assign a value to the cur shader in the script using overlay_effect shader.

Implementation
This code is very simple and does not need to be modified. The changes are as follows:
  1. As mentioned before, we need to add a condition judgment statement for color mixing. Therefore, we need to write a function that accepts the color values of each channel (RGB) in sequence and then performs the superposition operation. Define the following functions above the frag function:
    fixed OverlayBlendMode(fixed basePixel, fixed blendPixel) {if (basePixel < 0.5) {return (2.0 * basePixel * blendPixel);} else {return (1.0 - 2.0 * (1.0 - basePixel) * (1.0 - blendPixel));}}

  2. Finally, modify the frag function to mix the channels in the two textures:
    fixed4 frag(v2f_img i) : COLOR {//Get the colors from the RenderTexture and the uv‘s//from the v2f_img structfixed4 renderTex = tex2D(_MainTex, i.uv);fixed4 blendTex = tex2D(_BlendTex, i.uv);fixed4 blendedImage = renderTex;blendedImage.r = OverlayBlendMode(renderTex.r, blendTex.r);blendedImage.g = OverlayBlendMode(renderTex.g, blendTex.g);blendedImage.b = OverlayBlendMode(renderTex.b, blendTex.b);// Adjust amount of Blend Mode with a lerprenderTex = lerp(renderTex, blendedImage,  _Opacity);return renderTex;}

Finally, you can see the effect similar to the following: Overlay hybrid modeOpacity = 1.0:


Explanation
The overlapping effect is actually a combination of multiply and screen. It determines the mixed mode by determining the color value of each channel.
This is the last section of the image effect chapter ~ As you can see, image effects contain a lot of knowledge and content. It relies heavily on your platform and the memory capacity allocated to image effects. So enjoy and play with special effects as much as possible ~


[Unity shaders] uses unity render textures to implement screen effects-overlay hybrid mode in screen Effects

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.