Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Shader Forge - A visual, node-based shader editor

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

  1. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    881
    it's possible to make sprite material ?
     
  2. LighterS

    LighterS

    Joined:
    Nov 7, 2013
    Posts:
    8
    Hi, shader noob here. I'm trying to use ShaderForge to create a same shader as the "Toon/Basic" shader which you can get from importing "Toon Shading" package. First, let me describe what I want in case you're not familiar with the basic toon shader. Its brightness is not affected by lights, but only view angle and cubemap. It's similar to a reflection, but the difference is the cubemap will rotate with the camera angle. For example the faces facing upper right to the camera will always be brighter, it's like the cubemap is bound to the camera.

    But now I'm not quite there. Seems like I'm not sampling the cubemap in the right way, the shading do change when I change view direction, but not in the right way.

    Left: The toon basic shader I'm trying to re-create.
    Right: What I got.
    Screen Shot 2014-07-28 at 3.45.47 PM.png

    sf_toonshadertest_7282014.png

    Here's the shader I want to recreate in SF.
    Code (CSharp):
    1. Shader "Toon/Basic" {
    2.     Properties {
    3.         _Color ("Main Color", Color) = (.5,.5,.5,1)
    4.         _MainTex ("Base (RGB)", 2D) = "white" {}
    5.         _ToonShade ("ToonShader Cubemap(RGB)", CUBE) = "" { Texgen CubeNormal }
    6.     }
    7.  
    8.  
    9.     SubShader {
    10.         Tags { "RenderType"="Opaque" }
    11.         Pass {
    12.             Name "BASE"
    13.             Cull Off
    14.            
    15.             CGPROGRAM
    16.             #pragma vertex vert
    17.             #pragma fragment frag
    18.             #pragma fragmentoption ARB_precision_hint_fastest
    19.  
    20.             #include "UnityCG.cginc"
    21.  
    22.             sampler2D _MainTex;
    23.             samplerCUBE _ToonShade;
    24.             float4 _MainTex_ST;
    25.             float4 _Color;
    26.  
    27.             struct appdata {
    28.                 float4 vertex : POSITION;
    29.                 float2 texcoord : TEXCOORD0;
    30.                 float3 normal : NORMAL;
    31.             };
    32.            
    33.             struct v2f {
    34.                 float4 pos : POSITION;
    35.                 float2 texcoord : TEXCOORD0;
    36.                 float3 cubenormal : TEXCOORD1;
    37.             };
    38.  
    39.             v2f vert (appdata v)
    40.             {
    41.                 v2f o;
    42.                 o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
    43.                 o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    44.                 o.cubenormal = mul (UNITY_MATRIX_MV, float4(v.normal,0));
    45.                 return o;
    46.             }
    47.  
    48.             float4 frag (v2f i) : COLOR
    49.             {
    50.                 float4 col = _Color * tex2D(_MainTex, i.texcoord);
    51.                 float4 cube = texCUBE(_ToonShade, i.cubenormal);
    52.                 return float4(2.0f * cube.rgb * col.rgb, col.a);
    53.             }
    54.             ENDCG          
    55.         }
    56.     }
    57.  
    58.     SubShader {
    59.         Tags { "RenderType"="Opaque" }
    60.         Pass {
    61.             Name "BASE"
    62.             Cull Off
    63.             SetTexture [_MainTex] {
    64.                 constantColor [_Color]
    65.                 Combine texture * constant
    66.             }
    67.             SetTexture [_ToonShade] {
    68.                 combine texture * previous DOUBLE, previous
    69.             }
    70.         }
    71.     }
    72.    
    73.     Fallback "VertexLit"
    74. }
    75.  
    If there's any clue you have, please let me know. Thanks in advance.
     
  3. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,235
    It will affect the performance in a bad way, yes. The reason for this is that the shader is always ready to accept new parameters and adapt to change, without changing shader. In order for you to have optional parameters, I would have to first implement a new dependency system in SF, and then add static switches, which I would love to do! However, it's going to take some time, especially since I'm currently busy with my Unite 2014 talk.

    You can apply shaders to sprites, but there is currently no way of specifically marking a shader as a sprite shader, so you may run into some issues with, for instance, depth sorting.
     
  4. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,235
    I would guess you need to use the transform node, and transform the normal direction from world space, to some other space (try them all and see which looks right), before inserting it into the cubemap DIR input
     
  5. LighterS

    LighterS

    Joined:
    Nov 7, 2013
    Posts:
    8
    Thanks for the reply. I tried all the combinations, but still doesn't work. Any other ideas?
     
  6. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,235
    Here's one of the combinations:

     
  7. adamz

    adamz

    Joined:
    Jul 18, 2007
    Posts:
    1,023
    Where can I find the Shader Forge Extention zip file so I can use Skyshop with shader forge?

    Is there an example on how to setup edge length tessellation? And is it possible to create a distance based tessellated shader?

    Thanks!
     
    Last edited: Jul 29, 2014
  8. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    941
    Hello everyone!
    I've got two questions on how to make special shaders with Shader Forge. Maybe someone could help me.

    1.Texture splatting and spritesheets
    Does anybody know if it is possile to make animated texture splatting with sprite sheets?
    I'd like to use a script similar to this one for animating the sprites:
    http://www.chaoticownz.net/forums-base/topic/animated-sprite-sheet-script-java

    2. Texture splatting and texture atlases
    Is there a way to use texture atlases while texture splatting?
    It would be cool to be able to use different sub-textures and rotate them, align them next to each other with uv-unwrapping and afterwards use a splat map to make them show different textures.

    Here are some images that may help understanding what I want:
    nodes.PNG scene.PNG

    Those images don't have texture atlases, but what I want is more or less the same:
    I would like to use the unwraps of all the single quads for the textures, but at the same time, I would like to use the splat map on the whole floor.
    In Blender, one can choose "Generated" (Texture Coordinates). I guess an equivalent to that would make the splat map cover the whole floor.

    Many thanks in advance,
    Greetings,
    Shushustorm
     
  9. Lordinarius

    Lordinarius

    Joined:
    Sep 3, 2012
    Posts:
    88
    Hi i am new in shaders and have a simple question i am trying to bend texture trough mesh edges.

    like this. How can i achive this with using UV operations or geometry data ?.

    Ekran Alıntısı.JPG
     
  10. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,235
    The Skyshop zip should be somewhere in the Skyshop installation folder.

    SF has support for edge-length based (Which is also based on distance) and regular tessellation. That said, you can use regular tessellation with a distance yourself, biasing the tessellation amount with a distance check between world position and view position.

    I get animated textures, but I'm not entirely sure what you mean by animated "splatting".
    You can render segments of a texture using UV manipulation, similar to the code example you linked, on a shader level as well. I should add a node for accessing atlas/spritesheet-textures at some point.

    It sounds like you want to use world space X and Z coordinates for a planar projection along the Y axis. Append the X and Z coords from the world position node, divide it by a scale value, and then use that as UV coords for the splatmap


    Not quite sure what you mean. Did you mean "bend texture", "blend texture" or "bend geometry"?
     
  11. Lordinarius

    Lordinarius

    Joined:
    Sep 3, 2012
    Posts:
    88
    Simply, i want this simple diffuse shader

    Ekran Alıntısı.JPG

    Like this.

    Ekran Alıntısı1.JPG

    this is my current curve shader's vert and surf functions. I want to achive this in Shader Fogre :)

    Code (CSharp):
    1. void vert(inout appdata_full v, out Input o)
    2. {
    3.     o.Control_uv.x = (v.texcoord.y + 0.5) / _ControlSize;
    4.     o.Control_uv.y = 0;
    5.     o.Control_uv.z = v.color.x;
    6.     o.Splat01_uv.xy = _SplatParams0.z ? (v.texcoord1.xy / _SplatParams0.xy) : float2(v.texcoord.x  / _SplatParams0.x, v.color.x);
    7. }
    8.  
    9. void surf(Input IN, inout SurfaceOutput o)
    10. {
    11.     // need to offset U by half of the unit to center the texture around the curve nodes
    12.     half4 splatControl = tex2D (_Control, IN.Control_uv.xy);
    13.  
    14.  
    15.     // mix the color
    16.     half3 color = half3(0, 0, 0);
    17.     color +=  tex2D (_Splat0, IN.Splat01_uv.xy).rgb;
    18.  
    19.     // set the mixed color
    20.     o.Albedo = color;
    21.  
    22.  
    23.     // mix the alpha coef for fading out based on the textures we have
    24.     float alphaThreshold = 0;
    25.     alphaThreshold += splatControl.r * _SplatParams0.w;
    26.  
    27.     // compute alpha using the threshold
    28.     half alpha = splatControl.r + splatControl.g + splatControl.b + splatControl.a;
    29.     alpha *= saturate(IN.Control_uv.z / alphaThreshold);
    30.     o.Alpha = alpha;
    31. }
     
    Last edited: Jul 31, 2014
  12. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,235
    Not sure why you need a shader to handle the UV bending. Why not simply map the UV coords in that way, in the model itself?
     
  13. nasos_333

    nasos_333

    Joined:
    Feb 13, 2013
    Posts:
    8,473


    One question, i have modified the Refraction shader to assing a texture to give a blue tint, but when i assign the texture in the inspector, Unity hangs (Using Unity Free).

    I post a pic, do i do something wrong ? I have only touched on the path that goes to diffuse, nothing else.

    Thanks
     
  14. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,235
    Sounds like it may be a broken installation of SF. Try removing SF entirely, reset the Unity window layout, and then install SF again.
     
  15. nasos_333

    nasos_333

    Joined:
    Feb 13, 2013
    Posts:
    8,473
    Thanks, i will try that.
     
  16. Lordinarius

    Lordinarius

    Joined:
    Sep 3, 2012
    Posts:
    88
    Hi i made it . Thanks
     
  17. adamz

    adamz

    Joined:
    Jul 18, 2007
    Posts:
    1,023
    Thanks for the suggestion on how to setup distanced based tessellation. Any ideas on when you might integrate phong tessellation? That will make a world of difference when it comes to smoothing out models.

    Thanks!
     
  18. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    941
    Thank you very much for your answer, Acegikmo!

    This does seem to work. I didn't test it using actual atlases, but right now, I would just have to offset and maybe rotate a little bit to place the splatmap to the correct position.
    I will try to make this work.
    On the other hand, is there no easier solution? I'd really like to use.. well, two different unwraps of the same mesh.
    I wonder if that would be possible at all.
    Because then, one could easily modify the mesh's geometry without the material going crazy which I assume would happen if I use the planar projection method that you mentioned.
    You know, this is just a planar test, but in the end, I would like to make the geometry uneven.

    Well, maybe I didn't formulate it clearly:
    I would like to use two textures, blend them using a splat map and at least one of those textures is supposed to be an animated one that is embedded in a spritesheet.
    Maybe I find an example in a game. Then I can post it here.

    Here is an example of how I would like to use a texture atlas:
    test.PNG

    EDIT:
    Here is another node setup. I was able to access two of the four sub-textures of the texture map.
    I wasn't able to use the sub-textures 4 times, mirror them and combine the parts again, though.
    test2.PNG

    Also, I guess this procedure isn't a good idea when the texture atlases are more advanced.

    EDIT2:
    In Blender, one can unwrap an object multiple times and create different UV-maps.
    I don't know if this is possible, but it could be very useful if one could acces that data and use one unwrap for the tiling and the other unwrap for the splatmap. That could be done quickly and textures could be mirrored easily because of the tiled unwrap.
    blender.PNG
     
    Last edited: Jul 31, 2014
  19. FPires

    FPires

    Joined:
    Jan 5, 2012
    Posts:
    151
    I have a question:

    Is it possible to disturb a planar reflection in a way simular to anisotropic BRDF? For example, bright reflections in a lake looking vertically stretched.

    Such distortion should come naturally with low-roughness PBR but since I'm manually grabbing the planar reflection pass (with the Unity script) and converting it with UNITY_PROJ_COORD we only get a flat reflection. Is there any way to disturb it like that?
     
  20. daville

    daville

    Joined:
    Aug 5, 2012
    Posts:
    278
    Hi there...

    is there a way to assign a color to each polygon / triangle?

    Something like vertex color, but apply to the whole polygon, not like vertex color wich affects the corners and then gradients the colors.
     
  21. spurcell

    spurcell

    Joined:
    Oct 31, 2013
    Posts:
    2
    I'm very new to shader forge and need to create a simple, yet probably complicated to achieve effect.



    I need this to move in a wave like motion. So I need to perform some sort of vertex offset animation. But I guess I need to somehow change the normals so that they only move up and down in Y world space not based off their own object normals.
     
  22. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,235
    You should be able to do that with a sin( WorldPosition.z + Time ) * Vector3(0,1,0)
     
  23. spurcell

    spurcell

    Joined:
    Oct 31, 2013
    Posts:
    2
    Any chance I could get a quick screenshot of what the shader tree would look like?
     
  24. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,235
    Something like this should work:

     
  25. topofsteel

    topofsteel

    Joined:
    Dec 2, 2011
    Posts:
    976
    Is it possible to detect mesh edges? I would like to blend 2 materials based on the distance from the edge of a mesh. Similar to using vertex colors. Thanks.
     
  26. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,235
    You would have to paint those distances in the vert color data either manually or with a script. Shaders don't have a concept of "edge of a mesh".
     
  27. c2j

    c2j

    Joined:
    Jul 27, 2013
    Posts:
    11
    Hi Acegikmo,

    first of all, thanks you for your ShaderForge i love it ! :)

    I'm making a 2D mobile game, where i want to put my background day to night after an amount of time or character's altitude. I know i can do it with shader, but can you help me of doing this with ShaderForge ? :)

    Thanks you for your reply.
     
  28. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    7,163
    I don't suppose there's any way to get a bit more control out of outlines? I'm trying to recreate the Guilty Gear Xrd toon shader and everything is doable so far, but there's one thing that's hanging me up. Namely the z-offset mentioned in this paragraph of the translation:

    Can I do this, or am I going to have to take an easier route and just use two materials?
     
  29. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,235
    It's not currently possible to do it exclusively for only the outline pass, no
     
  30. c2j

    c2j

    Joined:
    Jul 27, 2013
    Posts:
    11
    Do you have an idea for me Ace ? :) please.
     
  31. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    7,163
    This could be easily accomplished using the Time Node and a simple world position check if you parent the background object to your camera.
     
  32. c2j

    c2j

    Joined:
    Jul 27, 2013
    Posts:
    11
    Thanks for your reply, but i'm new with that. How can i link the world position in the shader ? Can you give me an example ? Thanks you Murgilod
     
  33. binaryprogrammer

    binaryprogrammer

    Joined:
    Jul 17, 2014
    Posts:
    6
    Hey guys!

    I'd like to get some outlines around some models. However, when I load up even the example CustomLighting shader, I get:
    outline.jpg
    I'm assuming this probably isn't a bug, but rather something to do with the normals on my assets, but I can't figure out what it is. Any help is much appreciated.

    Thanks for any feedback!
     
  34. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    7,163
    It looks like your vertices aren't properly joined? I've never seen an outline shader... explode quite like that.

    I'll see if I can't whip up a basic example when I wake up tomorrow. I think a simple script/shader combo would work easily here.
     
  35. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,235
    The outline pass will move the vertices along their normals, and "hard edges" are simply normals pointing in separate directions, and thus offsetting in the wrong direction.

    There are ways to work around it. For instance, you could bake soft normals into the vertex colors, and then read the vertex colors instead of the normals, when doing the outline.

    You'd have to do that manually in the code now though, as SF doesn't have the option to use vertex colors as outline normals yet.
     
  36. c2j

    c2j

    Joined:
    Jul 27, 2013
    Posts:
    11
    Great Murgilod ! Thanks you, you will help me a lot ! I'm glad to find people like you :)
     
  37. env_warby

    env_warby

    Joined:
    Nov 22, 2013
    Posts:
    23
    I just updated shader forge from the asset store
    and after importing the files it wont work anymore!
    The "new shader/load shader"-window appears after a LONG delay but than the actual window is pretty much empty and there are also a lot of errors in the console

    see screenshot:
     

    Attached Files:

  38. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,403
    Hey env_warby,
    I had the same thing and I had to delete the shader forge from my project and reimport. Then it worked fine!

    Hey I was wondering, is there a way to not expose variables to the editor?
    For if you have a colour or a value that you don't want to be editable?

    Thanks,
    Pete
     
  39. adamz

    adamz

    Joined:
    Jul 18, 2007
    Posts:
    1,023
    PN-triangle support please :)
     
  40. bmccall1

    bmccall1

    Joined:
    Jul 28, 2013
    Posts:
    120
    Is there a good way to use "layers" or some other way to have a tiled material but with some simple deformations in the tiling and the base diffuse material so that it reduces the tiling effect? What about actually layering a transparent image over top of it that also tiled and had noise?

    (I am familiar with adding noise to an image, and it does definitely help some)

    Also, has anyone had problems using shader forge when it comes to baked lights, etc. For some reason shaderforge textures don't seem to recieve baked light properly
     
  41. wsz

    wsz

    Joined:
    Oct 20, 2012
    Posts:
    19

    Great question,
    A shader like this? http://docs.unity3d.com/Manual/shader-NormalDecal.html
     
  42. bmccall1

    bmccall1

    Joined:
    Jul 28, 2013
    Posts:
    120
  43. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,235
    You can use the Vector3 or Vector4 node as an "uneditable" color node :)

    This is a *huge* request. I would love to add this, but adding explicit control over the vert shader program is of higher priority, before adding support for the more advanced hull/domain shader programs, especially since it's DX only at the moment.

    The setup for this is quite simple. You can use the lerp node for that:
    A = Bottom layer
    B = Top layer
    T = Transparency of top layer

    The beast lightmapper has some very silly requirements. For instance, your diffuse texture needs to be named MainTex (so that the internal name is _MainTex)
     
    wsz likes this.
  44. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,235
    Oh, also, Shader Forge is nominated at the Unity awards 2014, for technical achievement :D
    If you like Shader Forge, more than the other products listed, a vote would be heavily appreciated!
    https://unity3d.com/awards/2014
     
  45. itsAditya

    itsAditya

    Joined:
    Jul 20, 2014
    Posts:
    3
    Please help me with this question .... How to achieve a realtime reflection using shader forge ? ..... and by realtime i mean the reflections on a car's body, i don't want to use any cubemaps coz they can work on still objects only, if anyone of you played Watch Dogs and if in the game you payed attention on the cars then you will see the reflections of building
     
  46. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,235
    You could do this: http://docs.unity3d.com/ScriptReference/Camera.RenderToCubemap.html
     
  47. Cactus_on_Fire

    Cactus_on_Fire

    Joined:
    Aug 12, 2014
    Posts:
    530
    Hey guys. Spent a few hours on Shader Forge with lots of experimentation and excitement. Finally achieved something close to what I wanted to make.

    It's a fully 3D nuke explosion mushroom cloud with lots of adjustable sliders for stuff like speed, flame intensity, parallax depth, smoke transmission, alpha, seperate cloud layers and flame layers. There are some issues with the alpha masks right now but they are easily fixable with changing the mesh.

    It only uses a single static mesh for the mushroom cloud and the animated texture offset is done inside SF using Time node. The gif is way too fast but you can try the exe to get the full effect. You can orbit around the nuke with the mouse.


    http://www.mediafire.com/download/v2.../NUKE+FULL.rar



     
  48. ratamorph

    ratamorph

    Joined:
    Sep 2, 2007
    Posts:
    458
    I keep getting lost in the canvas due to the infinite scrolling, how do I find my nodes when I get lost? I lost my main node and see no way of getting it back on screen. I zoomed out all the way and still can't see it.

    You make a change, the shaders starts compiling, you try to scroll because you are not aware of the compilation, compilation finishes all your scrolling gets processed and your nodes are nowhere to be seen.

    I'm hoping its not actually a bug
     
    Last edited: Aug 13, 2014
  49. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,235
    I've never had it be that extreme. In that case you'll have to reopen the shader, and it should auto-center
     
  50. Acegikmo

    Acegikmo

    Joined:
    Jun 23, 2011
    Posts:
    1,235
    Looks fantastic!
    I would love to include this in my Unite talk, if it's okay by you :)
    If you want to, let me know at webmaster@acegikmo.com
     
unityunity