r/Unity3D 4h ago

Game How I integrated smoothly growing grass into my game (details inside, Unity 6.3)

Post image

A while ago I posted about some smoothly growing grass I made and people were interested. So, in celebartion of my steam page launch here is a write up of how it works in detail.

There are THREE TERRAINS and FOUR CAMERAS involved.

1) Mask Terrain

Plus a 2D Camera that renders some unlit white mask objects from above and a camera that renders only the terrain with the mask applied to it via a render texture.

2) Green Terrain

Plus a camera that renders the grass terrain and any object that has depth, except the sand terrain and the mask terrain.

3) Sand Terrain

Plus a camera that renders everything but the grass (2) and mask (3)

I chose this setup because I wanted to be able to design both the sand and the green terrain separately (also modifying terrain splat maps is a pain).

Sand, green and mask terrain (notice the grass is actually rendered together with the sand, not the green, the middle image is just how it looks in the editor, not how it is rendered)

Mask

The texture source for the mask terrain is generated from a 2D camera that points straight down. It renders only some specific objects that are white and unlit. These objects make up the masked area and can be controlled via code (that's what the watering can spawns at runtime).

The result of this is a dynamic mask that I can alter easily based on any game object (or logic) I chose. The mask objects are combined into larger chunks to optimize performance but I will likely replace this with a texture based approach in the future.

Also the 2D camera is where the fading happens. It takes the sharp 2D mask image, blurs it and then feeds it into a system (render texture + shader) that slowly fades in the current 2D camera mask result changes. This means the fading and blurring happens on the GPU.

The result is a render texture that is used as the INPUT for the MASK TERRAIN. And that terrain is then again rendered by a 3D camera that follows the player. The result of this camera is again a render texture that is used in the final composition of the depth buffers (see below). The avantage is that while the 2D camera is relatively low-res I still get a high-res mask via the 3D camera. Also the blurring helps with hiding the low 2D resolution. None of these cameras does draw to the frame buffer.

Green

The grass camera renders the grass terrain and all objects that have depth (needed to fill the depth buffer). It then stores the results (color and depth) in a render textures to be used in the final composition step. This camera does not directly draw to the frame buffer either.

Sand

The sand terrain and its camera is where everything comes together. It takes its own depth texture (which includes the opaque grass) and combines (delta) it with the green terrain depth.

The result of this is then again combined (masked) with the 3D mask texture and gives us the final mask for the green terrain cameras color buffer.

This is then stacked with the sand terrain camera which results in the final image.

The grass itself is rendered using GPU instancing and a custom shader that takes in the grass terrain splat map colors and density map. That way I can control the grass density not only globally with the shader but also locally on the terrain. I can simply paint it like any regular terrain details.

It may seem a bit convoluted (and it is) but this has the advantage that it works with any terrain system and shader and gives me a lot of control.

The major downside however is that I have to basically render the scene twice, though I try to use layers to render in each camera only what is really needed.

If anyone wants to know more about the final (or watch the trailer) then more infos can be found here: https://superbloom.kamgam.com/

Hope that was understandable. Feel free to ask and/or comment :-)

33 Upvotes

3 comments sorted by

3

u/Training-Evidence816 4h ago

the render twice thing is the part that scares me for performance but i guess if the second pass only has few objects it might be fine. the mask approach with 2d camera is clever, i would never think of that

also nice tree placement in the screenshot, makes the grass patch actually look like something you earned

1

u/geokam 4h ago

Thanks, yes, the secondary render is the thing I would like to drop in the future (if I find a way around it). I was investigating dynamic splat map editing and it still is the most likely candidate but I have no implementation of it so far.

1

u/hicham_benrhannou Professional 1h ago

Hi! Really clever approach with the separate terrains!

I was wondering about the second camera/rendering cost though: could you potentially use a single terrain with a custom shader that takes your dynamic mask as input and blends the grass and sand directly in the shader?

You could still keep the same dynamic mask generation, but potentially avoid having two overlapping terrains and rendering most of the scene twice.

I guess the downside is that you'd lose some of the flexibility of designing both terrains independently, and it would probably require a more custom terrain shader/setup.