Search Unity

3D Volume Rendering using Raymarching Demo

Discussion in 'Made With Unity' started by brianasu, Jan 25, 2012.

  1. brianasu

    brianasu

    Joined:
    Mar 9, 2010
    Posts:
    369
    Volumetric Texture version here
    https://github.com/brianasu/unity-ray-marching/tree/volumetric-textures

    https://github.com/brianasu/unity-ray-marching

    Source code now available. Requires Unity pro 4.5.0 enjoy. I'll be updating it when I more free time.


    Ray marching in Unity3D. All the 3D is done by marching through a 3D texture. The only geometry used is a cube.
    Needs a decent shader 3.0+ card.

    Only works on webplayer 3.4

    Hold down right mouse button to rotate the cube.

    Web player Demo
    http://dl.dropbox.com/u/17249329/raymarching/WebPlayer/WebPlayer.html

    Can read more about it here
    http://graphicsrunner.blogspot.com/2009/01/volume-rendering-101.html
     
    Last edited: Dec 11, 2014
    Farelle likes this.
  2. andorov

    andorov

    Joined:
    Feb 10, 2011
    Posts:
    1,061
    Doesn't seem to work.. my system def. meets requirements.

    Win7 / 64 bit
     
  3. niosop2

    niosop2

    Joined:
    Jul 23, 2009
    Posts:
    1,059
    I just get a black screen, but am using 3.5 test player, so that might be it. Have you tried the 3.5 player? If it doesn't work for you in the test player could you submit a bug report so they know about it?
     
  4. brianasu

    brianasu

    Joined:
    Mar 9, 2010
    Posts:
    369
    Yup 3.5 webplayer doesnt work. Only 3.4
     
  5. Dev.D1

    Dev.D1

    Joined:
    Dec 29, 2010
    Posts:
    99
    Seems to render fine at my end (webplayer 3.4) . Is there a way to change the resolution/granularity of the intervals? I understand that we may need to recompute if we make such a change.

    I could suggest :
    1) please change the background from black to something lighter? It took sometime to figure out that the "Alpha Strength" "Scroll Speed" are sliders :)
    2) If you could add an fps counter.
    3) Allow the webplayer to go full screen.

    It's a cool tech demo but off the top of my head with this kind of massive fill rate hits, its only practical in small demos. Even Kyle Hayward's original demo runs at 27 fps on my GTX 260
     
  6. brianasu

    brianasu

    Joined:
    Mar 9, 2010
    Posts:
    369
    Thanks for the suggestions.

    Yeah you are right fill rate is a problem but if you are using it for soft particles like clouds you can probably do a low res off screen render then upsample.

    To change the intervals you'd have to recompute the 3D noise each time plus I would have to adjust the amount of steps the ray marcher takes. This might be hard because loops have to be a constant number.

    Kyle Hayward's is probably more computational complex because he uses a larger number of samples and uses transfer functions.
     
  7. techmage

    techmage

    Joined:
    Oct 31, 2009
    Posts:
    2,133
    really cooly, would love to see some code, or more explanation
     
  8. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    Man...i really wanna see it, but i got black screen too. Can you post some pic?
     
  9. ianjosephfischer

    ianjosephfischer

    Joined:
    Mar 6, 2012
    Posts:
    13
    I love the demo, you say that all 3d is done in a 3d texture. As unity does not support 3d textures how do you pull this off. I am assuming that you are using a 2d texture as 3d? Or is there a better way? Can you give code examples?
     
  10. 3dDude

    3dDude

    Joined:
    Jul 4, 2010
    Posts:
    1,067
    Images/video?
     
  11. Sir-Tiddlesworth

    Sir-Tiddlesworth

    Joined:
    Oct 19, 2011
    Posts:
    908
  12. brianasu

    brianasu

    Joined:
    Mar 9, 2010
    Posts:
    369
    Yeah Unity doesn't support tex3d. (Unity 4 does:)) I just simulate it by laying out my 3D texture in a grid. Then I just look up the uv coordinates from the 2D grid. The only problem is that you don't get any bilinear filtering in the "z" direction so you have to do that manually by taking the neighboring slices and blending them. It's probably better to wait for Unity 4 to get a proper tex3D which probably much faster to do.
     
  13. Wolfos

    Wolfos

    Joined:
    Mar 17, 2011
    Posts:
    951
    That looks really damn good.
     
  14. jcongote.unity

    jcongote.unity

    Joined:
    Feb 12, 2013
    Posts:
    7
  15. txasti

    txasti

    Joined:
    Oct 26, 2010
    Posts:
    10
    Hey jcongote!! Have you used a 3D Texture to do that? That's awesome!! And how did you use the MRI images to create the 3D texture?

    Thank you!!
     
  16. jcongote.unity

    jcongote.unity

    Joined:
    Feb 12, 2013
    Posts:
    7
  17. txasti

    txasti

    Joined:
    Oct 26, 2010
    Posts:
    10

    Hello again jcongote!! Your paper is impressive!! But you have developed it for WebGL. How did you developed it in Unity? What I mean: What do I need to create something similar?

    Thank you again and thank you for your quick response!
     
  18. jcongote.unity

    jcongote.unity

    Joined:
    Feb 12, 2013
    Posts:
    7
    Hello Txasti,

    I apply the shader to a cube, and use shaderlab with GLSL language, and then some small fixes. There is a lot of improvements which can be done.
     
  19. txasti

    txasti

    Joined:
    Oct 26, 2010
    Posts:
    10
    Hello jcongote!

    Is it possible to test that shader? It would be great! I have experience in unity so I could try to improve your code, and of course I would share with you my contributions. :D

    Thank you in advance!
     
  20. joel_b

    joel_b

    Joined:
    May 23, 2013
    Posts:
    4
    brianasu great job, is there any chance of releasing/providing the demo project?
     
    Last edited: Jul 4, 2013
  21. brianasu

    brianasu

    Joined:
    Mar 9, 2010
    Posts:
    369
    Yeah sure. Let me just clean up the code a bit:) So i'm not too embarrassed to release it to the public.
     
  22. Pyromuffin

    Pyromuffin

    Joined:
    Aug 5, 2012
    Posts:
    85
    Hey, I used your ray marching technique to visualize my sound wave field. Here's a video:
     
    Devilwhale and Aenigmaticus like this.
  23. smb02dunnal

    smb02dunnal

    Joined:
    May 5, 2012
    Posts:
    439
    +1 Love that!
     
  24. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,102
    Ok that's cool!
     
  25. Pyromuffin

    Pyromuffin

    Joined:
    Aug 5, 2012
    Posts:
    85
    Here's another video, showing off dynamic emitters. I'm making this game for the oculus/indiecade vrjam. Before ray marching, I used some crappy procedural geometry (actually it was pretty cool using compute buffers and drawindirect, but it didn't look nearly as good as this).



    I'm really glad you guys think this is cool.
     
  26. brianasu

    brianasu

    Joined:
    Mar 9, 2010
    Posts:
    369
    Wow really nice:) Have you solved the camera intersecting the volume? I think you have to render a reverse geometry to get a proper depth buffer.
     
  27. Alesk

    Alesk

    Joined:
    Jul 15, 2010
    Posts:
    340
    Hi,

    So ? any progress on a public release ? ;)
     
  28. 3

    3

    Joined:
    Sep 16, 2012
    Posts:
    387
    any chance of an asset store release?
     
  29. brianasu

    brianasu

    Joined:
    Mar 9, 2010
    Posts:
    369
    I'm actually redoing the code now. I'll release for free in a week or so.
     
  30. Alesk

    Alesk

    Joined:
    Jul 15, 2010
    Posts:
    340
    Woot ! \o/
     
  31. brianasu

    brianasu

    Joined:
    Mar 9, 2010
    Posts:
    369
  32. WhoRainZone1

    WhoRainZone1

    Joined:
    Oct 6, 2013
    Posts:
    15
    Heya,

    looks very promising, although I only get a pink screen in the demo scene - but no errors at all. What could I do?
     
  33. brianasu

    brianasu

    Joined:
    Mar 9, 2010
    Posts:
    369
    It might be a Mac / PC thing again. Usually happens. What kind of system do you have? I'll double check it on my win machine. I usually develop on mac.
     
  34. WhoRainZone1

    WhoRainZone1

    Joined:
    Oct 6, 2013
    Posts:
    15
    Yeah I'm also getting some win/mac lineending warnings - so that could have to do something with it, indeed. I'm using win7, 64bit.. supporting DX11 and all, but not working yet, all my mat's seem to stay pink.

    Thanks for the quick response, I'll definately follow this thread.. volumes are such a nice feature. :)
     
  35. brianasu

    brianasu

    Joined:
    Mar 9, 2010
    Posts:
    369
    Yup it was some silly errors in my code. Should be working on windows now just pull for Git hub again.
     
  36. WhoRainZone1

    WhoRainZone1

    Joined:
    Oct 6, 2013
    Posts:
    15
    Nice, thanks, works now! Time to dig deeper to get the repetive pattern smoothened when looking from the wrong side.. to get an actual cloudy look. But that is definately more than I was able to come up with ^^
     
  37. brianasu

    brianasu

    Joined:
    Mar 9, 2010
    Posts:
    369
    Thanks. From what I read you can sample a noise texture to jitter your viewray so it reduces the repetition. Another options is to increase the number of slices. I might try these options to see if they look smoother.
     
  38. tolstenko

    tolstenko

    Joined:
    May 31, 2012
    Posts:
    14
    How can I change the code to render my Texture3D? For a given Texture3D, how can I show it using your system?
     
  39. brianasu

    brianasu

    Joined:
    Mar 9, 2010
    Posts:
    369
    lmbarns, jason-fisher and Reanimate_L like this.
  40. Quisatz_Haderah

    Quisatz_Haderah

    Joined:
    Jan 25, 2014
    Posts:
    1
    Are 3D textures completely a Unity Pro feature, or your code requires some Pro features to be used?
     
  41. brianasu

    brianasu

    Joined:
    Mar 9, 2010
    Posts:
    369
    For Unity 4 it's pro only but Unity 5 personal should have everything you need to run it.
     
  42. brucezhu

    brucezhu

    Joined:
    May 19, 2015
    Posts:
    3
    brianasu, I download your latest volume render sample, I find the memory cost is very large in the "volumeColors = newColor[x*y*z];",if I set the x*y*z to 512*512*512, the editor will be error of out of memory ,so, could you tell me how I reduce the memory usage? Thank you!
     
    Last edited: May 19, 2015
  43. brianasu

    brianasu

    Joined:
    Mar 9, 2010
    Posts:
    369
    512x512x512 means you are basically using 512, 512x512 textures. The array is going to be huge. The easiest solution is just to reduce the size.

    A more complicated solution is to break down the volume into multiple chunks like 4 256x256x256 chunks. So you only need to allocate a subset of color array and reuse it.

    I didn't implement this because it would require me to have a method to render multiple volumes and sort them
     
  44. brucezhu

    brucezhu

    Joined:
    May 19, 2015
    Posts:
    3
    Thanks for your reply! I think the complicated is how to break down the volume and reconstruct it, and the shader will be added more?
     
  45. brucezhu

    brucezhu

    Joined:
    May 19, 2015
    Posts:
    3
    I find a small bug, the MRI tif flies No.99 seemed to be put into the No.1 position of the volume, but I don't know why, I check the code there seemed no any mistakes.
     

    Attached Files:

    jadware likes this.
  46. brianasu

    brianasu

    Joined:
    Mar 9, 2010
    Posts:
    369
    It probably require doing something like this

    http://graphicsrunner.blogspot.com.au/2009/02/volume-rendering-201-optimizations.html

    Ok thanks I'll take a look at that.


    Also you could possibly generate the volume textures offline first and then load them in. The final input is a 3D texture but it does not need to be constructed run-time.
     
  47. verinaFCL

    verinaFCL

    Joined:
    Jul 16, 2015
    Posts:
    1
    hi, how do we change the color/material in the master branch? Also, if i have external voxel data, basically i have to modify GenerateNoise function to load my data?

    also,
    @brianasu I am interested in your V-Fog volumetric fog shapes, wondering if it is extensible to procedurally generated artbitrary shape mesh?
     
  48. brianasu

    brianasu

    Joined:
    Mar 9, 2010
    Posts:
    369
    Yes you would have to modify the code. The volumetric texture one is better since you can just generate the textures outside then pass them into the system.

    V-Fog only works with convex shapes now because I haven't handled any of the complex multiple in-out scattering of concave shapes. I'll probably support 3d textures now since unity has updated that so you couldn't technically create a 3d texture and use that as a shape.

    If you email me at brianvlights@gmail.com I can email you a demo copy.
     
  49. Rockroll

    Rockroll

    Joined:
    Aug 2, 2015
    Posts:
    6
    Hi,Brianasu
    I download your volume rendering demo, but I found when I change only one dimension scale of xyz of the cube(I try to adjust the scale of the MRI head) and very close to the camera, for example, I change the z scale to 5, x and y is 1, then as a result, the
    head will be shown as multi-copy head like when it is rotated in some angle, you can see the screenshot below, but when the xyz scale to the same size will not happen.
    So, could you tell me why this will happen and how to fix it? Cache.png
     
    Last edited: Aug 3, 2015
  50. brianasu

    brianasu

    Joined:
    Mar 9, 2010
    Posts:
    369
    This is caused by unity baking the scaling of the mesh. This messes up the local vertex coordinates.