Home

Awesome

What is this package about?

This Unity Package was created for the use with Shadergraph for the UPR and HDRP. It contains a subgraph and two scripts for an interactable shader, with a dynamic interactor count.

Once you set up your material you can add any number of interactors and even create or destroy them during runtime. This interactive shader approach also supports unique radii for interactors.

InteractableShaderScreenshot
Each interactor provides the material with its position and radius of influence, allowing for dynamic materials to interact with objects in the scene.

How do I install this package?

Open the Package Manager and click the '+' button.
Choose 'add package from git URL...' then paste the git URL of this repository and press 'Add'.
GitURLButton
git URL: https://github.com/Umbrason/DynamicInteractableShader.git

How do I set up a dynamic material?

Create a new material

Start by creating a material using Shadergraph.
To be interactable the material must have the following two properties:

Properties

Create a ShaderInteractor

Next create a new GameObject and add a ShaderInteractor component to it.
Now drag the new material into the material slot of the ShaderInteractor component. This component will keep the shader properties updated.
ShaderInteractor

Create an InteractionAgent

Finally you need to add a InteractorAgent component to any object, that you want to interact with your material. You also need to drag the new material into the material slot of the InteractionAgent component.
The InteractionSize property can be used to control the the 'strength' or 'radius' of the influence the InteractionAgent has.
Interaction Agent

How do I make my material react to InteractionAgents?

Inside the Shadergraph you can access the InteractorAgent with the strongest influence at any given point using a Subgraph called NearestPositionFromTexture.
Plug in the Texture2D and Float properties into their respective sockets and a Vector3 into the position node of the Subgraph and it will output the position and InteractionSize of the InteractorAgent with the strongest influence on any given position.
NearestPositionSubgraph

Calculate the influence of the nearest InteractionAgent

Using this information you can now compute the influence of the nearest InteractorAgent and the specified point. The InteractionSize property of an InteractionAgent can be accessed via the 4th component of the InteractionAgent position. By dividing the distance by the InteractionSize you can normalize it so the affected area contains values only from 0-1.
normalizedDistance

Calculate the direction away from the nearest InteractionAgent

You can also compute the normalized direction away from the nearest InteractorAgent by taking a position and subtracting the position of the most influencial InteractorAgent for that point. By dividing by the InteractionSize (accessable via the 4th component of the position) you can normalize the distance to make working with easier.
NormalizedDirection

Use the influence and direction to displace geometry

Using this influence and direction its possible to move geometry away from the nearest InteractionAgent by multiplying the direction vector with the influence and adding this to the position of each vertex.
VertexDisplacement

An Interactive Grass Shader

Below is a GIF of a grass shader reacting to the position of multiple InteractionAgents represented as drones. The InteractionSize property of the InteractionAgent is scaled by the magnitude of the scale of each drone, resulting in a wider effect radius for the large drone. They move around in real time and their position is updated in the shader respectively.
InteractiveGrassShader

The effects in the GIF were achieved using the vector displacement method mentioned above but multiplied with the sine of the distance to the most influencial InteractionAgent and with its phase animated over time. The illusion of the grass bending in the wind is just the result of two gradient noise samples added to the vertex displacement and offset over time.