Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Keyword stripping?

Discussion in 'Shader Graph' started by AllanSmithZ3, Aug 26, 2021.

  1. AllanSmithZ3

    AllanSmithZ3

    Joined:
    May 21, 2019
    Posts:
    23
    Hi! Im a noob at shader graph (and even more at shader coding), but thanks to the awesome tool that shader graph is I managed to make a shader that encompasses a lot of the functionality I need in my project. However, Id like to use keywords to strip out part of the shader where not necessary.

    One example is I made a simple "wind" feature in the shader that will displace vertexes based on some parameters. So I thought "cool, Ill create a boolean keyword", then I noticed that the way it works is it takes 2 inputs (on and off) and outputs like a gate. Cool... however my question is... is the "on" calculation stripped when it is set to off? (see image) (EDIT: The answer is no, check my next reply, which then asks "how do I strip this?")


    Like... there are parameters and calculations and they all feed INTO the keyword, while from my programming mind I would actually want those to only be defined inside the "if true" scope. I know I can inspect the generated shader but honestly haha I tried, I will keep trying but if anyone can shed some light that would be good =P because what I want is not stripping the result of the calculation, is stripping the calculation itself...

    Thanks for the help!
     
    Last edited: Aug 26, 2021
  2. AllanSmithZ3

    AllanSmithZ3

    Joined:
    May 21, 2019
    Posts:
    23
    Ok I was being a bit dense, I went to explore a bit more the generated shader and found this which is what I was looking for:

    Code (CSharp):
    1.             #if defined(KEYWORD_PERMUTATION_0) || defined(KEYWORD_PERMUTATION_1)
    2.             Bindings_Z3VertexAlphaWind_e7e5f404e26832249907073a0aca217d _Z3VertexAlphaWind_433ed83bb96c4cee835c7010761d65df;
    3.             _Z3VertexAlphaWind_433ed83bb96c4cee835c7010761d65df.WorldSpaceNormal = IN.WorldSpaceNormal;
    4.             _Z3VertexAlphaWind_433ed83bb96c4cee835c7010761d65df.WorldSpaceTangent = IN.WorldSpaceTangent;
    5.             _Z3VertexAlphaWind_433ed83bb96c4cee835c7010761d65df.WorldSpaceBiTangent = IN.WorldSpaceBiTangent;
    6.             _Z3VertexAlphaWind_433ed83bb96c4cee835c7010761d65df.ObjectSpacePosition = IN.ObjectSpacePosition;
    7.             _Z3VertexAlphaWind_433ed83bb96c4cee835c7010761d65df.WorldSpacePosition = IN.WorldSpacePosition;
    8.             _Z3VertexAlphaWind_433ed83bb96c4cee835c7010761d65df.TimeParameters = IN.TimeParameters;
    9.             float3 _Z3VertexAlphaWind_433ed83bb96c4cee835c7010761d65df_OutVector4_1;
    10.             SG_Z3VertexAlphaWind_e7e5f404e26832249907073a0aca217d(_Property_0ec9f7df089d4ddcbdf8f3fae597529c_Out_0, _Property_6ba7c2112b61420fafe8874ffe492237_Out_0, _Property_1b974403869e4f33a381eddeda8c9177_Out_0, _Property_a393b1ba5b344407b97d54439ce8284f_Out_0, _Split_8c2eb40230d24ee5b55d2b7ba8f25a72_A_4, _Z3VertexAlphaWind_433ed83bb96c4cee835c7010761d65df, _Z3VertexAlphaWind_433ed83bb96c4cee835c7010761d65df_OutVector4_1);
    11.             #endif
    12.             #if defined(KEYWORD_PERMUTATION_0) || defined(KEYWORD_PERMUTATION_1)
    13.             #if defined(_WINDENABLED_ON)
    14.             float3 _Wind_ef679a5020da413bba00bbdbe8d25b79_Out_0 = _Z3VertexAlphaWind_433ed83bb96c4cee835c7010761d65df_OutVector4_1;
    15.             #else
    16.             float3 _Wind_ef679a5020da413bba00bbdbe8d25b79_Out_0 = IN.ObjectSpacePosition;
    17.             #endif
    18.             #endif
    The "KEYWORD_PERMUTATION_X" is defined based on the "Wind" keyword, and "_WINDENABLED_ON" is the actual boolean keyword, so in short the answer to my own question in the original post is "no, that part it not stripped". That changes the question to... "how do I strip that?" =P the code I wanted to have generated would be this:


    Code (CSharp):
    1.             #if defined(_WINDENABLED_ON)
    2.             Bindings_Z3VertexAlphaWind_e7e5f404e26832249907073a0aca217d _Z3VertexAlphaWind_433ed83bb96c4cee835c7010761d65df;
    3.             _Z3VertexAlphaWind_433ed83bb96c4cee835c7010761d65df.WorldSpaceNormal = IN.WorldSpaceNormal;
    4.             _Z3VertexAlphaWind_433ed83bb96c4cee835c7010761d65df.WorldSpaceTangent = IN.WorldSpaceTangent;
    5.             _Z3VertexAlphaWind_433ed83bb96c4cee835c7010761d65df.WorldSpaceBiTangent = IN.WorldSpaceBiTangent;
    6.             _Z3VertexAlphaWind_433ed83bb96c4cee835c7010761d65df.ObjectSpacePosition = IN.ObjectSpacePosition;
    7.             _Z3VertexAlphaWind_433ed83bb96c4cee835c7010761d65df.WorldSpacePosition = IN.WorldSpacePosition;
    8.             _Z3VertexAlphaWind_433ed83bb96c4cee835c7010761d65df.TimeParameters = IN.TimeParameters;
    9.             float3 _Z3VertexAlphaWind_433ed83bb96c4cee835c7010761d65df_OutVector4_1;
    10.             SG_Z3VertexAlphaWind_e7e5f404e26832249907073a0aca217d(_Property_0ec9f7df089d4ddcbdf8f3fae597529c_Out_0, _Property_6ba7c2112b61420fafe8874ffe492237_Out_0, _Property_1b974403869e4f33a381eddeda8c9177_Out_0, _Property_a393b1ba5b344407b97d54439ce8284f_Out_0, _Split_8c2eb40230d24ee5b55d2b7ba8f25a72_A_4, _Z3VertexAlphaWind_433ed83bb96c4cee835c7010761d65df, _Z3VertexAlphaWind_433ed83bb96c4cee835c7010761d65df_OutVector4_1);
    11.             float3 _Wind_ef679a5020da413bba00bbdbe8d25b79_Out_0 = _Z3VertexAlphaWind_433ed83bb96c4cee835c7010761d65df_OutVector4_1;
    12.             #else
    13.             float3 _Wind_ef679a5020da413bba00bbdbe8d25b79_Out_0 = IN.ObjectSpacePosition;
    14.             #endif
     
  3. AllanSmithZ3

    AllanSmithZ3

    Joined:
    May 21, 2019
    Posts:
    23
    I apologise for the bump, but does anyone know if this is possible in any way? For a case like this where you want pieces of your shader code to be completely stripped be done by hand with the generated code?

    Thanks in advance for the help!
     
  4. AllanSmithZ3

    AllanSmithZ3

    Joined:
    May 21, 2019
    Posts:
    23
    Sorry for the bump once again but this question comes to mind from time to time... I reached the conclusion that proper stripping like in my sample code above is impossible with the current system, but seems like an important feature to be added if that's the case?