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.

Question Changing instance shader parameters with GPU instancing (Graphics.RenderMeshInstanced)

Discussion in 'Shaders' started by andyjonesss, Oct 31, 2022.

  1. andyjonesss

    andyjonesss

    Joined:
    Oct 7, 2021
    Posts:
    1
    EDIT: TURNS OUT MAT PROPERTY BLOCKS ARE A THING, MANAGED TO GET IT WOKRING!

    So my ultimate goal is to have a LOT of grass, that sways in the wind, and is forced out of the way as the player character walks through it.

    I'm a bit of a beginner with shaders and GPU instancing, but I've managed to get to a point where 50k grass blades are being rendered fairly efficiently using the Graphics.RenderMeshInstanced function. I had previously used static batching to make it more performant, however when I ran the code that updates the grass blade shader as a result of the player character 'forcing' the blade out of the way -> (listOfGrassMaterials.SetFloat("_playerVelocityX", playerXVelocity)) , the batching was broken for these particular blades. This led to a rapid reduction in performance the more the player character would walk over the grass.

    The issue I'm having now is being able to update each specific blade of grass with its sway amount and sway direction as influenced by the player character. Within Graphics.RenderMeshInstanced I've noticed you can feed the RenderParams for the entire batch, but it seems like there is no way to set different RenderParams for individual instances of the grass blades, is this correct? If so, is there a different function I should be using instead?

    I've attached a snip of the vertex element of the shader graph below. The 'wind sway' is equal across all of the grass blade instances (though I will look to change this to be on an individual basis at some point, to make it more dynamic, if possible). The 'player influenced sway' has variables fed into it from a separate script when the player is within a certain range, this is the part of the shader that needs to update on a per-instance basis. If this is not possible with shader graph and the RenderMeshInstanced function, please do let me know.

    Any help is much appreciated.
    Thanks in advance.

    I'm on v2021.3.4 by the way.


    50kGrasssHADER.png




    50kGrass.png
     

    Attached Files:

    Last edited: Nov 1, 2022