Search Unity

Question Vertex Displacement by Another Object?

Discussion in 'Shader Graph' started by stevenbrent, Feb 12, 2021.

  1. stevenbrent

    stevenbrent

    Joined:
    Nov 22, 2015
    Posts:
    25
    Hi -- I'm just getting into Shader Graph and have completed the intro to vertex displacement here: Shader Graph: Vertex Displacement - Unity Learn

    In this lesson, we see how to use the Position and Normal Vector nodes in both World and Object space to achieve different types of displacement; what I'm wondering is whether there's a way to key these to another object's position. For example, in Blender, one can create an empty object and move it around the scene, affecting another object's displacement modifier. Think of the effect of a bird flying just above the surface of a lake, causing the water to move around for an idea of what I'm trying to do.

    Is there a limitation to this capability due to the realtime nature of Unity shaders? Or is there a particular node that will let me reference another object's position etc as input to a graph? I hope this is clear and am happy to clarify if not. And thanks for reading!
     
  2. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,555
    You can pass a Position node (vertex position) set to world space and a Vector3 property (your custom world space position) into a Distance node, divide the result of that by whatever radius you want (float), pass that through a Clamp between 0 and 1, then pass that through a One Minus node. Then for example you can just multiply alpha output by that value to fade something out based on vertex distance from your point.

    To displace vertices, take that value, multiply the Normalized difference between Position and your Vector3 (a Subtract node.. to get the direction from each other), then multiply that by whatever force (positive or negative for attractive or repulsive) and then your use a Transform node to convert that to object space, pass the result of that into Add node with a Position node (set to object space) and then the result of that goes to your Position output. You'd set your radius value high enough to affect the vertices you want, and your force value high enough to multiply the effect on the vertices by as much as you want. To set the position in your material you'd use renderer.sharedMaterial.SetVector.
     
    N04H08 and stevenbrent like this.
  3. stevenbrent

    stevenbrent

    Joined:
    Nov 22, 2015
    Posts:
    25
    Thanks, @polemical! -- I've made a lot of progress this evening... Haven't yet attempted the vertex manipulation, but I was able to pass the target object's position (as a Vector4 with 0 as the w component) to a shader property, and using that to affect a voronoi node. So this is a baby step, but very exciting. Thanks again! I'll report on my progress as this takes shape.

     
    adamgolden likes this.