Unity shaderGraph instance-interactive grassland

Show results

Please add image description

the whole frame

Please add image description

Content of each region

Area 1

Insert image description here
Calculate the distance between the interactive cube and the vertices of the grass
This can be understood as the distance from the center of the cube to the vertices of the grass. The distance is an integer from 0 to The process is as shown below
Insert image description here

Area 2

Insert image description here
Divide the distance by a certain value. This value is the interaction range, which is equivalent to moving the white bar range to the right.
Insert image description here
Then use the saturate node to limit the value to 0-1< /span>

Area 3

Insert image description here
First of all, the previous step has limited the value to 0-1. This step is reversed. The intuitive explanation is to swap the black and white bars. The closer to the cube, the whiter the part, which means the stronger the influence on the grass vertex shader.
Insert image description here

Area 4

Insert image description here
In order to move the vertex vector of the grass, we need to know the movement method. Subtract the world coordinates of the cube from the vertex position of the grass to obtain the directions of both.

Then multiplied by the influence calculated in area 3, you can get the effect of the closer the vertex is, the greater the offset will be.

Area 5

Insert image description here
This step may seem complicated, but it is actually very simple. Add the vertex offset obtained in area 4 to the vertex coordinates of the grassland. Since the grass does not need to move along the y-axis, you only need to add the x-axis and z-axis. .

Area 6

Insert image description here

When the grass moves, we don't need the roots of the grass to move, so we use the vertex color. That is, when making the grass model, the vertex color (note that it is not the color of the texture) is added to the vertex of the root. It is black and can be interacted with. The part is white, so that a Lerp can be used to smoothly transition between the areas that do not need to be moved and the areas that need to be moved. Of course, vertex coordinates can also be used to calculate here.

Area 7

Finally we need to give a texture
Insert image description here

GraphSetttings

Insert image description here

Finally, you need to write a script to tell the material where the cube is.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TrackPosition : MonoBehaviour
{
    
    
    public Material grassMat;
    private void Update()
    {
    
    
        grassMat.SetVector("_TrackerPosition_v3", transform.position);
    }
}

Then hang it on the cube and drag the material ball to the properties.

Process belowgitee site

Guess you like

Origin blog.csdn.net/weixin_44568736/article/details/134409530