Search Unity

Question Vector4 Data In per mesh

Discussion in 'Shader Graph' started by Sibz9000, May 24, 2019.

  1. Sibz9000

    Sibz9000

    Joined:
    Feb 24, 2018
    Posts:
    149
    Hi all,

    Shaders are completely new to me and using shader graph I have jumped right in and created some awesome stuff.
    One thing I haven't figured out, and my google fu is failing me on, is how to pass through data to my shader from the object. I have read that it can be encoded to UVs or vertex color.

    My current use case only needs a vector 4. I am creating my mesh using code, so I need to know how to put the data in there, and then how to use it from shader graph.

    Bonus question, could I easily pass in another vector 4 in future if needed?

    Thanks in advance for your help!
     
  2. Sibz9000

    Sibz9000

    Joined:
    Feb 24, 2018
    Posts:
    149
    Seem I can easily get the information in by setting the UV on a second UV layer, so first UV has x,y second has z, w and set the rest to 0.
    But how can I access the first 2 UV values?
     
  3. Sibz9000

    Sibz9000

    Joined:
    Feb 24, 2018
    Posts:
    149
    So with some more google fu I found:
    https://shaderforge.userecho.com/communities/1/topics/685-get-a-specific-uv-coordinate-entry
    which basically states that it's not possible.
    For the UV channel I would have to encode my information into one vector2, and set that for all UV's for the mesh.
    Then in the shader I can decode the UV.
    For the vertex color channel, the same goes, except it's a vector4.
    As I am using the data I need for modifying the UV, I'll encode into the UV. It seems a bit of a waste filling a buffer with the same values, but this does mean I can substantially reduce the number of vertices for my meshes.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    If it’s a consistent float4 value across the entire mesh, is there a reason why you don’t use a shader property?
     
  5. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,444
    Shader properties are shared across all renderers using that shader.

    You should look at "material shader blocks" to learn how you can override properties for individual renderers. It's kind of a clunky mechanism but it lets you tweak specific renderers without affecting all of them. Plays hell on your draw calls if you abuse it though.
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Using that material, yes. Shader, no.

    You can also use multiple materials with the property changed, but yes, material property blocks are a more efficient way of handling overriding values. Functionally on the GPU side the results are identical most of the time. However it also allows for instancing if you’re drawing many copies of the same mesh with different material properties, if you’ve setup your shader to use instanced properties... which Shader Graph doesn’t support.

    If you’re worried about batching, then storing data on the mesh’s vertices is a fine way to go about this. However SRP Batching cares a lot less about either of these and can batch both different meshes and different material properties on the same shader more efficiently.
     
  7. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,444
    Thanks for the correction.