Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Shader Forge - A visual, node-based shader editor

Discussion in 'Assets and Asset Store' started by Acegikmo, Jan 11, 2014.

  1. zachwithanh

    zachwithanh

    Joined:
    Oct 20, 2014
    Posts:
    3
    Sorry to add to the long thread, but I've stumped myself. How would I go about animating UVs over time in a wave type pattern? I tried using two panner nodes in opposing directions but can't seem to combine them to get the pattern I need. Any ideas?
     
  2. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,294
    This should help:
    http://acegikmo.com/shaderforge/wiki/index.php?title=Waveforms
     
  3. Ashinokami

    Ashinokami

    Joined:
    Oct 10, 2014
    Posts:
    3
    So I think I know what causes it now, just need a workaround.
    I change water color and opacity using Depth Blend node, but it isn't affected by refraction. So I need to get that (SceneDepth - PixelDepth) info in texture format, I could distort UVs of. As I use mostly deffered shaders in the scene, camera renders z-buffer pass, and I'd like to grab it. I added texture node with name _CameraDepthTexture, but it doesn't pick anything up.
    I'd be glad for any ideas how to accomplish that.
     
  4. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,083
    As far as making things visible behind other things is concerned, it's actually pretty simple. First, you'll want to have two materials on your mesh renderer. Just set the size in the materials section to two, then you'll want to play around with your sorting options for the second material. I use something like this:



    Finally, when all is said and done, you should have a character visible behind walls. This example looks something like this:



    The occluded part of the geometry appears with a fresnel effect fading off into alpha, while the non-occluded part is a solid colour. Obviously, this has some drawbacks (alpha sorting being an obvious one) and you'll need to take care with what is and isn't added to the depth buffer so small geometry details aren't a factor, but yeah.

    edit: with some scripting and special trickery, you could even use this to simulate water depth effects in Unity Free. Again, not perfectly, but better than nothing.
     
    Last edited: Nov 26, 2014
    PrawnJeremy likes this.
  5. zachwithanh

    zachwithanh

    Joined:
    Oct 20, 2014
    Posts:
    3
    Thanks! I added the sine wave but I don't know which node to use in order to move the UVs in that pattern. Hooking it into the dist. input on the panner node doesn't seem to be correct. Sorry I'm fairly new to shader authoring!
     
  6. PrawnJeremy

    PrawnJeremy

    Joined:
    Aug 20, 2013
    Posts:
    35
    Wow, thanks, I will certainly give it a try and see what I can come up with. I have Unity Pro but water depth is something I would like to get sorted out as well, specifically I have been looking at volumetric fog on the asset store, problem is I would want to use it with Dynamic Water and that is just a displaced plane, it isn't a solid volume. The other issue with it is that is is deforming so the volumetrics would need to follow the water surface. It is a very common effect but I don't yet have a way to handle it. Here is an example of the effect in Donkey Kong. It may actually not be volumetric but there is no way to create fog like this in Unity out of the box either and I am not sure how to attempt it.

    You can see some water sections in my game by going to PrawnJeremy.com and checking out the second video. Right now I just have the BG pop in and out based on camera being in or out of water but would love to have something like the DK setup. Thanks again for your material blend answer.

     
  7. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,083
    That's actually a lot more simple than you'd think. Just have it so your water is an enclosed volume rather than just a plane and boom, done.
     
  8. redstudio

    redstudio

    Joined:
    Nov 21, 2014
    Posts:
    9
    How one can create stereoscopic 3D shaders for different 3d formats like anaglyph, checkerboard, top-bottom, side by side etc. I know we have to use render to texture but how to use it in shader forge? How to combine two of such RT in one shader to get result as mentioned above?
     
  9. PrawnJeremy

    PrawnJeremy

    Joined:
    Aug 20, 2013
    Posts:
    35
    Aye, I have asked the developer of the Dynamic Water system on the Asset Store to include the option to generate a solid volume. Didn't get a response, guess I will try again :)
     
  10. dranelol

    dranelol

    Joined:
    Jan 24, 2014
    Posts:
    5
    Hi again. I've done some tweaking, and I think I'm on the right track of the effect I want.

    I've come up with the following nodes for the "black hole" effect:


    Here's what it looks like, on a simple quad placed in the scene:



    Here's the texture I'm using for the diffuse/alpha values:


    The sine wave just twists the refraction back and forth over time, I used that for testing.

    This is what I'd like help changing: See how the edges of the quad are very obviously visible? I'd like the effect to only be visible in the center of the quad, and have the edges of the quad be 100% transparent; the effect should only distort/warp the center of the quad. The quad in-scene will always face the camera, so that won't be an issue.

    The worst part is, I actually achieved this exact effect, randomly plugging in stuff into the main values! I distinctly remember using the fresnel node to get exactly what I wanted. I forgot to commit my work that night, lost it, and now I can't remember how I did that at all.

    Any ideas?

    e: Also, when I re-built the shader, the texture going into the diffuse/alpha values was a new addition as well, so that very well may have been how I achieved the desired effect (plugging something else into those values).
     
    Last edited: Nov 26, 2014
    IFL likes this.
  11. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,083
    Just going out on a limb here, but try plugging the R channel into the alpha instead of the A channel.
     
  12. dranelol

    dranelol

    Joined:
    Jan 24, 2014
    Posts:
    5
    Hm, that didn't seem to change anything.

    e: Well, not anything with respect to my problem. It did remove the dark gradient from the center.
     
  13. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,083
    OH! Okay, so one of my suspicions was confirmed. First up, select your diffuse texture in the Unity inspector and make sure it's getting the alpha from the alpha channel. There should be a checkbox for that. Then try plugging A back into Alpha. If it still just eliminates the centre bit, stick a One-Minus node between it and the alpha node and you should be set!

    EDIT: I AM A BIG DUMMY but also I solved the problem.

    Peep this:

     
    Last edited: Nov 27, 2014
    IFL likes this.
  14. IFL

    IFL

    Joined:
    Apr 13, 2013
    Posts:
    408
    Acegikmo likes this.
  15. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,083
    This coupled with a smokey particle effect that draws inwards would make for a really slick effect, I think.
     
    IFL likes this.
  16. PrawnJeremy

    PrawnJeremy

    Joined:
    Aug 20, 2013
    Posts:
    35
    I am not seeing any way to get rid of self shadowing, I presume it is because the Light Attenuation node is light falloff and shadows combined. Are there any plans to have the node split so people can get rid of self shadowing? it seems to be a pretty common thing for people to want to do.
     
  17. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,083
    Is there a specific effect you're trying to accomplish? Depending on what it is, I may have tried to do something similar before to some degree of success.
     
  18. PrawnJeremy

    PrawnJeremy

    Joined:
    Aug 20, 2013
    Posts:
    35
    I want to have the character accept and cast shadows but I do not want it to self shadow because it looks terrible when I point a directional light straight down. I am going for a look similar to Super Mario 3D World.
     
    rakkarage and IFL like this.
  19. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,083
    Okay yeah, this is actually a long-standing problem in Unity. The only real way around it I've found is by disabling receiving shadows on meshes you don't want to self shadow, then using shadow volumes to calculate shadows from things like level geometry and whatever objects you want to cast shadows. This comes with the following problems, however:
    1. A pain in every ass to implement properly
    2. Requires closed meshes with special geometry to ensure you don't get any ugly shadow gaps
    3. Absolute murder on your fillrate, making it not the best idea ever for things targeting lower end hardware
    I'm sure I could find something resembling a less intensive hack if I poked and prodded enough, but for the time being, that's all I've really found.
     
    PrawnJeremy likes this.
  20. IFL

    IFL

    Joined:
    Apr 13, 2013
    Posts:
    408
    This is what I've come up with, but it requires pro.
    1. Character receiving but not casting shadows
    2. Orthographic camera above character rendering only the character (use layers) to a runtime render texture
    3. Orthographic projector at the character's feet, attached to the character, to project captured image
    4. Modify the projector shader to only project the solid color (with render-texture's alpha) that you want projected
    5. ??
    6. Profit
    I've done it several times successfully. It's not difficult to create a script that manages it all for several characters after you figure out what works for your project.

    [Edited for clarity]
     
    Last edited: Nov 29, 2014
    PrawnJeremy and Murgilod like this.
  21. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,083
    I'm a little confused. Wouldn't this fall apart on models with concave surfaces?
     
  22. PrawnJeremy

    PrawnJeremy

    Joined:
    Aug 20, 2013
    Posts:
    35
    Murgilod: thanks, the best solution I had was to duplicate the character, scale it to .1 on Y and apply a shader to it to make it invisible but cast shadows.

    IFL: that sounds like it could work pretty well, thankfully I have pro.
     
  23. PrawnJeremy

    PrawnJeremy

    Joined:
    Aug 20, 2013
    Posts:
    35
    How would you go about making a Projector shader with Shader Forge? My attempts are failing...
     
  24. IFL

    IFL

    Joined:
    Apr 13, 2013
    Posts:
    408
    Since it's orthogonal, I don't think there's any problem with it..... why would concave surfaces make the projector or camera have problems (except for strictly-transparent, overlapping geometry)?
     
  25. IFL

    IFL

    Joined:
    Apr 13, 2013
    Posts:
    408
    Afaik, it's impossible currently. It's really easy to just duplicate and edit the projector multiply shader though. Change the first three parameters of the return to zero, or something like that. If you need me to resurrect mine, I can do that.
     
    PrawnJeremy likes this.
  26. PrawnJeremy

    PrawnJeremy

    Joined:
    Aug 20, 2013
    Posts:
    35
    I know very little about shader coding which is why I use Shader Forge ;) Please do post your script for reference. This is what I came up with but while it works, I am not sure I did it correctly... specifically I couldn't figure out how to project the alpha from the render texture... so I have a _Mask, which... is actually just a black image... but it works. I have the camera clear to a solid color. Oh, and something else I was looking into was trying to figure out how to blur it to fake a soft shadow.

    I am also running into a problem with this shader not working if the capsule uses a Shader Forge shader, which seems to be related to the alpha. Even a regular unity Diffuse won't work unless I set the Main Color Alpha below 255, which isn't good because I can't do that with my Shader Forge shaders and also makes me feel like I have done something wrong with this shader because 0-254 works but AlphaTest Greater 0 should mean 255 which is greater than 0 would work.



    Code (CSharp):
    1. Shader "Custom/RTSProjector"
    2. {
    3.    Properties {
    4.       _ShadowTex ("Cookie", 2D) = "gray" { TexGen ObjectLinear }
    5.       _Mask ("Mask", 2D) = "white" {}
    6.       _FalloffTex ("FallOff", 2D) = "white" { TexGen ObjectLinear }
    7.       _Tint ("Tint", Color) = (0,0,0,0)
    8.    }
    9.  
    10.    Subshader {
    11.       Tags { "RenderType"="Transparent-1" }
    12.       Pass
    13.       {
    14.          ZWrite Off
    15.          Fog { Color (1, 1, 1) }
    16.          AlphaTest Greater 0
    17.          ColorMask RGB
    18.          Blend DstColor Zero
    19.          Offset -1, -1
    20.  
    21.          SetTexture [_ShadowTex]
    22.          {
    23.             combine texture, ONE - texture
    24.             Matrix [_Projector]
    25.          }
    26.          SetTexture[_Mask]
    27.          {
    28.             combine texture, previous
    29.             Matrix [_Projector]
    30.          }
    31.          SetTexture [_FalloffTex]
    32.          {
    33.             constantColor (1,1,1,0)
    34.             combine previous lerp (texture) constant
    35.             Matrix [_ProjectorClip]
    36.          }
    37.          SetTexture [_FalloffTex]
    38.          {
    39.             constantColor [_Tint]
    40.             combine previous + constant
    41.          }
    42.       }
    43.    }
    44. }
     
    Last edited: Nov 30, 2014
  27. Tony_Beretta

    Tony_Beretta

    Joined:
    Aug 6, 2012
    Posts:
    17
    I'm trying to use the parallax node in a terrain shader but seems doesn't work.
    My mistake or terrain limit?
     
  28. toxtronyx

    toxtronyx

    Joined:
    Aug 21, 2014
    Posts:
    114
    From the Shader Forge Wiki:

    Normal maps won't work, because the terrains do not have tangents. Need a way of calculating perturbed normals that point upwards.

    However I could really need that working since Terrain Shaders in U5 are still weak I'd love to create something more sophisticated.
     
  29. Tony_Beretta

    Tony_Beretta

    Joined:
    Aug 6, 2012
    Posts:
    17
    So this means thath parallax node need tangents?
     
  30. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,294
    Looks super! We're currently working on putting up a proper website for our company - may we use your screenshot as a reference shot for Shader Forge? :)
     
    shkar-noori likes this.
  31. PrawnJeremy

    PrawnJeremy

    Joined:
    Aug 20, 2013
    Posts:
    35
    Here is my hacked together Super Mario 3D World style shader (my avatar character uses it). I put it together by looking at examples and trial and error. I figure it would probably be helpful for some people to get them on the right track with a similar look. Of course I am not a shader guru so if anyone sees room for optimization or stuff I did wrong please let me know and I will update it and try to post a less messy image but here it is for now. This is the full shader, I also created derivatives for different things that don't need all the options.

    Edit: updated image to remove some Multiply nodes and organize things better
    sf_pjfull_11302014.png
     
    Last edited: Dec 1, 2014
    Murgilod likes this.
  32. Deleted User

    Deleted User

    Guest

    No fresh notice about 0.37/1.0 release?

    0.36 doesn't work well with a lot of nodes.
     
  33. IFL

    IFL

    Joined:
    Apr 13, 2013
    Posts:
    408
    Do you still need my script then or did this replace the need for that?

    Sure, or use the ones below, if you want. Scene is from ManufacturaK4 East Asia pack.
    SF_Wormhole_02.jpg
    SF_Wormhole_02a.jpg
     
  34. PrawnJeremy

    PrawnJeremy

    Joined:
    Aug 20, 2013
    Posts:
    35
    Still need it. The shader I posted is for the Character, the one I need is for the Projector.
     
  35. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,083
    I looked at this and picked it apart for a while and I FINALLY figured out how to procedurally generate grids/lines:



    I was having a lot of trouble with this because I'm just ridiculously bad with procedural stuff, so this has been a great help. Ignore the two weird dark spots, they're part of a stencil effect I'm trying to figure out.
     
  36. IFL

    IFL

    Joined:
    Apr 13, 2013
    Posts:
    408
    Can't find mine. Just tested this one on the wiki, and it should work well with some tweaking

    Glad I could help. I love working the math behind shaders. Did a hair set the other day - took me a while to get the IBL+Ansiotropic perfect... (and getting post-processing to play nice with it)
    SF_HairIBLAnsio_01.jpg
     
    KWaldt, overthere and PrawnJeremy like this.
  37. PrawnJeremy

    PrawnJeremy

    Joined:
    Aug 20, 2013
    Posts:
    35
    No worries, I have done some more searching and have come up with something that seems to work, the only issue now would be figuring out how to blur it to fake soft shadows. I guess I can take that to a different forum though.

    Code (CSharp):
    1. Shader "Custom/RTSProjector"
    2. {
    3.   Properties
    4.   {
    5.      _Color ("Main Color", Color) = (1,1,1,1)    
    6.      _ShadowTex ("Cookie", 2D) = "" { TexGen ObjectLinear }
    7.      _FalloffTex ("FallOff", 2D) = "white" { TexGen ObjectLinear }
    8.   }
    9.   Subshader {
    10.      Tags { "RenderType"="Transparent-1" }
    11.      Pass {
    12.         ZWrite Off
    13.         AlphaTest Greater 0
    14.         Offset -1, -1
    15.         ColorMask RGB
    16.         Blend DstColor Zero
    17.         Color [_Color]
    18.         SetTexture [_ShadowTex] {
    19.             constantColor [_Color]
    20.             combine texture alpha * constant
    21.             Matrix [_Projector]
    22.         }
    23.         SetTexture [_FalloffTex] {
    24.            constantColor (0,0,0,0)
    25.            combine previous lerp (texture) constant
    26.            Matrix [_ProjectorClip]
    27.         }
    28.      }
    29.   }
    30. }
     
  38. IFL

    IFL

    Joined:
    Apr 13, 2013
    Posts:
    408
    On your render texture, find the smallest combination of resolution and antialiasing that looks good. 64x64 & 4x is what I think worked best for my needs. Glad you figured out the rest, and thanks for sharing the ShaderLab code. If you don't mind, post or pm me a shot of what you end up with - I'd like to see how it turned out.
     
  39. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,294
    In about a week, I would guess. Busy schedule these days, sorry about that :(

    Neat! What happened to the spiralness of the previous node previews though? I liked those :)
     
    Last edited: Dec 1, 2014
    Deleted User likes this.
  40. bigSky

    bigSky

    Joined:
    Jan 31, 2011
    Posts:
    114
    I am struggling with this situation:

    I use a vertex displacement height map, on a tessellated plane. I then want reflections on that surface. I can’t use the view reflection direction input into cube map, as this will just give me ‘flat’ unrealistic reflections. How do I include the changed surface normal (ie after the displacement) in the reflection calculation?
     
  41. Marco-Sperling

    Marco-Sperling

    Joined:
    Mar 5, 2012
    Posts:
    620
  42. IFL

    IFL

    Joined:
    Apr 13, 2013
    Posts:
    408
    Ah. That would be a tiny bug that happens with the previews in regard to the sliders (maybe all properties...?) when a shader is opened. I have no clue what causes it, but it's fixed as soon as the slider value is set. It's also entirely unimportant unless you're trying to open a shader only to take a screenshot. Here's a replacement to that last graph I posted.
    SF_Wormhole_02b.jpg
     
    Acegikmo likes this.
  43. Deleted User

    Deleted User

    Guest

    Thanks for your reply, so at most there will be a gift of christmas under three :D

    Another thing, there is a chance of see this nodes?:

    Split and move vertices (like fragmentum asset).
    Glow around obiect (or a way for implement alpha channel on outline color).
    A better outline working with cube or other non smoothing model.
    Multiple pass for fur or grass creations.

    Again thanks
     
    Last edited by a moderator: Dec 1, 2014
  44. PrawnJeremy

    PrawnJeremy

    Joined:
    Aug 20, 2013
    Posts:
    35
    I had actually figured out how to use the Image Effect for Blur on my camera to blur it as well. Obviously that is more performance intensive than your way, which I am now using, thanks. I had messed around with that in the past but some combination of the blending of textures and image settings made it look like it wasn't doing much, works perfectly fine now though. Here is an image of it setup. All colouring and lighting subject to change. I already have my shaders grab settings from the level itself for things like reflection maps and Fresnel colour maps, I will be setting it up so the shadows grab the ambient light colour which is complementary to the directional light so the shadows match. Really happy with where this is going.

    PJShadow.png
     
    IFL likes this.
  45. bigSky

    bigSky

    Joined:
    Jan 31, 2011
    Posts:
    114
    Yes, that's possible, and useful - there's even a suggestion from ace on the Wiki where he "might" make a normal reconstruct node.
    But your suggestion does not address situations like mine where I have a procedurally generated heightmap - I don't have any UV co-ords to offset, and therefore, can't re-construct this way. And, in the example I have given above (for a environment reflection using a cubemap), the ref.dir node won't know anything of the reconstructed normals. Can anyone help?
     
  46. IFL

    IFL

    Joined:
    Apr 13, 2013
    Posts:
    408
    @PrawnJeremy - That looks great! Thanks for sharing it.

    The idea is the same regardless of whether or not the displacement is done procedurally. And I'm pretty sure that I've done exactly this for tessellation+IBL. Whatever you're plugging into the displacement, use it (scaled, as Marco suggested) as the heightmap for reconstruction. Is that the answer you're after?
     
  47. bigSky

    bigSky

    Joined:
    Jan 31, 2011
    Posts:
    114
    Not really, but I deeply appreciate the response. From what I understand the normal reconstruction works with offsetting UV co-ordinates, so it won't work with a procedurally generated heightmap - or am I wrong?
     
  48. IFL

    IFL

    Joined:
    Apr 13, 2013
    Posts:
    408
    For the method shown on the wiki, you're correct. It uses the UVs to sample surrounding data whereas you'd want to sample using your own generation instead of texture samples. Is your procedural generation using world/local coordinates? If you're not using UVs or a procedural method that uses world/local coordinates, then it's going to be tons of work.
     
  49. PrawnJeremy

    PrawnJeremy

    Joined:
    Aug 20, 2013
    Posts:
    35
    Thanks! Will get to update my avatar shortly, get rid of that blob shadow :) It comes with its own issues like needing to use a layer for each object that needs to project a shadow but I coded up a solution for that and will be mixing real time shadows with projected ones so it should be ok.

    Right now the projector shader is alpha blended but that isn't so good if the color is lighter than the ground so I may change it to multiplicative, at which point the low render texture res + AA won't work... I would have to raise the res and use the Blur Image Effect... either that or make sure the shadow color is darker than every ground surface the character crawls on, anyway, I will have a better looking game here shortly :D
     
  50. bigSky

    bigSky

    Joined:
    Jan 31, 2011
    Posts:
    114
    Yea. I found this, asking a similar question for webGL, and the suggestion is that the mesh normals don't access the updated (generated) normals. My "displacement" is from a proc. noise function, from the wiki, with one iteration. I'm thinking that you could possibly find the world space for the displaced verts with obj.position - normals*displacement, and the correct Transform, but really, I'm just scratching my head at this problem. I'm cool with tons of work, but tons of banging my head needlessly against a shiny screen, less so.