Creating a Rock-Melting Gun Effect in Unity

Fickle Swimming explained the logic behind the gun that melts the rocks set up in Unity with Shader and VFX Graphs.

In case you missed it

You might find this article interesting

Introduction

I'm Fickle Swimming, and I have worked as a Unity developer for the past 3 years. My highest completed education is high school.

Project Idea

I had four weeks of vacation, and I decided I'd spend two of them working on some kind of personal project. My goal was to make some sort of "how did he do that" clip to post on Reddit.

I had previously made an Akira style explosion using dissolve shaders, and I had made a Star Wars snow globe using a novel stencil-based approach. I figured if I combine those two projects, and added some voxels, it could be a practical way of doing real-time destruction.

Standard techniques for rendering voxels in games limit the artistic style. I wanted to remove that limitation. If I could come up with some way to combine voxel destruction with regular geometry, then it would be possible to combine AAA quality graphics with the destructibility you find in games like Minecraft or Astroneer. I couldn't find anyone who had done this before, so it was an opportunity to make something new.

Production

First I had to come up with a way to do voxel destruction on regular meshes. After some trial and error, I arrived at this formula:

1. Generate an SDF from a mesh/multiple meshes and store it in a 3D texture. I made my own generator for this, but there are plenty of free SDF generators out there.

2. Render the mesh/meshes with a shader that samples the SDF texture, and discards the fragments that are on the outside. This will create holes in the mesh.

3. Ray march the holes to render the inner surface. This part is the most complicated. I tested multiple solutions using amongst other things a depth prepass and the stencil buffer before I arrived at something that worked.

4. The destruction is done via subtractive operations on the SDF, using compute shaders.

This solution should work on most 3D models you would put in a game. To prove the concept, I figured I had to demonstrate it working in a nice looking scene. Unity's HDRP, Shader Graph, VFX Graph, and Quixel Megascans gave me most of everything I needed.

I built the scene by scattering some Icelandic rocks around. HDRP gave me lightning and post-processing. VFX Graph gave me particle effects, like flames, smoke, refraction, lava, and sparks. VFX Graph has a node for colliding particles against an SDF, so I got that part for free. 

The beam was done in shader graph, it's just a cylinder with some vertex displacement and some noise with animated UV coordinates.

The gun is high effort programmer art. Texturing was done in Substance.

How Does It Work with Textures?

Textures are only being used on fresh cuts, where there is a glowing animated texture to smooth out the transition when spawning lava particles. Textures can be applied to the inside using tri-planar mapping.

Is It Possible to Add Destruction Physics?

This is a bit of a headache. I've seen it done in similar projects, so it should be possible. I'm thinking maybe I'll do connectivity testing on the GPU, then async readback to generate colliders CPU side. I've never done this before though. Another idea is to mirror the destruction CPU side, using some sort of acceleration structure.

Can It Be Used in Games?

The effect is not as heavy as it looks, and I believe its feasible to use this in a game. For it to work in an open-world type game, some kind of distance-based level of detail scheme would have to be implemented.

What Would You Improve?

I feel like I did lava wrong. I spent a lot of time just trying to make it look acceptable. Next time I might go a different route.

Fickle Swimming, Game Developer

Interview conducted by Arti Sergeev

Keep reading

You may find this article interesting

Join discussion

Comments 0

    You might also like

    We need your consent

    We use cookies on this website to make your browsing experience better. By using the site you agree to our use of cookies.Learn more