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

WHERE is the exposed Gradient node property in Shadergraph

Discussion in 'Editor & General Support' started by NerdIt-, Feb 6, 2020.

  1. NerdIt-

    NerdIt-

    Joined:
    Oct 19, 2018
    Posts:
    24
    Sorry if this isn't the right place to ask or whatever, but it seemed appropriate to me.

    Why can't you expose the Gradient property in the Shader Graph? I am using one of the latest versions of Unity, 2020.1.0a9 and still, there is no "expose" option for the Gradient property. What is even the point of being able to convert it to the property if I can't even change it in the editor? I understand I can change the value of it in a script using its ID, or whatever that string of numbers and letters are, but that changes the property for all the materials that use that shader, doesn't it? I really think the devs should have completely finished the Shader Graph before releasing it because we are missing quite a few major features. There aren't any tesselation options (from what I could find on it at least) and half the time, the scene depth and other camera-related nodes miss-function or just don't work at all. And let's not forget that a lot of the node arrangments that work in LWRP/URP don't work in HDRP or vice versa. Don't get me wrong, I think the Shader graph is a great idea, but it has been implemented half done and it is starting to get annoying. I wouldn't mind if the unity dev team didn't continue to try and push out new features and tools and just fixed up the ones that are dearly in need of repair.

    Anyway, I've gotten a little off-topic. Can someone please tell me if there is any way to expose the Gradient property or at least warn me once the devs implement it.
     
  2. cuzner

    cuzner

    Joined:
    Oct 1, 2015
    Posts:
    11
    I feel your pain.
    We need the Gradient to be exposable, otherwise it is not really much use for a Game Developer.

    Keep up the good work Unity team.
     
    LooperVFX and rthom like this.
  3. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    Properes that are exposed do have a matching datatype in the shader language.
    Gradients is not a datatype in the shader language.
    Unity would have to generate a large number of vector properties to represent the gradient.
     
  4. NerdIt-

    NerdIt-

    Joined:
    Oct 19, 2018
    Posts:
    24
    ok. thx for the answer
     
    Chmyke likes this.
  5. C4ro

    C4ro

    Joined:
    Oct 5, 2014
    Posts:
    9
    Then why is it so easily accesible in a Particle Effect?

    And is it even a large number of vectors? Isn't it just a lerp between two Vector4s?

    Also - why are you then even able to convert it to a Property?
     
    LooperVFX likes this.
  6. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    Do you mean the particle system? That is running on the CPU side and just updating the color data via vertext colors per particle per frame... so its never trainsfering the whol gradient to the shader.
    its more then just two Vector4s you can look in the compiled shader and see what unity is generating so its has a representation of the graindient in the shader.

    im not saying its impossible to do it but unity decided against it for the moment.
     
    Nintendo-Silence and rockear like this.
  7. namynnac

    namynnac

    Joined:
    Jun 4, 2020
    Posts:
    9
    How create gradient witout gradien in shader graph ?)
    Very needed
     
    Last edited: Jun 15, 2020
  8. KYL3R

    KYL3R

    Joined:
    Nov 16, 2012
    Posts:
    139
    It' stupid that you cannot expose Gradients...

    However, a simple 2-Color-Gradient can be achieved with Lerp, as @C4ro already said.
    Then simply expose the 2 Colors:

     
  9. ownezx

    ownezx

    Joined:
    Mar 29, 2018
    Posts:
    10
    Okay, I got fed up with this and went with the simplest solution I could think of without going too deep in the source code of Unity's graphics.
    I created a custom shader that takes in colours and positions (much like a gradient definition but with more objects). Then created a custom subgraph using that shader. It is basically the same thing as SampleGradient, but using colours and position directly.

    Limitations, I hardcoded it to be for 7 colours. If you need more or less, just change the code and custom node.

    Hope that helps.
    Elliot

    The shader code is the following goes in an hlsl file :
    Code (csharp):
    1.  
    2. // Shader to replace the SampleGradient node
    3.  
    4. void SampleGradient_float(float inputValue,
    5.                    float4 color1,
    6.                    float4 color2,
    7.                    float4 color3,
    8.                    float4 color4,
    9.                    float4 color5,
    10.                    float4 color6,
    11.                    float4 color7,
    12.                    float location1,
    13.                    float location2,
    14.                    float location3,
    15.                    float location4,
    16.                    float location5,
    17.                    float location6,
    18.                    float location7,
    19.                    out float4 outFloat)
    20. {
    21.     if(inputValue<location1)
    22.     {
    23.         outFloat = color1;
    24.     }
    25.     else if(inputValue<location2)
    26.     {
    27.         float pos = (inputValue-location1)/(location2-location1);
    28.         outFloat = lerp(color1, color2, pos);
    29.     }
    30.     else if(inputValue<location3)
    31.     {
    32.         float pos = (inputValue-location2)/(location3-location2);
    33.         outFloat = lerp(color2, color3, pos);
    34.     }
    35.     else if(inputValue<location4)
    36.     {
    37.         float pos = (inputValue-location3)/(location4-location3);
    38.         outFloat = lerp(color3, color4, pos);
    39.     }
    40.     else if(inputValue<location5)
    41.     {
    42.         float pos = (inputValue-location4)/(location5-location4);
    43.         outFloat = lerp(color4, color5, pos);
    44.     }
    45.     else if(inputValue<location6)
    46.     {
    47.         float pos = (inputValue-location5)/(location6-location5);
    48.         outFloat = lerp(color5, color6, pos);
    49.     }
    50.     else if(inputValue<location7)
    51.     {
    52.         float pos = (inputValue-location6)/(location7-location6);
    53.         outFloat = lerp(color6, color7, pos);
    54.     }
    55.     else
    56.     {
    57.         outFloat = color7;
    58.     }
    59. }
    60.  
    The node itself looks like this : Screenshot 2020-11-05 at 00.34.11.png

    Here it is in action for a procedural planet where I can change the colours of the different height :
    Screenshot 2020-11-05 at 00.57.09.png Screenshot 2020-11-05 at 00.57.14.png
     
    Last edited: Nov 5, 2020
  10. Dan_G

    Dan_G

    Joined:
    Dec 4, 2017
    Posts:
    31
    Noblauch and hawaiian_lasagne like this.
  11. qoobit

    qoobit

    Joined:
    Aug 19, 2014
    Posts:
    51
    This was made before they crippled custom nodes by locking you into a node that has to conform to their new custom node standard. Keijiro made one as well but it's using all deprecated custom node shader graph code now.
     
    VirtualPierogi likes this.
  12. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,184
    Not only gradient. There are some other property that cannot be exposed. Like Matrix. I don't know why unity need to make our life harder here
     
    VirtualPierogi likes this.
  13. Fressbrett

    Fressbrett

    Joined:
    Apr 11, 2018
    Posts:
    101
  14. qoobit

    qoobit

    Joined:
    Aug 19, 2014
    Posts:
    51
    Keep in mind there is no exact representation of a gradient node value in shader code. It is technically possible to make a translation from a UI to shader code with limited number of control points but it's another layer that has not been implemented by Unity at the moment.
     
    owlrazum and LooperVFX like this.
  15. VirtualPierogi

    VirtualPierogi

    Joined:
    Sep 3, 2012
    Posts:
    54
    Seriously the lack of gradient interface defeats the purpose of having this in shader code.
    You can always use something like 16x16 texture and use UV coords to get the colors for multistage gradient.

    Nice avatar
     
    LooperVFX likes this.
  16. LooperVFX

    LooperVFX

    Joined:
    Dec 3, 2018
    Posts:
    181
    as @qoobit mentioned, it's 100% possible, Shader Graph just does not yet have support for this. Visual Effect Graph (like Shader Graph, also essentially generates HLSL shader code and some C#) already has Gradient as an exposable type and many other types that shader graph does not support as exposable. https://docs.unity3d.com/Packages/c...raph@12.0/manual/Operator-InlineGradient.html

    Visual Effect Graph even allows for Custom Property Binders providing an API to extend and map any C# type or struct to the Visual Effect Graph compatible types: https://docs.unity3d.com/Packages/com.unity.visualeffectgraph@12.0/manual/PropertyBinders.html

    This divergence is because these graph tools were largely built in isolation from eachother to get them off the ground quickly with vastly different feature scopes, so they have a lot of puzzling differences. Shader Graph's development was broader and slower: It had many more targets / platforms to support, resulting in longer testing and development iteration, so it's behind on a lot of nice "quality of life" features that the Visual Effect Graph team was able to develop much faster as they only needed to target HDRP and compute capable platforms during initial development. But these features are not easily transferrable to Shader Graph as they don't share much in terms of common components and APIs. The framework to unify them to share more common ground is called Graph Tools Foundation and is currently under development. Some features may arrive sooner, but I imagine some may not come until after the Shader Graph front end is ported to use Graph Tools Foundation:
    https://forum.unity.com/threads/visual-scripting-roadmap-update-september-2020.978732/
    https://docs.unity3d.com/Packages/com.unity.graphtools.foundation@latest/
     
    Last edited: Jul 11, 2021
  17. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,184
    2022.1.0a12
    • Serialization: Added: Added SerializedProperty.gradientValue to the public API
    Not sure if it added in UI yet
     
  18. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    Honestly, this is just unacceptable on Unity's end. If you can make it a Material Property in Shader Graph, the resulting Materials SHOULD expose the property in the Inspector. They've had perfectly fine inspector drawers for Gradients for years, I fail to see how it could be so hard to use this in Material Drawers.

    It is S*** like this that makes me consider switching engines time and time again.
    The only thing keeping me from doing that is knowing that the grass is always greener until you get there.
     
  19. maurosso

    maurosso

    Joined:
    Feb 19, 2017
    Posts:
    50
  20. rothpletz5

    rothpletz5

    Joined:
    Dec 2, 2020
    Posts:
    27

    Thanks so much! After a couple hours of head scratching, I was able to implement this, and, with a few modifications, got it to fit my purpose perfectly!
     
  21. Kirsche

    Kirsche

    Joined:
    Apr 14, 2015
    Posts:
    121
  22. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,184
    LooperVFX likes this.
  23. mitaywalle

    mitaywalle

    Joined:
    Jul 1, 2013
    Posts:
    262
    Hello everyone. Faced same problem, and here is my solution:
    - Unity Gradient Texture generator | Fully free on Github
    If you like it - buy me a candy on Pateron
    Inspector_preview.png
    More info:
    - tested with Unity 2018-2022
    - UPM package
    - Create new gradient with ProjectWindow/RMB/Create/Texture/Gradient
    - Texture itself appear as GradientTexture-subasset
    - realtime editing
    - RGBA | HDR Gradients
    - Blend 'horizontalTop' and 'horizontalBottom' Gradients with 'verticalLerp' Curve.
    - 'verticalLerp' is blend-t-value
    - choose any resolution you want
    UPD:
    - export to PNG
     
    Last edited: May 6, 2022
  24. Waarten

    Waarten

    Joined:
    Aug 24, 2018
    Posts:
    38
    Listen up:
    * There is this property 'gradient' that you can use within ShaderGraph. This is a handy little tool they've made to make life easier for you
    * But it's actually a workaround bit of code, because natively, behind the scenes, shaders just don't work that way.
    * I agree that the presentation is confusing, because it is called a 'property', and you can 'expose' other properties.
    * When you keep asking for this feature, realize that there is no 'general', performant way to do this. Some people are satisfied with a two point gradient, others are okay with their gradient 'locations' being spaced evenly. Others are okay with a static number of control points, yet other would want the 'full package'. All these cases require different solutions behind the scenes.
    * Asking for this is like asking for a car that you could change the number of wheels of whenever you want: a 3 wheel car would simply need a totally different chassis than a 4, 8 or 2 wheeled car.

    Of course it can be done, I'm just pointing out that it's perfectly understandable that this is not supported 'by design'.
     
  25. mitaywalle

    mitaywalle

    Joined:
    Jul 1, 2013
    Posts:
    262
    Totaly agreed
     
  26. shibi2017

    shibi2017

    Joined:
    Jan 18, 2018
    Posts:
    153
    In some cases it does but in some cases it doesn't, for exampe if you got a crack texture and want to color it usign gradient but now using lerp:
    upload_2022-4-27_16-52-56.png
    this is wrong and you should using:
    upload_2022-4-27_16-53-24.png
     
  27. futer24

    futer24

    Joined:
    Oct 27, 2013
    Posts:
    13

    That works!

    But if you want a gradient in fixed mode instead of blend the colors?
     
    mitaywalle likes this.
  28. Dustyroom

    Dustyroom

    Joined:
    Sep 23, 2015
    Posts:
    108
    Shameless plug:
    Chroma on the asset store is a custom shader UI that exposes gradients as if they are part of Shader Graph.
     
    reveriejake and Thaina like this.
  29. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    743
    Well that's solidified unity never adding it now since they can just make money from your asset sales.
     
  30. maurosso

    maurosso

    Joined:
    Feb 19, 2017
    Posts:
    50
    I've learned that there is more to it than unity just being stubborn. Gradients are not exactly something that HLSL has. So there are some hacky ways to get it to work, but it will never be a 'clean' solution, unless Microsoft does something about it. And I have no faith that Unity has any power to pressure Microsoft to give us such functionality.

    So the: By design ( no mater whether we like it or not) is actually true.

    For example ShaderGraph Markdown is excellent and a worthwhile buy and it also supplies a 'gradient' drawer. I'll look into Chroma but I'd expect they do something similar as the gradient nodes are even named as texture samplers.

    https://assetstore.unity.com/packages/tools/gui/shader-graph-markdown-194781
     
    Last edited: Jun 6, 2023
  31. maurosso

    maurosso

    Joined:
    Feb 19, 2017
    Posts:
    50
    Hey Dustyroom, could I ask how is the gradient and curve evaluation passed into the hlsl code? Is there a texture that is being created during material creation or runtime that is sampled, a key/lerp plug or some other way?
    I'm asking, as I have use cases where additional texture samplers are not something I can afford.

    EDIT: Jup:) It does create a texture asset and is sampling is using a sampler. A neat solution, a very usable, but still sort of a 'hack' that you need to be aware of (that every gradient or curve you use is a texture sampling instruction). And unless Microsoft does something about it the core of HLSL and DirectX, it will stay that way, and there is no hope for Unity to change this, as it is not something they would be able to change.

    Chroma looks neat, I might try it out and compare it to their competitor: Markdown.

    Code (CSharp):
    1.  
    2. From Chroma Support site: https://chroma.dustyroom.com/#how-chroma-works
    3. Under the hood, Chroma uses custom Material Property Drawers to draw the gradients and
    4.  curves in the inspector, along with a custom context menu, where you can generate nice
    5.  random gradients, invert them, and so on. Our custom drawer quietly bakes the gradients and
    6.  curves into textures, and passes them to the shader as textures. The shader then reads the
    7.  textures and uses them as it sees fit. For things like headers and fodouts, we use a Custom
    8.  Shader GUI to draw them in the inspector.
     
    Last edited: Jun 6, 2023
  32. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    743
    It may not be built into hlsl but they can't be bothered to make the editor show it in the inspector when amplify shader does just fine. And asset makers have had to do it instead.
     
  33. maurosso

    maurosso

    Joined:
    Feb 19, 2017
    Posts:
    50
    Doing it the way the plugin authors do it is misleading and 'dangerous' combability wise. If they did it, there would be exactly the same type of topic elsewhere on the forum yeling: WHY THE SHADERGRAPH GRADIENT DRAWER NOT WORK ON MOBILE?! STUPYD UNITY! or some other bullshit.

    It is a feature that is not native to graphics programming. Deal with it. I have to deal with, and hate it as much as you. Still...not Unity fault.

    What the really should be doing, is allowing for disable of auto refresh of shaders. It drives me crazy when I have a semi-complicated graph, and even adding a variable to the blackboard or changing it's name takes like 30seconds. It defeats the point of SG...and that is miserable as it's been a problem for years...
     
  34. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    743
    If a regular user can make an asset to do it for shader graph so can unity, stop talking nonsense. Unity just could not be bothered to do it.

    If they can make a gradient element in the shader graph itself they can also add one to the inspector. It's the same code just referenced and exposed in the inspector.
     
  35. maurosso

    maurosso

    Joined:
    Feb 19, 2017
    Posts:
    50
    No, it is not. Stop talking nonsense.

    I have provided you the 'solution' a 'regular user' uses to get the inspector gradient drawer. It's a texture sampler. I believe it's not something that should be added without user knowledge, as that would interfere with the universality of the shaders shadergraph generates in general. And if it would be added by unity built-in, it would generate as much confusion as the whole "why is this not implemented".

    If you want more 'proof', I'll point you to the Unity documentation describing the way the Gradient and Sample Gradient nodes work in Shadergraph.

    https://docs.unity3d.com/Packages/com.unity.shadergraph@6.9/manual/Gradient-Node.html
    https://docs.unity3d.com/Packages/com.unity.shadergraph@6.9/manual/Sample-Gradient-Node.html

    There is no such thing as a 'gradient' in HLSL. You would need to expose a fixed number of colors and lerp between them, which would be just as clubersome as the current solutions.

    There are more pressing issues with Shadergraph that this...
     
    Last edited: Jun 10, 2023
  36. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    743
    I know theres no such thing as a gradient ..... said this already. Shadergraph ALREADY shows gradients with the sample gradient node, its an inspector element, just expose the same thing to the inspector on the material.
     
    LooperVFX and bdb400 like this.
  37. electorchstrauss

    electorchstrauss

    Joined:
    Mar 13, 2021
    Posts:
    11
    Hi if anyone wants fixed gradient you can just modify this code like that

    Code (csharp):
    1.  
    2. void SampleGradient1_float(float inputValue,
    3. float4 color1,
    4. float4 color2,
    5. float4 color3,
    6. float4 color4,
    7. float4 color5,
    8. float4 color6,
    9. float4 color7,
    10. float location1,
    11. float location2,
    12. float location3,
    13. float location4,
    14. float location5,
    15. float location6,
    16. out float4 outFloat)
    17. {
    18.     if (inputValue <= location1)
    19.     {
    20.         outFloat = color1;
    21.     }
    22.     else if (inputValue <= location2 && inputValue > location1)
    23.     {
    24.         outFloat = color2;
    25.     }
    26.     else if (inputValue <= location3 && inputValue > location2)
    27.     {
    28.         outFloat = color3;
    29.     }
    30.     else if (inputValue <= location4 && inputValue > location3)
    31.     {
    32.         outFloat = color4;
    33.     }
    34.     else if (inputValue <= location5 && inputValue > location4)
    35.     {
    36.         outFloat = color5;
    37.     }
    38.     else if (inputValue <= location6 && inputValue > location5)
    39.     {
    40.         outFloat = color6;
    41.     }
    42.     else
    43.     {
    44.         outFloat = color7;
    45.     }
    46. }
    47.  

     
    LooperVFX likes this.
  38. ravipatel0

    ravipatel0

    Joined:
    May 22, 2023
    Posts:
    1







    Here is the full Shader graph for those who want gradient effect using the depth texture .

    I used simple grayscale algorithm to get value of locations so that i can show colors to that location

    View attachment 1408674

    thanks.
     

    Attached Files: