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 How do I use global keywords

Discussion in 'Shaders' started by Micz84, Aug 3, 2023.

  1. Micz84

    Micz84

    Joined:
    Jul 21, 2012
    Posts:
    436
    I have created the global keyword RENDER_ONLY_COLOR using GlobalKeyword.Create.
    I am enabling it using Shader.EnableKeyword method.
    In shader I have:
    #pragma shader_feature RENDER_ONLY_COLOR
    and later in code I use like this:
    Code (CSharp):
    1. if (RENDER_ONLY_COLOR)
    2.         col1 = pixelColor;
    but I get an error:
    Shader error in 'test/ColorShader': undeclared identifier 'RENDER_ONLY_COLOR' at ... (on d3d11)

    what I am doing wrong?
    EDIT:
    Ok, I have found what I was doing wrong, but It behaves as if it was always true. When I disable the keyword it disappears from the list of enabled global keywords but the shader still behaves like it was enabled.
     
    Last edited: Aug 3, 2023
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,221
    #if defined(RENDER_ONLY_COLOR)
    or
    #ifdef RENDER_ONLY_COLOR


    Code (csharp):
    1. #if defined(RENDER_ONLY_COLOR)
    2. col1 = pixelColor;
    3. #endif
    if
    is a branch in the shader code itself. Keywords are preprocess defines, and preprocessor related code uses the
    #
    as a prefix, so you want to use
    #if
    .
     
    Micz84 likes this.