Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Porting (what should be) a simple reaction-diffusion shader from webgl to Unity

Discussion in 'Shaders' started by macho, Dec 15, 2015.

  1. macho

    macho

    Joined:
    Nov 30, 2012
    Posts:
    3
    I've been scratching my head over this for quite some time. I've been trying to port a simple reaction-diffusion shader (seen here) from webgl to Unity. Basically, reaction-diffusion involves the tracking and updating of two chemical compounds (stored in a rendertexture at a given uv coordinate) according to a simple formula. More info here. The basic RD shader involves 3 steps: reading the current values, calculating the updated value, and writing the new value. The rendertexture with the updated values is then processed by a colormapping shader, which reads the value and converts it into an rgb color. All of this sounds simple enough, and I expected this to be pretty straightforward, but the output that I get using the same parameters as the webgl shader, looks completely different. In fact I cannot get anything that looks even remotely like this and other examples that I found on the web.

    I've tried many different things; using different parameters, setting appropriate rendertexture properties (filtering, aniso, mipmapping) but the output just never looks like the original.

    I've attached a barebones unity project, I was hoping that someone here would be able to tell me what I am doing wrong here

    I've also attached two images showing what the original output is (coral like pattern) and what the output from the ported shader is, after 'seeding' the simulation by clicking in the center of the screen

    rd_original.png rd_ported.png
     

    Attached Files:

    • RD.zip
      File size:
      32.5 KB
      Views:
      385
  2. boundingbox

    boundingbox

    Joined:
    Mar 31, 2013
    Posts:
    30
    It looked to be working with only a few issues. I changed your script to do a number of iterations each frame to speed things up 20x. I changed the add reactant color to be lower as it was causing large black sections to appear. And I changed your render texture format to ARGBFloat and made the display texture bilinearly sampled to smooth it out.
     

    Attached Files:

  3. macho

    macho

    Joined:
    Nov 30, 2012
    Posts:
    3
    Boundingbox, thank you very much for taking a look, and letting me know what I did wrong! Setting the rendertextureformat to RGBA32 was just a dumb mistake to make, I can't believe how I overlooked that one. But I am still puzzled as to why the 'add reactant amount' has to be lower than the original, to get the same result. But that is something I can live with :) Thanks again!