Question

what is better way change a material in Unity,should i change material.shader or set a Float in shader to control effect?

I want to add an effect to my object, such as highlighting, but I don't always need it. Should I change the shader of the material when it needs to be displayed, or should I create a '_Llend' in Properties to set values to control my shader? Which one is better?

 2  27  2
1 Jan 1970

Solution

 1

There is no single answer to your question. It highly depends on the situation and the context.

Sometimes you need entirely new material, sometimes - not. Sometimes, you even need several copies of the object, each with the same material, but only one to be modified, etc.

Using new material will break batching. Sometimes, this is okay, and sometimes, it is not. Using parameters can also break batching in some cases.

If it is a unique object, you can choose any option. If you have, for example, 30-50 such objects and only need to change one of them, this will be a completely different task.

So, in general, I would suggest adding any option you want but making it a way to prevent other parts of your game depending on this concrete implementation. Let the game think of it as "object changed its state to highlighted". In this case, it would be completely not a problem to change the implementation completely in case of need.

2024-07-24
Morion

Solution

 1

For adding a dynamic effect like highlighting, it's generally better to use a shader property to control the effect. This method is more efficient and flexible than switching shaders.

Using a Shader Property
Modify the Shader:
Add a property to control the highlighting effect. Here’s an example shader with a _HighlightIntensity property:

Shader "Custom/HighlightShader"
{
    Properties
    {
        _MainTex ("Base (RGB)", 2D) = "white" {}
        _HighlightIntensity ("Highlight Intensity", Range(0, 1)) = 0.0
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        #pragma surface surf Standard

        sampler2D _MainTex;
        half _HighlightIntensity;

        struct Input
        {
            float2 uv_MainTex;
        };

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            half4 c = tex2D (_MainTex, IN.uv_MainTex);
            o.Albedo = c.rgb * (1.0 + _HighlightIntensity * 0.5); // Adjust this to control highlight effect
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

Control the Shader Property from Script: Use a script to toggle the highlighting effect by setting the _HighlightIntensity property.

public class HighlightController : MonoBehaviour
{
    public Material material;

    public void SetHighlight(bool enable)
    {
        if (enable)
        {
            material.SetFloat("_HighlightIntensity", 1.0f); // Enable highlight
        }
        else
        {
            material.SetFloat("_HighlightIntensity", 0.0f); // Disable highlight
        }
    }
}

Advantages of Using Shader Properties

  • Performance: Adjusting a shader property is more performant than switching shaders.
  • Flexibility: Allows for smooth transitions and animations.
  • Simplicity: Easier to manage a single shader with adjustable properties.
2024-07-24
Qusai Azzam