Search Unity

How to initialize, reset and purge shader/material variables

Discussion in 'Shaders' started by ScottieBizzle, Aug 27, 2015.

  1. ScottieBizzle

    ScottieBizzle

    Joined:
    Nov 9, 2013
    Posts:
    17
    As a quick recap, shader constants declared with "static const" for example use the defaults declared to the variable and are baked into the shader assembly when compiled. Dynamic shader variables on the other hand ignore defaults declared in the shader, but if you define a property for them in the shaderlab's property block, they will use the default value you gave them. However, any change to that variable from the material inspector or from script ( SetFloat or SetGlobalFloat ) will override the default value and will persist forever in the material. So if you remove the variable from the shader, then try to use the same name again later, it will map its old lingering value over your new shader variable.

    static const float ValueA = 0.45; // this default value is used since it gets baked into the compiled shader

    float ValueB = 0.8; // this default value is ignored and either set to 0 or overriden by any prior change to the value by a Material.SetFloat or from the material inspector

    So my questions are..

    Q) Is there any way to hint Unity to reset or purge lingering material variables values for exposed AND non-exposed variables similar to the material inspector's Reset menu function? Preferrably with the ability to do this independently so that I can reset only non-property variables back to their defaults?

    Q) If not, is there some per-material or per-shader script callback I can use to manually set these values when a shader is compiled or material is reloaded?

    Q) If variables that are not exposed as properties ignore their defaults, and require a script to set them, why would you keep these values saved in the material? It only make sense for hidden/exposed property variables.

    The reason I'm asking is that I've got a standalone shader authoring tool connecting to Unity via DLL which auto generates shader/csharp/shaderlab code, it also dynamically sends user tweaked variable values through my DLL to script, however these values are meant to be interim values for quicker iteration until the shader is regenerated and recompiled again. Problem is, the defaults are either not used when recompiled ( unless they are hidden property ) in which case the default is ignored and is using the last value lingering in the material.

    Thanks
     
    Last edited: Aug 27, 2015
  2. guyx10

    guyx10

    Joined:
    May 1, 2022
    Posts:
    1
    did you find an answer?