in Shaders, Tutorial, Unity

How to Use Shaders for Simulations

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.



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:


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


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.

⭐ Suggested Unity Assets ⭐
Unity is free, but you can upgrade to Unity Pro or Unity Plus subscriptions plans to get more functionality and training resources to power up your projects.

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.


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

Conclusion & Download

Become a Patron!
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:


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.


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

💖 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.


📧 Stay updated

You will be notified when a new tutorial is relesed!

📝 Licensing

You are free to use, adapt and build upon this tutorial for your own projects (even commercially) as long as you credit me.

You are not allowed to redistribute the content of this tutorial on other platforms. Especially the parts that are only available on Patreon.

If the knowledge you have gained had a significant impact on your project, a mention in the credit would be very appreciated. ❤️🧔🏻

Write a 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?


  • The Mathematics of Epidemics - Alan Zucconi July 17, 2020

    Awesome tutorial. Thanks for this intro! Helped me a lot.

  • GPU Sorting - Alan Zucconi July 17, 2020

    Awesome tutorial. Thanks for this intro! Helped me a lot.

  • How to Simulate Cellular Automata with Shaders - Alan Zucconi July 17, 2020

    Awesome tutorial. Thanks for this intro! Helped me a lot.

  • How to Simulate Smoke with Shaders - Alan Zucconi July 17, 2020

    Awesome tutorial. Thanks for this intro! Helped me a lot.