Search Unity

Question Gradients "exposed" property is ignored?

Discussion in 'Shader Graph' started by diesoftgames, Feb 29, 2020.

  1. diesoftgames

    diesoftgames

    Joined:
    Nov 27, 2018
    Posts:
    122
    The "Exposed" property is always disabled. If I create a gradient node and then turn it into a property, the exposed property is checked, vs when creating directly from property menu it is unchecked, but either way will be grayed out/disabled. It is not available to set in the material either way. Is this by design or a bug? (I'm on the macOS client)
     
  2. Korindian

    Korindian

    Joined:
    Jun 25, 2013
    Posts:
    584
    You can't edit gradients in a material inspector. This is a good thread on some alternatives.

    Basically, converting a script's gradient property into a texture ramp at edit time/runtime, or lerping in the shader between some specific colors which you CAN set as exposed properties.
     
    diesoftgames likes this.
  3. diesoftgames

    diesoftgames

    Joined:
    Nov 27, 2018
    Posts:
    122
    Thank you! Getting a handle on this now and the thing I'm stuck on atm is that if I have a Texture2D gradient in shader graph, how can I sample that as if it were a gradient? (What I'm after is coloring a grayscale texture with a variable gradient)

    EDIT: Oh I found what I was after. The Distance node (which plugs into the SampleTexture UV) allows me to specify an x position as a number between 0 and 1 so I can plug my grayscale channel into the x position (using a horizontal gradient).
     
    Last edited: Feb 29, 2020
  4. Korindian

    Korindian

    Joined:
    Jun 25, 2013
    Posts:
    584
    Another option that I found somewhere I can't remember which works well for me:

    If using the gradient as a texture and assuming a horizontal gradient texture of a large width vs small height - (let's say 256x1) which matches the script's gradient property, you can sample it using the grayscale texture as the x value of the UV coordinates (and 0 for y) you input into the Gradient map's Sample Texture 2D node. You can do additional things like multiply the grayscale value before using it as the input of the UV coordinate if you want to control the gradient bias. The attached pic of the shader graph assumes you're using a single channel (R) for the grayscale texture, but you could get that single grayscale float value from however you like.

    Keep the GradientBias at 1 to see the gradient as intended, bring it close to 0 to see more of the left side of the gradient, and higher values than 1 to see more of the right side of the gradient. Let me know if this works for you.
     

    Attached Files:

  5. diesoftgames

    diesoftgames

    Joined:
    Nov 27, 2018
    Posts:
    122
    Thanks! Yeah looks like the same thing, the Distance node is effectively just plugging in a Vector2 afaict. So something weird that's happening now is: when I use a texture 2D from reference from disk it works as expected (using the design basically as per what you linked), despite the fact that I'm using a flipbook sprite. I had assumed I might need to do something with that, but it just works. HOWEVER, when I generate a texture myself and pass that in using a MaterialPropertyBlock, suddenly the 0 to 1 sample is off, so it gets the incorrect color sampled which is only from a portion of the gradient that corresponds with what it would be if it were only one frame from the flipbook in the _MainTex sprite. I've been trying to dig in and figure out what's different here, but I'm at a loss at the moment if anyone has advice.

    EDIT: Just for clarity, this doesn't appear to be a problem with MaterialPropertyBlock interactions because if I simply use a Texture2D from disk eg:
    block.SetTexture("_GradientTexture", FakeTexture);
    it works just fine and samples the texture correctly. It's only when I attempt to SetPixels and Apply (either when generating a new Texture, or even just if I take that texture from disk, make it read/write enabled, and try writing over it) that this problem occurs. So if I take a working gradient, write over it with what appears to be the same gradient, then using that gradient no longer works.
     
    Last edited: Mar 1, 2020
  6. diesoftgames

    diesoftgames

    Joined:
    Nov 27, 2018
    Posts:
    122
    Follow up: I solved that last problem too. When generating the texture I just needed to set all the WrapModes to Clamp. Ooof, that was a lot of getting into the weeds, but I've finally got a gradient editor for shareable MaterialPropertyBlocks for this shader. :) Thanks for the help, Korindian!
     
    Korindian likes this.
  7. mitaywalle

    mitaywalle

    Joined:
    Jul 1, 2013
    Posts:
    253
    kypo9 likes this.