Esfog_unityshader Tutorial _ Dissolve effect dissolve

Source: Internet
Author: User

  Dissolution effects are common in the game, such as in some myths or magical worlds, where some NPC characters will gradually disappear when the plot is needed. Even some of the more flashy ones, like burning the target with a flamethrower. These can all be used to dissolve the effect. This article is mainly to explain how the comparative basis of the dissolution effect, the implementation of the method is not unique, this article is only one of the ideas.

Principle

  Now that you want the character body piece to fade away, you might as well let the character body do not render the corresponding parts (or change to transparent, we choose the former). What determines which part of the body needs to be dissolved, which requires an extra decal or an alpha channel using the character texture map (the former is selected). This map, like the texture map, corresponds to each position of the player's body so that we can control whether the body parts of the character are dissolved according to the color value of a specified channel on the map.

In addition, we will also be involved in a command called Discard, is provided by CG, if appearing in the Fragmentshader to immediately discard the current processing of the slice. That is, we use the Discard command When we decide that the current piece needs to be dissolved far away.

The above image is used to control the degree of dissolution of the texture, we are relatively simple here, only use the R channel, if you want to do very complex can also use the other channels. In this article, we will constantly dissolve the area where the R channel color value is smaller on the map according to the time. Art can use this map to control the character's arbitrary dissolution order and effect. Because my material is casually looking for, I do not draw, so this figure and character texture may not match, please do not care.

Take a look at the diagram below, I'll go ahead and talk about the various adjustable parameters provided in the properties of this article shader

1.Base (RGB) is a character texture map, not explained.

2.Noisetex (R) is very important, we used to control the character dissolution of the style map, we only use the R channel.

3.dissolvespeed (Second), the entire dissolution process takes time, unit is seconds

4.Edgewidth, this is an extra edge effect, such as when you watch the paper turning into ashes, his surroundings will first become black-brown. Our edgewidth here is to define the size of the surrounding area, note that this width is not the length of the reference, but the interval of transparency, that is, how much difference from the baseline value can be counted as edge processing.

5.Edgecolor, and 4, the color of the edge effect.

Realize

1Shader"Esfog/dissolve" 2 {3 Properties4     {5_maintex ("Base (RGB)", 2D) =" White" {}6_noisetex ("Noisetex (R)", 2D) =" White"{}7_dissolvespeed ("dissolvespeed (Second)", Float) =18_edgewidth ("Edgewidth", Range (0,0.5)) =0.19_edgecolor ("Edgecolor", Color) = (1,1,1,1)Ten     } One Subshader A     { -Tags {"Rendertype"="Opaque" } -          the Pass -         { - Cgprogram -             #pragmaVertex vert_img +             #pragmaFragment Frag -#include"Unitycg.cginc" +               A uniform sampler2d _maintex; at uniform sampler2d _noisetex; -Uniformfloat_dissolvespeed; -Uniformfloat_edgewidth; - uniform float4 _edgecolor; -              - float4 Frag (v2f_img i): COLOR in             { -                 floatDissolvefactor = saturate (_time.y/_dissolvespeed); to                 floatNoisevalue =tex2d (_NOISETEX,I.UV). R;  +                 if(Noisevalue <=dissolvefactor) -                 { the Discard; *                 } $                 Panax NotoginsengFLOAT4 Texcolor =tex2d (_MAINTEX,I.UV); -                 floatEdgefactor = Saturate ((noisevalue-dissolvefactor)/(_edgewidth*dissolvefactor)); theFLOAT4 Blendcolor = Texcolor *_edgecolor; +                                  A                 returnLerp (Texcolor,blendcolor,1-edgefactor); the             } +              - ENDCG $         } $     }  -      - FallBack Off the}

5~9 The line , explained before, here is no longer explained.

18 Rows , here I used unity-provided vert_img vertex shader, because our requirements are simple, only need to do vertex coordinate transformation and the UV passed to the back of the line, since there is no ready to write their own, it will automatically return the results to a v2f_ The IMG structure.

30 Lines , where the _time.y is from the beginning of the object rendering to now the time of the past unity gives us a few different time parameters are saved in the _time, where _time.y is the standard Time, unit (s). Use it to divide by _dissolvespeed. You can get the total dissolved time that is already occupied by the current time. Since we want to ensure that the ratio range is limited to the range of (0,1), the last call to a saturate () function, this functional CG provides is to accomplish this purpose.

  31 Rows , remove the value of the R channel from the Dissolve map.

  32~35 Line , we use just calculate the ratio to the dissolution map inside the R channel color values to judge, if the R channel color value is less than equal to this ratio then we discard discard the current slice far, the current piece far processing will immediately end.

  38 Rows , and a ratio is calculated, the molecule is Noisevalue-dissolvefactor, this value indicates the difference between the R channel value on the dissolution map and the current dissolution reference value, and (_edgewidth*dissolvefactor) It can be understood that _edgewidth means that the difference can be counted as the edge, and multiplied by a dissolvefactor, it means that the maximum width of the edge will change over time, the wider the width. The last two values are divided to represent the edge degree of the current slice, the smaller the value, the longer it will be dissolved from him, and vice versa that he is soon dissolved.

  39 Rows , we calculate the current texture color multiplied by the edge color, which needs to be used later.

  41 Line , here we use the LERP function to use the previous step calculated (1-edgefactor) to the original texture color and the blend color in the previous step, we have said in the 38th line, the smaller the edgefactor means the closer to dissolution time , that is, the heavier the color component of the edge, then because Lerp (x,y,a) = x* (1-a) + y*a; To make the color of the edge heavier we use 1-edgefactor as a factor, and vice versa.

  Well look at the effect, using a different dissolve map will produce a completely different effect, the following shows me from the internet casually found two stickers, because and the original model texture is not a set, so it looks a bit strange, do not care.

Use this decal 1:

The effect is as follows

Using decal 2:

The effect is as follows:

  Well the basic effect is so, overall this shader effect looks good and bad, mainly depends on the art provided by the dissolution map is appropriate, this section of material is not a set, it looks a bit strange.

Another point is that everyone in the process of learning shader you will always find others hit the source code in a lot of pow,lerp, or the subtraction of the parameters, I hope you don't too tangled up the meaning of these places, and sometimes there are certain mathematical principles, But most of the time the developer wrote an empirical formula for the purpose of adjusting the effect. As your experience grows, you will begin to use some empirical formulas. The effect is good, as long as the performance is not affected. After all, it is not the source code for the player to see.

  Respect for the wisdom of others, welcome reprint, please specify the author Esfog, the original address http://www.cnblogs.com/Esfog/p/4469025.html

  

Esfog_unityshader Tutorial _ Dissolve effect dissolve

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.