Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question The dreaded procedural grass

Discussion in 'Shaders' started by ATMLVE, Sep 23, 2023.

  1. ATMLVE

    ATMLVE

    Joined:
    Jun 11, 2023
    Posts:
    66
    Rendering grass. What a pain. What an endless rabbit hole. Send help.

    TL;DR my rant in case it makes you more likely to help: Read the use case paragraph, then the final paragraph.

    I've been researching various grass shader methods in unity off and on for days. I've spent hours tonight watching various videos and tried out some projects that didn't work quite right. The tutorials just didn't insight me with an obvious course of action, partly because geometry and compute shaders are difficult to grasp as a beginner but also, I realized, because no one is reaching my use case.

    Use case: (Important info is I'm working in URP). In my project I'm generating planets made of procedurally generated meshes. It's the technique used by Sebastian Lague in his first planet series found on youtube, which if you don't know is fine, the only important part is what I have to work with: a big singular mesh (well six, its a unit sphere) of vertices and triangles that were calculated in a c# script. I want to render grass on only some parts of this mesh, particularly on the parts that are locally "up" and not the slopes, but even then only in some places, not everywhere. And of course I need it to only render when I'm in range. As new planets are generated during gameplay, so too are new places for grass to appear.

    One problem for me with the many grass shaders on youtube is that they involve pre-determined positions and ground meshes. I tried out a grass project that I found cannot instantiate grass at runtime, for whatever reason like baked lighting or some other technical limitation. Not an option for me.

    Another issue for me is the tutorials only go into the technical details of how to make the shader work. They leave applying it up to the user, they don't show how to get an LOD system to work or culling or anything (the horror of having to actually solve your own problems!?). Here though I run into another roadblock because I don't even know fundamentally if their shader can even work in my use case. I would need to spend hours or days learning a process that may be totally incompatible.

    So here after hours of research and still finding myself nowhere closer than when I started, I come seeking advice. Someone more knowledgeable in geometry and compute shaders and other methods, what is the wisest course of action for my use case? How can I render lots of grass in positions that may not be known for a long time? In my planet generation script I could potentially create an array of vertices to put grass, but what method do I use to make them only appear when the camera is near, and can I even determine those positions at runtime?
     
  2. ATMLVE

    ATMLVE

    Joined:
    Jun 11, 2023
    Posts:
    66
    Update: I think I've figured out something for me, it's the procedural grass from this video:



    It was extremely easy to set up. It's three files - a c# script that runs everything, the compute shader, and the grass shader itself. You can put any mesh in and it plops that wherever you tell it from a single mesh or array of vertices. A great stepping stone to build off of.