Search Unity

  1. Engage, network and learn at Unite Austin 2017, Oct 3 - 5. Get your ticket today!
    Dismiss Notice
  2. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  3. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  4. Unity 2017.1 is now released.
    Dismiss Notice
  5. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  6. Unity 2017.2 beta is now available for download.
    Dismiss Notice

Triplanar Projection Shaders on the Asset Store.

Discussion in 'Assets and Asset Store' started by Broken-Toy, Apr 16, 2011.

  1. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    On the journey to make a volumetric terrain tool, I had to develop some very flexible shaders to fit textures on meshes that will be built at runtime and can literally take any form.

    Since there was demand for the shaders alone, I put them for sale on the Asset Store here

    [​IMG]

    The shaders use a technique called triplanar texture projection. An example may be seen in this webplayer, or on the Unify Wiki. The Strumpy Shader Editor also includes an example node graph.

    Update: Added a special version of the shader that lets you put different textures on all faces on moving objects. Related to this, I did some adjustments of the bumpmapping so it looks nicer.

    Local Projection has the added benefit that the mesh can be moved, rotated and scaled while the texture sticks in place.

    How to use:
    1. Assign the Shader to your mesh
    2. Assign the Textures you want
    3. Set the Tiling
    4. Enjoy!

    Also included:
    • A set of base textures to get you started (You can also get the textures here).
    • Shaders with texture blends based on mesh vertex color (5 textures supported).
    [​IMG]

    Test the webplayer | Purchase on Asset Store

    Note: Supported for Windows and Android. Not tested on Mac/iOS
     
    Last edited: Jun 5, 2014
  2. WebWolfRussian

    WebWolfRussian

    Joined:
    Feb 5, 2010
    Posts:
    123
  3. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,327
    Testing in OS 10.6.7:

    In fullscreen, on a GeForce 8600M GT, I see terrain and the bunny in fullscreen, but there are still black smudges all over everything. With a Radeon HD 6750M, it seems to work correctly in fullscreen. However, when not in fullscreen, everything is just black, as shown in the screenshot, with both GPUs.

    Edit: I see that you noted this on the link in your thread. I'd recommend specifically selling this for Windows.
     

    Attached Files:

    Last edited: Apr 17, 2011
  4. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    Yes, it's already noted in the asset store description: supported on windows, no guarantee on Mac. I added the notice in the OP, thanks. :)
     
  5. angel_m

    angel_m

    Joined:
    Nov 4, 2005
    Posts:
    1,012
    The terrain version is (also) for Unity terrains, isn´t it?
     
  6. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    I haven't tested it yet, although in theory it should be possible to override the first layer of terrain shaders with my basic terrain shader.

    You'd still need to somehow paint vertices for the advanced version, and I have no idea how distance LOD will react to that or if vertex color can be saved on Unity terrain at all.
     
  7. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,596
    unity terrains have no vertices to store anything on or a mesh you could access, they are dynamically generated from the heightmap.
     
  8. Majatek

    Majatek

    Joined:
    Apr 17, 2011
    Posts:
    19
    Really well done! Quite optimized too - I've tested this on two machines, and it maintains a high frame rate even on a PC limited to 2GB of RAm and a 2GHz single core CPU.
     
  9. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    If you need even more horsepower, I added even simpler versions to fallback to, should you need them.
     
  10. DukeOfDesmo

    DukeOfDesmo

    Joined:
    Oct 29, 2008
    Posts:
    67
    Is there any chance of having a triplanar-bump texture with a specular pass please? :D
     
  11. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    Sure, I'll look into that.
     
  12. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,032
    Werewraith, you mention on the asset store page "If more terrain detail is needed, combine up to five textures using vertex colors." How does that work? And for the initial 2 textures like in your example (grass/cliffs), is that textured procedurally?
     
  13. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    3,613
    doesn’t work for me on mac either,…

    lars
     
  14. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    It works using vertex colors, which can be either painted in a modelling app or with reissgant's Vertex Painter extension.

    It is also possible to procedurally color vertices through script.
     
  15. 2dfxman

    2dfxman

    Joined:
    Oct 1, 2010
    Posts:
    178
    I don't really understand what this does tbh. Does it just use vertex color to define where to put texture and then just tile uvs or is there something more? So far I don't see any projections. Please explain to a stupid person :)
     
  16. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    Indeed, it just uses vertex color to determine which texture to display per pixel, much like a splat map for terrain, but the color information is stored in the vertices instead of a terrain texture. See reissgant's Vertex Painter for more indepth uses of this technique. I merely made a shader compatible with his sytem.

    Also: The shaders are now 50% off.
     
  17. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    Update: Added a special version of the shader that lets you put different textures on all faces on moving objects. Related to this, I did some adjustments of the bumpmapping so it looks nicer.
     
  18. ArenMook

    ArenMook

    Joined:
    Oct 20, 2010
    Posts:
    1,902
    How many texture samples do you have in your bump mapped shader? Is it the brute force approach of 12 samples, or an optimized approach of 6? Also did you happen to resolve the issue with Unity's dynamically batched geometry not playing well with the scaled object-based texturing?

    P.S. Why is it Windows-only? I had to create a shader to tri-planar texture asteroids for one of my projects and it seems to run just fine on the Mac, iOS, and Android.
     
  19. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    I just updated the shaders to use 12 samples for bumpmapped versions because I noticed lighting inaccuracies on "backwards" projections with 6 samples, most notable when rotating meshes. The bumpmapped shaders already fallback to the diffuse-only version that uses 3 texture samplers when it's on a SM 2.0 card by the way.

    For batched geometry, anything using world-space projections will display just fine even when batched. When using local-space projections, batching meshes will put them all in the same local space so they will display the texture as if they were one big object (which they are, as far as the shader is concerned). Also, with local space, it's a good idea to input a slight scale offset in one axis (something as small as 0.0001 works) so the tiling happens at the correct scale.

    It's just me applying business ethics. Since I don't have access to a Mac nor to the iPhone and Android licenses, I cannot debug on those devices so cannot in good faith pretend that it will work there. It works on most Macs but I got bug reports about black artifacts on certain specific configurations, mostly older ones where the graphic card handles tiling textures differently. It can likely be circumvented in your Unity texture import settings, by not adding mipmaps.
     
  20. ArenMook

    ArenMook

    Joined:
    Oct 20, 2010
    Posts:
    1,902
    You can keep it at 6 if you flip the normal on negative sides. The only other tricky part is to sample the textures in such a way that no two sides have tangents pointing in the opposite directions, which I resolved in my case by ensuring that tangents always point down on the sides.
     
  21. synapsemassage

    synapsemassage

    Joined:
    Jul 27, 2009
    Posts:
    312
    Did anyone try this shader on iOS?
     
  22. virror

    virror

    Joined:
    Feb 3, 2012
    Posts:
    2,914
    Only works in windows.
     
  23. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    They are supported in windows, although I'll soon test on Android as well.
    I don't have access to a Mac nor an iOS device, so can't test or debug on those.
     
    Last edited: Aug 8, 2012
  24. WarpedAxiom

    WarpedAxiom

    Joined:
    Oct 27, 2006
    Posts:
    245
    I would buy this in a heart heat if I could fix this:
    [​IMG]
    MacBook Pro, NVIDIA GeForce 320M.
    Is it possible?
     
  25. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    Looking at your screenshot I think I have an idea of what's happening... but having no Apple products I can't check this myself. Looking again into what can be done about this.

    Alright, those of you with Macs, please have a look at this webplayer. It contains the basic shader ("Control") and tests for Mac. Screenshots would be appreciated.
     
    Last edited: Oct 20, 2012
  26. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,428
    All your webplayers have been working fine here. I'm on an old Macbook Pro with ATI X1600 graphics and OSX 10.6.8.

    After reading through this thread I'm still confused on one thing, will this work on Unity terrains (Unity version 3.5.6, target player would be a Windows stand alone)?

    $Screen shot 2012-10-20 at 5.35.07 AM.jpg
     
  27. Hynopsis

    Hynopsis

    Joined:
    Oct 25, 2012
    Posts:
    53
    I am having an issue with this shader. I am setting the vertex colors manually at runtime while creating procedural terrain. All of the vertex blended shaders seem to only display the alpha channel regardless of what color I am setting the individual vertices to. Using other vertex shaders, it works just fine and by changing the colors it changes the textures. These other test shaders do not include an alpha channel though, is there something I am missing...?
     
  28. Hynopsis

    Hynopsis

    Joined:
    Oct 25, 2012
    Posts:
    53
    I get it know...total noob but figured it out

    colors[v] = Color.green;
    colors[v].a = 0;

    Have to set the alpha channel to 0...now that I know that this shader is even better, great purchase indeed...

    - Now I can seem to get it working...

    This works with the other RGBA vertex color shaders but not this one...strange...I got the other one working,
    went back to this one and it doesn't work. I'm using Unity 3.5 does that make a difference?

    [Correction] Working now...

    There was my problem...comparing it to other shaders...

    Setting all color values to null will show the "Top" Texture...

    colors[arrayIndex].r = 0f;
    colors[arrayIndex].g = 0f;
    colors[arrayIndex].b = 0f;
    colors[arrayIndex].a = 0f;

    From there altering the color values allows a blend (found .4 works well), or setting to 1 which overrides the "Top" texture. I am creating procedural terrain, so when I alter the height values with noise I use the height value to
    determine what the vertex color will be set to, and then slowly blend them to other colors...it works great. Sorry, but I thought this might be useful for the other shader noobs out there...

    Also for those doing the same...when setting vertex colors by height...try adding some randomness to the color placement.

    if (heightOfVertex <=50){

    var random = Random.Range(0,9);

    if(random <=3){ - leave as "top" texture
    colors[v].r = 0f;
    colors[v].g = 0f;
    colors[v].b = .0f;
    colors[v].a = .0f;
    }
    else{ - blend another texture set to the blue channel 40% of the time...
    colors[v].r = 0f;
    colors[v].g = 0f;
    colors[v].b = .4f;
    colors[v].a = .0f;
    }

    I have been setting low texture(sand), then fading to sand and grass, then grass with random blends, then just grass, etc...it produces a nice feathered blend of texture to get away from the sometime too obvious height assignment....

    Between the top texture, blending or overriding with the other textures, setting some randomness, and of course the angled cliff texturing, this shader can produce some amazing results - Again GREAT PURCHASE...

    Edit Note : If anyone every reads this use colors[v] = colors[0,0,0,0] instead and upgrade to version 3.5.6 of Unity to use the new Color32 which is 3-4 faster...
     
    Last edited: Oct 29, 2012
  29. Hynopsis

    Hynopsis

    Joined:
    Oct 25, 2012
    Posts:
    53
    Okay so there vertex blend shader seems to work fine as I set the vertex colors, however, the clamp version with normal maps isn't working right. No matter what I set the color to it only displays the alpha texture, even when alpha is set to zero. I have the colors stored as an separate array and can clearly see them changing in the inspector via the terrain generator code...but in the game it doesn't seem to matter. I was using 3.5...didn't work...then upgraded to 3.5.6 and still doesn't work. Any ideas would be appreciated...since normal maps on terrain make an enormous difference.

    I am not using pro version...does that make a difference?
     
    Last edited: Oct 29, 2012
  30. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    Unfortunately it's not working on terrain - yet. That will require a specific terrain shader replacement using splat map color instead of vertex color, so should be very doable. In fact, I think someone did and released a terrain version for free last year; might be why I didn't see fit to redo the same thing. I should see about improving it with alpha clamping instead of linear blending though. That must be a sight to behold!

    @Hypnosis: I'll test the bumpmapped version again tomorrow morning and get back to you with it. It used to work just like the basic one by making sure that your vertex has all channels at zero except for the one you want to use. If it was a card incompatibility I believe it would just show unlit pink instead of the actual textures.

    Your experiments are very useful indeed. I'll play with them too and add them to the 'documentation' for everyone, if that's okay with you.

    Also: I'm currently selling at 33% off until halloween. Then it's back at 30$!
     
    Last edited: Oct 29, 2012
  31. 3dDude

    3dDude

    Joined:
    Jul 4, 2010
    Posts:
    1,064
    Works fine on my machine.
    Macbook pro retina
     
  32. Hynopsis

    Hynopsis

    Joined:
    Oct 25, 2012
    Posts:
    53
    Sorry I didn't clarify...I am using the standard unity planes for the terrain, not the built in terrain due to the various limitations...Like I said the non-bumped vertex shader works perfectly, but the bumped version just shows the alpha textures regardless of the vertex colors that I set. Sorry for the confusion and thanks for the quick response. As you can see below it is shaping up alright even just using basic unity assets, but you can imagine the difference the bump maps would make...
     

    Attached Files:

    Last edited: Oct 30, 2012
  33. Hynopsis

    Hynopsis

    Joined:
    Oct 25, 2012
    Posts:
    53
    So here you can see I have the edges of the plan vertices and their colors stored, so I can can make them and the normals the same at the edges...there are a variety of colors set but it only shows the alpha which is a sand texture even though all other texture slots are filled...maybe it works differently than the other shader...if so let me know...thanks again hope this helps...
     

    Attached Files:

    Last edited: Oct 30, 2012
  34. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    Ok. I think I figured it out. I had the same issue with textures without alpha info. The shader uses the vertex color value for alpha clamping of textures so if your texture has no alpha channel, it'll be white by default and cover everything regardless of the vertex color. The textures I provided in the package have an alpna channel, otherwise they would cover everything in snow.

    An important detail which I didn't mention is that the vertex colors have a priority order. It first puts the 'no vertex color' texture, then covers it with red, green, blue and alpha in that order.

    Alpha has highest priority so will cover everything else as soon as it's present.
    Then blue, green, red, and the final texture makes sure there is something decent to look at when no vertex color is used.
    Think of them as photoshop layers, with alpha layer being on top and the final texture being an always-on background

    Here are screenshots of my (earlier) work with this bumpmapped shader using randomized vertex colors
    I randomized each vertex using one of five possible cases:
    Background: [0,0,0,0]
    Red: [1,0,0,0]
    Green: [0,1,0,0]
    Blue: [0,0,1,0]
    Alpha: [0,0,0,1]

    In both screens I used the snow texture in the alpha layer so it covers things at highest priority.

    $TerrainPizza.jpg
    $TerrainPizza2.jpg

    Therefore, make sure alpha is always at zero if you don't want to see it at all. Also make sure your textures have variable information in their alpha channel (not white everywhere).
     
    Last edited: Nov 1, 2012
  35. Hynopsis

    Hynopsis

    Joined:
    Oct 25, 2012
    Posts:
    53
    Thanks for the input, you are right it is just that my textures are missing alpha channels. I was confused since the other non-clamped shader refers to the alpha channel but it must compensate if it doesn't have one. Long story short, when I use your provided textures it works just fine so the problem is on my end. Thanks for the help and the quick response.
     
  36. napster

    napster

    Joined:
    Jun 15, 2012
    Posts:
    313
    Looks good and superfine, would be great if anybody test this on iOS too !
     
  37. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    A quick and dirty way to get an alpha channel is to open your image in an advanced image editing program like gimp or protoshop, make a layer mask using a grayscale version of your texture then apply that as an alpha channel. Just make sure to save it as a png after (jpg doesn't support transparency).
     
  38. pvloon

    pvloon

    Joined:
    Oct 5, 2011
    Posts:
    526
    Just passing through, but:
    Sounds like you can also just check 'Alpha from grayscale' in the texture importer settings, does the same thing, no need for an editor.

    Cheers :)
     
  39. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    Cool beans!

    Thank you everyone for your feedback and suggestions. It's appreciated.

    To give you an idea of possible applications: I worked some variations into the projection shaders for Planet Clay (more an interactive 3D app than a game at this point, but the line is blurry) which I'm working on.

    The first is a faked noise blend; it takes a detail texture (noise or anything you like) as a color multiplier to make tiling much less of an issue at great distances (close to planetary scale). It's much faster than manually calculating noise through the shader.

    The second is a 3d cloud skybox; it animates a seamless texture across projections, the effect being enhanced by transparency and some additive blending. Again, kept as simple and fast as possible on an integrated laptop card.

    $v0.0.1_SkyNoon.png
     
    Last edited: Nov 4, 2012
  40. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
    Just a forewarning: I'll be updating the Triplanar Projection Shaders to Unity 4 at the end of next week (nov. 22nd) so if you need a Unity 3 version, get it now and make a backup.

    Edit - no new version will be required for now, as nothing broke when shifting to Unity 4. If you have any issues, do tell me.
     
    Last edited: Dec 7, 2012
  41. nirharpaz

    nirharpaz

    Joined:
    Jun 3, 2014
    Posts:
    1
    i tried to look for it in the asset store but the links only gave me error - as for page not found.
    can you please share an updated link?
     
  42. Broken-Toy

    Broken-Toy

    Joined:
    Jan 16, 2010
    Posts:
    455
  43. Hynopsis

    Hynopsis

    Joined:
    Oct 25, 2012
    Posts:
    53
    Is this being updated for unity 5? This is a great shader, been using it forever, but appears broke in Unity 5 - just pink textures.