Search Unity

Shader Forge - A visual, node-based shader editor

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

  1. Spou

    Spou

    Joined:
    Mar 26, 2015
    Posts:
    2
    So I'm basically in the same boat as you. The standard shader would work perfectly, however there are a few things that Id like to modify to suit our exact needs(using a different UV set for AO, using alpha for transparency or not, etc) Id like to have a working base that I could modify and stop wasting time trying to figure this puzzle out. Id love to take the time to poke around this, but unfortunately time is short these days and online resources are scarce.

    On a side note I have a problem with the shader that Ive managed to make so far, it seems like it multiplies the specular color, white in this case, with the albedo, resulting in a overly white chrome look-a-like. Every model I use this shader on is reflecting whitish specular instead of using the spec map purely as a, well, specular(rgb)gloss(a) map. Any ideas on how I can fix this, as this doesn't happen in the default standard shader provided with unity.
     
  2. Marco-Sperling

    Marco-Sperling

    Joined:
    Mar 5, 2012
    Posts:
    620
    @Sinaz20
    You do not need to mess with the generated shader code to create a shader that matches the builtin Unity shader(s).
    Just do as Acegikmo suggests. Maybe we are talking past each other but all the nesseccary inputs are there in the main node once you've switched to PBL (metal or specular).
     
  3. Deleted User

    Deleted User

    Guest

    this works nicely for soft edged masks, is it possible to shrink/contract a HARD edged mask?
    i need medthod NOT based on using the soft edges of a mask.
     
    Last edited by a moderator: May 27, 2015
  4. Stoofe

    Stoofe

    Joined:
    Oct 11, 2014
    Posts:
    1
    mgear likes this.
  5. Sinaz20

    Sinaz20

    Joined:
    Nov 16, 2009
    Posts:
    53
    Here's what I'm getting at...

    I have to make an educated guess as to how the properties on the stock shaders interact.

    Here is my attempt at stock Bumped Specular:


    Here's my inspector showing that the values are the same when I switch the material from one shader to the next.


    And here's the result:


    Stock Left, Shader Forge Right.

    If someone could give me some clue as to how that stock shader produces that output, that'd be swell. Better yet-- document that shhtuff.

    I am currently still in the latest Unity 4 SF, btw, until we get our demo out this week to some publishers... then we transition to Unity 5.

    EDIT: to be clear... I didn't mean I was messing around in shader code to make a SF shader... I meant I was messing around in the stock shader code to try and learn how the shader works so I could port it to SF-- since you can't just import a stock shader into SF and start customizing it.
     
  6. gfunk

    gfunk

    Joined:
    Mar 17, 2015
    Posts:
    23
    Hi guys, how would I go about making a simple refraction shader, like the example on Acegikmo's site.
    Can't figure it out, any help much appreciated!

    Edit: Nevermind, the refraction example is already included!
     
    Last edited: May 27, 2015
  7. Marco-Sperling

    Marco-Sperling

    Joined:
    Mar 5, 2012
    Posts:
    620
    @Sinaz20
    Now I see where you're coming from. The stock Bumped Specular shader from Unity 4 is not a PBL shader. It is using a blinn lighting model. That's why you are getting different results. I would go the extra mile with you and try it for myself but sadly I don't have the time (being at work).
     
  8. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,294
    Right, in Unity 4 it's a bit trickier.
    I think the thing that differs the most is that you need to use Blinn-Phong, and that your glossiness needs to be remapped in a different way. Unity remaps it from 1 to 128, rather than 1-2048. Uncheck the remap gloss checkbox, and make your own remap node before the gloss input, and make it Blinn-Phong instead of PBL
     
  9. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    @Acegikmo Really hoping you can help with this, I have a texture 2D with the RGB hooked up to the diffuse and the Alpha hooked up to Opacity.
    The Sphere with this setup is visible in the editor looking exactly how I need it to, but I cannot see it in the game view, it just isn't visible, I have put the default shader on it and it comes back but I want to use a custom one.

    Blend mode is set to Opacity Blended BTW.

    Really hoping you can help, searching has turned up nothing.
     
  10. Sinaz20

    Sinaz20

    Joined:
    Nov 16, 2009
    Posts:
    53
    That did the trick. Thanks.

    But, I'm still wondering if aspects of the stock shaders like this are documented anywhere? If not-- that would be pretty valuable.
     
  11. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,294
    Sounds like you forgot to check the target platform under render settings

    It's in the shaders themselves, which you can get here, by clicking the drop-down menu: https://unity3d.com/get-unity/download/archive
     
  12. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    I just went back and looked at the shader, all target platforms are checked.
     
  13. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,294
    It could be lighting that is disabled in the scene view, and enabled in the game view, or something similar.
    Not sure what could be wrong otherwise.
     
  14. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    I have checked all that, I cannot work out what is wrong with it, I have attached it.
     

    Attached Files:

  15. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,294
    Works fine on my end, so I'm guessing it's either:

    A. Your scene setup, could be missing light sources, camera settings set to the wrong values, or similar
    B. Your quality settings removing features, might be pixel light count
     
  16. PolyToots

    PolyToots

    Joined:
    Mar 15, 2014
    Posts:
    20
    Hey hey,

    I'm not sure where the issue here is exactly, but I'm doing some particles at the moment and I can't work out how to "fade" any of the shaders I make with SF on the particles themselves. I've tried setting the shader to be additive, as other material types that have a working fade are set to this, but SF just yells at me to switch it back to alpha blended.

    By "Fade" I'm referring to when you set the Color over Lifetime attribute in the particle system, and set its alpha to ramp down to 0.

    To be more specific, one of the examples is a sort of distorted "shockwave" effect that's simply applied to a growing sphere. The alpha needs to be set at 0 in the shader, and the actual distortion is controlled via the intensity of a normal map in the refraction slot.

    I'll include an image of my very basic refraction setup, any help would be appreciated.

    SimpleRefract.jpg

    I've taken a look at the refraction shader that comes with SF, but can't really make heads or tails of it. It also suffers from the same issue when used on a particle.
     
  17. DMeville

    DMeville

    Joined:
    May 5, 2013
    Posts:
    418
    @GaryDave If you take a look at the refraction examples that come with SF they have refraction set up a bit differently, I'm not sure if doing an rgb*intensity -> comp.mask is the best method; but I usually do a lerp between the texture.rgb and a vector3(0,0,1), with the lerp time being controlled by the intensity, then pluggin that into your comp.mask.

    Secondly, particles might set those colour settings on the particle-mesh's vertex colour data. Try plopping in a vertex colour node and use the vertexcolor.a input to plug into your lerp.t/multiply.b input.

    Kinda just shooting in the dark though :)
     
    Last edited: May 28, 2015
    IFL likes this.
  18. PolyToots

    PolyToots

    Joined:
    Mar 15, 2014
    Posts:
    20
    Hey @DMeville

    I understood the words... not so much the meaning behind them. I'm a complete and utter idiot when it comes to understanding nodes in SF.

    However, I dropped in a Lerp, and a vert colour, and swapped some things around, ditched the multiply, used a vector 3... and somehow, it works. Sort of. I mean it doesn't do what I thought it would, BUT... the particle no longer "pops" out of existence, and that will do for me.

    Here's the nodes:
    RefractFade.jpg

    And here's the result in an oddly framey gif:
    hK2bJ1.gif

    I have yet to see if I can get the same results without the lerp or vector 3, at this point I'm happy to just walk away and never think about it again.

    Cheers!
     
    DMeville likes this.
  19. mangax

    mangax

    Joined:
    Jul 17, 2013
    Posts:
    336
    hello guys, one question i hope i find an answer for..

    when it comes to shader performance, if i used clamp distance to apply certain effects like "refraction".. on close distance from view.. will this have any performance gain?

    as well as applying opacity on close distance while keeping it opaque color on far distance...
    i want to avoid unnecessary performance hit due to overdraw.. when viewing shader from long distance..

    will these calculations will result in better performance or simply add extra complexity?
     
  20. gfunk

    gfunk

    Joined:
    Mar 17, 2015
    Posts:
    23
    Hi guys, another question from me - is it possible to make an opacity shader equivalent to the 'Fade' standard shader in unity? If I plug my alpha into opacity clip, the results are very jagged, whereas with Fade it works fine... Thanks!!
     
  21. PolyToots

    PolyToots

    Joined:
    Mar 15, 2014
    Posts:
    20
    Me again!

    I've googled high and low for some decent tips on doing water type shaders, but nobody out there seems to want to share how they do a puddle, let alone an ocean or a river. I tried resorting to UDK tutorials, and transferring the knowledge, but transferring an understanding of zero = zero. I realise I may be in over my head... perhaps, out of my depth. GET IT!?

    I failed miserably at getting flow maps to work in SF, I followed this as best I could:
    http://acegikmo.com/shaderforge/wiki/index.php?title=Flowmap

    But it always resulted in the same thing, texture would warp something crazy, then reset back to normal. Rinse and repeat. (I found someone else in this thread who was running into the exact same issue)

    So I moved onto panners, and with the right normal map it can look pretty "flowy", and that's what I've been building on for the moment. Here's my nodes (thumbnail), and the result (the gif doesn't loop, so ignore that "pop"):

    waterwaternodes.gif
    If you're looking at these nodes and wondering how I even manage to dress myself in the morning, I don't blame you. 99% of what I do in SF is just guess work.

    waterwater.gif

    I've tried messing around with the depth in the past with this stuff, but I can't seem to hook it up to the refraction at all, so I ditched that for now. I had mild success with some sort of alpha based depth. I mean it looked kinda cool, but again not being refracted so sort of pointless.

    So that's issue #1

    #2 is... foam!? texture!? My water is just a refracted normal map, with reflections. So it looks all oily, too pristine. Which is fine for some examples, but what can I do if I wanted some sort of splishy splashy foamy soapy bubbly type thing? I've got "waves" in there, is there an easy way to say "when the verts go high, fade in a texture"? Maybe a different panner node going in the opposite direction to the original, somehow with an alpha'd texture? (I say somehow, because I'm already using the opacity slot)

    On a different note, I subscribed to gametutors and watched their SF tutorials, but is that the best stuff out there at the moment? I found it was too much of "do this, now this, and hook that up, add this here"... without actually explaining the process. I left feeling pretty underwhelmed. Though, I am a bit of an idiot.

    Edit
    Oh, I said I move onto panners... but I'm not using any! I remember I had a lot of issues using the panner nodes, I undoubtedly did something stupid but even following other peoples nodes, all it was doing for mine was both scaling the UV's and panning. Not ideal. So the solution I adopted I guess is a pre-panner node one.
     
    DMeville likes this.
  22. PolyToots

    PolyToots

    Joined:
    Mar 15, 2014
    Posts:
    20
    Have you tried setting the transparency mode in Lighting to Fade instead of Reflective? Also I was under the impression that alpha clips were a sort of boolean type thing? ie... invisible or not, no fading?
     
  23. jc_lvngstn

    jc_lvngstn

    Joined:
    Jul 19, 2006
    Posts:
    1,508
    I'd like to ask for help again, with an old issue I was working on and posted way back.
    I'm trying to create the shader in Shader Forge for the purpose of having polygons in a mesh to always face the camera.

    Each vertex is the pivot point in local space for the quad. The UV2 values contain the x offset for each vertex, and the y offset. What -should- happen is that a quad is created and faces the camera.


    Here is a somewhat working "regular" shader, however I need to translate this to shader forge.

    Code (CSharp):
    1. Shader "Crankshaft/Vegetation/Billboard Tree Tinted"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Tint Color", Color) = (1,1,1,1)
    6.         _MainTex ("Texture", 2D) = "white" { }
    7.         _Speed ("Wave Speed", Float) = 0.12
    8.     }
    9.  
    10.     SubShader
    11.     {
    12.         Tags { "Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="Transparent"}
    13.  
    14.         Cull Back Lighting On
    15.         //Blend One One
    16.         Pass
    17.         {
    18.             AlphaTest Greater 0.2
    19.            
    20.             CGPROGRAM
    21.  
    22.             #pragma vertex vert
    23.             #pragma fragment frag            
    24.  
    25.             #include "UnityCG.cginc"
    26.             //#include "../CrankshaftShared.cginc"            
    27.  
    28.             float4 _Color;
    29.             sampler2D _MainTex;
    30.             float _Speed;            
    31.  
    32.             struct appdata
    33.             {
    34.                 float4 vertex : POSITION;
    35.                 float4 texcoord : TEXCOORD0;//texture - uv1
    36.                 float4 texcoord1 : TEXCOORD1;//billboard offset - uv2
    37.                 float3 normal: NORMAL;
    38.                 float4 color : COLOR;
    39.             };
    40.  
    41.             struct v2f
    42.             {
    43.                 float4  pos : SV_POSITION;
    44.                 float2  uv : TEXCOORD0;
    45.                 float3 color : COLOR;
    46.             };
    47.  
    48.             v2f vert (appdata v)
    49.             {
    50.                 v2f o;
    51.                 //vertical billboarding
    52.                 float3 eyeVector = ObjSpaceViewDir( v.vertex );
    53.                 float3 upVector = float3(0,1,0);
    54.                 float3 sideVector = normalize(cross(eyeVector,upVector));
    55.                 float3 finalposition = v.vertex;
    56.                 finalposition += (v.texcoord1.x) * sideVector;
    57.                 finalposition += (v.texcoord1.y) * upVector;                          
    58.                 float4 pos = float4(finalposition, 1);
    59.                 o.pos = mul( UNITY_MATRIX_MVP, pos );
    60.                 o.uv = v.texcoord.xy;
    61.                 o.color = ShadeVertexLights(v.vertex, v.normal);
    62.                 //o.color.a = 1;//fragment shader need one, we use alpha in vertex shader to waving
    63.                 return o;
    64.             }
    65.            
    66.             half4 frag (v2f i) : COLOR
    67.             {
    68.                 return tex2D(_MainTex, i.uv) *  _Color;
    69.             }
    70.            
    71.             ENDCG
    72.         }
    73.  
    74.     }
    75.  
    76.     Fallback "Diffuse"
    77.  
    78. }
    Here is my shader forge shader:



    The shader I use above works...somewhat. The shader forge shader only displays gray squares, each square a different shade of gray.

    Thank you,
    JC
     
  24. AkiLap

    AkiLap

    Joined:
    Sep 14, 2014
    Posts:
    24
    Got cloud layer working somewhat, still need a bit of work with opacity (and light) control..



    For clouds I created a texture2d node which is panned slowly to x/y direction, and for cloud shadows the same texture is "negated" (with one minus node). Cloud shadows are positioned with small offset and is broken down to x and y components (comp mask) to give me control to position them related to real clouds. Shadows are panned with same speed. Finally the cloud layer is added to final diffuse texture.
     
    Last edited: May 29, 2015
    Acegikmo likes this.
  25. Becoming

    Becoming

    Joined:
    May 19, 2013
    Posts:
    781
    my 2 cents:
    Ditch the nodes going into metallic, transmission and lightwrapping. The look of water, at least clear water, comes from 50% reflection and 50% refraction, with some light absorbtion increasing with depth. The reflection blends to refraction from a viewangle parallel to the surface towards perpendicular viewangle to the surface. So, the most important ingedients for realistic water are:
    A cubemap for reflection, a high gloss value, an animated normalmap blended with it self 2 or more times times and refraction which i think you have going pretty well already ;)
    Oh and i almost forgot about the most important thing, fresnel... you'll need a fresnel node for the reflection.

    There is MUCH more to it but that are the basics :)
     
  26. open_sketchbook

    open_sketchbook

    Joined:
    Dec 7, 2012
    Posts:
    6
    Hey, we're considering getting this, but we need to know something first.

    The colour modifiers on the shader (Multiply etc), can these be made dynamic? Like, can you hook those up to RGB values or whatevs from elsewhere in the game so as to do custom player/team colour?
     
  27. DMeville

    DMeville

    Joined:
    May 5, 2013
    Posts:
    418
    @open_sketchbook If I understand correctly the answer is yes. Shaderforge has a few special "input" nodes that expose that data to your material inspector (like public variables). For example, you can create a "colour" node in your shader, and then set whatever colour you'd like on the material that uses that shader, and have the shader use that colour. You can even change these values at runtime and the changes will be visible!

    For example this image here: http://i.imgur.com/2KL9fda.png all my knigts are using the same shader but with two different colours on each knight that defines the "team colour".

    Hope that helps! Shader Forge is definitely one of the best assets available! I can't recommend it highly enough.
     
  28. open_sketchbook

    open_sketchbook

    Joined:
    Dec 7, 2012
    Posts:
    6
    Thanks! Purchased confirmed then.
     
  29. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,294
    In general, no, you won't gain any performance with this. GPUs are ridiculously fast with calculations, but not with branching.
     
  30. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,294
    Looks like the issue might be which space you're working in. The shader in the code is using local space vectors and offsets, while your view vector is in world space, for instance.

    You can change the vertex offset space in the settings. Set it to local, and make that view vector in local space rather than world space.
     
  31. jc_lvngstn

    jc_lvngstn

    Joined:
    Jul 19, 2006
    Posts:
    1,508
    Thanks Ace...I don't see where to set vertex offset space in the settings, so I tried doing a transform on the view direction from world space to local. No difference I'm afraid.
    The odd thing is...the polygons are showing. It's just showing blank, grey polys. I did notice that I have UV1 going as my diffuse uv input, and change it to be from UV0 (but kept UV1 being used for vertex offsets). Still no difference. I'll keep poking at it.
     
  32. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,294
    Just noticed that it's not possible, I was thinking of the normal direction. My bad. Still, careful about which space you're in.
     
  33. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,825
    Is it possible to use this asset to create a shader similar to the Unity5 standard shader in Transparent mode, but also RECEIVES shadows? It does not need to cast shadows.
    This is the issue I am having now with the Unity 5 shader in transparent mode, it cannot receive shadows.
    (I tried searching through this forum but it is 80 pages long)
    Thanks.
     
  34. mangax

    mangax

    Joined:
    Jul 17, 2013
    Posts:
    336
    Hello , thanks for replay ace!

    i have an issue with shader am working on recently.. am trying to apply a border texture around a box that remains at top..the shader should make my life easy when modeling platform boxes, but i run into following issues (in screens)

    everything looks good on the default unity 1m cube.. but when i use meshs or import other meshes.. things start look unstable..especially if mesh is high vertically..

    There is some kind of repeated pattern happens and causes color strength to multiples.
    maybe someone can help/improve what am doing ? am i approaching this the right way?


    images of issue:
    shaderIssue.jpg

    shader:
    sf_newshader_5312015.png

    note that before applying border texture on side , i use part of top texture on the on the sides at upper edge.. so when border applied later on.. it will fade in and out between the two textures giving it seamless look on close view.
     
    Last edited: May 31, 2015
  35. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,294
    This is trickier than what you might expect.

    Different models have different UV coordinates. UVs are what you use to map textures, they say which pixel of the texture goes where.
    In your case, you would need to tailor your shader to a specific mesh, and stick with it.
     
  36. mangax

    mangax

    Joined:
    Jul 17, 2013
    Posts:
    336
    You totally makes sense.

    but is it possible to make the shader force it's uv to align in relation to world ?
    so it force the effect of top & sides textures no matter how mesh looks or aligned.
     
  37. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,294
    In which case you need two pieces of information in the shader to map this:

    1. Where is the uppermost part of this mesh?
    2. How tall should the mapped texture be?

    Also note that they need to be in the same space, either world or object.
     
  38. mangax

    mangax

    Joined:
    Jul 17, 2013
    Posts:
    336
    1. upper most part is the normal or face that points upward in world.
    or simply the highest vertex in mesh is the starting point.

    2. i want it to be able to extend mapped texture enough to cover at least 10 meters high in unity.
    i do not think i need more than that for my current needs, but it would be better if shader can applies the shader effect regardless of mesh height.

    i prefer if shader follows world direction.
     
    Last edited: May 31, 2015
  39. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,294
    Exactly, so now you need to pass that data to the shader, since none of those two things is something the shader itself can calculate. (Unless the mesh pivot point is always at the very top of the mesh)
     
  40. mangax

    mangax

    Joined:
    Jul 17, 2013
    Posts:
    336
    hmmm... final Q (i hope )
    is it possible to detect or read bends on mesh then apply texture on an amount of size/offset?

    thanks for your support
     
  41. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,294
    Not really, shaders don't really have a concept of "bends". You can somewhat approximate curvature on a per-triangle basis with the ddy and ddx nodes, but it's still very approximate, and it won't handle hard edges at all. These things are usually baked into a texture
     
  42. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    @Acegikmo
    1) Is there any way to use a Texture 2D Node to create a "Mask" like in Photoshop so that anything applied to it only shows up in the white areas?
    2) Is there any way to apply a controllable variable to the Panner node to change the U and V values in the Unity Inspector?
     
  43. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    Not sure if this helps but a while ago I did find someone with a shader that could read the direction of the Normals on a model so he could detect the light curvature, if the models normals had been smoothed, this would have a similar effect to detecting the models curves, it would not be as accurate though.

    Again, may not help but I thought I should mention it just in-case you manage to find it.
     
  44. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,294
    1) Plug in the mask in the T input of a Lerp node. What you have in A will show where the mask is black, and what you have in B will show where the mask is white
    2) Construct a panner manually: http://acegikmo.com/shaderforge/wiki/index.php?title=UV_Rotation_&_Panning
     
  45. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    Does anyone know how to dynamically recreate this effect with refraction, a transparent window texture and a changable scroll speed and intensity without it looking off?
     

    Attached Files:

  46. Marco-Sperling

    Marco-Sperling

    Joined:
    Mar 5, 2012
    Posts:
    620
    You are on the right track to make it look good. Try some more variations on the mask you are panning down from top to bottom. You could mix a second mask into it that moves at a lower Speed and which modifies the original mask.

    The droplets themselves could be sampled from a normal map where the sample in turn is used to refract the Scene Color.
     
  47. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    Thanks for the suggestions, I am working on this now.

    Does anyone know of a way to offset a textures UV coordinates in Shader Forge (I don't want to do this in the editor)?
     
  48. djweinbaum

    djweinbaum

    Joined:
    Nov 3, 2013
    Posts:
    533
    @Acegikmo do you know about this:

    For me, all [shaderforge] forward shaders with alpha are broken with all SSAOs. 5.1 RC hasn't fixed it for me. Here is an example:
     
  49. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,294
    Is it alpha blended or just using opacity clip?
     
  50. djweinbaum

    djweinbaum

    Joined:
    Nov 3, 2013
    Posts:
    533
    Clip. The blend mode is "Opaque". Blended has the same artifacts.