Grass Shader

Purpose of this tutorial
This tutorial will show you how to create a fairly simple but effective way of making foliage and grass and maybe even other things part away from your player character, by using a meterial parameter collection and a bit of a shader to make it all work. No collisions. No fluff, just straight to the point, Okay? okay, lets do this =)

-Material parameter collection-
first we need a material parameter collection this can be created by:
open content browser > right click > materials & Textures > Material Parameter Collection
we will call out collection MPC_FoliageSphere

02.JPG

for our material collection we need to add 2 scalar parameters and 1 vector parameter these are:
Scalar Parameter 1 - Radius
Scalar Parameter 2 - Hardness
Vector parameter - Location

for now we will also set the Radius to 200 until we can see the effects later on.

02.JPG

-Setting the location of the grass effect-

Okay so first things first we need to set where that location vector parameter is going to get its information from.

  • Open up your characters blueprint and create a tick event
    [This important because we need to update the location every tick so that there is no lag between the player and the grass that will get deformed]

  • plug this into a set vector parameter value node.

  • create a get actor location node

  • plug the return value into the parameter value of the set vector parameter value

  • set collection to MPC_FoliageSphere

  • select Location in the parameter name drop down

01.jpg

-The Material-
Now here comes the fun bit.

  • create a new material open content browser > right click > material

  • name the material M_GrassShader

we will start by making our grass react to lighting in a nice way, and this is super simple.

  • first set the material to 2 sided

  • uncheck tangent space normal maps

05.JPG

  • for the diffuse you can use whatever but in this tutorial im going to be using the following setup:
    1 vector parameter named tip colour
    1 vector parameter named base colour
    1 liner gradient
    1 lerp

    these nodes are then set up like so, and the lerp is plugged into the diffuse slot of the material.

  • i like making my grass fully rough but this is optionalbut to do this:

  • create 2 constants

  • set the first to 1

  • set the second to 0

  • plug them into the metallic, specular and roughness like so

05.JPG

next up we need to set up a bit of a normal map so that they react to light properly when using tangent space normal maps in unreal ive found using just the blue channel is fine, but when not using tangent space it seems you have to use a base normal map value to get the best results.

  • create a constant 3 vector and give it the following settings

  • R:0.255, G:0.255, B:1

  • plug this into the normal material slot

06.JPG

Okay, Now things are going to get a little complex so make sure you pay close attention here.

  • first we need a lerp, plug it into the world position offset

we will work on the alpha first, that we will use to determine where the effect will be and how prominent it will be.

  • create a sphere mask node

  • Plug an absolute world position node into the A slot

  • create a collection parameter node, set its collection to MPC_FoliageSphere and its parameter name to Location

  • duplicate the collection parameter node and set its parameter name to Radius

  • duplicate the collection parameter node and set its parameter name to Hardness

  • plug the location collection parameter into a component mask with R,G and B ticked but A unticked

  • plug the component mask into the B of the sphere mask

  • plug the radius collection parameter in to the sphere mask radius

  • plug the hardness collection parameter into the hardness of the sphere mask

  • drag out a pin from the sphere mask and create a one minus node

  • drag a pin out from the one minus and create a height lerp, make sure the one minus is plugged into the transistion phase(s) of the height lerp too.

  • create a constant with a value of 0 and plug it into the height texture of the height lerp

  • create a constant with value of 0.5 and plug it into the constant of the height lerp

  • drag a pin out from the results of the height lerp and connect it to our original lerp nodes alpha input

you should have this sort of setup

07.JPG

next up we need some grass wind so create the following nodes

  • constant with a value of 0.5
  • constant with a value of 1.5
  • constant with a value of 0.3
  • simple grass wind node
  • linear gradient node
  • one minus node
  • multiply node
  • plug the multiply output into the A slot of our original lerp

connect them together as shown below

08.JPG

  • create an add node and plug the multiply from the grass wind into the B slot of it [this is VERY important] and then connect the add node to the B slot of our original lerp

now the next part is super complicated and will take longer to explain than to show so like before create the following nodes

  • absolute world position
  • duplicate of the location parameter collection node
  • mask [with RGB no A]
  • distance node
  • divide node
  • clamp node [Min:0, Max:1]
  • Scalar parameter named Strength with a value of 60
  • Scalar parameter named Distance with a value of 256
  • 2 one minus node
  • 4 multiply nodes
  • mask node [with only G ticked]
  • texture coordinate node
  • subtract node
  • normalize node
  • 2 append nodes
  • constant node with a value of 0

set them up as seen below

  • plug the multiply into the A slot of the Add node that we connected the grass wind too.

what this will do is allow the grass wind to still function weather or not it is being pressed down by the player.

this should finish off the grass shader, and should give you results similar to the following

12.JPG

if you have any questions or want to follow me for more tutorials check out Tritone over at any of the following places:

](Redirecting...) twitter.png](https://twitter.com/tritonethegame?lang=en) youtube.png](Tritone The Game - YouTube)

thanks guys

Hey, @Gravzer333!

I moved thread into Community content section so more people who is looking for such content could see.

Thanks for the tutorial!

Hello i followed your tutorial but my result when applied to a cube from the basic shapes just moves it like in a big chunck, not many tiny pieces as you have.

I too tried this tutorial and got the same result, just a big moving blob/chunk.

I see 2 unclear steps…

  1. I noticed there is something plugged into the material opacity mask… but no mention of opacity mask in any of the steps.
  2. I noticed there is a 2nd link going from the first LinearGradient but no mention of where that goes (and plugging that into opacity mask does nothing).

Maybe an completed view of the entire material would help.

Yeah, just screencapping the whole material would be better imo. Unreal materials tend to speak for themselves aslong they’re commented properly.

Hello,
If you want to test the shader, you have to apply the material on a foliage like “grass”… not on a cube… (blob…)
The tuto is good.

I just want to say thank you for this shader. Its does me a great deal.