Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Any way to add same vertex modification to all shaders?

Discussion in 'Shaders' started by lee3072, Jul 26, 2022.

  1. lee3072

    lee3072

    Joined:
    Jun 11, 2021
    Posts:
    5
    Hello, I am new the unity and working on a vertex shader these days.
    I am searching for a way to add an equal modification to all the existing shaders instead of modifying all existing shaders and materials.
    Is there any fundamental way to access the graphics processing pipeline of unity for it?
     
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,496
    Accessing the graphics pipeline itself isn't the important thing here. How shaders work makes this not really a possible thing, no matter the engine.

    Now, you could modify the include files for Surface shaders to have some code added to the vertex functions it uses when generating code... But any custom shaders in your project that aren't surface shaders, or surface shaders that already override the vertex program aren't going to see this change either.

    Shaders run as isolated kernels, data goes in, results come out, there is no modifying all those different kernels globally.

    What is it exactly you are trying to achieve as a result? What is the overall purpose/goal for your game/program? As people may be able to suggest a better route.

    For example, if you're compiling for platforms that support compute shaders, what you could do is have a component on meshes that updates their vertices from a compute shader, and then these changes will be seen by any shader that happens to be rendering that mesh, and those changes will persist across frames, so you don't need to update it each frame if you don't have to. And example project is here: https://github.com/keijiro/NoiseBall6

    But this could get messy if you have thousands of objects running compute shaders to do this each frame. In which case it'd be better to just have custom shaders for everything to do what you need to do, if no alternative solution can be suggested.
     
    lee3072 likes this.
  3. lee3072

    lee3072

    Joined:
    Jun 11, 2021
    Posts:
    5
    Thank you for letting me know how the shader works.
     
  4. lee3072

    lee3072

    Joined:
    Jun 11, 2021
    Posts:
    5
    What I want to do is shifting vertex of all objects and terrian based on the player position to create some visual effects.
    Such as translating all vertexes on the left side of the user towards more left to create a gap.
    When I tried this, I faced another issue that the object orientation is applyed after the shader computation.
    Resulting in vertex shifting towards diagnal direction with the object rotation value.

    If anybody knows how to fix this second issue, it will be very helpful for me.
     
  5. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,496
    For the direction issue, you need to transform that world direction to the object's local-space.

    float3 localDir = mul(unity_WorldToObject, float4(worldDir, 0)).xyz;

    Then add it to the vertex.xyz how you want, at the start of the vertex program. (You want it to happen before clip-space conversion)

    As for the shader issue here... Well what I suggested with Compute Shaders could work for this. You'll have to store the object's original positions in one of their UV channels or color channel though so the shape can be reverted as you move away... This will result in extra memory usage for meshes. (though I could see a computer buffer being used with offsets for each mesh you're in contact with to store its mesh data in when you're in range, and then remove when away, but this would require decent programming knowledge).

    So ultimately it's a choice between increased memory usage but less overall potential work that doesn't care about the shaders you're using, or use custom shaders on everything you want to be able to deform.