Search Unity

  1. Get the latest news, tutorials and offers directly to your inbox with our newsletters. Sign up now.
    Dismiss Notice

Help Wanted Performance of shader that reacts to player position

Discussion in 'Shader Graph' started by Thorce, Mar 10, 2021.

  1. Thorce

    Thorce

    Joined:
    Jul 3, 2019
    Posts:
    4
    Hey!
    I'm kinda dabbling in player reactive VFX and shaders. Currently I let most of my shaders react to the players position. I do this by using Material.SetVector() every update to inform the shader about the players position.
    I'm kinda wondering though, how much of an impact this has on performance since sending stuff to the graphics card is known to be quite costly.
    I know it's not that bad for 1 objectmaterial, but what if i have hundrets of objects with shaders that react to the player position? I would have to call Material.SetVector() hundrets of time every single update...
    Is this reasonable? Can I achieve my desired effect in a better way?

    Any help is appreciated!
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    10,220
    This is probably the wrong sub forum for this question. The General Graphics or maybe even the Shader sub forum are probably better places to ask this question as it's not specific to Shader Graph.

    Sending stuff to the GPU is quite fast. Just to move an object around the CPU is sending the GPU at minimum 4 times as much information in the form of that object's transform matrix, and likely way more than that. The amount of information the CPU sends to the GPU every frame is way more than people think about. The entire design around how discrete GPUs work is to allow lots and lots of data to be passed from the CPU to the GPU as quickly as possible.

    What's expensive is getting data back from the GPU. But that's not an issue here.

    As for your specific question, the best solution if you need many things to react to a shared value is to to use shader globals.
    https://docs.unity3d.com/ScriptReference/Shader.SetGlobalVector.html

    In your shader, do not add this variable to the Properties (or if you are using Shader Graph, set the property to not be exposed). Then you can assign the vector from script once and all materials that need it will get it.
     
  3. Thorce

    Thorce

    Joined:
    Jul 3, 2019
    Posts:
    4
    Thank you! You answered everything I needed to know!
    And thanks for telling me where I can/should post stuff like this. I don't post often and.
     
unityunity