in shader, tutorial, Unity3D

How to Use Shaders for Simulations

Share Button

This series of tutorials will teach you how use shaders for simulations; in particular how to use them to simulate fluids. This first post will focus on how to continuously process a texture using a shader. This technique is at the heart of most simulations and will be used in this series to implement shaders that simulate smoke and liquids.

texture6

Introduction

A previous post (Screen Shaders & Image Effects) explained how shaders can affect not only objects, but also cameras. Similarly, we will use a shader to process a texture. As far as the GPU is concerned, we’re only manipulating pixels – regardless whether they come from a camera or a 3D model. If you’re familiar with Unity, you might know how the pipeline for post processing works:

texture1

The technique shown in this post is substantially different, because the edited texture is fed again to the shader, allowing to be processed continuously:

texture2

In the context of traditional materials, this doesn’t really make sense. There are situations, however, in which we want to repeat a certain process. Simulating how smoke diffuse, for instance, requires a constant computation. For this toy example we will create a shader that simply inverts the colours of a texture.

The Script

Since this new pipeline is not standard, we will need a script ( ApplyShader) that triggers it. To process a texture with a shader, Unity offers the function Graphics.Blit:

The shader needs to be wrapped into a material and the two textures have to be different. If we want to update the original texture, we need to use an additional buffer texture:

The original texture is processed into buffer, which is then copied again into texture. We also need to start the loop somewhere, so is necessary to initialise the with an image for the first iteration:

The buffer is allocated with the same properties of the original render texture.

Updating periodically

The last step is now to invoke the UpdateTexture function periodically.

The Shader

All the code written so far requires a shader in order to process pixels. Post processing requires a vertex and fragment shader in order to work. For this specific example, we will create one that simply inverts the colour of the image it receives.

texture4

For the invert shader to work as a post processing effect, is necessary to set the following properties:

Conclusion & Download

To make ApplyShader works, you will need to provide it with the following:

  • Initial Texture: A texture to initialise the process;
  • Texture: A render texture, which will be continuously fed to the shader;
  • Material: A material that wraps the shader you want to use for the computation;
  • Update Interval: how often (in seconds) this scripts will be called.

There is another important part that you must not skip: if you want your rendered texture to be displayed in the game, you need another material to render it, like in the picture below:

textire3

There are many interesting applications of this technique. The next tutorials in this series will explore how the ApplyShader script can be used to simulate water and smoke that are both realistic and interactive. There are other interesting applications, especially when it comes to computations that are highly parallalisable. A future tutorial will explain how to efficiently simulate Conway’s Game of Life within a shader.

texture5

If you want to use shaders to perform computation, you might be better off using Compute Shaders instead. Unfortunately they are not supported on all platforms, and there’s a general lack of resources on them. Using “traditional” shaders to simulate smoke and water is a little bit of a stretch, but requires little new knowledge and runs on everything that support shaders.

You can download the Unity package for this tutorial here.

The next part of this tutorial (How to Simulate Smoke with Shaders) will focus on how this technique can be used to simulate the diffusion component of particles (such as the ones that compose smoke) into a fluid (like air).

Other resources

📧 Stay updated

A new tutorial is released every week.

💖 Support this blog

This websites exists thanks to the contribution of patrons on Patreon. If you think these posts have either helped or inspired you, please consider supporting this blog.

Paypal
Twitter_logo

Write a Comment

Comment

  1. Graphics.Blit (texture, texture, mat);
    is also working (instead Graphics.Blit (texture, buffer, mat); Graphics.Blit (buffer, texture, mat); )
    what’s different? does the buffer is necessary needed?

Webmentions

  • How to Simulate Cellular Automata with Shaders - Alan Zucconi May 13, 2016

    Graphics.Blit (texture, texture, mat);
    is also working (instead Graphics.Blit (texture, buffer, mat); Graphics.Blit (buffer, texture, mat); )
    what’s different? does the buffer is necessary needed?

  • How to Simulate Smoke with Shaders - Alan Zucconi May 13, 2016

    Graphics.Blit (texture, texture, mat);
    is also working (instead Graphics.Blit (texture, buffer, mat); Graphics.Blit (buffer, texture, mat); )
    what’s different? does the buffer is necessary needed?