Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question I used #pragma shader_feature macro declaration methods, but I didn't successfully enable them in my

Discussion in 'Shaders' started by wechat_os_Qy01Clc1a-n6lbMBoILatSp6w, Apr 11, 2024.

  1. wechat_os_Qy01Clc1a-n6lbMBoILatSp6w

    wechat_os_Qy01Clc1a-n6lbMBoILatSp6w

    Joined:
    Oct 6, 2022
    Posts:
    25
    Code (CSharp):
    1.  #pragma shader_feature _TRANSITIONS_ERASE _TRANSITIONS_ROUND _TRANSITIONS_CHECKERBOARD _TRANSITIONS_MICROARC _TRANSITIONS_DRAWERS _TRANSITIONS_TURBULENT
    I defined multiple macros in my shader, they are mutually exclusive effects, and only one macro can be turned on, so I used the macro definition above (don't worry about the code format, there is no shaderlab syntax in the forum). I've defined /[KeywordEnum(Round,Erase,Checkerboard,Microarc,drawers,turbulent)] _Transitions("Transitions mode", Float) = 0 in the Materials panel I use the enumeration parameters of the Material panel to manually switch macros, which is fine, but as long as I use C#
    Code (CSharp):
    1.  public Material TransitionMaterial;
    2.     private string[] Key = new[]
    3.     {
    4.         "_TRANSITIONS_ERASE", "_TRANSITIONS_ROUND", "_TRANSITIONS_CHECKERBOARD", "_TRANSITIONS_MICROARC",
    5.         "_TRANSITIONS_DRAWERS", "_TRANSITIONS_TURBULENT"
    6.     };
    7.  
    8.     private string[] Value = new[] { "_Erase", "_Round", "_Checkerboard", "_Microarc", "_Drawers", "_turbulent" };
    9.     //private int oldkey = 100;
    10.  
    11.     private void Start()
    12.     {
    13.      
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         if (Input.GetKeyDown (KeyCode.Alpha0))
    19.         {
    20.             Debug.Log("0");
    21.             SetRoadom(0);
    22.         }
    23.         if (Input.GetKeyDown (KeyCode.Alpha1))
    24.         {
    25.             Debug.Log("1");
    26.             SetRoadom(1);
    27.         }
    28.         if (Input.GetKeyDown (KeyCode.Alpha2))
    29.         {
    30.             Debug.Log("2");
    31.             SetRoadom(2);
    32.         }
    33.         if (Input.GetKeyDown (KeyCode.Alpha3))
    34.         {
    35. Debug.Log(“3”);
    36. SetRoadom(3;
    37.         }
    38. if (Input.GetKeyDown (KeyCode.Alpha4))
    39.         {
    40. Debug.Log(“4”);
    41. SetRoadom(4;
    42.         }
    43. if(Input.GetKeyDown (KeyCode.Alpha5))
    44.         {
    45. Debug.Log(“5”);
    46. SetRoadom(5;
    47.         }
    48.  
    49.     }
    50.  
    51. private void SetRoadom(int t)
    52.     {
    53.      
    54. TransitionMaterial.EnableKeyword(Key[t]);
    55. TransitionMaterial.SetFloat(Value[t],0.5f);
    56. }[/代码]
    57. Code to switch He often fails to switch the correct macro. I've tried many times but can't, get the switch right. So I modified the macro definition in the shader:
    58. #pragma shader_feature _TRANSITIONS_ERASE
    59. #pragma shader_feature _TRANSITIONS_ROUND
    60. #pragma shader_feature _TRANSITIONS_CHECKERBOARD
    61. #pragma shader_feature _TRANSITIONS_MICROARC
    62. #pragma shader_feature _TRANSITIONS_DRAWERS
    63. #pragma shader_feature _TRANSITIONS_TURBULENT
    64. In this way, I was able to successfully perform a key world switch for the shader using the C# code above. As long as I close the current keyworld before setting up a new one, because declaring the keyworld in this way causes them to exist at the same time. BUT FREQUENTLY CLOSING AND OPENING KEYWORLD WILL CAUSE PERFORMANCE ISSUES,#pragma shader_feature _TRANSITIONS_ERASE _TRANSITIONS_ROUND _TRANSITIONS_CHECKERBOARD _TRANSITIONS_MICROARC _TRANSITIONS_DRAWERS _TRANSITIONS_ TURBULENT
    I also tried changing the shader_feature to multi_compile but I'm getting the same result, I'm doing all of this in the Unity editor, I'm hoping for your help
     
    Last edited: Apr 11, 2024
  2. wechat_os_Qy01Clc1a-n6lbMBoILatSp6w

    wechat_os_Qy01Clc1a-n6lbMBoILatSp6w

    Joined:
    Oct 6, 2022
    Posts:
    25
    Here's the code for some of my fragment shading
    Code (CSharp):
    1.  half4 color = half4(0,0,0,1);
    2.                  #ifdef _TRANSITIONS_ROUND
    3.                     half w = 0.01;
    4.                     _Round = Unity_Remap_half(_Round,half2(0,1),half2(0,2));
    5.                     half alpha = clamp((1.0 / w * sqrt(pow(IN.uv.x - 0.5, 2.0) + pow((IN.uv.y - 0.5) * _ScreenParams.y/ _ScreenParams.x, 2.0))) + 1.0 + (2-_Round)* (-0.5 * sqrt(2.0) / w - 1.0), 0.0, 1.0);
    6.                     color.a = alpha;
    7.                 #endif
    8.                 #ifdef _TRANSITIONS_ERASE
    9.                     float w = 0.01;
    10.                     float alpha = clamp(-1 / w * IN.uv.x + (1 + w) / w + (1-_Erase) * (-(1 + w) / w), 0, 1);
    11.                     color.a = alpha;
    12.                 #endif
    13.                 #ifdef _TRANSITIONS_CHECKERBOARD
    14.                     half2 gridNum = half2(6,9);
    15.                     float2 axisPos = floor(IN.uv * gridNum) / gridNum + 0.5 / gridNum;
    16.                     float rotateTheta = clamp(_Checkerboard * (10.0 * random(axisPos) + gridNum.x * 8.0 * pow((1.0 - axisPos.x), 5.0)), 0.0, 1.0) * PI;
    17.  
    18.                     float2 texCoordAfterTransform = transform(IN.uv, rotateTheta, axisPos, gridNum);
    19.                     //half4 MainColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex,texCoordAfterTransform);
    20.                     texCoordAfterTransform.x = floor(texCoordAfterTransform.x * gridNum.x + 1.0) / gridNum.x - texCoordAfterTransform.x + floor(texCoordAfterTransform.x * gridNum.x) / gridNum.x;
    21.                     //half4 TagetColor = SAMPLE_TEXTURE2D(_TargetTex, sampler_TargetTex,texCoordAfterTransform);
    22.                     half rotateThetaNor = (rotateTheta - 0.5 * PI) * 2.0 / PI;
    23.                     color.a = (0.5 + rotateThetaNor * 0.5);
    24.                    
    25.                 #endif
     
  3. wechat_os_Qy01Clc1a-n6lbMBoILatSp6w

    wechat_os_Qy01Clc1a-n6lbMBoILatSp6w

    Joined:
    Oct 6, 2022
    Posts:
    25
    English is not my first language, this is obtained through translation software, and I am sorry if it makes it inconvenient for you to check the problem.
     
  4. tw00275

    tw00275

    Joined:
    Oct 19, 2018
    Posts:
    97
    https://stackoverflow.com/questions/41113458/different-between-multi-pragma-vs-shader-feature

    "pragma shader_feature is very similar to #pragma multi_compile, the only difference is that unused variants of shader_feature shaders will not be included into game build."

    I believe #pragma multi_compile would be better in this case. #pragma shader_feature doesn't allow you to change the shader while the game is playing.


    In your function SetRoadom(), you may also need to disable the other unused keywords. Only one should be enabled. You can use this before TransitionMaterial.EnableKeyword(Key[t]);

    Code (CSharp):
    1. for (int i = 0; i < Key.length; i++)
    2. {
    3.   if (i != t)
    4.   {
    5.     TransitionMaterial.DisableKeyword(Key[i]);
    6.   }
    7. }