unity-game-engineshaderfragment-shadershaderlab

How to cache common (for all pixels) variables in ShaderLab?


Suppose I have the following shader:

Shader "Sprites/X"
{
    Properties
    {
        [HideInInspector] _MainTex("Sprite Texture", 2D) = "black" {}
    }

    SubShader
    {
        Pass
        {
            CGPROGRAM

            // bla bla

            fixed4 frag(v2f i) : SV_Target
            {
                float a = calculateA();
                float b = calculateB();
                .
                .
                .
                float z = calculateZ();

                // Here the values above mix with x-y position of the pixel
                // and then return a color
            }
            ENDCG
        }
    }
}

In the above code I need to calculate the values of the variables a - z from the related functions calculateA() - calculateZ() (it's just a generalization example). Suppose these functions are complex and that they depend from other properties of the shader that change in each frame, instead of the pixel itself. We know that the complex functions are computed every time for each pixel.

There is a way to "cache" these values to avoid the calculation each time for each pixel? In every frame their values is the same for all the pixels.

Currently I'm solving the problem by setting the values in the C# script with:

gameObject.GetComponent<Renderer>().material.SetFloat("a", calculateA());
.
.
.
gameObject.GetComponent<Renderer>().material.SetFloat("z", calculateZ());

but i prefer to mantain the shader and the C# script code separated.


Solution

  • Well, that's exactly how you'd do it :)

    It depends on why exactly you'd want to avoid using C#