0
\$\begingroup\$

I've created 2 shaders using shader graph which I'm applying to a sphere to simulate a ball of water/ice. The water shader distorts the shape of the sphere over time to look like it's flowing a bit. What I would like to do is swap the shader from water to ice, and keep the shape of the distorted sphere when the materials swap over.

I'm pretty new to unity and shaders in general so I don't know where to start.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

You'll need to do one of two things:

  1. (easiest) Copy the code that distorts the vertices over time from the water shader to the ice shader.

    Replace any reference to a "time" variable in that code with a reference to a new shader parameter that you set to the current time value at the moment you do the material swap. That will effectively lock the waves in their state from that frame, until you modify that parameter.

    (You could even drive that parameter in slow-mo for a couple frames, if you want it to look like the waves gradually freezing in place, rather than a hard stop)

  2. (harder) Copy the code that distorts the vertices over time from the water shader, and implement it in a C# script.

    At the moment you swap the materials, create a copy of the mesh, and use your C# script to change the vertices of the copy to match the output of the water vertex shader. Then upload that mesh data to the GPU, and swap your object to use this modified mesh instead of the original.

    Now your ice shader can keep its usual vertex shader logic, because you've baked the frozen waves into the mesh it's acting on.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .