Search Unity

[WIP] Dynamic Terrain Shaders

Discussion in 'Made With Unity' started by Broken-Toy, Nov 15, 2010.

  1. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    Good evening,

    Are heightmap fields getting old for you? I sure am looking for something new for terrain. So I have been tweaking some of the incredible shaders found on the Unity Community forums. The two shaders are a triplanar projection shader and a vertex color shader with a clamped alpha blend. What I came out with is a pretty flexible shader that can be applied to arbitrary meshes.

    Extra textures are added on the top of the terrain depending on vertex color. In the demo only the red channel is used, but I tested the green/blue/alpha as can be seen in the pizza-like screenshot below:

    $ArtisticShot.jpg

    Something interesting to note; all textures are 256x256, yet it remains very clean. As you can see, layers are placed ontop of one another in a constant order (r-g-b-a). I plan to create a tool to carve terrain models, save them as terrain definition files, and have the vertex colors interact with different colliders (water, buildings, trees, etc) if it proves to be possible.

    Triplanar Shaders (Done) Webplayer | Buy on Asset Store

    == UPDATES ==
    (12/10/2010) - Implemented marching cubes from the wiki; I will call them marching blobs for now, due to the way they behave.: http://www.brokentoygames.com/post/2150736331/fun-with-terrain-cont-marching-blobs

    (03/23/2011) - Set some aside time to work some more on it, ditched the MCBlob algo and rolled a true marching cubes one... working on mesh optimization and getting the normals to smooth properly.

    (03/27/2011) - Normals solved. Optimized the mesh so it stops duplicating vertices...

    (04/15/2011) - Shaders for Windows now on the Asset Store: http://u3d.as/content/broken-toy-games/triplanar-texturing/1Ds
    This works on Windows; there is a known issue with certain Mac hardware configurations where the projection techniques show black triangles.
     
    Last edited: Oct 1, 2011
  2. DavidB

    DavidB

    Joined:
    Dec 13, 2009
    Posts:
    530
    Wow this is absolutely amazing. I definitely need to jump into some shader stuff lol. Fantastic work. I'm positive there will be demand for this :p

    Great job!
     
  3. Hans

    Hans

    Joined:
    Feb 20, 2007
    Posts:
    422
    that is ausome, i would diffently use this
     
  4. Geenz

    Geenz

    Joined:
    Sep 1, 2010
    Posts:
    99
    For a second there, I thought this was a Unity implementation of Eric Lengyel's voxel terrain techniques :p
     
  5. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    Although it's not there yet, I'm definately looking into voxel techniques.
     
  6. jedy

    jedy

    Joined:
    Aug 1, 2010
    Posts:
    579
    Excellent!
     
  7. Geenz

    Geenz

    Joined:
    Sep 1, 2010
    Posts:
    99
    I would recommend looking into this: http://www.terathon.com/lengyel/Lengyel-VoxelTerrain.pdf

    It's the technique used in the C4 game engine. It provides examples of how to handle LOD, cube marching, and how it handles texturing as well on the terrain (the texturing approach is quite similar to the tri-planar approach you're using, only it projects based off of a cube).
     
  8. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Very nicely done. I look forward to watching your progress on this. As a side note though, and this has nothing to do with what you've done but, boy, the Unity shadows sure look ugly in this scene. Very rough, pixelated edges and on the vertical wall the shadow flicker is pretty nasty. You're using Unity 3, right? Have you tried using 2.6.1 instead?
     
  9. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    @bigkahuna
    Unfortunately I am only able to keep my sanity in programming shaders through Unity 3's simplified language.
    As for the shadows, yes, it's UT's problem to solve. If we can show more samples of where it creates ugly artifacts in common game situations, maybe it'll help push that fix upwards in their priority list.
     
  10. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Yes, let's hope so. Unfortunately for me, U3 is unusable so all I can do is build with 2.6.1 or wait for U3 to be fixed. :(
     
  11. the_gnoblin

    the_gnoblin

    Joined:
    Jan 10, 2009
    Posts:
    722
    bigkahuna, could you provide a small example scene where shadows are "unusable"?
    thanks
     
  12. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Actually, U3 is unusable because of an intermittent crash bug (Windows 7), loss of some features, and the shadow quality is inferior to 2.6.1 IMO. Consequently I'm sticking with 2.6.1. BTW, bug reports have been posted.
     
  13. Tinus

    Tinus

    Joined:
    Apr 6, 2009
    Posts:
    437
    Holy smokes, that's excellent! A definite 'DO WANT' from me. :)
     
  14. Samsson

    Samsson

    Joined:
    Oct 12, 2010
    Posts:
    165
    I would like to know when you will submit it in the asset store ?
     
  15. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    When It's Done(tm)
    2.0 Shaders should happen soon enough though.

    The C4 engine is what first got me inspired to do this, its terrain implementation is downright awesome :) I'm defintely going to read that paper, and have actually been messing around with some marching cubes algorhithm I found laying about.
     
    Last edited: Nov 16, 2010
  16. kurylo3d

    kurylo3d

    Joined:
    Nov 7, 2009
    Posts:
    1,123
    very cool stuff. This is one of the reasons I love unity so much... a community of motivated individuals like yourself improving thigns.
     
  17. LamentConfig

    LamentConfig

    Joined:
    Sep 28, 2010
    Posts:
    292
    Really would love to see what is happening with this now :)
     
  18. lochlainn1066

    lochlainn1066

    Joined:
    Nov 22, 2010
    Posts:
    12
    Go for it! A voxel terrain system like Lenyel outlined in that paper would be a must have!

    You're doing great so far, keep it up!
     
  19. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    Update: I've tried implementing the marching cubes algorhithm from the wiki; works great with the shaders!

    I see the marching cubes as a "rendering" method for a future use of voxels, nothing more... but it needs to work correctly before going further.
    While I've found a scripted one that works nicely (it does put missing triangles where needed), I need to get the "blob accumulation" behavior straightened out before binding it to any form of voxel management.



    Longer explanation in my blog:
    http://www.brokentoygames.com/post/2150736331/fun-with-terrain-cont-marching-blobs
     
    Last edited: Mar 14, 2011
  20. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    Hi,

    I'm just posting this to tell you the project is still alive (working on and off on it), however I'm hitting a stumbling block with the marching cubes algorhithm.

    Looking for more solutions I posted on the Thermite/PolyVox forums as well for assistance:
    http://www.thermite3d.org/phpBB3/viewtopic.php?f=2&t=149

    The Unity-relevant part, for your convenience:
     
  21. PolyMad

    PolyMad

    Joined:
    Mar 19, 2009
    Posts:
    2,350
    I love this stuff... any webplayer available?
     
  22. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    Not at the moment. As I mentionned in the post just before yours I'm still trying to get it to work properly.
     
    Last edited: Feb 27, 2011
  23. tomnullpointer

    tomnullpointer

    Joined:
    Sep 20, 2010
    Posts:
    142
    Hi were,

    Nice work!

    I think I can help a little with the code issues. What I ended up doing was junking the Metaball code completely and re-implementing the functions from info available on Paul Bourkes site. The metaballs code is cool and fun but its designed to fill the volume density cube every frame, and does so using a set of spherical range checks to make values linked to the centre points of the metaballs. The actual skinning functions in the script you have been using are pretty general, but the metaball stuff is probably throwing you off track a little. I'd suggest checking out a few other implementations in c/ c++ so you can see what bits you actually want. Theres a great nvidia gpu gems article on the same process that might help too.

    Mines starting to look a bit like this now
     
  24. Frank Oz

    Frank Oz

    Joined:
    Oct 13, 2010
    Posts:
    1,560
  25. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    I redid the site a while ago, the correct link would be: http://www.brokentoygames.com/post/1465408734/fun-with-terrains

    @tomnullpointer: I have been looking far and wide on the net for a while, found at least three implementations in C++ (including the ones you pointed at), and finally understood and got a translation working in Unity. This is a sphere function but properly translates to a voxel grid. (Still need to integrate a voxel drawing tool...). I'm currently looking for a way to better smooth the normals.

     
    Last edited: Mar 24, 2011
  26. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    Normals solved. As a related bonus, I also figured out how to optimize the procedural mesh.

    Here's a little test. The scenes are being affected by one directional light, one point light and one spot light, all with maxed shadows. A moderate number of objects are added to the mix.


    Shader Model 2.0, Forward Lighting


    Shader Model 3.0, Deferred lighting, SSAO

    One of the spheres is a primitive, the other is made of marching cubes. Guess which one.

    Next: Making a voxel drawing tool.
     
    Last edited: Mar 28, 2011
  27. Frank Oz

    Frank Oz

    Joined:
    Oct 13, 2010
    Posts:
    1,560
  28. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    There is not much to see in action until I'm done with the terrain brushes. ;)
     
  29. Frank Oz

    Frank Oz

    Joined:
    Oct 13, 2010
    Posts:
    1,560
    Bah, you tease! :D
     
  30. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    Some more teasing :p (Shader work complete)



    Making the boatload of terrain textures was pretty fun, too.
     
  31. janpec

    janpec

    Joined:
    Jul 16, 2010
    Posts:
    3,520
    How are you able to paint textures on such a step terrain? You have almost 90 degree angle of terrain surface wherre you have that dark texture. how that it isnt stretched at all?
     
  32. PolyMad

    PolyMad

    Joined:
    Mar 19, 2009
    Posts:
    2,350
    It's called "triplanar projection". The shader takes care of projecting in the right direction the texture based on the face normal and also blends on the edges :)
    It's another of the main great features of the tool.
    No offense, but I wonder what exactly are those 30 coders of Unity doing all the time... there's so much stuff from guys around but the main Unity tools are starting to feel old... since some time.
     
  33. U2

    U2

    Joined:
    Aug 12, 2008
    Posts:
    216
    Hey,

    You might consider just releasing your shaders for the terrain / objects to the asset store and the voxel system separately. I would be interested in purchasing a terrain shader like that as is right now if it was available.
     
  34. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    The shaders, along with sample textures and terrain mesh, have been uploaded to the Asset Store and are pending approval!
     
  35. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    Okay, there seems to be an issue with the shaders and Caitlyn's Mac, does anyone of you who is a Mac user can report (PM or here) whether it works for them?
    $TerrainMacIssue.jpg $TerrainMacIssue.jpg
    I added a webplayer here to test it out.

    Thank you.
     
    Last edited: Apr 12, 2011
  36. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Works here (MacBook Pro, x1600 graphics, OS X 10.6.7).
     
  37. RandAlThor

    RandAlThor

    Joined:
    Dec 2, 2007
    Posts:
    1,293
    Here are some parts working.
    But i have black parts on the landscape and on the rabbit.
    I have taken a screencapture and can mail it if you want.

    Mac OS X 10.6.7

    iMac Core 2 Duo 2.66
    4 GB Ram
    NVIDIA GeForce 9400 256 MB 32 bit
     
    Last edited: Apr 12, 2011
  38. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    Thanks, yes, please send the screen capture :)
     
    Last edited: Apr 12, 2011
  39. Hintze

    Hintze

    Joined:
    Nov 6, 2009
    Posts:
    40
    after ~0.2 sec the terrain plus the bunny goes black.


    Mac OS X 10.6.7

    iMac Core 2 Duo 2.66
    2 GB Ram
    ATI radeon 2600PRO 256 MB
     
  40. Falin

    Falin

    Joined:
    Sep 29, 2009
    Posts:
    242
    works fine on my iMac

    iMac i5 2.66
    4GB Ram
    Ati Radeon HD 4850
     
  41. RandAlThor

    RandAlThor

    Joined:
    Dec 2, 2007
    Posts:
    1,293
    Oh, did not see that you have a picture with the black color (was on mobile and now at home).
    Mine looks about the same so i think you do not need the screencapture from me anymore.
     
  42. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    Thanks for your help guys!

    As I'm suspecting the problem could also have something to do with the imported obj meshes (I get import errors on the terrain and stanford bunny), I added a basic sphere primitive to the scene to get a better idea of where the problem might stem from.

    http://goo.gl/QYbJX
     
    Last edited: Apr 12, 2011
  43. RandAlThor

    RandAlThor

    Joined:
    Dec 2, 2007
    Posts:
    1,293
    So now i looked at the new scene and there everything that is made out of polygons is black and the background blue.
    I think you can imagine that, so i do not send the capture again.

    My home Mac

    Core 2 Duo 2.4 GHz
    2GB Ram
    ATI RadeonHD2600 256 MB
     
  44. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    Alright, thanks. I'll send Unity a bug report.
     
  45. holyjewsus

    holyjewsus

    Joined:
    Mar 7, 2011
    Posts:
    624
    All is black for me too macbookpro nvidia 320m
     
  46. lucidcoder

    lucidcoder

    Joined:
    Mar 23, 2010
    Posts:
    138
    When (or if) you manage to get this working, I'll be sure to put it in my current project. Absolutely outstanding job.

    The internet connection in my classes won't allow me to visit the asset store--I'll get a chance to check tonight, but in case I don't (or someone else knows sooner), what is the price tag on the tools for making these terrains?
     
  47. Deleted User

    Deleted User

    Guest

    Cant wait to try it :) Hope for a free version or somekinda indi license.
     
  48. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    Thanks! I am looking at what I can do on my end, but shader support on different platforms is handled internally by Unity's shader compiler. I sent a bug report over to UT, we'll see what comes out of that.

    I used Sculptris to sculpt the terrain mesh seen in the webplayer demo. Very user-friendly, powerful and flexible sculpting software, which you can get for free at http://www.sculptris.com/.

    Note that I have no terrain tools included at the moment. I am pushing the shaders out sooner because there seems to be demand for just the shaders, and I can understand why. Many developers don't need in-editor terrain/modeling tools thanks to the external artistic software they already use or the model style they are going for, which is why I think just the shaders as a package makes sense, for them at least. I think it should also be possible to override the Unity terrain shaders with mine.

    My main project is an in-game voxel drawing tool for which the shaders are very important, but it's not presentable yet.

    Finally: The price won't be overly expensive but will obviously take the "outstanding job" into account.
     
  49. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    Last edited: Apr 16, 2011
  50. tomnullpointer

    tomnullpointer

    Joined:
    Sep 20, 2010
    Posts:
    142
    Hi all, The shaders look pretty good Were!, but i just felt I should point out that you can get a similar (but simpler) effect using the built in triplanar shader from the free strumpy shader editor. These arent in any way as detailed as weres versions - in that they lack the vertex color aspect and some otther features. But I just thought Id point it out to anyone whos low on cash but doesnt mind doing a bit more work with downloading and tweaking stuff.