Search Unity

DX11 Pyroclastic Noise

Discussion in 'Developer Preview Archive' started by smb02dunnal, Sep 18, 2012.

  1. smb02dunnal

    smb02dunnal

    Joined:
    May 5, 2012
    Posts:
    439
    I am attempting to remake the nice explosions seen in the Unity 4 tech demo videos and in the butterfly demo, as seen at unite. But I am having some troubles and I was wondering if anyone here could help me?


    Here is a screenshot of my progress.
    $Explosion Screenshot.png


    I am taking the following steps:

    1. Generate a 3D noise texture, I am doing this inside a compute shader and updating it when ever the sphere moves (to avoid having tiled noise).

    2. The noise I am using is fractal noise based off the functio,n N = sum( sin( abs( perlinNoise(x, y, z) ) ) ). Although I have tried different noise functions, this one seems to be giving me the best results just now.

    3.
    Pass 3d texture into a tesselation shader, displacing the vertices along their normal.

    4. Using the same value as above for displacement, colour the sphere in the pixel shader, using a gradient map.


    As you can see my noise isn't as sharp and detailed as this one :
    $Target.png

    Apart from the last step (which I havent implemented yet) volume rendering, is there anything I have missed here because somethings not right... :(


    If anyone can help me on this, it would be much appreciated and I'll be more than up for sharing the source when it's more completed.
     
    Last edited: Sep 18, 2012
  2. smb02dunnal

    smb02dunnal

    Joined:
    May 5, 2012
    Posts:
    439
    Okay, so I'm going to post an update.

    I have added a better gradient map (which surprisingly goes a long way) and tweaked the noise to get a better result.


    Here is an updated screenshot:
    $Explosion Screenshot 2.png


    It's still a long way off from what I want my final result to look like, but it's definately a step in the right direction.
     
  3. Owen

    Owen

    Joined:
    Apr 20, 2011
    Posts:
    79
    If you use displacement mapping with 3D noise, then you will only capture details in the noise that intersect the surface of the base mesh, where you actually sample the noise pattern. As a result, you are actually only doing 2D noise across the surface of the sphere.
     
  4. smb02dunnal

    smb02dunnal

    Joined:
    May 5, 2012
    Posts:
    439
    What do you suggest I do then?
     
  5. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    IIRC the shader in the butterfly effect demo was not using a 3D noise texture; whole noise was computed directly in the shader.
     
  6. smb02dunnal

    smb02dunnal

    Joined:
    May 5, 2012
    Posts:
    439
    Thanks Aras!

    So should I still be displacing the tesselated geometry along it's normal? And what is pyroclastic noise really? Is it just fractal noise with a fancy name or is there a particular function associated with it?
     
  7. Owen

    Owen

    Joined:
    Apr 20, 2011
    Posts:
    79
    I don't know how they generated the noise, but for that screenshot they wrote density values into a volume texture and then rendered it using either ray marching or planar slice rendering.

    In addition to the noise, you will need to encode a distance function for your sphere, where values of 1 are deep inside the sphere, values of 0 are far outside it, and 0.5 is on its surface for instance. The noise just perturbs those values to add turbulence. In the simplest case, your ray marching algorithm just finds the first sample point that crosses the 0.5 boundary. A surface normal can be computed from the gradient of the volume texture. Coloring the volume is less straightforward, and may require you to store additional volume data.
     
    Last edited: Sep 19, 2012
  8. smb02dunnal

    smb02dunnal

    Joined:
    May 5, 2012
    Posts:
    439
    Thanks Owen!

    Of course! That makes some sense to me! Combined with what Aras said about computing the noise inside the shader would account for the lack of detail in my original attempts.

    Could I not just calculate the colour using the displacement amount?

    Thanks guys, I will post my progress next time I have a chance to work on this (probably the weekend if I'm lucky!).
     
  9. alexandre-fiset

    alexandre-fiset

    Joined:
    Mar 19, 2012
    Posts:
    715
    Wow, what should I read to learn to make shaders this complex? I can easily write surface shaders, but nothing with vertex, fragment or geometry...
     
  10. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    From what mentioned at Unite, the explosion uses a compute shader (U4 DX11 windows) to calculate that on the fly.
    If I recall right they are also thinking about sharing the shaders used for this and a few other effects (not the project itself though and at 60gb thats definitely better ;)), if we are lucky in the form of a Unity 4.0 available asset store package (for free) to just download, use and enjoy
     
  11. smb02dunnal

    smb02dunnal

    Joined:
    May 5, 2012
    Posts:
    439
    Hello dreamora, I have read that on another forum but wheres the fun in that :p I want to figure it out myself.

    alexandre, try get hold of some of the old ShaderX series, while the older ones wont teach you any of the new domain or hull shader stuff, they are still one of my favourite series of books on the subject!
     
  12. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Like I said above, that one is not using a volume texture (it could, but it was not). It's raymarching the distance function directly.

    Technical minor correction: that particular explosion is not using a compute shader. It's just a really long pixel shader that does raymarching.
     
  13. Owen

    Owen

    Joined:
    Apr 20, 2011
    Posts:
    79
    It actually evaluates the distance and noise functions for every ray sample? That seems overly wasteful. So then its not using any new features of Unity 4, unless maybe you need the higher instruction count limit of PS5.0.
     
  14. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    You could also say "it's saving a S***ton of memory that would be required to store the 3D texture" :)
    It is using native integer types in the shader (for noise), so that requires shader model 4.0 at least.
     
  15. smb02dunnal

    smb02dunnal

    Joined:
    May 5, 2012
    Posts:
    439
    Heres an update:

    Started making a raymarch renderer. I am evaluating the distance per sample rather than using a volume field (I might try both and see which on works better in the future).

    Here is a screen from my latest scene :

    $Raymarch.png

    As you can see I am computing the normals and showing them as the colours.

    Next step is to port my noise functions from the compute shader into the pixel shader. (Shouldn't be too difficult).

    Expect another update today.
     
  16. smb02dunnal

    smb02dunnal

    Joined:
    May 5, 2012
    Posts:
    439
  17. smb02dunnal

    smb02dunnal

    Joined:
    May 5, 2012
    Posts:
    439
    I have added the noise functions to the distance field calculations and the result is good, but it's still missing something.

    Can anyone tell me what?

    Here is a screenshot :
    $Explosion.png


    And here is my latest shader (excluding noise library) :
    View attachment $PyroclasticNoise.shader
     
  18. smb02dunnal

    smb02dunnal

    Joined:
    May 5, 2012
    Posts:
    439
    Hello,

    Thanks for all your help. Here is an image of the ray marching + gradient map, fully working.

    $RM Exp - 2.png
    $RM Exp - 1.png

    The shader code is actually much the same as the one above, it was actually a problem with my noise (which isn't included in the code above).
     
  19. pvloon

    pvloon

    Joined:
    Oct 5, 2011
    Posts:
    591
    That looks awesome!
     
  20. alexandre-fiset

    alexandre-fiset

    Joined:
    Mar 19, 2012
    Posts:
    715
    Will you sell it on the Asset Store (with noise library)? I'd buy that.
     
  21. smb02dunnal

    smb02dunnal

    Joined:
    May 5, 2012
    Posts:
    439
    I might be wrong but I think unity are going to release their version of this fairly soon. Maybe some one could confirm that?

    If not I'd be happy to put this up there, probably for free without source.

    There is stil one thing I need to do with it though, you might notice that the noise is very sharp around the edges, this needs to be softened before the effect is complete.
     
  22. smb02dunnal

    smb02dunnal

    Joined:
    May 5, 2012
    Posts:
    439
    If any one is still interested I have uploaded an executable and an example project that includes the shader for achieving this effect.

    You can find it on this page : http://dunnalex.com/RMExp.html
     
  23. bigzer

    bigzer

    Joined:
    May 31, 2011
    Posts:
    160
    Thanks, I wanted to do some experimentation on this ^^
     
  24. bigzer

    bigzer

    Joined:
    May 31, 2011
    Posts:
    160
    Ooops your RMExp.zip contains NewHeur_PE virus ? Oo
     
  25. smb02dunnal

    smb02dunnal

    Joined:
    May 5, 2012
    Posts:
    439
    I am sorry to hear that. I did scan the archive before uploading it, and have just now scanned it on a completely different machine with a different virus scanner and nothing shows up. Are you using heuristic scanning in NOD32? I've heard that this can give people false positives all the time. Would you be kind enough to double check this for me?
     
  26. Agent_007

    Agent_007

    Joined:
    Dec 18, 2011
    Posts:
    899
  27. smb02dunnal

    smb02dunnal

    Joined:
    May 5, 2012
    Posts:
    439
    Good shout! Thanks.
     
  28. bigzer

    bigzer

    Joined:
    May 31, 2011
    Posts:
    160
    My bad, still it's possible to run the project instead of the exe if you really don't want to take any risk ^^ . Again thanks and sorry for the false alarm ^^
     
  29. Hotsun

    Hotsun

    Joined:
    Aug 11, 2010
    Posts:
    122
    Your next step should be optimizing your shader.
    Even my GTX 580 ran it on 7 to 10 frame per seconds when I set octaves and distortion to their maximum.
    So your GTX 560 Ti can't run it smoothly too(I found your vga's model from the output_log.txt file in the executable folder :D )
    Right now your VGA is in the list of recommended system requirements of many great games but it can't even run this little test nicely.
     
  30. smb02dunnal

    smb02dunnal

    Joined:
    May 5, 2012
    Posts:
    439
    TBH, the distortion shouldn't really do much to the performance, it's mainly increasing the octaves that gives you the performance drop. The difference in looks between the higher octaves is minimal, so I would recommend using 4 if your graphics card can't handle the higher numbers.

    Anyway, as for an optimized version of this shader, probably wont happen anytime soon, I have moved onto better things :) But, making this shader run much faster wouldn't be hard at all, just simply calculate and store the noise in a 3D texture and use that at run time. Calculating the noise at runtime is quite wasteful! (to say the least) And it's what this shader is currently doing.
     
  31. Owen

    Owen

    Joined:
    Apr 20, 2011
    Posts:
    79
    To really optimize it, if you are going to use volume textures, the best approach is probably to store not just the noise but the final voxelized explosion.

    You can use a compute shader to evaluate your distance fields, apply noise, and evaluate some color coefficients, then store them in the volume each frame. You could encode it in 16 bits per voxel for instance with 8 bits of density and 8 bits of temperature (used as a lerp value between your fire and smoke colors, or as a lookup into a gradient texture).

    Then your pixel shader only has to do a raymarch through the volume texture to find the temperature value where density == 0.5 (if any), and use that value to choose a color. This way your performance will not vary too much by how much screen space your explosion occupies, because a lot of the volume texture samples you take should hit the cache, also the performance costs of the smoke geometry and rendering will be decoupled. The downside is that the spatial detail of the explosion will be fixed at a certain size, no matter how close you get to it.

    You could also make it partially transparent by accumulating densities instead to use as your alpha, adding and subtracting temperature as you go (for instance march back to front, temp > 0.5 adds light, temp < 0.5 absorbs light, use total for color).
     
    Last edited: Oct 25, 2012
  32. smb02dunnal

    smb02dunnal

    Joined:
    May 5, 2012
    Posts:
    439
    Interesting thoughts!

    I have a slightly newer version of this explosion effect that I haven't put online anywhere. In this version I calculate density in a very similar approach to what you just described. The problem comes when I use that value to compute the alpha. If I have two objects infront of eachother, the alpha, around the edges, for the object infront, gets applied to the object behind.

    Heres a screenshot :

    $Explosion.png


    How could I get rid of this artifact?
     
  33. Owen

    Owen

    Joined:
    Apr 20, 2011
    Posts:
    79
    Thats where temperature accumulation would come into play, the idea is that each volume sample should absorb some light (adding to output alpha and subtracting from output color, depends on density) and/or emit some light (adding to output color, depends on temperature) along the ray.

    That way having a small amount of smoke in front of some flame should give you a slight reduction in brightness.
     
  34. simesgreen

    simesgreen

    Joined:
    Nov 1, 2012
    Posts:
    1
    I just came across this. I wrote the original shader in the Butterfly effect demo, and this looks as good if not better!

    If you specify the bounding objects as transparent in the Unity shader, set the blend mode correctly, and output alpha from your shader, the transparency should all just work.

    -S.
     
  35. smb02dunnal

    smb02dunnal

    Joined:
    May 5, 2012
    Posts:
    439
    Thanks alot!

    So does that mean that each fireball was a separate material rendered on a separate quad?