Home  >  Article  >  Backend Development  >  Unity 3D briefly discusses the problems encountered in Shader runtime status and rendering mode

Unity 3D briefly discusses the problems encountered in Shader runtime status and rendering mode

little bottle
little bottleforward
2019-04-29 18:03:094190browse

The Shader in Unity has four rendering modes, which are:

——Opaque(不透明)
——Cutout(镂空)
——Fade(隐现)
——Transparent(透明)

Before I encountered a need to add a freezing effect to a specific game object, but to my Shader
is a completely frozen Shader, and its color value cannot be adjusted through parameters. Later, my colleague told me that I could
change the Alpha value of the object itself by modifying the rendering mode, so the first step was to find the code on the Internet about modifying the runtime Shader rendering mode, as follows.
http://www.voidcn.com/article...

Related tutorials:

C# Video tutorial

Then there are simple code operations ,as follows.

       //获取目标身上的渲染组件
       var renderer = GetComponentsInChildren<renderer>();
       // 建立一个冰冻数组 第二个数组元素是事先找好的冰冻特效
       Material[] frostMatetials = new Material[2] { renderer.material,
        frostEffect };
       //添加冰冻特效到人物身上的数组中
       renderer.materials = frostMatetials;
       //实例化一个渲染模式变量
       var fadeMode = SetMaterialRenderingMode.RenderingMode.Fade;
       //根据网上的方法更改你的 Shader 渲染模式为 Fade
           
      SetMaterialRenderingMode.SetMaterialRenderingModeMethod(
      renderer.materials[0],fadeMode);</renderer>
Since the Alpha value of Color is read-only and cannot be modified, but Color can be modified, so you need to get the RGB value of the target

object Color, and then instantiate a Color and use the constructor to change it to what you want. The desired Alpha value. As follows:

       Color currentcolor03 = new Color
        {
            a = 0.8f,
            r = renderer.materials[0].color.r,
            g = renderer.materials[0].color.g,
            b = renderer.materials[0].color.b
        };
        //替换目标 Color 属性就可以实现了
In fact, this method is not very good, mainly because the given Shader cannot modify the attribute value. Later, I got a Shader that can change the

attribute value, and things became much simpler.

Unity 3D briefly discusses the problems encountered in Shader runtime status and rendering mode

This is the material I got. You can find the Shader contained in the material through the material, and you can see the parameters corresponding to each value.

Unity 3D briefly discusses the problems encountered in Shader runtime status and rendering mode

In this case, the modification only needs to be made by calling the method Material.SetFloat(); and similar methods. There are two

parameters in the brackets. One is a parameter of String type. , indicating that the parameter you want to change is obtained through String, and the second one is the changed value.

During this period, I encountered a problem. After searching for a long time, I found that it was a problem with the compiler. When I used the first method, I could modify the rendering mode under normal

conditions, and I used Three Colors are replaced in sequence to achieve a gradient effect, but if I select this object in the
Hierarchy panel, the changing object will suddenly return to the default state before the change, or if
you select it in advance The same effect will occur if the object to be changed is selected, that is, the game object cannot be selected in the compilation state, and its rendering status cannot be viewed in the Inspector panel. Finally, I came up with a runtime rendering Words to
describe this behavior,
If you change the rendering mode of a game object Shader at runtime, then viewing its rendering mode will cause its rendering mode
properties to be reset.

The above is the detailed content of Unity 3D briefly discusses the problems encountered in Shader runtime status and rendering mode. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete